Search in sources :

Example 41 with Quaternion

use of com.jme3.math.Quaternion in project jmonkeyengine by jMonkeyEngine.

the class BillboardControl method rotateScreenAligned.

/**
     * Rotate the billboard so it points directly opposite the direction the
     * camera's facing
     *
     * @param camera
     *            Camera
     */
private void rotateScreenAligned(Camera camera) {
    // coopt diff for our in direction:
    look.set(camera.getDirection()).negateLocal();
    // coopt loc for our left direction:
    left.set(camera.getLeft()).negateLocal();
    orient.fromAxes(left, camera.getUp(), look);
    Node parent = spatial.getParent();
    Quaternion rot = new Quaternion().fromRotationMatrix(orient);
    if (parent != null) {
        rot = parent.getWorldRotation().inverse().multLocal(rot);
        rot.normalizeLocal();
    }
    spatial.setLocalRotation(rot);
    fixRefreshFlags();
}
Also used : Quaternion(com.jme3.math.Quaternion) Node(com.jme3.scene.Node)

Example 42 with Quaternion

use of com.jme3.math.Quaternion in project jmonkeyengine by jMonkeyEngine.

the class CameraControl method controlUpdate.

// fields used, when inversing ControlDirection:
@Override
protected void controlUpdate(float tpf) {
    if (spatial != null && camera != null) {
        switch(controlDir) {
            case SpatialToCamera:
                camera.setLocation(spatial.getWorldTranslation());
                camera.setRotation(spatial.getWorldRotation());
                break;
            case CameraToSpatial:
                // set the localtransform, so that the worldtransform would be equal to the camera's transform.
                // Location:
                TempVars vars = TempVars.get();
                Vector3f vecDiff = vars.vect1.set(camera.getLocation()).subtractLocal(spatial.getWorldTranslation());
                spatial.setLocalTranslation(vecDiff.addLocal(spatial.getLocalTranslation()));
                // Rotation:
                Quaternion worldDiff = vars.quat1.set(camera.getRotation()).subtractLocal(spatial.getWorldRotation());
                spatial.setLocalRotation(worldDiff.addLocal(spatial.getLocalRotation()));
                vars.release();
                break;
        }
    }
}
Also used : Quaternion(com.jme3.math.Quaternion) Vector3f(com.jme3.math.Vector3f) TempVars(com.jme3.util.TempVars)

Example 43 with Quaternion

use of com.jme3.math.Quaternion in project jmonkeyengine by jMonkeyEngine.

the class Quaternion method lookAt.

/**
     * <code>lookAt</code> is a convienence method for auto-setting the
     * quaternion based on a direction and an up vector. It computes
     * the rotation to transform the z-axis to point into 'direction'
     * and the y-axis to 'up'.
     *
     * @param direction
     *            where to look at in terms of local coordinates
     * @param up
     *            a vector indicating the local up direction.
     *            (typically {0, 1, 0} in jME.)
     */
public void lookAt(Vector3f direction, Vector3f up) {
    TempVars vars = TempVars.get();
    vars.vect3.set(direction).normalizeLocal();
    vars.vect1.set(up).crossLocal(direction).normalizeLocal();
    vars.vect2.set(direction).crossLocal(vars.vect1).normalizeLocal();
    fromAxes(vars.vect1, vars.vect2, vars.vect3);
    vars.release();
}
Also used : TempVars(com.jme3.util.TempVars)

Example 44 with Quaternion

use of com.jme3.math.Quaternion in project jmonkeyengine by jMonkeyEngine.

the class Quaternion method toRotationMatrix.

/**
     * <code>toRotationMatrix</code> converts this quaternion to a rotational
     * matrix. The result is stored in result. 4th row and 4th column values are
     * untouched. Note: the result is created from a normalized version of this quat.
     * 
     * @param result
     *            The Matrix4f to store the result in.
     * @return the rotation matrix representation of this quaternion.
     */
public Matrix4f toRotationMatrix(Matrix4f result) {
    TempVars tempv = TempVars.get();
    Vector3f originalScale = tempv.vect1;
    result.toScaleVector(originalScale);
    result.setScale(1, 1, 1);
    float norm = norm();
    // we explicitly test norm against one here, saving a division
    // at the cost of a test and branch.  Is it worth it?
    float s = (norm == 1f) ? 2f : (norm > 0f) ? 2f / norm : 0;
    // compute xs/ys/zs first to save 6 multiplications, since xs/ys/zs
    // will be used 2-4 times each.
    float xs = x * s;
    float ys = y * s;
    float zs = z * s;
    float xx = x * xs;
    float xy = x * ys;
    float xz = x * zs;
    float xw = w * xs;
    float yy = y * ys;
    float yz = y * zs;
    float yw = w * ys;
    float zz = z * zs;
    float zw = w * zs;
    // using s=2/norm (instead of 1/norm) saves 9 multiplications by 2 here
    result.m00 = 1 - (yy + zz);
    result.m01 = (xy - zw);
    result.m02 = (xz + yw);
    result.m10 = (xy + zw);
    result.m11 = 1 - (xx + zz);
    result.m12 = (yz - xw);
    result.m20 = (xz - yw);
    result.m21 = (yz + xw);
    result.m22 = 1 - (xx + yy);
    result.setScale(originalScale);
    tempv.release();
    return result;
}
Also used : TempVars(com.jme3.util.TempVars)

Example 45 with Quaternion

use of com.jme3.math.Quaternion in project jmonkeyengine by jMonkeyEngine.

the class CubeField method camTakeOver.

/**
     * Forcefully takes over Camera adding functionality and placing it behind the character
     * @param tpf Tickes Per Frame
     */
private void camTakeOver(float tpf) {
    cam.setLocation(player.getLocalTranslation().add(-8, 2, 0));
    cam.lookAt(player.getLocalTranslation(), Vector3f.UNIT_Y);
    Quaternion rot = new Quaternion();
    rot.fromAngleNormalAxis(camAngle, Vector3f.UNIT_Z);
    cam.setRotation(cam.getRotation().mult(rot));
    camAngle *= FastMath.pow(.99f, fpsRate * tpf);
}
Also used : Quaternion(com.jme3.math.Quaternion)

Aggregations

Quaternion (com.jme3.math.Quaternion)115 Vector3f (com.jme3.math.Vector3f)100 Geometry (com.jme3.scene.Geometry)42 DirectionalLight (com.jme3.light.DirectionalLight)37 Material (com.jme3.material.Material)36 Node (com.jme3.scene.Node)30 FilterPostProcessor (com.jme3.post.FilterPostProcessor)26 Spatial (com.jme3.scene.Spatial)26 KeyTrigger (com.jme3.input.controls.KeyTrigger)22 Box (com.jme3.scene.shape.Box)21 TempVars (com.jme3.util.TempVars)16 ActionListener (com.jme3.input.controls.ActionListener)15 ColorRGBA (com.jme3.math.ColorRGBA)15 Quad (com.jme3.scene.shape.Quad)15 AmbientLight (com.jme3.light.AmbientLight)14 Sphere (com.jme3.scene.shape.Sphere)11 Bone (com.jme3.animation.Bone)9 PointLight (com.jme3.light.PointLight)8 AnimControl (com.jme3.animation.AnimControl)7 SpotLight (com.jme3.light.SpotLight)7