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

The following examples show how to use com.jme3.math.FastMath#invSqrt() . 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: BillboardControl.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
/**
 * Rotate the billboard towards the camera, but keeping a given axis fixed.
 *
 * @param camera
 *            Camera
 */
private void rotateAxial(Camera camera, Vector3f axis) {
    // Compute the additional rotation required for the billboard to face
    // the camera. To do this, the camera must be inverse-transformed into
    // the model space of the billboard.
    look.set(camera.getLocation()).subtractLocal(
            spatial.getWorldTranslation());   
    spatial.getParent().getWorldRotation().mult(look, left); // coopt left for our own
    // purposes.
    left.x *= 1.0f / spatial.getWorldScale().x;
    left.y *= 1.0f / spatial.getWorldScale().y;
    left.z *= 1.0f / spatial.getWorldScale().z;

    // squared length of the camera projection in the xz-plane
    float lengthSquared = left.x * left.x + left.z * left.z;
    if (lengthSquared < FastMath.FLT_EPSILON) {
        // camera on the billboard axis, rotation not defined
        return;
    }

    // unitize the projection
    float invLength = FastMath.invSqrt(lengthSquared);
    if (axis.y == 1) {
        left.x *= invLength;
        left.y = 0.0f;
        left.z *= invLength;

        // compute the local orientation matrix for the billboard
        orient.set(0, 0, left.z);
        orient.set(0, 1, 0);
        orient.set(0, 2, left.x);
        orient.set(1, 0, 0);
        orient.set(1, 1, 1);
        orient.set(1, 2, 0);
        orient.set(2, 0, -left.x);
        orient.set(2, 1, 0);
        orient.set(2, 2, left.z);
    } else if (axis.z == 1) {
        left.x *= invLength;
        left.y *= invLength;
        left.z = 0.0f;

        // compute the local orientation matrix for the billboard
        orient.set(0, 0, left.y);
        orient.set(0, 1, left.x);
        orient.set(0, 2, 0);
        orient.set(1, 0, -left.y);
        orient.set(1, 1, left.x);
        orient.set(1, 2, 0);
        orient.set(2, 0, 0);
        orient.set(2, 1, 0);
        orient.set(2, 2, 1);
    }

    // The billboard must be oriented to face the camera before it is
    // transformed into the world.
    spatial.setLocalRotation(orient);
    fixRefreshFlags();
}
 
Example 2
Source File: BillboardControl.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 4 votes vote down vote up
/**
 * Rotate the billboard towards the camera, but keeping a given axis fixed.
 *
 * @param camera
 *            Camera
 */
private void rotateAxial(Camera camera, Vector3f axis) {
    // Compute the additional rotation required for the billboard to face
    // the camera. To do this, the camera must be inverse-transformed into
    // the model space of the billboard.
    look.set(camera.getLocation()).subtractLocal(
            spatial.getWorldTranslation());   
    spatial.getParent().getWorldRotation().mult(look, left); // coopt left for our own
    // purposes.
    left.x *= 1.0f / spatial.getWorldScale().x;
    left.y *= 1.0f / spatial.getWorldScale().y;
    left.z *= 1.0f / spatial.getWorldScale().z;

    // squared length of the camera projection in the xz-plane
    float lengthSquared = left.x * left.x + left.z * left.z;
    if (lengthSquared < FastMath.FLT_EPSILON) {
        // camera on the billboard axis, rotation not defined
        return;
    }

    // unitize the projection
    float invLength = FastMath.invSqrt(lengthSquared);
    if (axis.y == 1) {
        left.x *= invLength;
        left.y = 0.0f;
        left.z *= invLength;

        // compute the local orientation matrix for the billboard
        orient.set(0, 0, left.z);
        orient.set(0, 1, 0);
        orient.set(0, 2, left.x);
        orient.set(1, 0, 0);
        orient.set(1, 1, 1);
        orient.set(1, 2, 0);
        orient.set(2, 0, -left.x);
        orient.set(2, 1, 0);
        orient.set(2, 2, left.z);
    } else if (axis.z == 1) {
        left.x *= invLength;
        left.y *= invLength;
        left.z = 0.0f;

        // compute the local orientation matrix for the billboard
        orient.set(0, 0, left.y);
        orient.set(0, 1, left.x);
        orient.set(0, 2, 0);
        orient.set(1, 0, -left.y);
        orient.set(1, 1, left.x);
        orient.set(1, 2, 0);
        orient.set(2, 0, 0);
        orient.set(2, 1, 0);
        orient.set(2, 2, 1);
    }

    // The billboard must be oriented to face the camera before it is
    // transformed into the world.
    spatial.setLocalRotation(orient);
    fixRefreshFlags();
}