class Updateable
{
D3DXVECTOR3 Pos,Vel,Omega; //position vector in world coord, velocity in local coords, angular velocities
D3DXMATRIX Axes,T; //store a matrix for compound rotations, and the final transformation matrix
public:
Updateable( float yaw, float pitch, float roll,
float Px, float Py, float Pz,
float Vx, float Vy, float Vz,
float Wx, float Wy, float Wz
);
void Update();
};
Updateable::Updateable( float yaw, float pitch, float roll,
float Px, float Py, float Pz,
float Vx, float Vy, float Vz,
float Wx, float Wy, float Wz ) : Pos(Px,Py,Pz) , Vel(Vx,Vy,Vz) , Omega(Wx,Wy,Wz)
{
//set up initial rotations
D3DXMatrixRotationYawPitchRoll(&Axes,yaw,pitch,roll);
//Set the Transformation matrix
D3DXMatrixTranslation(&T,Px,Py,Pz);
D3DXMatrixMultiply(&T,&Axes,&T);
}
void Updateable::SetVel(float x,float y,float z)
{
Vel.x=x; Vel.y=y; Vel.z=z;
}
void Updateable::SetOmega(float x,float y,float z)
{
Omega.x=x; Omega.y=y; Omega.z=z;
}
void Updateable::Update()
{
D3DXMATRIX RotateBy; //how much the model rotates each frame
D3DXMatrixRotationYawPitchRoll(&RotateBy,Omega.y,Omega.x,Omega.z);
D3DXMatrixMultiply(&Axes,&Axes,&RotateBy); //update rotation matrix
//Axes/=D3DXMatrixfDeterminant(&Axes); //aim of this: normalise axes vectors?
D3DXVECTOR3 TranslateBy; //how much world coords will change by
D3DXVec3TransformCoord(&TranslateBy,&Vel,&Axes);
Pos+=TranslateBy;
//Now build our 4x4 combined 'rotate then translate' matrix
D3DXMatrixTranslation(&T,Pos.x,Pos.y,Pos.z);
D3DXMatrixMultiply(&T,&Axes,&T);
}
Do you reckon this is any good?
This is my class to keep track of movement and rotation of a 3d model. It is designed to eliminate gimbal lock problems by keeping track of the axes and rotating them with each update. Is this an efficient way of doing things?
Also, shouldn't I have to normalise the axis vectors due to floating point errors?
I think the code works properly!
Looking at the D3DXQuaternion stuff it seems that does exactly the same job. But I thought quaternions were a lot slower to multiply and especially to calculate a world transform matrix from a Quaternion. Which method (mine/proper version of mine/quaternions) do you guys think is most efficient?
[edited by - d000hg on June 5, 2002 4:19:39 PM]
First of all, implement framerate-independent motion. Increment things by delta*frametime, not by just delta.
Second, as long as your class keeps you happy, there''s no need to change it.
For my fps-style camera, I use the following data:
Vec3 Position;
// Basis
Vec3 Right, Up, Forward;
float Yaw, Pitch, Roll;
Basis is used for translations, Quake-style: you can strafe independently of where you''re looking. Euler angles define where you''re looking relative to the basis. I choose them instead of quaternions because they''re easier to constrain (say, max angle at which you can be looking up is 80 degrees).
Second, as long as your class keeps you happy, there''s no need to change it.
For my fps-style camera, I use the following data:
Vec3 Position;
// Basis
Vec3 Right, Up, Forward;
float Yaw, Pitch, Roll;
Basis is used for translations, Quake-style: you can strafe independently of where you''re looking. Euler angles define where you''re looking relative to the basis. I choose them instead of quaternions because they''re easier to constrain (say, max angle at which you can be looking up is 80 degrees).
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement