Android Open Source - gameengine Math Utils






From Project

Back to project page gameengine.

License

The source code is released under:

Apache License

If you think the Android project gameengine listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

/*******************************************************************************
 * Copyright 2011 See AUTHORS file./* w ww. java2  s.c o m*/
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 ******************************************************************************/

package com.badlogic.gdx.math;

import java.util.Random;

import com.badlogic.gdx.utils.NumberUtils;

/**
 * Utility and fast math functions.
 * <p>
 * Thanks to Riven on JavaGaming.org for the basis of sin/cos/atan2/floor/ceil.
 * 
 * @author Nathan Sweet
 */
public class MathUtils {
    static public final float PI = 3.1415927f;

    static private final int SIN_BITS = 13; // Adjust for accuracy.
    static private final int SIN_MASK = ~(-1 << SIN_BITS);
    static private final int SIN_COUNT = SIN_MASK + 1;

    static private final float radFull = PI * 2;
    static private final float degFull = 360;
    static private final float radToIndex = SIN_COUNT / radFull;
    static private final float degToIndex = SIN_COUNT / degFull;

    static public final float radiansToDegrees = 180f / PI;
    static public final float radDeg = radiansToDegrees;
    static public final float degreesToRadians = PI / 180;
    static public final float degRad = degreesToRadians;

    static private class Sin {
        static final float[] table = new float[SIN_COUNT];
        static {
            for (int i = 0; i < SIN_COUNT; i++)
                table[i] = (float) Math.sin((i + 0.5f) / SIN_COUNT * radFull);
            for (int i = 0; i < 360; i += 90)
                table[(int) (i * degToIndex) & SIN_MASK] = (float) Math.sin(i * degreesToRadians);
        }
    }

    static private class Cos {
        static final float[] table = new float[SIN_COUNT];
        static {
            for (int i = 0; i < SIN_COUNT; i++)
                table[i] = (float) Math.cos((i + 0.5f) / SIN_COUNT * radFull);
            for (int i = 0; i < 360; i += 90)
                table[(int) (i * degToIndex) & SIN_MASK] = (float) Math.cos(i * degreesToRadians);
        }
    }

    static public final float sin(float rad) {
        return Sin.table[(int) (rad * radToIndex) & SIN_MASK];
    }

    static public final float cos(float rad) {
        return Cos.table[(int) (rad * radToIndex) & SIN_MASK];
    }

    static public final float sinDeg(float deg) {
        return Sin.table[(int) (deg * degToIndex) & SIN_MASK];
    }

    static public final float cosDeg(float deg) {
        return Cos.table[(int) (deg * degToIndex) & SIN_MASK];
    }

    // ---

    static private final int ATAN2_BITS = 7; // Adjust for accuracy.
    static private final int ATAN2_BITS2 = ATAN2_BITS << 1;
    static private final int ATAN2_MASK = ~(-1 << ATAN2_BITS2);
    static private final int ATAN2_COUNT = ATAN2_MASK + 1;
    static final int ATAN2_DIM = (int) Math.sqrt(ATAN2_COUNT);
    static private final float INV_ATAN2_DIM_MINUS_1 = 1.0f / (ATAN2_DIM - 1);

    static private class Atan2 {
        static final float[] table = new float[ATAN2_COUNT];
        static {
            for (int i = 0; i < ATAN2_DIM; i++) {
                for (int j = 0; j < ATAN2_DIM; j++) {
                    float x0 = (float) i / ATAN2_DIM;
                    float y0 = (float) j / ATAN2_DIM;
                    table[j * ATAN2_DIM + i] = (float) Math.atan2(y0, x0);
                }
            }
        }
    }

    /** Returns atan2 in radians from a lookup table. */
    static public final float atan2(float y, float x) {
        float add, mul;
        if (x < 0) {
            if (y < 0) {
                y = -y;
                mul = 1;
            } else
                mul = -1;
            x = -x;
            add = -PI;
        } else {
            if (y < 0) {
                y = -y;
                mul = -1;
            } else
                mul = 1;
            add = 0;
        }
        float invDiv = 1 / ((x < y ? y : x) * INV_ATAN2_DIM_MINUS_1);
        int xi = (int) (x * invDiv);
        int yi = (int) (y * invDiv);
        return (Atan2.table[yi * ATAN2_DIM + xi] + add) * mul;
    }

    // ---

    static public Random random = new Random();

    /**
     * Returns a random number between 0 (inclusive) and the specified value
     * (inclusive).
     */
    static public final int random(int range) {
        return random.nextInt(range + 1);
    }

    /** Returns a random number between start (inclusive) and end (inclusive). */
    static public final int random(int start, int end) {
        return start + random.nextInt(end - start + 1);
    }

