1
\$\begingroup\$

I'm using Gameplay3d as a game engine to develop a simple board game, PEG Solitaire.

I've come to the part where I've created a 2D grid for all the movable object, empty locations and also where it's not movable. I'm now trying to somehow map that 2D grid to my actual game coordinates, translations of my pieces. Using Gameplay I can select my objects. What I'm trying to figure out is how to solve this actual problem to map my grid to the actual movement of the game pieces?

I was maybe thinking of using an std::map<char*, int> where "char*" is the NodeID and "int" is the index in the 2D grid list so that I can identify each Node and where they are in my 2D grid. The problem I'm having now is how can I solve the problem if the user select places on my board that doesn't have moveable piece. Let's say the user clicks on an empty hole on the PEG Solitaire board. How can I map that location to my 2D Grid?

I've created my board model and Sphere using Blender. I can using Gameplay use raypicking and select the differente Sphere Nodes and the whole Board Node. The problem is that I can't pick the individual holes on the Board as Nodes. I know the center position of each hole on the board. So my solution was to somehow first figure out the Spheres world size maybe translate it to the screen size. When the user then clicks on the screen I can check if the position of x and y is inside the selection area of that hole so to say. I'm probably just wondering away in my own thought. But any detailed help here would be appreciated.

Get object world size and translate to screen size of possible.

The 2D grid looks like this:

0022200
0022200
2222222
2221222
2222222
0022200
0022200
\$\endgroup\$
6
  • \$\begingroup\$ Do you mean you're trying to store the locations of your pieces in the grid, or that you're trying to convert their locations in the grid to their locations in 3D space? \$\endgroup\$
    – Polar
    Mar 26, 2013 at 19:42
  • \$\begingroup\$ I'm trying to map the location in the 2D grid to the actual world location. Maybe what I need is OpenGL grid selection. Could that be something? \$\endgroup\$ Mar 26, 2013 at 19:58
  • \$\begingroup\$ I don't believe that OpenGL grid selection is at all related... \$\endgroup\$
    – Polar
    Mar 26, 2013 at 20:00
  • \$\begingroup\$ Ok how can I go about solving my problem? using just ray picking I get a x and y coordinate but I would like for the selection to be a of a certain range instead.. like a rectangle \$\endgroup\$ Mar 26, 2013 at 20:06
  • \$\begingroup\$ A question about your question - what exactly do you need to do? Do you need to go from your selected rectangle to what pieces are encompassed by that? If so, is the rectangle a plane in 3D space, or a 2D selection? \$\endgroup\$
    – Polar
    Mar 26, 2013 at 20:42

1 Answer 1

1
\$\begingroup\$

If you're trying to convert a position in 2D to the position in 3D, then it degenerates to a maths problem. Basically, you have to produce a transformation matrix which then translates, rotates, and scales the 3D point (presume Z = 0) to a new one. If you know how you want it to be orientated and placed, then this suddenly becomes a trivial problem. Wikipedia explains it relatively well here, although I suggest that you Google for more information.

Edit:

Based on your comments, you seem to be trying to solve to separate problems. The first is converting your selection in 3D to 2D, and the second is selecting what is in that area. To convert 3D into 2D, you need to ray cast to obtain the point on the surface. Then, as I said above, you need to transform the points into 2D space. If you already have the other matrix, then you can calculate the inverse of it and use that.

Once you have your transformed point in 2D (note - this means discard the hypothetical Z = 0 from before), you then need to get the objects in it. My personal advice would be to iterate through all of your objects, and then check to see if their centre intersects with the polygon. I've linked the ray-casting algorithm as it relates back to the ray casting before. If the centre of the object intersects, then it's in the chosen area, and you can add it to another list (or a pointer of it, etc.)

Edit 2:

From the information I now have, it seems that you're trying to work out if you have a valid area to place your sphere / peg. At this point, presuming you have the transformed point in 2D (from above, if you don't already have it), you merely need to work out if the point lies in an empty position. This is a bit more maths, which goes as follows:

  • Take the point Vector2 x, which is our point in 2D.
  • Transform x by some other vector Vector2 y to get the point relative to your board ( x + y ).
  • Now scale the expression by a scalar float a to get the point with unit x-values and y-values corresponding to the grid of the board ( a(x + y) ).
  • Finally, cast the individual parts of the resultant vector to integers so that they can be used directly as array indexes.

The final code would be (in C++, sorry if it's the wrong language):

Vector2 getArrayIndices(Vector2 initalVector)
{
    Vector2 translationVector(x_value, y_value); // Pre-determined
    Vector2 translatedVector = initialVector + translationVector;

    int scalar = scale_value; // Also pre-determined
    Vector2 scaledVector = scalar * translatedVector;

    Vector2 integerVector((int)scaledVector.getX(), (int)scaledVector.getY());
    return integerVector;
}

I'll presume that from here you can check the x and y values of the vector against your array / grid (as shown in the question) to see if the value is a 1 (or whatever an empty square is represented as), and thereby check if the location is valid.

\$\endgroup\$
11
  • \$\begingroup\$ Let say I click at a certain location in my game. if I would like to create a rectangle to visualize my selection area. How can I somehow check if the rectangle intercepts any movable location? \$\endgroup\$ Mar 26, 2013 at 20:13
  • \$\begingroup\$ @user1185305 Ah, that's a different question. I'll edit my answer for that in a minute. \$\endgroup\$
    – Polar
    Mar 26, 2013 at 20:15
  • \$\begingroup\$ Thanks Polar. The problem is that if I don't use this method I will probably only have specific locations to check if I can move my piece. \$\endgroup\$ Mar 26, 2013 at 20:16
  • \$\begingroup\$ I have come so far that I can convert my screen mouse coordinates to world coordinates, using ray picking and I can then select my objects. The problem I had was that what happens if the object I'm selecting is part of a bigger object. I would have liked to know what area of that object I'm selecting so to say. If I have drawn a plan in Opengl on the screen. Can I somehow divide that plane into a grid ? \$\endgroup\$ Mar 26, 2013 at 21:15
  • \$\begingroup\$ @user1185305 Wow, that's another completely different question - I must be incredibly tired to keep on missing these :) Unfortunately, I can't seem to see exactly what you're trying to do. Could you try improving the question? \$\endgroup\$
    – Polar
    Mar 26, 2013 at 21:25

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .