Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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?

share|improve this question
    
Don't use the absolute time, instead use delta time and add to the previous result. –  API-Beast Oct 27 '12 at 11:21
add comment

1 Answer 1

up vote 1 down vote accepted

It looks like you want 3 states for your enemy: facing away, transitioning, and facing in.

Try something like this:

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

    // set rotation matrix
}
// transitioning
else if (timeAlive < 7) // i arbitrarily picked a transition time of 2 (7 - 5 = 2)
{
    float oldAngle = (float)Math.Atan((0 - position.X) / (0 - position.Y));
    float newAngle = -1 * (float)Math.Atan((0 - position.X) / (0 - position.Y));
    angle = MathHelper.Lerp(oldAngle, newAngle, timeAlive - 5);

    // set rotation matrix
}
// facing in
else
{
    float angle = -1 * (float)Math.Atan((0 - position.X) / (0 - position.Y));

    // set rotation matrix
}
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.