• Create Account

We're offering banner ads on our site from just \$5!

Don't forget to read Tuesday's email newsletter for your chance to win a free copy of Construct 2!

### #ActualEdvinas Kilbauskas

Posted 10 May 2013 - 08:40 AM

I'm not sure why nobody mentioned the right way to handle delta time.

Try this. It should independent of FPS, and computer's speed.


long startTime = System.nanoTime();
...
public void update(long milliseconds) {

// calculate delta time
long time = System.nanoTime();
float deltaTime = (time - startTime)/1000000000.f;
startTime = time;

// Handle all of the ship logic

// Update player position base on velocity

// movement code

if(direction.equals("left"))
{
position.setX(position.getX() - speed*deltaTime ); // for example if speed = 100, then it will move 100 pixels per second.
}
else if(direction.equals("right"))
{

position.setX(position.getX() + speed*deltaTime );
}
}


### #1Edvinas Kilbauskas

Posted 10 May 2013 - 08:38 AM

I'm not sure why nobody mentioned the right way to handle delta time.

Try this. It should independent of FPS, and computer's speed.


long startTime = System.nanoTime();
...
public void update(long milliseconds) {

// calculate delta time
long time = System.nanoTime();
float deltaTime = (time - startTime)/1000000000.f;
startTime = time;

// Handle all of the ship logic

// Update player position base on velocity

// movement code

if(direction.equals("left"))
{
position.setX(position.getX() - speed*deltaTime );
}
else if(direction.equals("right"))
{

position.setX(position.getX() + speed*deltaTime );
}
}


PARTNERS