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

The following examples show how to use com.jme3.math.FastMath#sin() . 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: FbxNodeUtil.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
public static Quaternion quatFromBoneAngles(float xAngle, float yAngle, float zAngle) {
    float angle;
    float sinY, sinZ, sinX, cosY, cosZ, cosX;
    angle = zAngle * 0.5f;
    sinZ = FastMath.sin(angle);
    cosZ = FastMath.cos(angle);
    angle = yAngle * 0.5f;
    sinY = FastMath.sin(angle);
    cosY = FastMath.cos(angle);
    angle = xAngle * 0.5f;
    sinX = FastMath.sin(angle);
    cosX = FastMath.cos(angle);
    float cosYXcosZ = cosY * cosZ;
    float sinYXsinZ = sinY * sinZ;
    float cosYXsinZ = cosY * sinZ;
    float sinYXcosZ = sinY * cosZ;
    // For some reason bone space is differ, this is modified formulas
    float w = (cosYXcosZ * cosX + sinYXsinZ * sinX);
    float x = (cosYXcosZ * sinX - sinYXsinZ * cosX);
    float y = (sinYXcosZ * cosX + cosYXsinZ * sinX);
    float z = (cosYXsinZ * cosX - sinYXcosZ * sinX);
    return new Quaternion(x, y, z, w).normalizeLocal();
}
 
Example 2
Source File: SpotLight.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
private void computeAngleParameters() {
    float innerCos = FastMath.cos(spotInnerAngle);
    outerAngleCos = FastMath.cos(spotOuterAngle);
    packedAngleCos = (int) (innerCos * 1000);
    
    //due to approximations, very close angles can give the same cos
    //here we make sure outer cos is bellow inner cos.
    if (((int) packedAngleCos) == ((int) (outerAngleCos * 1000))) {
        outerAngleCos -= 0.001f;
    }
    packedAngleCos += outerAngleCos;

    if (packedAngleCos == 0.0f) {
        throw new IllegalArgumentException("Packed angle cosine is invalid");
    }
    
    // compute parameters needed for cone vs sphere check.
    outerAngleSin    = FastMath.sin(spotOuterAngle);
    outerAngleCosSqr = outerAngleCos * outerAngleCos;
    outerAngleSinSqr = outerAngleSin * outerAngleSin;
    outerAngleSinRcp = 1.0f / outerAngleSin;
}
 
Example 3
Source File: Circle.java    From OpenRTS with MIT License 6 votes vote down vote up
protected void updateGeometry() {
	FloatBuffer positions = BufferUtils.createFloatBuffer(samples * 3);
	FloatBuffer normals = BufferUtils.createFloatBuffer(samples * 3);
	short[] indices = new short[samples * 2];

	float rate = FastMath.TWO_PI / samples;
	float angle = 0;
	for (int i = 0; i < samples; i++) {
		float x = FastMath.cos(angle) + center.x;
		float y = FastMath.sin(angle) + center.y;
		positions.put(x * radius).put(y * radius).put(center.z);
		normals.put(new float[] { 0, 1, 0 });
		indices[i * 2] = (short) i;
		indices[i * 2 + 1] = (short) ((i + 1) % samples);
		angle += rate;
	}

	setBuffer(Type.Position, 3, positions);
	setBuffer(Type.Normal, 3, normals);
	setBuffer(Type.Index, 2, indices);

	setBuffer(Type.TexCoord, 2, new float[] { 0, 0, 1, 1 });

	updateBound();
}
 
Example 4
Source File: EditorCamera.java    From jmonkeybuilder with Apache License 2.0 5 votes vote down vote up
/**
 * Compute position.
 */
protected void computePosition() {

    float highDistance = (distance) * FastMath.sin((FastMath.PI / 2) - verticalRotation);

    position.set(highDistance * FastMath.cos(rotation), (distance) * FastMath.sin(verticalRotation), highDistance * FastMath.sin(rotation));
    position.addLocal(target.getWorldTranslation());
}
 
Example 5
Source File: TestBlendEquations.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public void simpleUpdate(float tpf) {
    timer += tpf;

    float xOffset = FastMath.sin(timer * 0.5f) * 2f;
    leftQuad.setLocalTranslation(xOffset - 2f, 0f, 0.5f);
    rightQuad.setLocalTranslation(xOffset + 1f, 0f, 0.5f);
}
 
Example 6
Source File: PhysicsTestHelper.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private static float getY(float x, float z, float max) {
    float yMaxHeight = 8;
    float xv = FastMath.unInterpolateLinear(FastMath.abs(x - (max / 2)), 0, max) * FastMath.TWO_PI;
    float zv = FastMath.unInterpolateLinear(FastMath.abs(z - (max / 2)), 0, max) * FastMath.TWO_PI;

    float xComp = (FastMath.sin(xv) + 1) * 0.5f;
    float zComp = (FastMath.sin(zv) + 1) * 0.5f;

    return -yMaxHeight * xComp * zComp;
}
 
