Jump to content

  • Log In with Google      Sign In   
  • Create Account

Interested in a FREE copy of HTML5 game maker Construct 2?

We'll be giving away three Personal Edition licences in next Tuesday's GDNet Direct email newsletter!

Sign up from the right-hand sidebar on our homepage and read Tuesday's newsletter for details!


Moving a constant number of pixels per second


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
6 replies to this topic

#1 Pasanova   Members   -  Reputation: 119

Like
0Likes
Like

Posted 24 September 2012 - 11:45 AM

Hello Everyone,
  • I update the position of my objects every time I go through my game loop
  • I have a function that calculates the number of loops per second, lets say there are 100
  • I want to move my object at a constant rate of 30 pixels per second (which is already quite fast)
  • This means that I should move my object 0.3 pixels per loop
  • This is a problem!
How do you normally get around this? Thanks!

Sponsor:

#2 Cornstalks   Crossbones+   -  Reputation: 6991

Like
0Likes
Like

Posted 24 September 2012 - 11:50 AM

Use floating point data types instead of integers?
[ I was ninja'd 71 times before I stopped counting a long time ago ] [ f.k.a. MikeTacular ] [ My Blog ] [ SWFer: Gaplessly looped MP3s in your Flash games ]

#3 Pasanova   Members   -  Reputation: 119

Like
0Likes
Like

Posted 24 September 2012 - 12:00 PM

But surely you have to either move a pixel or not? You can't move
0.02 pixels?

#4 jwezorek   Crossbones+   -  Reputation: 1897

Like
3Likes
Like

Posted 24 September 2012 - 12:02 PM

First of all, I assume we are talking about a 2D game in which your objects are sprites, right? -- (because otherwise you wouldn't be talking about pixels).

So, yeah, use floats for position values like Cornstalks says above.

But what game framework or library are you using? Does it support drawing with subpixel accuracy? If not you have to round to the nearest pixel ... this tends to look jerky imho though. Basically do all your logic/updates in terms of floating point values and convert to the nearest pixel just before blitting. Or switch to a 2D framework that lets you draw to float locations.

Edited by jwezorek, 24 September 2012 - 12:04 PM.


#5 Pasanova   Members   -  Reputation: 119

Like
0Likes
Like

Posted 24 September 2012 - 12:06 PM

Thanks Posted Image Yes, its 2D I am using SDL. okay looks like
that is the way to go!

EDIT: Works a treat!

Edited by Pasanova, 24 September 2012 - 12:16 PM.


#6 superman3275   Crossbones+   -  Reputation: 2061

Like
0Likes
Like

Posted 28 September 2012 - 08:41 PM

Move it using the frame time of your function.
That means that you multiply your speed variables by the frame time of your window. Since time is constant, it will move at the same speed on everyones screen.

I'm a game programmer and computer science ninja ph34r.png!

Here's my 2D RPG-Ish Platformer Programmed in Python + Pygame, with a Custom Level Editor and Rendering System!

 

Here's my Custom IDE / Debugger Programmed in Pure Python and Designed from the Ground Up for Programming Education!

Want to ask about Python, Flask, wxPython, Pygame, C++, HTML5, CSS3, Javascript, jQuery, C++, Vimscript, SFML 1.6 / 2.0, or anything else? Recruiting for a game development team and need a passionate programmer? Just want to talk about programming? Email me here:

hobohm.business@gmail.com

or Personal-Message me on here smile.png!


#7 EddieV223   Members   -  Reputation: 1406

Like
0Likes
Like

Posted 28 September 2012 - 09:41 PM

When you place your object just cast the float to an int. When you cast it the decimal part will be ignored. So you can round it first if you wish, then anything over .5 should be +1.

Edited by EddieV223, 28 September 2012 - 09:50 PM.

If this post or signature was helpful and/or constructive please give rep.

 

// C++ Video tutorials

http://www.youtube.com/watch?v=Wo60USYV9Ik

 

// Easy to learn 2D Game Library c++

SFML2.1 Download http://www.sfml-dev.org/download.php

SFML2.1 Tutorials http://www.sfml-dev.org/tutorials/2.1/

 

// SFML 2 book

http://www.amazon.com/gp/product/1849696845/ref=as_li_ss_tl?ie=UTF8&camp=1789&creative=390957&creativeASIN=1849696845&linkCode=as2&tag=gamer2creator-20

 





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