    static public final boolean randomBoolean() {
        return random.nextBoolean();
    }

    static public final float random() {
        return random.nextFloat();
    }

    /**
     * Returns a random number between 0 (inclusive) and the specified value
     * (inclusive).
     */
    static public final float random(float range) {
        return random.nextFloat() * range;
    }

    /** Returns a random number between start (inclusive) and end (inclusive). */
    static public final float random(float start, float end) {
        return start + random.nextFloat() * (end - start);
    }

    // ---

    /**
     * Returns the next power of two. Returns the specified value if the value
     * is already a power of two.
     */
    static public int nextPowerOfTwo(int value) {
        if (value == 0)
            return 1;
        value--;
        value |= value >> 1;
        value |= value >> 2;
        value |= value >> 4;
        value |= value >> 8;
        value |= value >> 16;
        return value + 1;
    }

    static public boolean isPowerOfTwo(int value) {
        return value != 0 && (value & value - 1) == 0;
    }

    // ---

    static public int clamp(int value, int min, int max) {
        if (value < min)
            return min;
        if (value > max)
            return max;
        return value;
    }

    static public short clamp(short value, short min, short max) {
        if (value < min)
            return min;
        if (value > max)
            return max;
        return value;
    }

    static public float clamp(float value, float min, float max) {
        if (value < min)
            return min;
        if (value > max)
            return max;
        return value;
    }

    // ---

    static private final int BIG_ENOUGH_INT = 16 * 1024;
    static private final double BIG_ENOUGH_FLOOR = BIG_ENOUGH_INT;
    static private final double CEIL = 0.9999999;
    static private final double BIG_ENOUGH_CEIL = NumberUtils.longBitsToDouble(NumberUtils.doubleToLongBits(BIG_ENOUGH_INT + 1) - 1);
    static private final double BIG_ENOUGH_ROUND = BIG_ENOUGH_INT + 0.5f;

    /**
     * Returns the largest integer less than or equal to the specified float.
     * This method will only properly floor floats from -(2^14) to
     * (Float.MAX_VALUE - 2^14).
     */
    static public int floor(float x) {
        return (int) (x + BIG_ENOUGH_FLOOR) - BIG_ENOUGH_INT;
    }

    /**
     * Returns the largest integer less than or equal to the specified float.
     * This method will only properly floor floats that are positive. Note this
     * method simply casts the float to int.
     */
    static public int floorPositive(float x) {
        return (int) x;
    }

    /**
     * Returns the smallest integer greater than or equal to the specified
     * float. This method will only properly ceil floats from -(2^14) to
     * (Float.MAX_VALUE - 2^14).
     */
    static public int ceil(float x) {
        return (int) (x + BIG_ENOUGH_CEIL) - BIG_ENOUGH_INT;
    }

    /**
     * Returns the smallest integer greater than or equal to the specified
     * float. This method will only properly ceil floats that are positive.
     */
    static public int ceilPositive(float x) {
        return (int) (x + CEIL);
    }

    /**
     * Returns the closest integer to the specified float. This method will only
     * properly round floats from -(2^14) to (Float.MAX_VALUE - 2^14).
     */
    static public int round(float x) {
        return (int) (x + BIG_ENOUGH_ROUND) - BIG_ENOUGH_INT;
    }

    /**
     * Returns the closest integer to the specified float. This method will only
     * properly round floats that are positive.
     */
    static public int roundPositive(float x) {
        return (int) (x + 0.5f);
    }
}




Java Source Code List

com.badlogic.gdx.math.MathUtils.java
com.badlogic.gdx.math.Matrix3.java
com.badlogic.gdx.math.Matrix4.java
com.badlogic.gdx.math.Quaternion.java
com.badlogic.gdx.math.Vector2.java
com.badlogic.gdx.math.Vector3.java
com.badlogic.gdx.physics.box2d.BodyDef.java
com.badlogic.gdx.physics.box2d.Body.java
com.badlogic.gdx.physics.box2d.ChainShape.java
com.badlogic.gdx.physics.box2d.CircleShape.java
com.badlogic.gdx.physics.box2d.ContactFilter.java
com.badlogic.gdx.physics.box2d.ContactImpulse.java
com.badlogic.gdx.physics.box2d.ContactListener.java
com.badlogic.gdx.physics.box2d.Contact.java
com.badlogic.gdx.physics.box2d.DestructionListener.java
com.badlogic.gdx.physics.box2d.EdgeShape.java
com.badlogic.gdx.physics.box2d.Filter.java
com.badlogic.gdx.physics.box2d.FixtureDef.java
com.badlogic.gdx.physics.box2d.Fixture.java
com.badlogic.gdx.physics.box2d.JointDef.java
com.badlogic.gdx.physics.box2d.JointEdge.java
com.badlogic.gdx.physics.box2d.Joint.java
com.badlogic.gdx.physics.box2d.Manifold.java
com.badlogic.gdx.physics.box2d.MassData.java
com.badlogic.gdx.physics.box2d.PolygonShape.java
com.badlogic.gdx.physics.box2d.QueryCallback.java
com.badlogic.gdx.physics.box2d.RayCastCallback.java
com.badlogic.gdx.physics.box2d.Shape.java
com.badlogic.gdx.physics.box2d.Transform.java
com.badlogic.gdx.physics.box2d.WorldManifold.java
com.badlogic.gdx.physics.box2d.World.java
com.badlogic.gdx.physics.box2d.joints.DistanceJointDef.java
com.badlogic.gdx.physics.box2d.joints.DistanceJoint.java
com.badlogic.gdx.physics.box2d.joints.FrictionJointDef.java
com.badlogic.gdx.physics.box2d.joints.FrictionJoint.java
com.badlogic.gdx.physics.box2d.joints.GearJointDef.java
com.badlogic.gdx.physics.box2d.joints.GearJoint.java
com.badlogic.gdx.physics.box2d.joints.MouseJointDef.java
com.badlogic.gdx.physics.box2d.joints.MouseJoint.java
com.badlogic.gdx.physics.box2d.joints.PrismaticJointDef.java
com.badlogic.gdx.physics.box2d.joints.PrismaticJoint.java
com.badlogic.gdx.physics.box2d.joints.PulleyJointDef.java
com.badlogic.gdx.physics.box2d.joints.PulleyJoint.java
com.badlogic.gdx.physics.box2d.joints.RevoluteJointDef.java
com.badlogic.gdx.physics.box2d.joints.RevoluteJoint.java
com.badlogic.gdx.physics.box2d.joints.RopeJointDef.java
com.badlogic.gdx.physics.box2d.joints.RopeJoint.java
com.badlogic.gdx.physics.box2d.joints.WeldJointDef.java
com.badlogic.gdx.physics.box2d.joints.WeldJoint.java
com.badlogic.gdx.physics.box2d.joints.WheelJointDef.java
com.badlogic.gdx.physics.box2d.joints.WheelJoint.java
com.badlogic.gdx.utils.Array.java
com.badlogic.gdx.utils.ComparableTimSort.java
com.badlogic.gdx.utils.Disposable.java
com.badlogic.gdx.utils.GdxRuntimeException.java
com.badlogic.gdx.utils.LongArray.java
com.badlogic.gdx.utils.LongMap.java
com.badlogic.gdx.utils.NumberUtils.java
com.badlogic.gdx.utils.Pool.java
com.badlogic.gdx.utils.Sort.java
com.badlogic.gdx.utils.StringBuilder.java
com.badlogic.gdx.utils.TimSort.java
com.garrapeta.MathUtils.java
com.garrapeta.gameengine.Actor.java
com.garrapeta.gameengine.AsyncGameMessage.java
com.garrapeta.gameengine.BitmapManager.java
com.garrapeta.gameengine.Box2DActor.java
com.garrapeta.gameengine.Box2DWorld.java
com.garrapeta.gameengine.GameMessage.java
com.garrapeta.gameengine.GameView.java
com.garrapeta.gameengine.GameWorld.java
com.garrapeta.gameengine.ShapeDrawer.java
com.garrapeta.gameengine.SyncGameMessage.java
com.garrapeta.gameengine.Viewport.java
com.garrapeta.gameengine.actor.Box2DAtomicActor.java
com.garrapeta.gameengine.actor.Box2DCircleActor.java
com.garrapeta.gameengine.actor.Box2DEdgeActor.java
com.garrapeta.gameengine.actor.Box2DLoopActor.java
com.garrapeta.gameengine.actor.Box2DOpenChainActor.java
com.garrapeta.gameengine.actor.Box2DPolygonActor.java
com.garrapeta.gameengine.actor.IAtomicActor.java
com.garrapeta.gameengine.actor.SimpleActor.java
com.garrapeta.gameengine.module.LevelActionsModule.java
com.garrapeta.gameengine.module.LoadedLevelActionsModule.java
com.garrapeta.gameengine.module.SoundModule.java
com.garrapeta.gameengine.module.VibrationModule.java
com.garrapeta.gameengine.utils.IOUtils.java
com.garrapeta.gameengine.utils.LogX.java
com.garrapeta.gameengine.utils.PhysicsUtils.java
com.garrapeta.gameengine.utils.Pool.java