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

The following examples show how to use com.jme3.math.FastMath#sqrt() . 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 TerrainQuad(String name, int patchSize, int quadSize,
                        Vector3f scale, float[] heightMap, int totalSize,
                        Vector2f offset, float offsetAmount)
{
    super(name);

    if (heightMap == null)
        heightMap = generateDefaultHeightMap(quadSize);

    if (!FastMath.isPowerOfTwo(quadSize - 1)) {
        throw new RuntimeException("size given: " + quadSize + "  Terrain quad sizes may only be (2^N + 1)");
    }
    if (FastMath.sqrt(heightMap.length) > quadSize) {
        Logger.getLogger(this.getClass().getName()).log(Level.WARNING, "Heightmap size is larger than the terrain size. Make sure your heightmap image is the same size as the terrain!");
    }

    this.offset = offset;
    this.offsetAmount = offsetAmount;
    this.totalSize = totalSize;
    this.size = quadSize;
    this.patchSize = patchSize;
    this.stepScale = scale;
    split(patchSize, heightMap);
}
 
Example 2
Source File: SweepSphere.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 6 votes vote down vote up
private static float getLowestRoot(float a, float b, float c, float maxR) {
    float determinant = b * b - 4f * a * c;
    if (determinant < 0){
        return Float.NaN;
    }

    float sqrtd = FastMath.sqrt(determinant);
    float r1 = (-b - sqrtd) / (2f * a);
    float r2 = (-b + sqrtd) / (2f * a);

    if (r1 > r2){
        float temp = r2;
        r2 = r1;
        r1 = temp;
    }

    if (r1 > 0 && r1 < maxR){
        return r1;
    }

    if (r2 > 0 && r2 < maxR){
        return r2;
    }

    return Float.NaN;
}
 
Example 3
Source File: TerrainQuad.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 6 votes vote down vote up
protected TerrainQuad(String name, int patchSize, int quadSize,
                        Vector3f scale, float[] heightMap, int totalSize,
                        Vector2f offset, float offsetAmount)
{
    super(name);
    
    if (heightMap == null)
        heightMap = generateDefaultHeightMap(quadSize);
    
    if (!FastMath.isPowerOfTwo(quadSize - 1)) {
        throw new RuntimeException("size given: " + quadSize + "  Terrain quad sizes may only be (2^N + 1)");
    }
    if (FastMath.sqrt(heightMap.length) > quadSize) {
        Logger.getLogger(this.getClass().getName()).log(Level.WARNING, "Heightmap size is larger than the terrain size. Make sure your heightmap image is the same size as the terrain!");
    }
    
    this.offset = offset;
    this.offsetAmount = offsetAmount;
    this.totalSize = totalSize;
    this.size = quadSize;
    this.patchSize = patchSize;
    this.stepScale = scale;
    split(patchSize, heightMap);
}
 
Example 4
Source File: BoundingCollisionTest.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Test
public void testBoxSphereCollision() {
    BoundingBox box1 = new BoundingBox(Vector3f.ZERO, 1, 1, 1);
    BoundingSphere sphere2 = new BoundingSphere(1, Vector3f.ZERO);
    checkCollision(box1, sphere2, 1);
    
    // Put it at the very edge - for sphere vs. box, it will not intersect
    sphere2.setCenter(new Vector3f(2f, 0f, 0f));
    checkCollision(box1, sphere2, 0);
    
    // Put it a wee bit closer - should intersect.
    sphere2.setCenter(new Vector3f(2f - FastMath.ZERO_TOLERANCE, 0, 0));
    checkCollision(box1, sphere2, 1);
    
    // Test if the algorithm converts the sphere 
    // to a box before testing the collision (incorrect)
    float sqrt3 = FastMath.sqrt(3);
    
    sphere2.setCenter(Vector3f.UNIT_XYZ.mult(2));
    sphere2.setRadius(sqrt3);
    checkCollision(box1, sphere2, 0);
    
    // Make it a wee bit larger.
    sphere2.setRadius(sqrt3 + FastMath.ZERO_TOLERANCE);
    checkCollision(box1, sphere2, 1);
}
 
