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

The following examples show how to use com.jme3.math.FastMath#cos() . 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: LeaderFollowingBehavior.java    From MonkeyBrains with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * @see AbstractStrengthSteeringBehavior#calculateRawSteering()
 */
@Override
protected Vector3f calculateRawSteering() {
    Vector3f steer;
    float distanceBetwen = this.agent.distanceRelativeToGameEntity(this.getTarget());

    //See how far ahead we need to leed
    Vector3f fullProjectedLocation = this.getTarget().getPredictedPosition();
    Vector3f predictedPositionDiff = fullProjectedLocation.subtract(this.getTarget().getLocalTranslation());
    Vector3f projectedLocation = this.getTarget().getLocalTranslation().add(predictedPositionDiff.mult(
            this.calculateFocusFactor(distanceBetwen)));

    this.arriveBehavior.setSeekingPosition(projectedLocation);

    steer = this.arriveBehavior.calculateRawSteering();

    if (!(distanceBetwen > this.distanceToEvade) && !(this.getTarget().forwardness(this.agent) < FastMath.cos(this.minimumAngle))) { //Incorrect angle and Is in the proper distance to evade -> Evade the leader

        Vector3f arriveSteer = steer.mult(distanceBetwen / this.distanceToEvade);
        Vector3f evadeSteer = this.evadeBehavior.calculateRawSteering();
        evadeSteer.mult(this.distanceToEvade / (1 + distanceBetwen));
        steer = (new Vector3f()).add(arriveSteer).add(evadeSteer);
    }

    return steer;
}
 
Example 2
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 3
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 4
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 5
Source File: TangentBinormalGenerator.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 5 votes vote down vote up
public static void setToleranceAngle(float angle) {
    if (angle < 0 || angle > 179) {
        throw new IllegalArgumentException(
                    "The angle must be between 0 and 179 degrees.");
    }
    toleranceDot = FastMath.cos(angle*FastMath.DEG_TO_RAD);
    toleranceAngle = angle;
}
 
Example 6
Source File: Agent.java    From MonkeyBrains with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Check if this agent is considered in the same "neighborhood" in relation
 * with another agent. <br> <br>
 *
 * If the distance is lower than minDistance It is definitely considered in
 * the same neighborhood. <br> <br>
 *
 * If the distance is higher than maxDistance It is defenitely not
 * considered in the same neighborhood. <br> <br>
 *
 * If the distance is inside [minDistance. maxDistance] It is considered in
 * the same neighborhood if the forwardness is higher than "1 -
 * sinMaxAngle".
 *
 * @param GameEntity The other agent
 * @param minDistance Min. distance to be in the same "neighborhood"
 * @param maxDistance Max. distance to be in the same "neighborhood"
 * @param maxAngle Max angle in radians
 *
 * @throws SteeringExceptions.NegativeValueException If minDistance or
 * maxDistance is lower than 0
 *
 * @return If this agent is in the same "neighborhood" in relation with
 * another agent.
 */
public boolean inBoidNeighborhood(GameEntity neighbour, float minDistance, float maxDistance, float maxAngle) {
    if (minDistance < 0) {
        throw new SteeringExceptions.NegativeValueException("The min distance can not be negative.", minDistance);
    } else if (maxDistance < 0) {
        throw new SteeringExceptions.NegativeValueException("The max distance can not be negative.", maxDistance);
    }
    boolean isInBoidNeighborhood;
    if (this == neighbour) {
        isInBoidNeighborhood = false;
    } else {
        float distanceSquared = distanceSquaredRelativeToGameEntity(neighbour);
        // definitely in neighborhood if inside minDistance sphere
        if (distanceSquared < (minDistance * minDistance)) {
            isInBoidNeighborhood = true;
        } // definitely not in neighborhood if outside maxDistance sphere
        else if (distanceSquared > maxDistance * maxDistance) {
            isInBoidNeighborhood = false;
        } // otherwise, test angular offset from forward axis.
        else {
            if (this.getAcceleration() != null) {
                Vector3f unitOffset = this.offset(neighbour).divide(distanceSquared);
                float forwardness = this.forwardness(unitOffset);
                isInBoidNeighborhood = forwardness > FastMath.cos(maxAngle);
            } else {
                isInBoidNeighborhood = false;
            }
        }
    }

    return isInBoidNeighborhood;
}
 
Example 7
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 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: TangentBinormalGenerator.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public static void setToleranceAngle(float angle) {
    if (angle < 0 || angle > 179) {
        throw new IllegalArgumentException(
                "The angle must be between 0 and 179 degrees.");
    }
    toleranceDot = FastMath.cos(angle * FastMath.DEG_TO_RAD);
}
 
Example 10
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 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: 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 13
Source File: SilentTangentBinormalGenerator.java    From OpenRTS with MIT License 4 votes vote down vote up
public static void setToleranceAngle(float angle) {
	if (angle < 0 || angle > 179) {
		throw new IllegalArgumentException("The angle must be between 0 and 179 degrees.");
	}
	toleranceDot = FastMath.cos(angle * FastMath.DEG_TO_RAD);
}
 
Example 14
Source File: GameEntity.java    From MonkeyBrains with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
/**
 * @param positionVector Offset vector.
 * @return The forwardness in relation with a position vector
 */
public float forwardness(Vector3f offsetVector) {
    Vector3f agentLooks = getLocalRotation().mult(new Vector3f(0, 0, 1)).normalize();
    float radiansAngleBetwen = agentLooks.angleBetween(offsetVector.normalize());
    return FastMath.cos(radiansAngleBetwen);
}
 
Example 15
Source File: SpotLight.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 4 votes vote down vote up
private void computePackedCos() {
    float innerCos=FastMath.cos(spotInnerAngle);
    float outerCos=FastMath.cos(spotOuterAngle);
    packedAngleCos=(int)(innerCos*1000);
    packedAngleCos+=outerCos;
}
 
Example 16
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 17
Source File: GameEntity.java    From MonkeyBrains with BSD 3-Clause "New" or "Revised" License 2 votes vote down vote up
/**
 * Calculates the forwardness in relation with another game entity. That is
 * how "forward" is the direction to the quarry (1 means dead ahead, 0 is
 * directly to the side, -1 is straight back)
 *
 * @param gameEntity Other game entity
 * @return The forwardness in relation with another agent
 */
public float forwardness(GameEntity gameEntity) {
    Vector3f agentLooks = fordwardVector();
    float radiansAngleBetwen = agentLooks.angleBetween(offset(gameEntity).normalize());
    return FastMath.cos(radiansAngleBetwen);
}