# Lerping to a center point while in motion

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?

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

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
}
``````
-