Home > Code, Development Notes, HowTo > Fast Game Mathematics

Fast Game Mathematics

As for myself, I never had anything to do with game programming. I’m a normal J2EE Developer / Architect in my “real” life, working for a big IT-Company here in Germany. If you are entering game programming you have to learn to throw some common java habits overboard. Besides the “Aiming for Performance” Arcticle ” down below, there are some more technical issues that can speed up your game. A good starting point is a wiki article about fast square roots (Link). The thing with the square roots is especially interesting if you are going 3D.

Another aspect is the Math factor.

int result = inputInt / 2;
int result2 = inputInt >> 1;

If you have some division by the power of 2, then you can simple shift the bits. In my TestCase this is like 8x faster than the standard division. Another useful approach are Data Tables, when you don’t need exact values. If you are calculating angles with Math.tan() or something similar, you might want to consider to pre calculate the angles your need for your game. In my case the rotation of arrows in 10 degree steps. So the only thing you have to to is to divide the cathetus and search for a Number in the table. In my case 5x times faster that Java Math.

  1. Michael
    April 29th, 2011 at 03:46 | #1

    Thanks I was wondering about using shift. Your other performance article had good tips too.

  2. U A
    November 4th, 2011 at 21:32 | #2

    Hey did you ever implement the fast square root for Java?

  3. U A
    November 4th, 2011 at 21:42 | #3

    Ah nevermind, I found an implementation for invSqrt():


    Unfortunately, the tests show it’s just as fast as 1.0f / sqrt() 🙁

    I wonder if that’s still true in Android….

    • November 4th, 2011 at 22:48 | #4

      Hi UA, depends on how you test it. On your “local” Computer there may not be a big difference, because the CPU can handle those big floating point stuff better than the CPU on Android. If you are interested I can send you my implementation of square root based on a static table.

  4. November 4th, 2016 at 15:12 | #5

    Definitely believe that which you said. Your favorite justification appeared to be
    on the net the simplest thing to be aware of. I say to you, I certainly
    get annoyed while people think about worries that they plainly do not know about.
    You managed to hit the nail upon the top and also defined out
    the whole thing without having side-effects , people could take a signal.
    Will probably be back to get more. Thanks

  5. November 28th, 2016 at 07:53 | #6

    What’s up mates, how is the whole thing, and what you would like to say on the topic
    of this paragraph, in my view its genuinely awesome in support of me.

  6. March 3rd, 2017 at 19:47 | #7

    WOW just what I waas looking for. Came here by searching for rencontrer
    des gens sur paris

  1. No trackbacks yet.