Search in sources :

Example 1 with TempVars

use of com.jme3.util.TempVars in project jmonkeyengine by jMonkeyEngine.

the class KinematicRagdollControl method kinematicUpdate.

protected void kinematicUpdate(float tpf) {
    //the ragdoll does not have the controll, so the keyframed animation updates the physic position of the physic bonces
    TempVars vars = TempVars.get();
    Quaternion tmpRot1 = vars.quat1;
    Quaternion tmpRot2 = vars.quat2;
    Vector3f position = vars.vect1;
    for (PhysicsBoneLink link : boneLinks.values()) {
        //but to allow smooth transition, we blend this transformation with the saved position of the ragdoll
        if (blendedControl) {
            Vector3f position2 = vars.vect2;
            //initializing tmp vars with the start position/rotation of the ragdoll
            position.set(link.startBlendingPos);
            tmpRot1.set(link.startBlendingRot);
            //interpolating between ragdoll position/rotation and keyframed position/rotation
            tmpRot2.set(tmpRot1).nlerp(link.bone.getModelSpaceRotation(), blendStart / blendTime);
            position2.set(position).interpolateLocal(link.bone.getModelSpacePosition(), blendStart / blendTime);
            tmpRot1.set(tmpRot2);
            position.set(position2);
            //updating bones transforms
            if (boneList.isEmpty()) {
                //we ensure we have the control to update the bone
                link.bone.setUserControl(true);
                link.bone.setUserTransformsInModelSpace(position, tmpRot1);
                //we give control back to the key framed animation.
                link.bone.setUserControl(false);
            } else {
                RagdollUtils.setTransform(link.bone, position, tmpRot1, true, boneList);
            }
        }
        //setting skeleton transforms to the ragdoll
        matchPhysicObjectToBone(link, position, tmpRot1);
        modelPosition.set(targetModel.getLocalTranslation());
    }
    //time control for blending
    if (blendedControl) {
        blendStart += tpf;
        if (blendStart > blendTime) {
            blendedControl = false;
        }
    }
    vars.release();
}
Also used : Quaternion(com.jme3.math.Quaternion) Vector3f(com.jme3.math.Vector3f) TempVars(com.jme3.util.TempVars)

Example 2 with TempVars

use of com.jme3.util.TempVars in project jmonkeyengine by jMonkeyEngine.

the class KinematicRagdollControl method blendToKinematicMode.

/**
     * Smoothly blend from Ragdoll mode to Kinematic mode This is useful to
     * blend ragdoll actual position to a keyframe animation for example
     *
     * @param blendTime the blending time between ragdoll to anim.
     */
public void blendToKinematicMode(float blendTime) {
    if (mode == Mode.Kinematic) {
        return;
    }
    blendedControl = true;
    this.blendTime = blendTime;
    mode = Mode.Kinematic;
    AnimControl animControl = targetModel.getControl(AnimControl.class);
    animControl.setEnabled(true);
    TempVars vars = TempVars.get();
    for (PhysicsBoneLink link : boneLinks.values()) {
        Vector3f p = link.rigidBody.getMotionState().getWorldLocation();
        Vector3f position = vars.vect1;
        targetModel.getWorldTransform().transformInverseVector(p, position);
        Quaternion q = link.rigidBody.getMotionState().getWorldRotationQuat();
        Quaternion q2 = vars.quat1;
        Quaternion q3 = vars.quat2;
        q2.set(q).multLocal(link.initalWorldRotation).normalizeLocal();
        q3.set(targetModel.getWorldRotation()).inverseLocal().mult(q2, q2);
        q2.normalizeLocal();
        link.startBlendingPos.set(position);
        link.startBlendingRot.set(q2);
        link.rigidBody.setKinematic(true);
    }
    vars.release();
    for (Bone bone : skeleton.getRoots()) {
        RagdollUtils.setUserControl(bone, false);
    }
    blendStart = 0;
}
Also used : Quaternion(com.jme3.math.Quaternion) Vector3f(com.jme3.math.Vector3f) TempVars(com.jme3.util.TempVars) Bone(com.jme3.animation.Bone) AnimControl(com.jme3.animation.AnimControl)

Example 3 with TempVars

use of com.jme3.util.TempVars in project jmonkeyengine by jMonkeyEngine.

the class TangentBinormalGenerator method processTriangle.

