Turning towards goal (seeking missile)

If you are a new Irrlicht Engine user, and have a newbie-question, this is the forum for you. You may also post general programming questions here.
Post Reply
suliman
Posts: 379
Joined: Sat Sep 23, 2006 2:06 pm

Turning towards goal (seeking missile)

Post by suliman »

Hi

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:

Code: Select all

vector3df missRot;
vector3df missPos;
vector3df targetPos; 
float turnSpeed;
What would be a possible way to do this? thanks a lot
Eigen
Competition winner
Posts: 375
Joined: Fri Jan 27, 2006 2:01 pm
Location: Estonia
Contact:

Post by Eigen »

I found this code somewhere and I'm using it in my project. Also, I made some slight changes.

Code: Select all

// 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;
} 

Use it like this:

Code: Select all


..

// 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.
zeno60
Posts: 342
Joined: Sun May 21, 2006 2:48 am
Location: NC, USA
Contact:

Post by zeno60 »

You could also find some methods on google by searching for something like Steering Behaviors & Seek.
Post Reply