Example 7
Source File: TestMovingParticle.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public void simpleUpdate(float tpf) {
    angle += tpf;
    angle %= FastMath.TWO_PI;
    float x = FastMath.cos(angle) * 2;
    float y = FastMath.sin(angle) * 2;
    emit.setLocalTranslation(x, 0, y);
}
 
Example 8
Source File: TestMovingParticle.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 5 votes vote down vote up
@Override
public void simpleUpdate(float tpf) {
    angle += tpf;
    angle %= FastMath.TWO_PI;
    float x = FastMath.cos(angle) * 2;
    float y = FastMath.sin(angle) * 2;
    emit.setLocalTranslation(x, 0, y);
}
 
Example 9
Source File: TestMovingParticle.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 5 votes vote down vote up
@Override
public void simpleUpdate(float tpf) {
    angle += tpf;
    angle %= FastMath.TWO_PI;
    float x = FastMath.cos(angle) * 2;
    float y = FastMath.sin(angle) * 2;
    emit.setLocalTranslation(x, 0, y);
}
 
Example 10
Source File: Torus.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
private void setGeometryData() {
    // allocate vertices
    int vertCount = (circleSamples + 1) * (radialSamples + 1);
    FloatBuffer fpb = BufferUtils.createVector3Buffer(vertCount);
    setBuffer(Type.Position, 3, fpb);

    // allocate normals if requested
    FloatBuffer fnb = BufferUtils.createVector3Buffer(vertCount);
    setBuffer(Type.Normal, 3, fnb);

    // allocate texture coordinates
    FloatBuffer ftb = BufferUtils.createVector2Buffer(vertCount);
    setBuffer(Type.TexCoord, 2, ftb);

    // generate geometry
    float inverseCircleSamples = 1.0f / circleSamples;
    float inverseRadialSamples = 1.0f / radialSamples;
    int i = 0;
    // generate the cylinder itself
    Vector3f radialAxis = new Vector3f(), torusMiddle = new Vector3f(), tempNormal = new Vector3f();
    for (int circleCount = 0; circleCount < circleSamples; circleCount++) {
        // compute center point on torus circle at specified angle
        float circleFraction = circleCount * inverseCircleSamples;
        float theta = FastMath.TWO_PI * circleFraction;
        float cosTheta = FastMath.cos(theta);
        float sinTheta = FastMath.sin(theta);
        radialAxis.set(cosTheta, sinTheta, 0);
        radialAxis.mult(outerRadius, torusMiddle);

        // compute slice vertices with duplication at end point
        int iSave = i;
        for (int radialCount = 0; radialCount < radialSamples; radialCount++) {
            float radialFraction = radialCount * inverseRadialSamples;
            // in [0,1)
            float phi = FastMath.TWO_PI * radialFraction;
            float cosPhi = FastMath.cos(phi);
            float sinPhi = FastMath.sin(phi);
            tempNormal.set(radialAxis).multLocal(cosPhi);
            tempNormal.z += sinPhi;
            fnb.put(tempNormal.x).put(tempNormal.y).put(
                    tempNormal.z);
   
            tempNormal.multLocal(innerRadius).addLocal(torusMiddle);
            fpb.put(tempNormal.x).put(tempNormal.y).put(
                    tempNormal.z);

            ftb.put(radialFraction).put(circleFraction);
            i++;
        }

        BufferUtils.copyInternalVector3(fpb, iSave, i);
        BufferUtils.copyInternalVector3(fnb, iSave, i);

        ftb.put(1.0f).put(circleFraction);

        i++;
    }

    // duplicate the cylinder ends to form a torus
    for (int iR = 0; iR <= radialSamples; iR++, i++) {
        BufferUtils.copyInternalVector3(fpb, iR, i);
        BufferUtils.copyInternalVector3(fnb, iR, i);
        BufferUtils.copyInternalVector2(ftb, iR, i);
        ftb.put(i * 2 + 1, 1.0f);
    }
}
 
Example 11
Source File: BoundingSphereDebug.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
/**
 * builds the vertices based on the radius
 */
