Movement speed vary on different machines

Started by
13 comments, last by Nicholas Kong 10 years, 11 months ago
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 );
}
}
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?

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.

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.

60fps in milliseconds stored as 'long' truncates to 16 from 16.666 which is highly inaccurate

Also, long(16/16.666) = 0

Anything above 60fps wouldn't be able to move (bug), anything between 58.82fps and 29.41fps would be multiplied against '1', which means the fast machine running at 58.82fps would move almost twice as fast as the machine running at 29.41fps (bug)

That time scaling code is completely broken.

Also, fix your timestep, and represent your time delta as a 32-bit long in microseconds, not milliseconds, and once you're multiplying/dividing, keep it as a float, not as integer.

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.

Current Project

World Storm

out our Android app! (My roommate and I)

https://play.google.com/store/apps/details?id=com.drsupersocks.ninja

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!

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 );
}
}
“There are thousands and thousands of people out there leading lives of quiet, screaming desperation, where they work long, hard hours at jobs they hate to enable them to buy things they don't need to impress people they don't like.”? Nigel Marsh

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?


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!

“There are thousands and thousands of people out there leading lives of quiet, screaming desperation, where they work long, hard hours at jobs they hate to enable them to buy things they don't need to impress people they don't like.”? Nigel Marsh

This topic is closed to new replies.

Advertisement