Moving to Target not working
I''m trying to move a ship in 3D space to a target''s coordinates. I''ve been following the tutorials by Stefan Zebst and Gary Simmons but am having some difficulty. My ship just seems to fly off in the wrong direction. Any help would be appreciated as this problem is driving me crazy. Following is a snippet that is used to move the ship:
void Enemy_AI(void)
{
D3DXMATRIX PitchMatrix,YawMatrix, RollMatrix;
D3DXVECTOR3 vTraj, vUp, vRight, vLook;
vLook=D3DXVECTOR3 (0.0f,0.0f,1.0f);
vUp=D3DXVECTOR3 (0.0f,1.0f,0.0f);
vRight=D3DXVECTOR3 (1.0f,0.0f,0.0f);
D3DXVec3Subtract(&vTraj,&objTiger.Target,&objTiger.vPos);
D3DXVec3Normalize(&vTraj, &vTraj);
if(!Right_Direction(vTraj,objTiger.vHead))
Yaw_Left(&objTiger);
else
{
if (D3DXVec3Dot(&objTiger.vUp, &vTraj) > 0)
Pitch_Up(&objTiger);
else if (D3DXVec3Dot(&objTiger.vUp, &vTraj) < 0)
Pitch_Down(&objTiger);
if (D3DXVec3Dot(&objTiger.vRight, &vTraj) > 0)
Roll_Right(&objTiger);
else if (D3DXVec3Dot(&objTiger.vRight, &vTraj) < 0)
Roll_Left(&objTiger);
}
//If we want to simulate YAW we must Create a Matrix that rotates around the Cameras UP vector
D3DXMatrixRotationAxis(&YawMatrix, &vUp, objTiger.fRotY); //Pitch
//Two apply the ''Yaw'' we rotate the LOOK & RIGHT Vectors about the UP Vector.(Giving us Yaw)
D3DXVec3TransformCoord(&vLook, &vLook, &YawMatrix);
D3DXVec3TransformCoord(&vRight, &vRight, &YawMatrix);
//If we need to simulate Pitch we Create a Matrix that rotates around the Camera RIGHT vector
D3DXMatrixRotationAxis(&PitchMatrix, &vRight, objTiger.fRotX); //Yaw
//And then we rotate the LOOK & UP Vectors about the RIGHT Vector.(Giving us Pitch)
D3DXVec3TransformCoord(&vLook, &vLook, &PitchMatrix);
D3DXVec3TransformCoord(&vUp, &vUp, &PitchMatrix);
D3DXMatrixRotationAxis(&RollMatrix, &vLook, objTiger.fRotZ); //Roll
//And then we rotate the UP & RIGHT Vectors about the LOOK Vector.(Giving us Roll)
D3DXVec3TransformCoord(&vRight, &vRight, &RollMatrix);
D3DXVec3TransformCoord(&vUp, &vUp, &RollMatrix);
D3DXVec3Normalize(&vLook,&vLook);
D3DXVec3Cross(&vRight,&vUp,&vLook);
D3DXVec3Normalize(&vRight,&vRight);
D3DXVec3Cross(&vUp, &vLook, &vRight);
D3DXVec3Normalize(&vUp, &vUp);
//move in direction vLook, speed set to 0.5 as test
objTiger.vPos.x+=0.5f*vLook.x;
objTiger.vPos.y+=0.5f*vLook.y;
objTiger.vPos.z+=0.5f*vLook.z;
objTiger.vRight=vRight;
objTiger.vHead=vLook;
objTiger.vDir=vLook;
objTiger.vUp=vUp;
} //end Enemy_AI
///////////////////////////////////////////////////////////
void Roll_Right(EDOBJECT *pObj)
{
pObj->fRotZ -= 0.01f;
} // Roll_Right
///////////////////////////////////////////////////////////
void Roll_Left(EDOBJECT *pObj)
{
pObj->fRotZ += 0.01f;
} // Roll_Right
///////////////////////////////////////////////////////////
void Pitch_Up(EDOBJECT *pObj)
{
pObj->fRotX -= 0.01f;
} // Pitch_Up
///////////////////////////////////////////////////////////
void Pitch_Down(EDOBJECT *pObj)
{
pObj->fRotX += 0.01f;
} // Pitch_Up
///////////////////////////////////////////////////////////
void Yaw_Right(EDOBJECT *pObj)
{
pObj->fRotY += 0.01f;
} // Pitch_Up
///////////////////////////////////////////////////////////
void Yaw_Left(EDOBJECT *pObj)
{
pObj->fRotY -= 0.01f;
} // Pitch_Up
///////////////////////////////////////////////////////////
BOOL Right_Direction(D3DXVECTOR3 traj, D3DXVECTOR3 dir)
{
BOOL right_direction;
D3DXVECTOR3 add;
add.x = traj.x + dir.x;
add.y = traj.y + dir.y;
add.z = traj.z + dir.z;
right_direction = (add.x*add.x + add.y*add.y + add.z*add.z) >
(traj.x*traj.x + traj.y*traj.y + traj.z*traj.z);
return right_direction;
} //end Right_Direction
I doubt anyone is going to wade through your code to figure out what it is that you are doing, or doing wrong. Your question would be better served by distilling the algorithm from your code and posting it here... or in the AI forum (but don''t cross post please).
Cheers,
Timkin
Cheers,
Timkin
I concur with Timkin''s statement. Please summarize your current algorithm, with text and perhaps with a FEW equations, and clearly mark where you think the problem is.
Graham Rhodes
Senior Scientist
Applied Research Associates, Inc.
Graham Rhodes
Senior Scientist
Applied Research Associates, Inc.
Graham Rhodes Moderator, Math & Physics forum @ gamedev.net
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement