Jump to content
  • Advertisement
Sign in to follow this  
Nicholas Kong

Movement speed vary on different machines

This topic is 1855 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

So I wrote this code for my monster movement logic. On my 32-bit laptop, the movement speed is so slow but the movement speed is so fast on my 64 bit desktop. Is the millisecond the culprit to this movement speed issue?
 
public void update(long milliseconds) {
// Handle all of the ship logic
 
// Update player position base on velocity
 
position = position.add(velocity.multiply((long) (milliseconds/16.667)));
 

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

Share this post


Link to post
Share on other sites
Advertisement

If you're not clamping your framerate anywhere, it could be that your millisecond timer is too coarse.

 

On my PC my current game seems to top out at 1000fps (not sure whether that's directx or the graphics driver that's limiting it to such a suspiciously round number).

 

If you're getting framerates anywhere near that fast, then your millisecond timer is really not going to cut it. I recommend you switch to microseconds instead.

 

But also, where does 'speed' get initialised? It also needs to be scaled by the frame time. Or perhaps the movement buttons should be setting the velocity instead of messing with the position?

Share this post


Link to post
Share on other sites

If you're not clamping your framerate anywhere, it could be that your millisecond timer is too coarse.

 

On my PC my current game seems to top out at 1000fps (not sure whether that's directx or the graphics driver that's limiting it to such a suspiciously round number).

 

If you're getting framerates anywhere near that fast, then your millisecond timer is really not going to cut it. I recommend you switch to microseconds instead.

 

But also, where does 'speed' get initialised? It also needs to be scaled by the frame time. Or perhaps the movement buttons should be setting the velocity instead of messing with the position?

Here is the speed declaration:

 

private static final double speed = 0.03;

 

Millisecond is computed using the standard game tick declaration.

 

// calculate the time since the last loop

 
long milliseconds = System.currentTimeMillis() - lastTick;
lastTick = System.currentTimeMillis();
 
I forgot to mention I am using Java. 
Edited by warnexus

Share this post


Link to post
Share on other sites

Then I think your problem is definitely that speed needs to be scaled by the frametime. Alternatively, the movement should set the velocity rather than directly altering the position.

Share this post


Link to post
Share on other sites

You need to multiply your speed, in seconds, by the delta time in seconds. So, if your update function is receiving the delta time in milliseconds, divide the milliseconds by 1000 to get your delta time in seconds. That, or you could fix your time step as Matias said, which you'll have to do anyway if you want to implement good multiplayer. The link he provided is very good.

 

On top of that stuff, your logic looks a bit off. So, you have a velocity variable that moves your ship based on delta time, but then when ship controls are pressed you don't even touch the velocity variable and just move your ship by hand ignoring delta time.

Share this post


Link to post
Share on other sites

You need to multiply your speed, in seconds, by the delta time in seconds. So, if your update function is receiving the delta time in milliseconds, divide the milliseconds by 1000 to get your delta time in seconds. That, or you could fix your time step as Matias said, which you'll have to do anyway if you want to implement good multiplayer. The link he provided is very good.

 

On top of that stuff, your logic looks a bit off. So, you have a velocity variable that moves your ship based on delta time, but then when ship controls are pressed you don't even touch the velocity variable and just move your ship by hand ignoring delta time.

Thanks for the tip!

Share this post


Link to post
Share on other sites

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 );
}
}
Edited by Edvinas Kilbauskas

Share this post


Link to post
Share on other sites

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 );
}
}

Holy cow! Thanks Edvinas! The code worked on both computers.

 

Why is nanoTime() being used and why divide by 1000000000.f? And why does the code actually work for both 32 bit and 64 bit computers?


Share this post


Link to post
Share on other sites

I'm happy to help you! Thank god I helped at least for someone at this forum :)

 

I use nanoTime() because it is very precise, and I divide it by 1000000000.f so that it would convert it to seconds. So that everything that you multiply by deltaTime variable will return, in this case, the amount of pixels it should move in this frame so that by adding up all frames per second, it will move exact value you specified in the left side, for example:

 

value = 150 * deltaTime;  // this will increase the value variable by 150 every second,
value1 = 45.2 * deltaTime; // this will increase the value1 variable by 45.2 every second.

 

 

I don't really know how to explain this in the right way.

 

And about 32bit and 64bit systems, I don't know why SHOUDLN'T it work. 32 bit and 64 bit only means that your computer's bus width is 32 or 64 bit, and it determines how much different RAM addresses your computer can recognize. So in reality it has nothing to do with performance of your computer, let alone the game which you are making.

 

I hope that answered all of your questions, if not, tell me what you didn't understand yet.

 

Cheers!

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!