private void setGeometryData() {
    setMode(Mode.Lines);

    FloatBuffer posBuf = BufferUtils.createVector3Buffer((radialSamples + 1) * 3);
    FloatBuffer colBuf = BufferUtils.createVector3Buffer((radialSamples + 1) * 4);

    setBuffer(Type.Position, 3, posBuf);
    setBuffer(Type.Color, 4, colBuf);

    // generate geometry
    float fInvRS = 1.0f / radialSamples;

    // Generate points on the unit circle to be used in computing the mesh
    // points on a sphere slice.
    float[] afSin = new float[(radialSamples + 1)];
    float[] afCos = new float[(radialSamples + 1)];
    for (int iR = 0; iR < radialSamples; iR++) {
        float fAngle = FastMath.TWO_PI * fInvRS * iR;
        afCos[iR] = FastMath.cos(fAngle);
        afSin[iR] = FastMath.sin(fAngle);
    }
    afSin[radialSamples] = afSin[0];
    afCos[radialSamples] = afCos[0];

    for (int iR = 0; iR <= radialSamples; iR++) {
        posBuf.put(afCos[iR])
                .put(afSin[iR])
                .put(0);
        colBuf.put(ColorRGBA.Blue.r)
                .put(ColorRGBA.Blue.g)
                .put(ColorRGBA.Blue.b)
                .put(ColorRGBA.Blue.a);

    }
    for (int iR = 0; iR <= radialSamples; iR++) {
        posBuf.put(afCos[iR])
                .put(0)
                .put(afSin[iR]);
        colBuf.put(ColorRGBA.Green.r)
                .put(ColorRGBA.Green.g)
                .put(ColorRGBA.Green.b)
                .put(ColorRGBA.Green.a);
    }
    for (int iR = 0; iR <= radialSamples; iR++) {
        posBuf.put(0)
                .put(afCos[iR])
                .put(afSin[iR]);
        colBuf.put(ColorRGBA.Yellow.r)
                .put(ColorRGBA.Yellow.g)
                .put(ColorRGBA.Yellow.b)
                .put(ColorRGBA.Yellow.a);
    }

    updateBound();
    setStatic();
}
 
Example 12
Source File: ChaseCamera.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
protected void computePosition() {

        float hDistance = (distance) * FastMath.sin((FastMath.PI / 2) - vRotation);
        pos.set(hDistance * FastMath.cos(rotation), (distance) * FastMath.sin(vRotation), hDistance * FastMath.sin(rotation));
        pos.addLocal(target.getWorldTranslation());
    }
 
Example 13
Source File: Torus.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 4 votes vote down vote up
private void setGeometryData() {
    // allocate vertices
    int vertCount = (circleSamples + 1) * (radialSamples + 1);
    FloatBuffer fpb = BufferUtils.createVector3Buffer(vertCount);
    setBuffer(Type.Position, 3, fpb);

    // allocate normals if requested
    FloatBuffer fnb = BufferUtils.createVector3Buffer(vertCount);
    setBuffer(Type.Normal, 3, fnb);

    // allocate texture coordinates
    FloatBuffer ftb = BufferUtils.createVector2Buffer(vertCount);
    setBuffer(Type.TexCoord, 2, ftb);

    // generate geometry
    float inverseCircleSamples = 1.0f / circleSamples;
    float inverseRadialSamples = 1.0f / radialSamples;
    int i = 0;
    // generate the cylinder itself
    Vector3f radialAxis = new Vector3f(), torusMiddle = new Vector3f(), tempNormal = new Vector3f();
    for (int circleCount = 0; circleCount < circleSamples; circleCount++) {
        // compute center point on torus circle at specified angle
        float circleFraction = circleCount * inverseCircleSamples;
        float theta = FastMath.TWO_PI * circleFraction;
        float cosTheta = FastMath.cos(theta);
        float sinTheta = FastMath.sin(theta);
        radialAxis.set(cosTheta, sinTheta, 0);
        radialAxis.mult(outerRadius, torusMiddle);

        // compute slice vertices with duplication at end point
        int iSave = i;
        for (int radialCount = 0; radialCount < radialSamples; radialCount++) {
            float radialFraction = radialCount * inverseRadialSamples;
            // in [0,1)
            float phi = FastMath.TWO_PI * radialFraction;
            float cosPhi = FastMath.cos(phi);
            float sinPhi = FastMath.sin(phi);
            tempNormal.set(radialAxis).multLocal(cosPhi);
            tempNormal.z += sinPhi;
            fnb.put(tempNormal.x).put(tempNormal.y).put(
                    tempNormal.z);
   
            tempNormal.multLocal(innerRadius).addLocal(torusMiddle);
            fpb.put(tempNormal.x).put(tempNormal.y).put(
                    tempNormal.z);

            ftb.put(radialFraction).put(circleFraction);
            i++;
        }

        BufferUtils.copyInternalVector3(fpb, iSave, i);
        BufferUtils.copyInternalVector3(fnb, iSave, i);

        ftb.put(1.0f).put(circleFraction);

        i++;
    }

    // duplicate the cylinder ends to form a torus
    for (int iR = 0; iR <= radialSamples; iR++, i++) {
        BufferUtils.copyInternalVector3(fpb, iR, i);
        BufferUtils.copyInternalVector3(fnb, iR, i);
        BufferUtils.copyInternalVector2(ftb, iR, i);
        ftb.put(i * 2 + 1, 1.0f);
    }
}
 
Example 14
Source File: ChaseCamera.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 4 votes vote down vote up
private void computePosition() {

        float hDistance = (distance) * FastMath.sin((FastMath.PI / 2) - vRotation);
        pos.set(hDistance * FastMath.cos(rotation), (distance) * FastMath.sin(vRotation), hDistance * FastMath.sin(rotation));
        pos.addLocal(target.getWorldTranslation());
    }