(C++) Guided missiles: pitching and rolling to the target

Post those lines of code you feel like sharing or find what you require for your project here; or simply use them as tutorials.
Post Reply
Xaron
Posts: 310
Joined: Sun Oct 16, 2005 7:39 am
Location: Germany
Contact:

(C++) Guided missiles: pitching and rolling to the target

Post by Xaron »

Dear all,

today I show you how to use guided missiles.
The following code gives you the delta pitch and delta roll to the target. So you can roll and pitch into the target which looks very nice instead of just using delta pitch and yaw, especially if you have a tail unit.
You just have to get these delta pitch and roll values to zero in your steering update section. If these (return) values are zero, your target is straight ahead. :)

Code: Select all

/**
 * @brief guided missile steering helper function
 *
 * @param missilePosition - current absolute position of the missile
 * @param missileRotation - current absolute rotation of the missile
 * @param targetPosition  - current absolute target position
 * @param oversteer       - oversteer factor in degrees
 *
 * @return targetPitchRoll - delta pitch (x) and delta roll (z) values to the target
 *
 *******************************************************************************************************/
core::vector3df getToTargetPitchRoll( core::vector3df& missilePosition, core::vector3df& missileRotation, core::vector3df& targetPosition, float oversteer = 0.0f )
{
  core::matrix4 rotMatrix, invRotMatrix;
  rotMatrix.setRotationDegrees( missileRotation );
  core::vector3df targetPitchRoll( 0.0f, 0.0f, 0.0f );
  // get the current missile direction
  core::vector3df currentMissileTargetVec( 0.0f, 0.0f, 1.0f );

  // get the vector to the target (from the missile position)
  core::vector3df missileTargetVec = ( targetPosition - missilePosition ).normalize();
  // invert the rotation matrix to transform the target vector into the missile's local space
  rotMatrix.getInverse( invRotMatrix );
  // transform the target vector into missile's local space
  invRotMatrix.rotateVect( missileTargetVec );
  // compute the roll angle to the target
  float deltaRoll = atan2f( missileTargetVec.Y, missileTargetVec.X ) * core::RADTODEG - 90.0f;
  if( deltaRoll < 0.0f )
    deltaRoll += 360.0f;
  if( deltaRoll >= 360.0f )
    deltaRoll -= 360.0f;

  if( deltaRoll > 180.0f )
    deltaRoll -= 360.0f;

  // compute the pitch angle to the target
  float deltaPitch = acosf( currentMissileTargetVec.dotProduct( missileTargetVec ) )
    * core::RADTODEG + oversteer;

  targetPitchRoll.X = -deltaPitch;
  targetPitchRoll.Z = deltaRoll;

  return targetPitchRoll;
}
Regards - Xaron
JPulham
Posts: 320
Joined: Sat Nov 19, 2005 12:06 pm

Post by JPulham »

Nice
pushpork
Crazy3dMax

Post by Crazy3dMax »

Thanks - that helped me a lot - math is the only thing where I go down in programming
Xaron
Posts: 310
Joined: Sun Oct 16, 2005 7:39 am
Location: Germany
Contact:

Post by Xaron »

You are welcome! :)

Regards - Xaron
NotYou

Post by NotYou »

Hey,

This is a bit of a noob question but I'm trying to learn....

How would I get it to update the missile every frame?
Would I just put it in the main while loop?(a check or call) Will that cause it to check it every frame?



Thanks
xhrit
Posts: 140
Joined: Mon Jun 14, 2004 8:54 am
Location: earth
Contact:

...

Post by xhrit »

>How would I get it to update the missile every frame?
I don't think you can use this on an animator, if that is what you are asking.

while we are with the n00b questions, how do I find yaw?
Core2Duo E8400 3.0ghz - 2048mb DDR2 800 - geForce 9600 - Slackware12.1

Word-image-symbol programming limits, controls, and imprisons the individual.
Smash the control images, smash the control machine.
Post Reply