Example 5
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 6
Source File: HeightfieldCollisionShape.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
protected void createCollisionHeightfield(float[] heightmap, Vector3f worldScale) {
    this.scale = worldScale;
    this.heightScale = 1;//don't change away from 1, we use worldScale instead to scale

    this.heightfieldData = heightmap;

    float min = heightfieldData[0];
    float max = heightfieldData[0];
    // calculate min and max height
    for (int i = 0; i < heightfieldData.length; i++) {
        if (heightfieldData[i] < min) {
            min = heightfieldData[i];
        }
        if (heightfieldData[i] > max) {
            max = heightfieldData[i];
        }
    }
    // we need to center the terrain collision box at 0,0,0 for BulletPhysics. And to do that we need to set the
    // min and max height to be equal on either side of the y axis, otherwise it gets shifted and collision is incorrect.
    if (max < 0) {
        max = -min;
    } else {
        if (Math.abs(max) > Math.abs(min)) {
            min = -max;
        } else {
            max = -min;
        }
    }
    this.minHeight = min;
    this.maxHeight = max;

    this.upAxis = 1;
    flipQuadEdges = true;

    heightStickWidth = (int) FastMath.sqrt(heightfieldData.length);
    heightStickLength = heightStickWidth;


    createShape();
}
 
Example 7
Source File: HeightfieldCollisionShape.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
protected void createCollisionHeightfield(float[] heightmap, Vector3f worldScale) {
	this.scale = worldScale;
	this.heightScale = 1;//don't change away from 1, we use worldScale instead to scale
	
	this.heightfieldData = heightmap;

	float min = heightfieldData[0];
	float max = heightfieldData[0];
	// calculate min and max height
	for (int i=0; i<heightfieldData.length; i++) {
		if (heightfieldData[i] < min)
			min = heightfieldData[i];
		if (heightfieldData[i] > max)
			max = heightfieldData[i];
	}
	// we need to center the terrain collision box at 0,0,0 for BulletPhysics. And to do that we need to set the
	// min and max height to be equal on either side of the y axis, otherwise it gets shifted and collision is incorrect.
	if (max < 0)
		max = -min;
	else {
		if (Math.abs(max) > Math.abs(min))
			min = -max;
		else
			max = -min;
	}
	this.minHeight = min;
	this.maxHeight = max;

	this.upAxis = HeightfieldTerrainShape.YAXIS;
	this.flipQuadEdges = false;

	heightStickWidth = (int) FastMath.sqrt(heightfieldData.length);
	heightStickLength = heightStickWidth;


	createShape();
}
 
Example 8
Source File: SpotLight.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public boolean intersectsBox(BoundingBox box, TempVars vars) {
    if (this.spotRange > 0f) {
        // Check spot range first.
        // Sphere v. box collision
        if (!Intersection.intersect(box, position, spotRange)) {
            return false;
        }
    }
    
    Vector3f otherCenter = box.getCenter();
    Vector3f radVect = vars.vect4;
    radVect.set(box.getXExtent(), box.getYExtent(), box.getZExtent());
    float otherRadiusSquared = radVect.lengthSquared();
    float otherRadius = FastMath.sqrt(otherRadiusSquared);
    
    // Check if sphere is within spot angle.
    // Cone v. sphere collision.
    Vector3f E = direction.mult(otherRadius * outerAngleSinRcp, vars.vect1);
    Vector3f U = position.subtract(E, vars.vect2);
    Vector3f D = otherCenter.subtract(U, vars.vect3);

    float dsqr = D.dot(D);
    float e = direction.dot(D);

    if (e > 0f && e * e >= dsqr * outerAngleCosSqr) {
        D = otherCenter.subtract(position, vars.vect3);
        dsqr = D.dot(D);
        e = -direction.dot(D);

        if (e > 0f && e * e >= dsqr * outerAngleSinSqr) {
            return dsqr <= otherRadiusSquared;
        } else {
            return true;
        }
    }
    
    return false;
}
 
