Android Open Source - TinyVoxel Simple Math






From Project

Back to project page TinyVoxel.

License

The source code is released under:

GNU General Public License

If you think the Android project TinyVoxel 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

package com.toet.TinyVoxel.Util;
/*from  w  w  w  . j a  va  2s .  co m*/
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.Pixmap;
import com.badlogic.gdx.math.Matrix4;
import com.badlogic.gdx.math.Vector3;
import com.badlogic.gdx.math.collision.BoundingBox;
import com.toet.TinyVoxel.Config;

import java.nio.ByteBuffer;
import java.nio.IntBuffer;

/**
 * Created by Kajos on 7/10/2014.
 */
public class SimpleMath {
    public static float squared(float value) {
        return value * value;
    }

    public static boolean sphereAabbOverlap ( BoundingBox aabb, Vector3 sphere, float radius) {

        float squaredDistance = 0;

        // process X
        if (sphere.x < aabb.min.x) {
            float diff = sphere.x - aabb.min.x;
            squaredDistance += diff * diff;
        }

        else if (sphere.x > aabb.max.x) {
            float diff = sphere.x - aabb.max.x;
            squaredDistance += diff * diff;
        }

        // process Y
        if (sphere.y < aabb.min.y) {
            float diff = sphere.y - aabb.min.y;
            squaredDistance += diff * diff;
        }

        else if (sphere.y > aabb.max.y) {
            float diff = sphere.y - aabb.max.y;
            squaredDistance += diff * diff;
        }

        // process Z
        if (sphere.z < aabb.min.z) {
            float diff = sphere.z - aabb.min.z;
            squaredDistance += diff * diff;
        }

        else if (sphere.z > aabb.max.z) {
            float diff = sphere.z - aabb.max.z;
            squaredDistance += diff * diff;
        }

        return squaredDistance <= radius * radius;
    }

    public static IntBuffer getFrameBufferPixmap (IntBuffer buffer, int x, int y, int w, int h) {
        buffer.rewind();
        Gdx.gl.glPixelStorei(GL20.GL_PACK_ALIGNMENT, 1);
        Gdx.gl.glReadPixels(x, y, w, h, GL20.GL_RGBA, GL20.GL_UNSIGNED_BYTE, buffer);
        buffer.rewind();

        return buffer;
    }

    private static int shades[][][] = new int[Config.TINY_GRID_SIZE][Config.TINY_GRID_SIZE][Config.TINY_GRID_SIZE];
    public static void init() {
        for (int x = 0; x < Config.TINY_GRID_SIZE; x++)
            for (int y = 0; y < Config.TINY_GRID_SIZE; y++)
                for (int z = 0; z < Config.TINY_GRID_SIZE; z++) {
                    shades[x][y][z] = shadeFunc(x,y,z);
                }
    }

    private static Vector3 pos = new Vector3();
    private static int shadeFunc(int tx, int ty, int tz) {
        pos.set(tx - Config.TINY_GRID_SIZE / 2, ty - Config.TINY_GRID_SIZE / 2, tz - Config.TINY_GRID_SIZE / 2);
        return (int)((Math.sin((double)pos.len() / (double)Config.TINY_GRID_SIZE * 6.6f) * .125 + .875) * 254.0);
    }

    public static int shadeLookUp(int tx, int ty, int tz) {
        return shades[tx][ty][tz];
    }

    public static int setShadow(int shadow, int color) {
        color = color & 0x00ffffff;
        color |= shadow << 24;
        return color;
    }

    private static Vector3 col = new Vector3();
    public static Vector3 IntToRGB888( int color )
    {
        float r = (float)((color >> 0) & 0xff);
        float g = (float)((color >> 8) & 0xff);
        float b = (float)((color >> 16) & 0xff);

        col.set(r, g, b);
        return col;
    }

    public static int RGB888ToInt( Vector3 col )
    {
        col.x = Math.max(0f, Math.min(col.x, 255f));
        col.y = Math.max(0f, Math.min(col.y, 255f));
        col.z = Math.max(0f, Math.min(col.z, 255f));

        return RGB888ToInt((int)col.x, (int)col.y, (int)col.z);
    }

    public static int RGB888ToInt( int r, int g, int b )
    {
        int result = (r & 0xff) << 0;
        result |= (g & 0xff) << 8;
        result |= (b & 0xff) << 16;
        result |= 0xff << 24;
        return result;
    }
}




Java Source Code List

com.badlogic.gdx.backends.gwt.GwtApplicationConfiguration.java
com.badlogic.gdx.backends.gwt.GwtApplication.java
com.badlogic.gdx.backends.gwt.GwtGL20.java
com.badlogic.gdx.backends.gwt.GwtInput.java
com.badlogic.gdx.backends.gwt.GwtNet.java
com.badlogic.gdx.graphics.Pixmap.java
com.toet.TinyVoxel.Config.java
com.toet.TinyVoxel.Game.java
com.toet.TinyVoxel.IOSConfig.java
com.toet.TinyVoxel.IOSLauncher.java
com.toet.TinyVoxel.OuyaController.java
com.toet.TinyVoxel.Time.java
com.toet.TinyVoxel.Character.Character.java
com.toet.TinyVoxel.Debug.LogHandler.java
com.toet.TinyVoxel.GameControllers.CharacterController.java
com.toet.TinyVoxel.GameControllers.CustomTouchPad.java
com.toet.TinyVoxel.GameControllers.KeyBoardController.java
com.toet.TinyVoxel.GameControllers.TouchPadController.java
com.toet.TinyVoxel.Importer.BinvoxImporter.java
com.toet.TinyVoxel.Importer.DataInputStream.java
com.toet.TinyVoxel.Importer.MeshImporter.java
com.toet.TinyVoxel.Renderer.BlockBuilder.java
com.toet.TinyVoxel.Renderer.Floor.java
com.toet.TinyVoxel.Renderer.Manager.java
com.toet.TinyVoxel.Renderer.Bundles.ArrayBundle.java
com.toet.TinyVoxel.Renderer.Bundles.Bundle.java
com.toet.TinyVoxel.Renderer.Bundles.GridBundle.java
com.toet.TinyVoxel.Renderer.Bundles.GridInterface.java
com.toet.TinyVoxel.Renderer.Bundles.Grid.java
com.toet.TinyVoxel.Renderer.Bundles.GroundBundle.java
com.toet.TinyVoxel.Renderer.Bundles.SingleBundle.java
com.toet.TinyVoxel.Renderer.Bundles.TinyGrid.java
com.toet.TinyVoxel.Renderer.Tools.BrushUtils.java
com.toet.TinyVoxel.Renderer.Tools.GridUtils.java
com.toet.TinyVoxel.Renderer.Wrapped.WrappedBoolean.java
com.toet.TinyVoxel.Renderer.Wrapped.WrappedInteger.java
com.toet.TinyVoxel.Screens.GUI.java
com.toet.TinyVoxel.Screens.Menu.java
com.toet.TinyVoxel.Shaders.ShaderManager.java
com.toet.TinyVoxel.Shadow.ShadowManager.java
com.toet.TinyVoxel.Util.Box.java
com.toet.TinyVoxel.Util.FullscreenQuad.java
com.toet.TinyVoxel.Util.JobManager.java
com.toet.TinyVoxel.Util.NonBackedTexture.java
com.toet.TinyVoxel.Util.Position.java
com.toet.TinyVoxel.Util.RLEInputStream.java
com.toet.TinyVoxel.Util.RLEOutputStream.java
com.toet.TinyVoxel.Util.SimpleMath.java
com.toet.TinyVoxel.Util.StreamUtil.java
com.toet.TinyVoxel.android.AndroidConfig.java
com.toet.TinyVoxel.android.AndroidConfig.java
com.toet.TinyVoxel.android.AndroidLauncher.java
com.toet.TinyVoxel.android.AndroidLauncher.java
com.toet.TinyVoxel.client.GwtConfig.java
com.toet.TinyVoxel.client.HtmlLauncher.java
com.toet.TinyVoxel.desktop.DesktopConfig.java
com.toet.TinyVoxel.desktop.DesktopLauncher.java