I want to move an object to a point in the game world, I have the distance value which is far the player should move. I want to complete the movement in X seconds.
I have tried many equations and formulas to no success.
Speed = Distance/Time
This one doesn't seem to give me the right result. Thing is the game is updated 60 times a second and the movement is affected by delta time.
I'm pretty sure I am not taking the framerate in consideration.
What am I doing wrong?
This is how I calculate the speed:
int dst = 500; int time = 2; // in seconds float speed = dst / time;
Doing it this way is making the movement instant and the game object is not traveling the distance in 2 seconds.
Every frame each game objects gets updated this way:
p.x += dir.x * speed * deltaTime; p.y += dir.y * speed * deltaTime;
The dir vector holds the direction of movement which is calculated on mouse click:
Vector2 dir = new Vector2(mouseX, mouseY).subtract(objectX, objectY).normalize();
Delta time calculation:
long time = System.nanoTime(); float deltaTime = (time - lastTime) / 1000000000.0f; lastTime = time;
Please note: The game object stops upon arrival.