Example 9
Source File: FaultHeightMap.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
protected void addLineFault(float[][] tempBuffer, Random random, float faultHeight, float range) {
    int x1 = random.nextInt(size);
    int x2 = random.nextInt(size);
    int y1 = random.nextInt(size);
    int y2 = random.nextInt(size);


    for (int i = 0; i < size; i++) {
        for (int j = 0; j < size; j++) {
            float dist = ((x2 - x1) * (j - y1) - (y2 - y1) * (i - x1))
                    / (FastMath.sqrt(FastMath.sqr(x2 - x1) + FastMath.sqr(y2 - y1)));
            tempBuffer[i][j] += calcHeight(dist, random, faultHeight, range);
        }
    }
}
 
Example 10
Source File: LODGeomap.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 5 votes vote down vote up
private Vector3f getNormal(Vector3f firstPoint, Vector3f rootPoint, Vector3f secondPoint, Vector3f scale, Vector3f store) {
    float x1 = firstPoint.x - rootPoint.x;
    float y1 = firstPoint.y - rootPoint.y;
    float z1 = firstPoint.z - rootPoint.z;
    x1 *= scale.x;
    y1 *= scale.y;
    z1 *= scale.z;
    float x2 = secondPoint.x - rootPoint.x;
    float y2 = secondPoint.y - rootPoint.y;
    float z2 = secondPoint.z - rootPoint.z;
    x2 *= scale.x;
    y2 *= scale.y;
    z2 *= scale.z;
    float x3 = (y1 * z2) - (z1 * y2);
    float y3 = (z1 * x2) - (x1 * z2);
    float z3 = (x1 * y2) - (y1 * x2);
    
    float inv = 1.0f / FastMath.sqrt(x3 * x3 + y3 * y3 + z3 * z3);
    store.x = x3 * inv;
    store.y = y3 * inv;
    store.z = z3 * inv;
    
    
    /*firstPoint.multLocal(scale);
    rootPoint.multLocal(scale);
    secondPoint.multLocal(scale);
    firstPoint.subtractLocal(rootPoint);
    secondPoint.subtractLocal(rootPoint);
    firstPoint.cross(secondPoint, store);*/
    return store;
}
 
Example 11
Source File: TerrainQuad.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public float[] createHeightSubBlock(float[] heightMap, int x,
                int y, int side) {
    float[] rVal = new float[side * side];
    int bsize = (int) FastMath.sqrt(heightMap.length);
    int count = 0;
    for (int i = y; i < side + y; i++) {
        for (int j = x; j < side + x; j++) {
            if (j < bsize && i < bsize)
                rVal[count] = heightMap[j + (i * bsize)];
            count++;
        }
    }
    return rVal;
}
 
Example 12
Source File: TerrainQuad.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 5 votes vote down vote up
public float[] createHeightSubBlock(float[] heightMap, int x,
                int y, int side) {
    float[] rVal = new float[side * side];
    int bsize = (int) FastMath.sqrt(heightMap.length);
    int count = 0;
    for (int i = y; i < side + y; i++) {
        for (int j = x; j < side + x; j++) {
            if (j < bsize && i < bsize)
                rVal[count] = heightMap[j + (i * bsize)];
            count++;
        }
    }
    return rVal;
}
 
Example 13
Source File: FaultHeightMap.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 5 votes vote down vote up
protected void addLineFault(float[][] tempBuffer, Random random, float faultHeight, float range) {
    int x1 = random.nextInt(size);
    int x2 = random.nextInt(size);
    int y1 = random.nextInt(size);
    int y2 = random.nextInt(size);


    for (int i = 0; i < size; i++) {
        for (int j = 0; j < size; j++) {
            float dist = ((x2 - x1) * (j - y1) - (y2 - y1) * (i - x1))
                    / (FastMath.sqrt(FastMath.sqr(x2 - x1) + FastMath.sqr(y2 - y1)));
            tempBuffer[i][j] += calcHeight(dist, random, faultHeight, range);
        }
    }
}
 
