1
\$\begingroup\$

I'm trying to get this object to move right across the screen until it gets close to the center and arc down smoothly into a straight downward movement. Here's what I've tried:

if (position.X < -5000)
   Velocity = Vector3.Right * speed;
else if (position.X < -1000)
{
   Velocity = new Vector3(
   MathHelper.Lerp(Velocity.X, 0, (float)gameTime.ElapsedGameTime.TotalSeconds), 
   MathHelper.Lerp(Velocity.Y, -1, (float)gameTime.ElapsedGameTime.TotalSeconds), 0) 
   * speed;
}
else
   Velocity = Vector3.Down * speed;

However, when it gets to where it should be arcing, it just moves to the right at blinding speed and vanishes before it hits the point where it should be going straight down. If I remove the else if, it does a sharp direction shift no problem. What am I doing wrong in that section?

\$\endgroup\$

1 Answer 1

2
\$\begingroup\$

Your problem is (float)gameTime.ElapsedGameTime.TotalSeconds. Those Lerp functions you're using want a value that varies from 0 to 1, while TotalSeconds is going to keep increasing forever.

Maybe instead pass in the amount of time that has passed since the object started turning, and stop lerping after a time of 1?

\$\endgroup\$
2
  • \$\begingroup\$ gameTime.ElapsedGameTime.TotalSeconds is going to be the time since the last update. You're thinking of gameTime.TotalGameTime.TotalSeconds, which is the total from the start of the game. \$\endgroup\$
    – Fibericon
    Mar 14, 2012 at 3:17
  • \$\begingroup\$ Whoops! Either way, it's not the value that he actually wants to be passing into the Lerp. \$\endgroup\$ Mar 14, 2012 at 4:58

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .