Lerping to a center point while in motion

Posted by Fibericon on Game Development See other posts from Game Development or by Fibericon
Published on 2012-10-27T07:59:52Z Indexed on 2012/10/27 11:24 UTC
Read the original article Hit count: 212

Filed under:
|

I have an enemy that initially flies in a circular motion, while facing away from the center point. This is how I achieve that:

position.Y = (float)(Math.Cos(timeAlive * MathHelper.PiOver4) * radius + origin.Y);
position.X = (float)(Math.Sin(timeAlive * MathHelper.PiOver4) * radius + origin.X);

if (timeAlive < 5)
{
   angle = (float)Math.Atan((0 - position.X) / (0 - position.Y));

   if (0 < position.Y)
      RotationMatrix = Matrix.CreateRotationX(MathHelper.PiOver2) * Matrix.CreateRotationZ(-1 * angle);
   else
      RotationMatrix = Matrix.CreateRotationX(MathHelper.PiOver2) * Matrix.CreateRotationZ(MathHelper.Pi - angle);
}

That part works just fine. After five seconds of this, I want the enemy to turn inward, facing the center point. However, I've been trying to lerp to that point, since I don't want it to simply jump to the new rotation. Here's my code for trying to do that:

else
{
   float newAngle = -1 * (float)Math.Atan((0 - position.X) / (0 - position.Y));
   angle = MathHelper.Lerp(angle, newAngle, (float)gameTime.ElapsedGameTime.Milliseconds / 1000);

if (0 < position.Y)
   RotationMatrix = Matrix.CreateRotationX(MathHelper.PiOver2) * Matrix.CreateRotationZ(MathHelper.Pi - angle);
else
   RotationMatrix = Matrix.CreateRotationX(MathHelper.PiOver2) * Matrix.CreateRotationZ(-1 * angle);
}

That doesn't work so fine. It seems like it's going to at first, but then it just sort of skips around. How can I achieve what I want here?

© Game Development or respective owner

Related posts about XNA

Related posts about linear-algebra