#### Archived

This topic is now archived and is closed to further replies.

# moving using a direction vector?

This topic is 5433 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I''m trying to create an Moveable class for my engine, and I want to have a direction vector that specify what direction the object is moving source:
class cMoveable {

cMatrix m_Matrix; // the final matrix with the rotation position etc.

// This is the objects local axis in world-space

cVector3f m_Up;
cVector3f m_Right;
cVertex3f m_Position;

// Objects rotation/movement in model-space

cQuaternion m_Rotation;
cVertex3f   m_Pivot;

public:

cMoveable();

// move the object along it''s z-axis ( forward when positiv value )

void Move(float fSpeed);

// Move the object along it''s x-axis ( going right when positiv )

void Strafe(float fSpeed);

// fly ( going up when positive value )

void Fly( float fSpeed );

// this 3 functions changes the m_Forward, m_Right, m_Up so the directions turns in the specefic angle depending on Yaw,Pitch,Roll functions

void Yaw(float angle);
void Pitch(float angle);
void Roll(float angle);

// Rotates the object around it''s own Piviot point independent of the m_Forward, m_Right and m_Up vectors.

void RotateX(float angle);
void RotateY(float angle);
void RotateZ(float angle);

// Moves the piviot point or moves the object in model-space

void Translate(float _x, float _y, float _x);
};

cMoveable::cMoveable()
{
// initial state

m_Forward = cVector3f(0,0,-1);
m_Up = cVector3f(0,1,0);
m_Right = cVector3f(1,0,0);
m_Position = cVertex3f(0,0,0);

m_Rotation.Identity();
m_Piviot = cVertex3f(0,0,0);

m_Matrix.Identity();
}

so what I want is the above functions working like this: //to move an object forward ( the objects piviot point is center) : Object->Move( 2.0f ); // to turn the object left 45 degrees, the object will not rotate or anything only the m_Forward, m_Right and m_Up vectors will be re-calculated. Object->Yaw( 45.0f ); // then when use move to move now the object will move in the new direction ( as if it would be a car that have turned left but the object HAS NOT been rotated have to do it self with cMoveable::RotateY() ) Object->Move( 2.0f ); it should work something like this: first create a matrix that rotates everything so that the Models Z-Axis is pointing the same direction as the m_Forward vector does in world-space, and do the same for X-Axis and m_Right and Y-Axis and m_Up. then create a matrix that rotates the object around the m_Piviot vertex using the rotation information in the m_Rotation quaternion. maybe like this?!: cMatrix tMat, rMat; // two new identity matrix for object rotation and translation; m_Rotation.QuaternionToMatrix( rMat ); tMat.Translate( m_Position ); // multiply the rotation before the translation becouse it should rotate around the point, if the point is 0,0,0 (world-space) and the objects center is 0,10,0 (world-space) then it would rotate in a circle around the the center. m_Matrix.Multiply( rMat ); m_Matrix.Multiply( tMat ); but how do the rest of the rotations work? how to rotate the forward, right and up vectors in the Yaw,Pitch,Roll functions? and I will use this class as a camera to, then I have heard of the gimbal lock thing, how to prevent that?

##### Share on other sites
If you know how to rotate one vector, you know how to rotate all of them. To calculate the transformed forward vector, you transform (0,0,1). To transform the up vector, you transform (0,1,0), etc. I wrote code for this if you want it. It works like:

vector(0,0,1) rotated_by vector(180,0,0)

~CGameProgrammer( );

-- Post screenshots of your projects. 100+ posts already in the archives.

##### Share on other sites
yes I would like to have the code you have written

I have tried alot to create a camera class with this.. but I always run into that gimbal lock thing..

##### Share on other sites
what is the code for this function to work properly??

cMatrix::Rotate(float angle, float x_axis, float y_axis, float z_axis);

##### Share on other sites
Here''s my code. It''s right-handed, so you''ll have to fiddle with signs of some things for left-handed.

#define rotated_by ^vector vector :: operator rotated_by ( vector Rotation ){    static D3DXMATRIX   Matrix;    static D3DXVECTOR3  LookAt;    static vector       Return;    LookAt.x = X;    LookAt.y = Y;    LookAt.z = -Z;    D3DXMatrixRotationYawPitchRoll( &Matrix, -float(PI_OVER_180) * (float)Rotation.Y, -float(PI_OVER_180) * (float)Rotation.X, -float(PI_OVER_180) * (float)Rotation.Z );    D3DXVec3TransformCoord( &LookAt, &LookAt, &Matrix );    Return.X = (Type)LookAt.x;    Return.Y = (Type)LookAt.y;    Return.Z = (Type)LookAt.z;    return Return;}

Hope everything is self-explanatory. As you can see, it uses D3DX, so it''s nice and simple.

~CGameProgrammer( );

-- Post screenshots of your projects. 100+ posts already in the archives.

1. 1
2. 2
3. 3
4. 4
5. 5
Rutin
11

• 12
• 19
• 10
• 14
• 10
• ### Forum Statistics

• Total Topics
632665
• Total Posts
3007711
• ### Who's Online (See full list)

There are no registered users currently online

×