Ok so this is probably a stupid quesiton with an easy answer. Im learning directx and trying to create a simple 3d game. I need to move my character in the direction its facing rather than just using by the world axis.
I think its some easy maths i feel asleep during at school :P a quick hint would be appriecated.

Are your transforms 'full 3d' or 'half 3d'? That is, when your character rotates, can they rotate over all axes, or just around an 'up' vector? Are you using matrices as the authoritative representation of the character's transform, or some scalars ( typically x, y,z and rotation ).

MATRICES

If you're using transform matrices, the vector that points 'forward' in the character's space is one of the vectors in the upper 3x3 submatrix, the actual vector depends on the way you define 'up' and 'forward', and whether the vector is a row or column of the matrix depends on whether you treat vectors as rows or columns.. but for e.g., in OpenGL, it would usually be:

m00 m01 [b]m02[/b] m03
m10 m11 [b]m12[/b] m13
m20 m21 [b]m22[/b] m23
m30 m31 m32 m33

the usual 'forward' vector (z) is emboldened. If you extract 'forward' from the character's matrix, and add if to the character's position, it will move the character in their own 'forward'.

I know for a fact that this is different in most DX setups, but it will be quite similar, most likely the row m20->m22, assuming that z is forward, which it isn't always.

SCALARS

If you're just using x, y, z and rotation scalars.... then assuming that positive z is 'up' and positive x is 'forward'... the forward direction vector is:

x = cos ( r )
y = sin ( r )

so,

player.x += cos ( r ) * speed;
player.y += sin ( r ) * speed;

should work. if it comes out sideways ( i.e moving to the left/right rather than back/forward, then change to cos ( r + ( pi / 2 ) ) and sin ( r + ( pi / 2 ) ), or minus, perhaps multiply all by -1: it all depends on how you interpret components in your code.

commented: Another great post. +18

Thank you for your detailed responce, i will try this when i get home from work and get back to you.

nb. i am using half 3d but wil probably implement full 3d for code reusability as im attempting to create a framework for multiple game demos.

For anyone else reading directx matrix is:

http://msdn.microsoft.com/en-us/library/bb206269(VS.85).aspx

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.