Tuesday, June 8, 2010

[android-developers] Re: Avoiding float operations in game design

I've done numerous testing on this issue when I was coding my audio
apps.

Using fixed point in the Java code gains NO benefit at all.

Using fixed point in NDK native code gives you a MASSIVE speed boost.
Like 2 to 4 times faster.

-niko

On Jun 8, 9:24 am, Mario Zechner <badlogicga...@gmail.com> wrote:
> First: what you are in need off is called fixed point math which is
> implemented by using integers, deciding how many low bits to use for
> the fractional part of a number and then do some shift magic to do
> basic math operations. It's an old trick used mainly on devices that
> do not have an FPU like many of the first generation android devices.
>
> From what i can tell from your posts you are using Java to develop
> your game(s). I strongly advise against using fixed point math in Java
> on Android, there's hardly any speedups gained due to this. All it
> does is obfuscate your code without any real benefit. If you go the
> native route you will see some speed up when using fixed point math
> though on older devices without an FPU. Seeing that most new devices
> all have an FPU (N1, Droid etc.) i'd suggest not thinking about it and
> going with floating point math. If you ever reach a point where your
> performance is not acceptable go native with the performance critical
> parts. If that does not help than you can consider using fixed point
> math.
>
> I'm also pretty sure that the Google Maps API uses fixed point math
> for the same reason financial applications use fixed point math:
> precision. It's nothing to do with speed.
>
> On 8 Jun., 16:08, Neilz <neilhorn...@gmail.com> wrote:
>
>
>
> > Right, ok.
>
> > So in my example, I have Bitmaps being drawn to the Canvas (using an
> > adaptation of SpriteMethodTest which I referred to a few days ago).
>
> > 1) The Canvas.drawBitmap() method takes two floats as coordinates, so
> > how can I avoid using them? Or if i was to pass 'integer' values,
> > would that mean they were treated as such, regardless of the method
> > signature?
>
> > 2) Typical x and y points for my bitmaps are X: 281.01068 Y:
> > 455.04297. When bitmaps get drawn to the screen, is this all wasted
> > precision? Is drawing at 281.01068 any different from drawing at
> > 281.31343?
>
> > On Jun 8, 2:32 pm, Mark Murphy <mmur...@commonsware.com> wrote:
>
> > > Neilz wrote:
> > > > Hi all. I've read this in a few articles, that one should avoid
> > > > floating point operations within the physics update of a game.
>
> > > > But how do you really achieve this? Surely all half decent games are
> > > > going to involve this kind of maths?
>
> > > Use integer math.
>
> > > > Does it mean, convert all your variables to int, or use double
> > > > instead, or simply just cut it out as much as possible?
>
> > > For example, look at the Google Maps API, compared to the Location API.
>
> > > A Location object has a double for getLatitude() and getLongitude(). For
> > > light uses, that's fine.
>
> > > However, for Google Maps, trying to do everything in floating point
> > > would get too slow. Hence, the GeoPoint class has latitude and longitude
> > > as integer microdegrees -- 1E6 times the double value in degrees. This
> > > allows Google Maps to maintain a fair bit of precision while sticking
> > > with integer math.
>
> > > What you need to do for your game is come up with appropriate units of
> > > measurement such that you won't need fractional units, for whatever
> > > degree of precision you want. For example, instead of kilometers, do
> > > your calculations in meters, or centimeters, or millimeters.
>
> > > --
> > > Mark Murphy (a Commons Guy)http://commonsware.com|http://github.com/commonsguyhttp://commonsware.com/blog|http://twitter.com/commonsguy
>
> > > Warescription: Three Android Books, Plus Updates, One Low Price!

--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscribe@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

No comments:

Post a Comment