public static TriangleData processTriangle(int[] index, Vector3f[] v, Vector2f[] t) {
    TempVars tmp = TempVars.get();
    try {
        Vector3f edge1 = tmp.vect1;
        Vector3f edge2 = tmp.vect2;
        Vector2f edge1uv = tmp.vect2d;
        Vector2f edge2uv = tmp.vect2d2;
        Vector3f tangent = tmp.vect3;
        Vector3f binormal = tmp.vect4;
        Vector3f normal = tmp.vect5;
        t[1].subtract(t[0], edge1uv);
        t[2].subtract(t[0], edge2uv);
        float det = edge1uv.x * edge2uv.y - edge1uv.y * edge2uv.x;
        boolean normalize = false;
        if (Math.abs(det) < ZERO_TOLERANCE) {
            log.log(Level.WARNING, "Colinear uv coordinates for triangle " + "[{0}, {1}, {2}]; tex0 = [{3}, {4}], " + "tex1 = [{5}, {6}], tex2 = [{7}, {8}]", new Object[] { index[0], index[1], index[2], t[0].x, t[0].y, t[1].x, t[1].y, t[2].x, t[2].y });
            det = 1;
            normalize = true;
        }
        v[1].subtract(v[0], edge1);
        v[2].subtract(v[0], edge2);
        tangent.set(edge1);
        tangent.normalizeLocal();
        binormal.set(edge2);
        binormal.normalizeLocal();
        if (Math.abs(Math.abs(tangent.dot(binormal)) - 1) < ZERO_TOLERANCE) {
            log.log(Level.WARNING, "Vertices are on the same line " + "for triangle [{0}, {1}, {2}].", new Object[] { index[0], index[1], index[2] });
        }
        float factor = 1 / det;
        tangent.x = (edge2uv.y * edge1.x - edge1uv.y * edge2.x) * factor;
        tangent.y = (edge2uv.y * edge1.y - edge1uv.y * edge2.y) * factor;
        tangent.z = (edge2uv.y * edge1.z - edge1uv.y * edge2.z) * factor;
        if (normalize) {
            tangent.normalizeLocal();
        }
        binormal.x = (edge1uv.x * edge2.x - edge2uv.x * edge1.x) * factor;
        binormal.y = (edge1uv.x * edge2.y - edge2uv.x * edge1.y) * factor;
        binormal.z = (edge1uv.x * edge2.z - edge2uv.x * edge1.z) * factor;
        if (normalize) {
            binormal.normalizeLocal();
        }
        tangent.cross(binormal, normal);
        normal.normalizeLocal();
        return new TriangleData(tangent.clone(), binormal.clone(), normal.clone());
    } finally {
        tmp.release();
    }
}
Also used : Vector2f(com.jme3.math.Vector2f) Vector3f(com.jme3.math.Vector3f)

Example 4 with TempVars

use of com.jme3.util.TempVars in project jmonkeyengine by jMonkeyEngine.

the class BufferUtils method addInBuffer.

/**
     * Add to a Vector3f in-buffer.
     * 
     * @param toAdd
     *            the vector to add from
     * @param buf
     *            the buffer to find the Vector3f within
     * @param index
     *            the position (in terms of vectors, not floats) of the vector
     *            to add to
     */
public static void addInBuffer(Vector3f toAdd, FloatBuffer buf, int index) {
    TempVars vars = TempVars.get();
    Vector3f tempVec3 = vars.vect1;
    populateFromBuffer(tempVec3, buf, index);
    tempVec3.addLocal(toAdd);
    setInBuffer(tempVec3, buf, index);
    vars.release();
}
Also used : Vector3f(com.jme3.math.Vector3f)

Example 5 with TempVars

use of com.jme3.util.TempVars in project jmonkeyengine by jMonkeyEngine.

the class BufferUtils method normalizeVector3.

/**
     * Normalize a Vector3f in-buffer.
     * 
     * @param buf
     *            the buffer to find the Vector3f within
     * @param index
     *            the position (in terms of vectors, not floats) of the vector
     *            to normalize
     */
public static void normalizeVector3(FloatBuffer buf, int index) {
    TempVars vars = TempVars.get();
    Vector3f tempVec3 = vars.vect1;
    populateFromBuffer(tempVec3, buf, index);
    tempVec3.normalizeLocal();
    setInBuffer(tempVec3, buf, index);
    vars.release();
}
Also used : Vector3f(com.jme3.math.Vector3f)

Aggregations

TempVars (com.jme3.util.TempVars)103 Vector3f (com.jme3.math.Vector3f)50 Quaternion (com.jme3.math.Quaternion)13 Matrix4f (com.jme3.math.Matrix4f)12 BoundingBox (com.jme3.bounding.BoundingBox)10 Bone (com.jme3.animation.Bone)8 Spatial (com.jme3.scene.Spatial)7 CollisionResult (com.jme3.collision.CollisionResult)6 Vector2f (com.jme3.math.Vector2f)6 FloatBuffer (java.nio.FloatBuffer)6 BoundingSphere (com.jme3.bounding.BoundingSphere)5 BoundingVolume (com.jme3.bounding.BoundingVolume)5 Transform (com.jme3.math.Transform)5 DirectionalLight (com.jme3.light.DirectionalLight)4 PointLight (com.jme3.light.PointLight)4 Geometry (com.jme3.scene.Geometry)4 SpotLight (com.jme3.light.SpotLight)3 ColorRGBA (com.jme3.math.ColorRGBA)3 Matrix3f (com.jme3.math.Matrix3f)3 Vector4f (com.jme3.math.Vector4f)3