Example 14
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 15
Source File: HeightfieldCollisionShape.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 5 votes vote down vote up
protected void createCollisionHeightfield(float[] heightmap, Vector3f worldScale) {
    this.scale = worldScale;
    this.heightScale = 1;//don't change away from 1, we use worldScale instead to scale

    this.heightfieldData = heightmap;

    float min = heightfieldData[0];
    float max = heightfieldData[0];
    // calculate min and max height
    for (int i = 0; i < heightfieldData.length; i++) {
        if (heightfieldData[i] < min) {
            min = heightfieldData[i];
        }
        if (heightfieldData[i] > max) {
            max = heightfieldData[i];
        }
    }
    // we need to center the terrain collision box at 0,0,0 for BulletPhysics. And to do that we need to set the
    // min and max height to be equal on either side of the y axis, otherwise it gets shifted and collision is incorrect.
    if (max < 0) {
        max = -min;
    } else {
        if (Math.abs(max) > Math.abs(min)) {
            min = -max;
        } else {
            max = -min;
        }
    }
    this.minHeight = min;
    this.maxHeight = max;

    this.upAxis = 1;
    this.flipQuadEdges = false;

    heightStickWidth = (int) FastMath.sqrt(heightfieldData.length);
    heightStickLength = heightStickWidth;


    createShape();
}
 
Example 16
Source File: HeightfieldCollisionShape.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 5 votes vote down vote up
protected void createCollisionHeightfield(float[] heightmap, Vector3f worldScale) {
	this.scale = worldScale;
	this.heightScale = 1;//don't change away from 1, we use worldScale instead to scale
	
	this.heightfieldData = heightmap;

	float min = heightfieldData[0];
	float max = heightfieldData[0];
	// calculate min and max height
	for (int i=0; i<heightfieldData.length; i++) {
		if (heightfieldData[i] < min)
			min = heightfieldData[i];
		if (heightfieldData[i] > max)
			max = heightfieldData[i];
	}
	// we need to center the terrain collision box at 0,0,0 for BulletPhysics. And to do that we need to set the
	// min and max height to be equal on either side of the y axis, otherwise it gets shifted and collision is incorrect.
	if (max < 0)
		max = -min;
	else {
		if (Math.abs(max) > Math.abs(min))
			min = -max;
		else
			max = -min;
	}
	this.minHeight = min;
	this.maxHeight = max;

	this.upAxis = HeightfieldTerrainShape.YAXIS;
	this.flipQuadEdges = false;

	heightStickWidth = (int) FastMath.sqrt(heightfieldData.length);
	heightStickLength = heightStickWidth;


	createShape();
}
 
Example 17
Source File: LODGeomap.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private Vector3f getNormal(Vector3f firstPoint, Vector3f rootPoint, Vector3f secondPoint, Vector3f scale, Vector3f store) {
    float x1 = firstPoint.x - rootPoint.x;
    float y1 = firstPoint.y - rootPoint.y;
    float z1 = firstPoint.z - rootPoint.z;
    x1 *= scale.x;
    y1 *= scale.y;
    z1 *= scale.z;
    float x2 = secondPoint.x - rootPoint.x;
    float y2 = secondPoint.y - rootPoint.y;
    float z2 = secondPoint.z - rootPoint.z;
    x2 *= scale.x;
    y2 *= scale.y;
    z2 *= scale.z;
    float x3 = (y1 * z2) - (z1 * y2);
    float y3 = (z1 * x2) - (x1 * z2);
    float z3 = (x1 * y2) - (y1 * x2);
    
    float inv = 1.0f / FastMath.sqrt(x3 * x3 + y3 * y3 + z3 * z3);
    store.x = x3 * inv;
    store.y = y3 * inv;
    store.z = z3 * inv;
    
    
    /*firstPoint.multLocal(scale);
    rootPoint.multLocal(scale);
    secondPoint.multLocal(scale);
    firstPoint.subtractLocal(rootPoint);
    secondPoint.subtractLocal(rootPoint);
    firstPoint.cross(secondPoint, store);*/
    return store;
}
 
