Sunday, May 19, 2019

2d - How to move an object along a vector


Let's say I got an object A and object B in a 2D game. I create a vector leading from A to B. It's name is AB.


How can I make A move along the vector AB and reach B?



One way I was thinking of doing this, is calculate the angle between AB and the x axis, and then move the object every game-loop cycle in that angle, using trigonometry.


I would calculate that angle by making a new normalized vector (1,0) (the x axis), normalize AB, and then get the angle between them by getting their dot product and using arccos on it.


But is there an easier way to make an object follow the path of a vector?


EDIT:


In this question: Make objects follow a strict path (Xna), the way someone suggested to move an object along a vector, is like so:


position += direction * speed * elapsed;

Where:



  • position = current position of the object.


  • direction = a normalized vector pointing in the direction of the destination.

  • speed = a scalar to decide how much to advance the object every cycle of the game-loop. (Is this a 'scalar'? Am I using this word correctly?)

  • elapsed = what is this?


I get everything but elapsed. What is this? Is this necessary?


Anyhow, is this method a good method? Would you recommend it?


Thanks




No comments:

Post a Comment

Simple past, Present perfect Past perfect

Can you tell me which form of the following sentences is the correct one please? Imagine two friends discussing the gym... I was in a good s...