• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
Nicholas Kong

Movement speed vary on different machines

14 posts in this topic

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

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?

0

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
0

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.

0

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.

2

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!

0

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
1

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?


0

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!

2

Share this post


Link to post
Share on other sites

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.

0

Share this post


Link to post
Share on other sites

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
0

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

 

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
1

Share this post


Link to post
Share on other sites

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.
0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0