I have a missile and its propelled "forwards" using its current rotation. It can have a target in which case it knows the pos of this target.
I want it to gradually turn towards the target, resulting in a "target-seeking" routine. Problem is i dont know how Lets assume:
// MAX macro
#ifndef MAX
#define MAX(a,b) a > b ? a : b
#endif
// MIN macro
#ifndef MIN
#define MIN(a,b) a < b ? a : b
#endif
// Normalize to interval 0 - 360
void NormalizeAngle( float& ang )
{
while( ang >= 360 ) ang -= 360;
while( ang < 0 ) ang += 360;
}
// Try angles
// tolerance - the dif in rotation, under which it stops rotating
int FindDirection( float from, float to, float tolerance)
{
f32 diff=fabs(to-from);
if( diff < tolerance ) return 0;
if( diff > 360-tolerance ) return 0;
// Normalize
NormalizeAngle( from );
NormalizeAngle( to );
// Equal, do nothing
if( from == to ) return 0;
// max and min
float max = MAX(from, to);
float min = MIN(from, to);
// Differences
float difN = max - min;
float difI = (360 - max) + min;
// If normal is lower than inverse
if( difN <= difI )
if( from > to )
return 1;
else
return 2;
else if( difN > difI )
if( from > to )
return 2;
else
return 1;
else
return 0;
}
..
// Depending on how many axes you need ..
// Note that getHorizontalAngle() only returns Y and Z
vector3df targetHeading = vector3df(targetPos - missPos).getHorizontalAngle();
int rotationDirectionOnY = FindDirection(missRot.Y, targetHeading .Y, 2);
//int rotationDirectionOnZ = FindDirection(missRot.Z, targetHeading .Z, 2);
// On Y ..
if(rotationDirectionOnY == 1) // Have to turn left
{
missile->setRotation( missRot - vector3df(0, turnSpeed, 0) );
}
else if(rotationDirectionOnY == 2) // Have to turn right
{
missile->setRotation( missRot + vector3df(0, turnSpeed, 0) );
}
else // If 0 is returned, the missile has turned enough (the dif in angles is less than 2 eg.)
{
missile->setRotation( targetHeading );
// Do nothing .. or do something
}
..
Last edited by Eigen on Sun Nov 30, 2008 10:51 am, edited 2 times in total.