Jump to content

  • Log In with Google      Sign In   
  • Create Account

FREE SOFTWARE GIVEAWAY

We have 4 x Pro Licences (valued at $59 each) for 2d modular animation software Spriter to give away in this Thursday's GDNet Direct email newsletter.


Read more in this forum topic or make sure you're signed up (from the right-hand sidebar on the homepage) and read Thursday's newsletter to get in the running!


Movement speed vary on different machines


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
14 replies to this topic

#1 warnexus   Prime Members   -  Reputation: 1504

Like
0Likes
Like

Posted 09 May 2013 - 08:33 AM

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


Sponsor:

#2 C0lumbo   Crossbones+   -  Reputation: 2501

Like
0Likes
Like

Posted 09 May 2013 - 08:55 AM

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?



#3 warnexus   Prime Members   -  Reputation: 1504

Like
0Likes
Like

Posted 09 May 2013 - 09:29 AM

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, 09 May 2013 - 09:31 AM.


#4 C0lumbo   Crossbones+   -  Reputation: 2501

Like
0Likes
Like

Posted 09 May 2013 - 10:18 AM

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.



#5 Matias Goldberg   Crossbones+   -  Reputation: 3718

Like
6Likes
Like

Posted 09 May 2013 - 10:45 AM

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.



#6 tedsta   Members   -  Reputation: 258

Like
2Likes
Like

Posted 09 May 2013 - 11:11 AM

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


#7 warnexus   Prime Members   -  Reputation: 1504

Like
0Likes
Like

Posted 09 May 2013 - 09:42 PM

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!



#8 _greyfox()   Members   -  Reputation: 1134

Like
1Likes
Like

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 ); // 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, 10 May 2013 - 08:40 AM.

“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

#9 warnexus   Prime Members   -  Reputation: 1504

Like
0Likes
Like

Posted 14 May 2013 - 05:51 PM

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?




#10 _greyfox()   Members   -  Reputation: 1134

Like
2Likes
Like

Posted 16 May 2013 - 08:06 AM

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

#11 warnexus   Prime Members   -  Reputation: 1504

Like
0Likes
Like

Posted 16 May 2013 - 09:44 AM

Interesting. Perhaps the current milliseconds returned from the System class might vary? This part is still a mystery on why it's movement vary from one computer to another.



#12 Satharis   Members   -  Reputation: 1272

Like
0Likes
Like

Posted 17 May 2013 - 12:41 AM

Interesting. Perhaps the current milliseconds returned from the System class might vary? This part is still a mystery on why it's movement vary from one computer to another.

Keep in mind that unless you use interpolation the visual of movement may vary wildly between computers. For example even if you use a delta to move 10 meters in your game every second, one machine may loop 120 times a second, thus incrementing your visible position slightly each frame, the other may loop 40 times a second and although you will reach the same position, the movement will appear completely different.

 

Then again I'm not sure what you mean about the movement varying, is it just a visual difference or is it actually moving farther? If you multiply your movement by time it should be consistant between systems.

 

Also, you're citing the only difference as that the desktop is 64 bit while the laptop is 32, but it sounds to me like they probably vary a lot in performance and hardware as well.


Edited by Satharis, 17 May 2013 - 12:41 AM.


#13 SimonForsman   Crossbones+   -  Reputation: 6323

Like
1Likes
Like

Posted 17 May 2013 - 12:59 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 );
}
}

 

That method isn't necessarily the "right" way as it actually makes the speed of the system affect the result of the simulation rather than the speed at which the simulation runs. The Fix your timestep link posted by Matias describes a more robust way to handle updates and is worth reading. (variable timesteps work great for some games but once you start mixing in more advanced physics it can give you some rather "interesting" results)


Edited by SimonForsman, 17 May 2013 - 01:01 AM.

I don't suffer from insanity, I'm enjoying every minute of it.
The voices in my head may not be real, but they have some good ideas!

#14 _greyfox()   Members   -  Reputation: 1134

Like
0Likes
Like

Posted 18 May 2013 - 05:25 AM

Interesting. Perhaps the current milliseconds returned from the System class might vary? This part is still a mystery on why it's movement vary from one computer to another.

 

 

Oh so you think it's speed changed because they have different bus width, by that I mean that one computer has 32-bit OS and other 64-bit? Answer is noooo.
 
The only thing that is responsible to that speed change is by ONLY how powerful your computer is, it can be 32-bit can go faster than 64-bit if it has better GPU and CPU.
 
Just think about it, if your game runs at 800FPS on desktop, and at 400FPS on laptop, then if in your code, on every frame you increase some kind of variable by 1, that means that every  second that variable will be increased by 800 on desktop, and by 400 for laptop, and if that variable is position, then that means that your player or something will move 2 times faster on desktop than laptop.
 
Also the algorithm I just gave you not only fixes the speed issue on different systems, but it also makes game run smoother, because all other background processes might affect your computers performance, and so one second your player might move 400 pixels, and other second player might move 350 pixels. That algorithm just fixes the performance issue, and it is used widely across in game development.
 
I hope it is getting clearer now.

“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

#15 warnexus   Prime Members   -  Reputation: 1504

Like
0Likes
Like

Posted 19 May 2013 - 05:28 PM

So it depends on CPU and GPU. I understand now.






Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS