Java Code Examples for com.jme3.math.FastMath#floor()

The following examples show how to use com.jme3.math.FastMath#floor() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TerrainQuad.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
protected Vector3f getNormal(float x, float z, Vector2f xz) {
    x-=0.5f;
    z-=0.5f;
    float col = FastMath.floor(x);
    float row = FastMath.floor(z);
    boolean onX = false;
    if(1 - (x - col)-(z - row) < 0) // what triangle to interpolate on
        onX = true;
    // v1--v2  ^
    // |  / |  |
    // | /  |  |
    // v3--v4  | Z
    //         |
    // <-------Y
    //     X
    Vector3f n1 = getMeshNormal((int) FastMath.ceil(x), (int) FastMath.ceil(z));
    Vector3f n2 = getMeshNormal((int) FastMath.floor(x), (int) FastMath.ceil(z));
    Vector3f n3 = getMeshNormal((int) FastMath.ceil(x), (int) FastMath.floor(z));
    Vector3f n4 = getMeshNormal((int) FastMath.floor(x), (int) FastMath.floor(z));

    return n1.add(n2).add(n3).add(n4).normalize();
}
 
Example 2
Source File: TerrainQuad.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 6 votes vote down vote up
protected Vector3f getNormal(float x, float z, Vector2f xz) {
    x-=0.5f;
    z-=0.5f;
    float col = FastMath.floor(x);
    float row = FastMath.floor(z);
    boolean onX = false;
    if(1 - (x - col)-(z - row) < 0) // what triangle to interpolate on
        onX = true;
    // v1--v2  ^
    // |  / |  |
    // | /  |  |
    // v3--v4  | Z
    //         |
    // <-------Y
    //     X 
    Vector3f n1 = getMeshNormal((int) FastMath.ceil(x), (int) FastMath.ceil(z));
    Vector3f n2 = getMeshNormal((int) FastMath.floor(x), (int) FastMath.ceil(z));
    Vector3f n3 = getMeshNormal((int) FastMath.ceil(x), (int) FastMath.floor(z));
    Vector3f n4 = getMeshNormal((int) FastMath.floor(x), (int) FastMath.floor(z));
    
    return n1.add(n2).add(n3).add(n4).normalize();
}
 
Example 3
Source File: FaultHeightMap.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
protected void addCircleFault(float[][] tempBuffer, Random random, float faultHeight, float range) {
    float radius = random.nextFloat() * (maxRadius - minRadius) + minRadius;
    int intRadius = (int) FastMath.floor(radius);
    // Allox circle center to be out of map if not by more than radius.
    // Unlucky cases will put them in the far corner, with the circle
    // entirely outside heightmap
    int x = random.nextInt(size + 2 * intRadius) - intRadius;
    int y = random.nextInt(size + 2 * intRadius) - intRadius;

    for (int i = 0; i < size; i++) {
        for (int j = 0; j < size; j++) {
            float dist;
            if (i != x || j != y) {
                int dx = i - x;
                int dy = j - y;
                float dmag = FastMath.sqrt(FastMath.sqr(dx) + FastMath.sqr(dy));
                float rx = x + dx / dmag * radius;
                float ry = y + dy / dmag * radius;
                dist = FastMath.sign(dmag - radius)
                    * FastMath.sqrt(FastMath.sqr(i - rx) + FastMath.sqr(j - ry));
            } else {
                dist = 0;
            }
            tempBuffer[i][j] += calcHeight(dist, random, faultHeight, range);
        }
    }
}
 
Example 4
Source File: FaultHeightMap.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 5 votes vote down vote up
protected void addCircleFault(float[][] tempBuffer, Random random, float faultHeight, float range) {
    float radius = random.nextFloat() * (maxRadius - minRadius) + minRadius;
    int intRadius = (int) FastMath.floor(radius);
    // Allox circle center to be out of map if not by more than radius.
    // Unlucky cases will put them in the far corner, with the circle
    // entirely outside heightmap
    int x = random.nextInt(size + 2 * intRadius) - intRadius;
    int y = random.nextInt(size + 2 * intRadius) - intRadius;

    for (int i = 0; i < size; i++) {
        for (int j = 0; j < size; j++) {
            float dist;
            if (i != x || j != y) {
                int dx = i - x;
                int dy = j - y;
                float dmag = FastMath.sqrt(FastMath.sqr(dx) + FastMath.sqr(dy));
                float rx = x + dx / dmag * radius;
                float ry = y + dy / dmag * radius;
                dist = FastMath.sign(dmag - radius)
                    * FastMath.sqrt(FastMath.sqr(i - rx) + FastMath.sqr(j - ry));
            } else {
                dist = 0;
            }
            tempBuffer[i][j] += calcHeight(dist, random, faultHeight, range);
        }
    }
}
 
Example 5
Source File: NoiseGenerator.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 4 votes vote down vote up
/**
 * Not 'pure' Worley, but the results are virtually the same. Returns distances in da and point coords in pa
 */
public static void voronoi(float x, float y, float z, float[] da, float[] pa, float distanceExponent, int distanceType) {
    float xd, yd, zd, d, p[] = new float[3];

    DistanceFunction distanceFunc = distanceFunctions.get(Integer.valueOf(distanceType));
    if (distanceFunc == null) {
        distanceFunc = distanceFunctions.get(Integer.valueOf(0));
    }

    int xi = (int) FastMath.floor(x);
    int yi = (int) FastMath.floor(y);
    int zi = (int) FastMath.floor(z);
    da[0] = da[1] = da[2] = da[3] = Float.MAX_VALUE;// 1e10f;
    for (int i = xi - 1; i <= xi + 1; ++i) {
        for (int j = yi - 1; j <= yi + 1; ++j) {
            for (int k = zi - 1; k <= zi + 1; ++k) {
                NoiseMath.hash(i, j, k, p);
                xd = x - (p[0] + i);
                yd = y - (p[1] + j);
                zd = z - (p[2] + k);
                d = distanceFunc.execute(xd, yd, zd, distanceExponent);
                if (d < da[0]) {
                    da[3] = da[2];
                    da[2] = da[1];
                    da[1] = da[0];
                    da[0] = d;
                    pa[9] = pa[6];
                    pa[10] = pa[7];
                    pa[11] = pa[8];
                    pa[6] = pa[3];
                    pa[7] = pa[4];
                    pa[8] = pa[5];
                    pa[3] = pa[0];
                    pa[4] = pa[1];
                    pa[5] = pa[2];
                    pa[0] = p[0] + i;
                    pa[1] = p[1] + j;
                    pa[2] = p[2] + k;
                } else if (d < da[1]) {
                    da[3] = da[2];
                    da[2] = da[1];
                    da[1] = d;
                    pa[9] = pa[6];
                    pa[10] = pa[7];
                    pa[11] = pa[8];
                    pa[6] = pa[3];
                    pa[7] = pa[4];
                    pa[8] = pa[5];
                    pa[3] = p[0] + i;
                    pa[4] = p[1] + j;
                    pa[5] = p[2] + k;
                } else if (d < da[2]) {
                    da[3] = da[2];
                    da[2] = d;
                    pa[9] = pa[6];
                    pa[10] = pa[7];
                    pa[11] = pa[8];
                    pa[6] = p[0] + i;
                    pa[7] = p[1] + j;
                    pa[8] = p[2] + k;
                } else if (d < da[3]) {
                    da[3] = d;
                    pa[9] = p[0] + i;
                    pa[10] = p[1] + j;
                    pa[11] = p[2] + k;
                }
            }
        }
    }
}
 
Example 6
Source File: TerrainGrid.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 3 votes vote down vote up
/**
 * Get the location in cell-coordinates of the specified location.
 * Cell coordinates are integer corrdinates, usually with y=0, each 
 * representing a cell in the world.
 * For example, moving right in the +X direction:
 * (0,0,0) (1,0,0) (2,0,0), (3,0,0)
 * and then down the -Z direction:
 * (3,0,-1) (3,0,-2) (3,0,-3)
 */
public Vector3f getCamCell(Vector3f location) {
    Vector3f tile = getTileCell(location);
    Vector3f offsetHalf = new Vector3f(-0.5f, 0, -0.5f);
    Vector3f shifted = tile.subtract(offsetHalf);
    return new Vector3f(FastMath.floor(shifted.x), 0, FastMath.floor(shifted.z));
}
 
Example 7
Source File: TerrainGrid.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 3 votes vote down vote up
/**
 * Get the location in cell-coordinates of the specified location.
 * Cell coordinates are integer corrdinates, usually with y=0, each 
 * representing a cell in the world.
 * For example, moving right in the +X direction:
 * (0,0,0) (1,0,0) (2,0,0), (3,0,0)
 * and then down the -Z direction:
 * (3,0,-1) (3,0,-2) (3,0,-3)
 */
public Vector3f getCamCell(Vector3f location) {
    Vector3f tile = getTileCell(location);
    Vector3f offsetHalf = new Vector3f(-0.5f, 0, -0.5f);
    Vector3f shifted = tile.subtract(offsetHalf);
    return new Vector3f(FastMath.floor(shifted.x), 0, FastMath.floor(shifted.z));
}