Hey guys,
I have been trying to figure out this stupid rotation algorithim for my fighter to direct him to a nav point in my own D3D engine I'm playing with. Problem is I keep finding bugs in it that make it look like it's being piloted by a drunk. So far I've been able to make him fly from the origin to any point in space with no problem, he just points and shoots. But here is another math issue that has me dead in the water:
Here are my knowns: vDir(0,1,0) vPos(-50,0,100) vNav(50,0,100)
I decided I needed to break it down into 2 planes, one for the Y rotation and one for the X rotation. The rotation along the objects X axis works fine in this instance, but the Y rotation seems to break because the direction vector is (0,0) as the direction is directly up... If I pitch the fighter down the vector changes but it increases along the Nav vector, screwing everything up...
Does anyone have a solution to by-pass this rotation mess? I suspect I'll have the exact same problem yawing my fighter if it's direction vector is (1,0,0) and the nav vector is (0,1,0).
D3DXVec3Normalize(&vDir,&vDir);
D3DXVECTOR2 vDirX(vDir.y, vDir.z);
D3DXVECTOR2 vPosX(vPos.y, vPos.z);
D3DXVECTOR2 vNavX(vNav.y, vNav.z);
D3DXVECTOR2 vDirY(vDir.z, vDir.x);
D3DXVECTOR2 vPosY(vPos.z, vPos.x);
D3DXVECTOR2 vNavY(vNav.z, vNav.x);
if(vNavX != vPosX)
vNavX = vNavX - vPosX;
if(vNavY != vPosY)
vNavY = vNavY - vPosY;
D3DXVec2Normalize(&vNavX,&vNavX);
D3DXVec2Normalize(&vNavY,&vNavY);
float anglex = 0;
float angley = 0;
float xSpeed =0;
float ySpeed =1;
if(((vNavX.y * vDirX.y)+(vNavX.x * vDirX.x)))
anglex = acos( ((vNavX.y * vDirX.y)+(vNavX.x * vDirX.x)) / (D3DXVec2Length(&vNavX) * D3DXVec2Length(&vDirX)) );
else
anglex= acos(0.0f);
if(((vNavY.y * vDirY.y)+(vNavY.x * vDirY.x)))
angley = acos( ((vNavY.y * vDirY.y)+(vNavY.x * vDirY.x)) / (D3DXVec2Length(&vNavY) * D3DXVec2Length(&vDirY)) );
else
angley= acos(0.0f);
if(vNav.z < 0){
anglex = anglex * -1;
xSpeed = xSpeed * -1;
}
if(anglex > 0.001){ //Need to round it off cause of floating point accuraccy.
if(xSpeed > anglex)
xSpeed = anglex;
}
else if(anglex < -0.001){
if(xSpeed < anglex)
xSpeed = anglex;
}
else xSpeed = 0;
if(vNavY.y < 0){
angley = angley * -1;
ySpeed = ySpeed * -1;
}
if(angley > 0.001){
if(ySpeed > angley)
ySpeed = angley;
}
else if(angley < -0.001){
if(ySpeed < angley)
ySpeed = angley;
}
else ySpeed = 0;
Obj->Rotate(xSpeed,ySpeed,0);
I have been trying to figure out this stupid rotation algorithim for my fighter to direct him to a nav point in my own D3D engine I'm playing with. Problem is I keep finding bugs in it that make it look like it's being piloted by a drunk. So far I've been able to make him fly from the origin to any point in space with no problem, he just points and shoots. But here is another math issue that has me dead in the water:
Here are my knowns: vDir(0,1,0) vPos(-50,0,100) vNav(50,0,100)
I decided I needed to break it down into 2 planes, one for the Y rotation and one for the X rotation. The rotation along the objects X axis works fine in this instance, but the Y rotation seems to break because the direction vector is (0,0) as the direction is directly up... If I pitch the fighter down the vector changes but it increases along the Nav vector, screwing everything up...
Does anyone have a solution to by-pass this rotation mess? I suspect I'll have the exact same problem yawing my fighter if it's direction vector is (1,0,0) and the nav vector is (0,1,0).
D3DXVec3Normalize(&vDir,&vDir);
D3DXVECTOR2 vDirX(vDir.y, vDir.z);
D3DXVECTOR2 vPosX(vPos.y, vPos.z);
D3DXVECTOR2 vNavX(vNav.y, vNav.z);
D3DXVECTOR2 vDirY(vDir.z, vDir.x);
D3DXVECTOR2 vPosY(vPos.z, vPos.x);
D3DXVECTOR2 vNavY(vNav.z, vNav.x);
if(vNavX != vPosX)
vNavX = vNavX - vPosX;
if(vNavY != vPosY)
vNavY = vNavY - vPosY;
D3DXVec2Normalize(&vNavX,&vNavX);
D3DXVec2Normalize(&vNavY,&vNavY);
float anglex = 0;
float angley = 0;
float xSpeed =0;
float ySpeed =1;
if(((vNavX.y * vDirX.y)+(vNavX.x * vDirX.x)))
anglex = acos( ((vNavX.y * vDirX.y)+(vNavX.x * vDirX.x)) / (D3DXVec2Length(&vNavX) * D3DXVec2Length(&vDirX)) );
else
anglex= acos(0.0f);
if(((vNavY.y * vDirY.y)+(vNavY.x * vDirY.x)))
angley = acos( ((vNavY.y * vDirY.y)+(vNavY.x * vDirY.x)) / (D3DXVec2Length(&vNavY) * D3DXVec2Length(&vDirY)) );
else
angley= acos(0.0f);
if(vNav.z < 0){
anglex = anglex * -1;
xSpeed = xSpeed * -1;
}
if(anglex > 0.001){ //Need to round it off cause of floating point accuraccy.
if(xSpeed > anglex)
xSpeed = anglex;
}
else if(anglex < -0.001){
if(xSpeed < anglex)
xSpeed = anglex;
}
else xSpeed = 0;
if(vNavY.y < 0){
angley = angley * -1;
ySpeed = ySpeed * -1;
}
if(angley > 0.001){
if(ySpeed > angley)
ySpeed = angley;
}
else if(angley < -0.001){
if(ySpeed < angley)
ySpeed = angley;
}
else ySpeed = 0;
Obj->Rotate(xSpeed,ySpeed,0);