Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Dark GDK / 2D Game - Float Based Movement?

Author
Message
reapz
15
Years of Service
User Offline
Joined: 4th Jun 2009
Location:
Posted: 14th Jun 2009 09:15
Is there anyway, using Images or Sprites with DarkGDK to get float based movement. The exposed methods seem to be limited to integer position and velocity and it is quite limiting.
Mireben
16
Years of Service
User Offline
Joined: 5th Aug 2008
Location:
Posted: 14th Jun 2009 12:02
If what you are talking about is that the dbSprite, dbPasteSprite, dbPasteImage, etc. functions are taking integer position parameters: of course, because these are pixels, and pixels are integers. But nothing prevents you from defining float variables for your calculations. Calculate the movement in float, then round the result to an integer just before you pass it to the sprite display functions.
Lilith
16
Years of Service
User Offline
Joined: 12th Feb 2008
Location: Dallas, TX
Posted: 14th Jun 2009 19:18
I've taken to using float values in my positioning in consideration that integer values can start to get misaligned, drift a bit, due to imprecison. This is especially true if you're having to use float values when keeping your frame rate synchronized. I do, however, use a little trick to even out the placement.

Shorthand code follows:

float xpos; // assuming these values are calculated somewhere
float ypos;

dbSprite (spritenum, (int) (xpos + 0.5f), (int) (ypos + 0.5f), imagenum);

The second and third arguments are the x and y components of the sprite's position. Of course the (int) type casts the results into and integer for the sake of the function. Adding 0.5f to the float position allows us to round up or down when we cast it to an int.

Example:

// rounds down
xpos = 243.2;

(int) (xpos + 0.5f)
(int) (243.2 + 0.5f)
(int) (243.7)
243


// rounds up
xpos = 243.8;

(int) (xpos + 0.5f)
(int) (243.8 + 0.5f)
(int) (244.3)
244

When I coded my Pong game for the Dark GDK challenge I used floats for the ball position to help keep it on a straight line. It also helped when calculating the velocity x and y components since those were based on trig values that had to represent floats. But since the ball was a class object I kept integer values of the x and y positions as class data after calculating the float values because I could use them in the on-screen positioning but also because I would need to reference them later to check for collisions.

Lilith, Night Butterfly
I'm not a programmer but I play one in the office
Phosphoer
16
Years of Service
User Offline
Joined: 8th Dec 2007
Location: Seattle
Posted: 14th Jun 2009 21:51
I just use float variables for the positions of my sprites, and use the same variables when I call dbSprite. I don't cast them or anything, and it seems to work out fine.



Login to post a reply

Server time is: 2024-11-05 23:30:48
Your offset time is: 2024-11-05 23:30:48