Grid for my 3D board game

Started by
8 comments, last by BroderickCalpe 11 years, 1 month ago

Hi!

I have a 3D model of a board game. Peg Solitaire to be exact. I created my board in Blender with holes and so on.

Now I have my spheres and they are placed on the board. What I don't know how to solve is to make sure that the translation of the spheres is only possible to the position of the holes. So they can't be moved to any other place on the board not having holes.

Someone told me to use a Grid. My question is, how should I create this grid?

2D or 3D grid?

Any examples, tutorials ?

Please help.

Advertisement

Peg solitaire is 2D, so you should use a 2D grid. Assuming equidistant spacing between sockets,
you can just use a grid that is 7x7 (there will be 4 2x2 areas in the corners where the grid initially filled with "invalid" state (for instance.)), and then sample this array for the state of the corresponding socket.

The grid could be a 1D array, or arrays or vectors contained within oneanother to create a 2D array.


pegsockets[7*7]
// initialize array with peg states (1's for integer states, for instance).
// manually invalidate corners, as the sockets there don't exist (2's for integer states, for instance)
// remove the 3, 3 -peg, (set 0 for integer states, at index [7*3+3] )

-And then just use the array.

What language are you using?

Peg is 2D but I created my model in 3D. Does this still mean that It only moves in 2D space ?

What about the holes, the sphere moves in X and Y but I guess even i Z position when going up or down to the hole or have I got it wrong ?

Any tutorial maybe or example to have a look at?

Peg is 2D but I created my model in 3D. Does this still mean that It only moves in 2D space ?

What about the holes, the sphere moves in X and Y but I guess even i Z position when going up or down to the hole or have I got it wrong ?

Any tutorial maybe or example to have a look at?

What language are you using? General examples on arrays and such can be found many places, i often refer to cplusplus.com.

I suppose the pegs could be moved in a xy-plane, and will be transformed along an up vector when they're undergoing placement(into board) or picking(from board)

Do you plan on implementing this inside blender, or have you chosen a rendering API to render the board?

I'm writing it in C++. Sorry for missing that question

I created my board using Blender. Then I use Gameplay3D.org, as a game engine to load my model.

I'm writing it in C++. Sorry for missing that question

I created my board using Blender. Then I use Gameplay3D.org, as a game engine to load my model.

Oh, Gameplay3D.org looks neat. Through Gameplay3D.org you should be able to define transformation matrices for your models.

Like you talked with Alvaro about in one of your recent threads, a transformation stack implies being able to transform some objects on levels further "in" by transforming their parents.

You need to make sure first, how to manage this tree/stack/hierarchy.

In C++, you could write (for instance)


#include <vector>
...
std::vector< int > board_map(7*7, 1);  // Initialize a 7x7 (49 long 1D vector) board with value 1 (meaning it's full of pegs)
...
void setregion(const int &value, const unsigned &x0, const unsigned &y0, const unsigned &width, const unsigned &height)
{
  for(unsigned y = y0; y0+height != y; ++y) {
    for(unsigned x = x0; x0+width != x; ++x) {
      board_map[y*7+x] = value;
    }
  }
}
...
setregion(2, 0, 0, 2, 2);  // Invalidate top-left corner
setregion(2, 5, 0, 2, 2);  // Invalidate top-right corner
setregion(2, 0, 5, 2, 2);  // Invalidate bottom-left corner
setregion(2, 5, 5, 2, 2);  // Invalidate bottom-right corner
 
setregion(0, 3, 3, 1, 1);  // Remove center peg (setting its value to 0)
...
// When playing:
// create a function
int sample_board(x, y)  // that returns the board value for the given coords, and perhaps a
bool attempt_move(x0, y0, x1, y1) // that attemps to move a peg.
 
// The abovementioned functions should update the visual representation somehow. (transform the pegs on the level inside the // board transform level.

As you see, a simple container object can hold the entire state of your solitaire game.

As I was referring to in my earlier post, the mentioned states could be:

0: Empty (as in no peg)

1: Peg

2: Invalid (Board doesn't allow pegs here)

Since this is in the for begginers I will assume you are pretty new to programming, so don't get offended if I say something that may sound obvious ok?

I believe people told you to use a grid in order to implement your game logic, not the movement.

If you have the center of a piece and the board is proportional all you have to do to move a piece is to add twice the distance between the pieces to its front or side (mostly x and z axis). If you just want to move the piece instantly you will only need to change a single coordinate (up - mostly y axis - and one of the other two axis is not changed, as you can't move in a diagonal).

The problem is that not every move is legal and here is where a 2d grid (which is a basically a matrix) takes place. You can use it to map the places of your board that exists and the ones that are filled with a piece, so you can easily check if a movement is legal or not. For instance, even if you use only a simple integer matrix, you could have:


      0   1  2  3  4   5   6   <- x 
 0  [-1, -1, 1, 1, 1, -1, -1]
 1  [-1, -1, 1, 1, 1, -1, -1]
 2  [ 1,  1, 1, 1, 1,  1,  1]
 3  [ 1,  1, 1, 0, 1,  1,  1]
 4  [ 1,  1, 1, 1, 1,  1,  1]
 5  [-1, -1, 1, 1, 1, -1, -1]
 6  [-1, -1, 1, 1, 1, -1, -1]
/ \
 |
 y

Where:

1 - Means that a piece exists.

-1 - Means the cell doesn't exist.

0 - Means an empty cell.

Having this, let's say the player want to move the from (0, 3) to (0, 1); you can check that (0, 1) doesn't exists, so it is an illegal movement.

But if the player wants to move from (1,3) to (3, 3), you can check that (3, 3) exists and is empty (because it is zero) and (2, 3) exists and has a piece (because it is 1) so the movement is legal. You update the matrix, move the piece, check if the game is not over and let the game continue.

Hope this clarifies things a little.

PS: You can also use the grid to render the pieces every frame (if there is a a piece, you calculate the coords by the indexes of the grid and render a piece in the position). This will work, but you won't be able to annimate the movements.

Currently working on a scene editor for ORX (http://orx-project.org), using kivy (http://kivy.org).

Thanks for the detailed explanation. This is really helpful.

I have some questions. If I want to animate the movement. How should I go about doing that ?

Another question. I get the part by using the Grid.

How is this mapped to the actual translation positions ?

If I check if a move is legal, how is that then mapped to the actual translation move. Let's say that the center piece is at origin, (0, 0).

The hole in position (3, 3) is at this position in blender (0.0, 0.0, 0.0). The sphere at position (6, 3) is in Blender at position (0.0, 0.0, -3.29).

Using game play if I translate that sphere to position (0.0, 0.0, 0.0) it moves to the middle, where the hole is. How is all this mapped to the actual grid so I can check legal moves and position and so on ?

Sorry for the newbie questions. :-)

Basically you know that distance between each piece is 1.096666667.

So when you need to move a piece, it will move 1.096666667 * 2 = 2.193333334 in a single coord. You have to analize the direction of the change to detemine if the change will be positive or negative and which axis is going to be moved.

To map the index to a coord is simple math, you know the distance in x and z is 1.096666667 and (3, 3) is (0, 0, 0).

So cell on index i, j will be (assuming that the z axis decreases as the the index increases):

(3 - i) * 1.096666667, 0, (3 - j) * 1.096666667

Also, when a movement happens you will need to update the matrix, as one piece will be removed (the cell must be marked as 0), one cell will be left empty and the former empty cell will be marked occupied now.

To annimate the move you must look for position interpolation, which is move a piece a little every second. If you tell us what engine you are using we may help a little more.

Currently working on a scene editor for ORX (http://orx-project.org), using kivy (http://kivy.org).

KnolanCross - Your explanations are really good. This really explains a lot to me. Very common to get answers assuming that you know a bunch of stuff. Thumbs up for your answers.

I'm using Gameplay3D found here. www.gameplay3d.org

This topic is closed to new replies.

Advertisement