2

I am not asking for someone to program this for me, I still would like to do all the programming, however I need some additional mathematical programming background for this kind of stuff because I haven't really dabbled into 3D spaces much and I am thinking lots of potential if-thens and switches could be simplified way down if I think of this concept the right way. I will be using a programming language that supports 2D tiled or text screen output (think Java). I want to know if there are simple ways to program in the viewing, orienting and moving about this virtual space I am about to describe without resorting to lots of switch statements to control i.e. what the screen will be displaying depending on the PC's location, facing direction and rotation.

Okay so far I am working out concepts of a way to explore a 3D top-down maze type environment with a procedurally generated voxel landscape and a 1x1x1 player character which can view only the eye level plane and the plane 1 unit below it. the PC can move in the following ways; Please note I intend whichever way the PC considers down to be which direction gravity is acting upon it, (unrealistic):

  • Walk forward to another square unobstructed by a wall or cliff
  • Turn the face another direction, in which case a different direction becomes the PC's forward face (visually). The PC will always appear to be facing up for the user.
  • Start scaling a wall, in which case the previously upward direction (out of the screen, towards the user) becomes forward.
  • Climb down and scale the wall of a pit below, in which case the previously down direction become forward.
  • Flip upside down and tether off the now ceiling, scaling up or down the tether until the PC hits a floor. in which case it can choose other transportation modes again.
  • Each time the PC moves, all other entities get their chance to move in a turn based fashion.

This won't be like a Minecraft-esque sandbox, so editing the landscape after generation won't be likely necessary. I will have a screen that views 16x16x2 voxels at a time. I do however like the idea of dynamically allocated chunks. I plan on generating or loading previously generated 16x16x16 chunks of voxels and the adjacent 26 chunks as needed. while unloading out of range chunks and saving them to a file. I want to know the mathematical/conceptual ideas behind generating these chunks and loading already generated chunks. Note I still use the term voxel even though I will only view them as squares on a text screen, because the game space is a 3D grid.

1 Answer 1

1

There will be 24 different orientations for your character. So you definitely don't want to go with if/then/else statements.

If you describe your character orientation as a 3x3 matrix (consisting of 0, 1 and -1) then each type of turn is achieved by multiplying that matrix by a rotation matrix (also 0,1&-1).

The orientation matrix

Consider a positive orthonormal axis system X'Y'Z', describing the orientation of your character in your 3D world. Typically, X' would point to the characters right. Y' would point upwards (from the characters perspective) and Z' would point backwards. (forward would seem more logical, but that would not be a right-handed system.)

These 3 vectors make up the columns of the 3x3 orientation matrix.

Note that you still have to keep track of the characters position separately.

Rotations

Whenever the character makes a turn, its orientation matrix changes. This can be achieved through rotation matrices. To perform the rotation, you multiply the rotation matrix it with a vector, or in this case with the entire orientation matrix:

NewOrientationMatrix = RotationMatrix * OldOrientationMatrix

To describe all possible 90 degree rotations around main axes X, Y and Z, you need 6 rotation matrices.

eg. A 90 degree rotation around the Z axis, clockwise when looking against the positive Z direction, would be:

0 -1  0
1  0  0
0  0  1

Note that these rotation matrices are specified in absolute space (XYZ), not in X'Y'Z'.

Transform to the Characters Viewpoint

Suppose you have a point in 3D space, and you want to know where this point lies, relative to your character:

First you subtract the characters position:

point - charactersPosition

Now, you want to decompose this vector to X', Y' and Z' components. For that you multiply with the inverse (which is in this case the transpose) of your orientation matrix:

relativePoint = Transpose(OrientationMatrix) * (point - charactersPosition)

Eg. If the result is (3,0,-1) then this point lies 3 units to the characters right, one unit forward and same height.

6
  • Can you explain to me how pi/2 or -pi/2 rotations about X, Y or Z can be performed with rotation matrices? I tried the wikipedia article but "my eyes glazed over."
    – 0xFFF1
    Commented May 30, 2013 at 18:12
  • A rotation of pi/2 along the Z axis: [[0,1,0][-1,0,0][0,0,1]] Commented May 30, 2013 at 20:55
  • what would be an orientation matrix of my character facing north <x,y,z> = <0,-1,0> how would I generate an initial orientation matrix? Since I am using a text screen, I think down the screen is positive, rightward is positive and towards the user is positive.
    – 0xFFF1
    Commented May 30, 2013 at 23:00
  • I would recommend that you stick to right-handed systems. So you either you inverse your Y, or you inverse your Z. Commented Jun 1, 2013 at 7:41
  • Did I get this right? pastebin.com/zUvE0YV9 format for this language is array[ind0,ind1]=val;
    – 0xFFF1
    Commented Jun 3, 2013 at 4:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.