Example 18
Source File: RawHeightMap.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
public RawHeightMap(float heightData[]) {
    this.heightData = heightData;
    this.size = (int) FastMath.sqrt(heightData.length);
    this.format = FORMAT_8BIT;
}
 
Example 19
Source File: LightFilterTest.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Test
public void testPointFiltering() {
    PointLight pl = new PointLight(Vector3f.ZERO);
    geom.addLight(pl);
    checkFilteredLights(1); // Infinite point lights must never be filtered
    
    // Light at origin does not intersect geom which is at Z=10
    pl.setRadius(1);
    checkFilteredLights(0);
    
    // Put it closer to geom, the very edge of the sphere touches the box.
    // Still not considered an intersection though.
    pl.setPosition(new Vector3f(0, 0, 8f));
    checkFilteredLights(0);
    
    // And more close - now its an intersection.
    pl.setPosition(new Vector3f(0, 0, 8f + FastMath.ZERO_TOLERANCE));
    checkFilteredLights(1);
    
    // Move the geometry away
    geom.move(0, 0, FastMath.ZERO_TOLERANCE);
    checkFilteredLights(0);
    
    // Test if the algorithm converts the sphere 
    // to a box before testing the collision (incorrect)
    float sqrt3 = FastMath.sqrt(3);
    
    pl.setPosition(new Vector3f(2, 2, 8));
    pl.setRadius(sqrt3);
    checkFilteredLights(0);
    
    // Make it a wee bit larger.
    pl.setRadius(sqrt3 + FastMath.ZERO_TOLERANCE);
    checkFilteredLights(1);
    
    // Rotate the camera so it is up, light is outside frustum.
    cam.lookAtDirection(Vector3f.UNIT_Y, Vector3f.UNIT_Y);
    checkFilteredLights(0);
    
    // ==================================
    // Tests for bounding Sphere
    geom.setModelBound(new BoundingSphere(1f, Vector3f.ZERO));
    geom.setLocalTranslation(0, 0, 2);
    pl.setPosition(new Vector3f(0, 0, 2f));

    // Infinite point lights must never be filtered
    pl.setRadius(0);
    checkFilteredLights(1);
 
    pl.setRadius(1f);
    // Put the light at the very close to the geom,
    // the very edge of the sphere touches the other bounding sphere
    // Still not considered an intersection though.
    pl.setPosition(new Vector3f(0, 0, 0));
    checkFilteredLights(0);

    // And more close - now its an intersection.
    pl.setPosition(new Vector3f(0, 0, 0f + FastMath.ZERO_TOLERANCE));
    checkFilteredLights(1);
           
    geom.setLocalTranslation(0, 0, 0);
    // In this case its an intersection for pointLight v. box
    // But not for pointLight v. sphere
    // Vector3f(0, 0.5f, 0.5f).normalize().mult(2) ~ >= (0.0, 1.4142135, 1.4142135)
    //pl.setPosition(new Vector3f(0, 0.5f, 0.5f).normalizeLocal().multLocal(2 + FastMath.ZERO_TOLERANCE));
    pl.setPosition(new Vector3f(0f, 1.4142135f, 1.4142135f).multLocal(1+FastMath.ZERO_TOLERANCE));
    checkFilteredLights(0);
    
    // Make the distance a wee bit closer, now its an intersection
    //pl.setPosition(new Vector3f(0, 0.5f, 0.5f).normalizeLocal().multLocal(2 - FastMath.ZERO_TOLERANCE));
    pl.setPosition(new Vector3f(0f, 1.4142135f, 1.4142135f).multLocal(1-FastMath.ZERO_TOLERANCE));
    checkFilteredLights(1);
    
    // it's a point light, also test for the other corner
    pl.setPosition(new Vector3f(0f, -1.4142135f, -1.4142135f).multLocal(1-FastMath.ZERO_TOLERANCE));
    checkFilteredLights(0);

}
 
Example 20
Source File: RawHeightMap.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 4 votes vote down vote up
public RawHeightMap(float heightData[]) {
    this.heightData = heightData;
    this.size = (int) FastMath.sqrt(heightData.length);
    this.format = FORMAT_8BIT;
}