Quantcast
Channel: Community | MonoGame - Latest topics
Viewing all articles
Browse latest Browse all 6821

How to calculate the duration to travel between two vectors?

$
0
0

@EnemyArea wrote:

Hey guys, I need some help :slight smile:
I want to move a sprite along a path. That path is over 256px. It starts from A:(X:160, Y:576) to B:(X:416, Y:576). How can I get the duration that I need to travel von a to b? My first try was to calculate the distance between the to vectors and than divide that result by the speed in pixels.

var movementSpeed = 32 * 5;
var length = Vector2.Distance(startPosition, targetPosition);
var duration = (length / movementSpeed); // 1.6 seconds

After that, I use Vector2.Lerp to move. It traveled within 1.6 seconds to its target.

But If I use the way to move it by gametime I would wrote something like:

var direction = Vector2.Normalize(targetPosition - startPosition);
var vel = direction * movementSpeed * (float)gameTime.ElapsedGameTime.TotalSeconds;

I move the same way in 1.4 seconds. Both times I set fixed timestamp + vsync (60 fps).
Where is my mistake?

Posts: 3

Participants: 3

Read full topic


Viewing all articles
Browse latest Browse all 6821

Trending Articles