# ball movment without floats? [j2me]

This topic is 4645 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hello. I just started out with mobilephone game develompment(sounds serious, but I'm a noob) well doing pong I'm confused by one thing, I want the ball to move faster and faster, there by increasing the difficulty...but if I increase by integers the movment is...well it don't look good... How would you do this...I don't know any thing about fixed point math(any more than the name anyway) Well would apprishiate any help at all on this subject =) Thanks.

##### Share on other sites
You can use a floating point match library, like this one.
Or increase the range of integers (0 - 100000, instead of 0 - 100 for instance) to achieve a greater precision (not recommended!).

##### Share on other sites
Why would increasing integer ranges be not recommended? For exaple, if you wanted to move a ball a horizontal distance D, but D should be multiplied by a difficulty factor of 1.5, then couldnt you write:

//keep difficulty factor of 1.5 stored as an int of 15 for more efficiencyball.x += (D * 10 * 15) / 10;

I would imagine that the extra multiplication for shifting the precision would take a small toll on the processor, but it shouldn't be too bad.

I think an even better approach would be to maintain your coordinates in their extended ranges, and do all your math with large integer numbers. When it comes time to finally draw your objects, you can divide your x and y coordinates by your precision factor.

##### Share on other sites
Quote:
 Original post by MafianI would imagine that the extra multiplication for shifting the precision would take a small toll on the processor, but it shouldn't be too bad.
Using a power of two precision helps a lot, reducing the multiplications and divisions to simple bit shifts.
Oh, and try to use actual bit shits, at least for right shifts (the rounding is different).

##### Share on other sites
Thats actually a pretty smart idea. I dont see why a precision factor should be a multiple of 10 anyway. I bet that would be a significant performance boost. Good call.

##### Share on other sites
Thanks for the info...I'll implement that when I get back to my code =)

##### Share on other sites
Bsaically, you're just using an ad-hoc fixed point system with the solutions suggested. You might want to instead implement a full floating point system (or find a library that implements it for your platform and use that).

##### Share on other sites
Quote:
 Original post by ExtrariusBsaically, you're just using an ad-hoc fixed point system with the solutions suggested. You might want to instead implement a full floating point system (or find a library that implements it for your platform and use that).
Aren't regular floating point operations are available already? Though using them on any scale on a J2ME platform without hardware support seems a bit suicidal to me.
And if the built-in and (hopefully) heavily optimized emulator isn't fast enough then I have a really bad feeling about user-level Java code trying to do the same..

Maybe it's not that big of a deal for pong clone though. But considering what I've heard about J2ME so far I'm not exactly sure.

##### Share on other sites
I don't know about a java implementation of fixed point math, but in general fixed point can be faster than floating point because they use fundamentally different representation systems. Floating point basically uses scientific notation, which makes things like adding more complicated. Fixed point just means you have a fixed number of digits in certain places.

For example, to add 5+13 in the two systems, you have
1.250*2^2 + 1.625*2^3 = 1.X * 2^Y for floating point
vs
05.00 + 13.00 = Z for fixed point

##### Share on other sites
Yeah Im not sure if I would trust a 3rd party user-level implementation either. If the developers didnt include floating point math in j2me, they must have had a reason. anything user level probably wont be as fast as floating point math should be.

• 11
• 20
• 12
• 9
• 34
• ### Forum Statistics

• Total Topics
631399
• Total Posts
2999847
×