2
\$\begingroup\$

I want to move my character along the x axis with constantly speed. I thought move depends on frame rate. So, technically I should write

sprite.body.velocity.x = speed * deltaTime

where deltaTime = game.time.elapsedMS / 1000;

But if I'm doing that - my character moves vvvvverrry slooow, even if speed = 1000.

But if I'm writing

sprite.body.velocity.x = speed

it works fine. My fps = 60;


Phaser Documentation says:

velocity - The velocity, or rate of change in speed of the Body. Measured in pixels per second.

no deltatime....

and all demos do not have deltatime

http://phaser.io/examples/v2/arcade-physics/platformer-basics

http://phaser.io/examples/v2/arcade-physics/asteroids-movement

etc.

So, I don't understand: shoud I calculate deltaTime or just use velocity.x?

\$\endgroup\$

1 Answer 1

2
\$\begingroup\$

Well.... I think I'm stupid...

My calculation of the deltaTime was wrong

The correct formula will be

deltaTime = (elapsedMS * fps) / 1000

elapsedMS - The time in ms since the last time update, in milliseconds, based on time.

fps - Frames per second. (Only calculated if advancedTiming is enabled).

So, that was my problem.


As the result

body.velocity doesn't include calculation of deltaTime and for smooth movement should use deltaTime that calculated by the formula above.

And it will be something like that

function update() { // <-- it is phaser state method...is called every frame
    deltaTime = (elapsedMS * fps) / 1000;
    sprite.body.velocity.x = velocityX * deltaTime;
    sprite.body.velocity.y = velocityY * deltaTime;
}
\$\endgroup\$
6
  • \$\begingroup\$ Awesome. I had the same exact issue with the deltaTime. Now, how to add speed? Would we just need to add ` * playerSpeed`? \$\endgroup\$ Jul 13, 2016 at 10:30
  • 1
    \$\begingroup\$ @NiCkNewman In my example velocityX and velocityY is my player speed on axis x/y ..... so, you can write playerSpeed * deltaTime instead velocityX/velocityY.....)) also it is located inside the update method.........And also ( just in case ) -- body.velocity measured in pixels per second.....So, if I understood the documentation right, you don't need even the "+" (plus) sign in the formula ...just sprite.body.velocity.x = speed * deltaTime; instead of sprite.body.velocity.x += velocityX * deltaTime;.. \$\endgroup\$ Jul 13, 2016 at 11:17
  • \$\begingroup\$ i know i'm a bit late here, but why are you multiplying velocity by time to get velocity? that doesn't make any sense; either add velocity to velocity, or multiply acceleration by time to get velocity. \$\endgroup\$
    – Eevee
    May 12, 2017 at 7:29
  • \$\begingroup\$ @Eevee read a comment above) I can edit the post if this is confusing you \$\endgroup\$ May 12, 2017 at 9:01
  • \$\begingroup\$ but speed is just the magnitude of velocity. multiply speed by time gives you a distance, which you can't add to velocity. that's why your player was moving very slowly. in the code in this answer, elapsed * fps / 1000 is going to be about 1 — not delta time — because the time between frames is naturally about 1/fps \$\endgroup\$
    – Eevee
    May 12, 2017 at 23:22

You must log in to answer this question.

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