Ray casting with a custom class

I have a custom class that contains 2 integers and a boolean variable, but I want to be able to select the instance of this class using ray picking.

  class MapNode{
    public int x;
    public int y;
    public boolean isEmpty;
    
    public MapNode(int x, int y)
    {
        this.x = x;
        this.y = y;
        isEmpty = true;
    }

Now when I’m using this ray picking technique, I’m looking for a geometry and performing whatever action to that specific geometry. I have an array of these MapNodes, and when I click, I want to return whatever value I’m looking for and perform some action. For instance, if I click on a MapNode at [0][1] and it isnt empty, I want to return whatever is inside and do something with it.

Can this be done just by adding a Spatial parameter to the MapNode class? I was thinking about creating an imaginary cube and then setting the spatial parameter to that cube so that any click inside the spacial/cube returns whatever MapNode I’m clicking on. I just dont’ know if making a Spatial for every MapNode is efficient, especially when I move up to a bigger array of MapNodes

You just need a way to correlate click coordinates and map nodes, e.g. 0.1 to 0.2 x / 0.1 to 0.2 y means node 1. You can define an algorithm for that.

Furthermore, keeping an array for the map nodes probably isn’t a good idea either, as you say there’ll be a lot of empty map nodes so why keep an array, just make a method like getNode(x,y) that returns an object for that tile (or whatever you do there).

The reason for the array is that the MapNodes are going to be changed frequently. I figured I would need some kind of list to search through, but you are suggesting to set all the nodes during initialization rather than keeping an array?

A list, hashmap or something similar probably makes more sense. Advantage of the method approach is you can hide anything behind it, even a database. Otherwise you don’t say what you actually want to do so how am I supposed to know.

Oh I gotcha. Either way I’d need to keep track of an empty node in case I want to place something there. My plan is to eventually highlight the area that the mouse is over so I can see which node I’m clicking, which is why I wanted a square type area representing the world coordinate of the node.

Why do you need a node when you maybe want to place something there. Just place the node when you actually do place something. Keep the whole game and data logic abstract. If your game grows you might even want to unload nodes and objects the player doesn’t currently see and just keep your base data about the tile. Then when the player moves to that location you just recreate it based on that data. Also, when your player zooms out very far you might just want to display a particle of a particle emitter at that location as the player will only see a pixel anyway.

So by creating an algorithm mentioned I’m your post above, I can create nodes based on where I’m clicking if I am placing something on the terrain?

Click → get location on the land
→ get grid cell location
→ look up thing in the HashMap of grid cells
→ if grid cell not exist, create grid cell

Which part is confusing?

You should do it like that, yes. Keeping a huge array of objects that you never use just doesn’t make sense.

It’s not confusing anymore. I was trying to make sense of the logic behind this since I haven’t done anything like it before.