Jump to content

  • Log In with Google      Sign In   
  • Create Account

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

1. Details HERE. 2. GDNet+ Subscriptions HERE. 3. Ad upload HERE.


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

 
position = position.add(velocity.multiply(deltaTime));
 

 
// 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

 
position = position.add(velocity.multiply(deltaTime));
 

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

PARTNERS