Search in sources :

Example 61 with Quaternion

use of com.jme3.math.Quaternion 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 62 with Quaternion

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

the class KinematicRagdollControl method write.

/**
     * serialize this control
     *
     * @param ex
     * @throws IOException
     */
@Override
public void write(JmeExporter ex) throws IOException {
    super.write(ex);
    OutputCapsule oc = ex.getCapsule(this);
    oc.write(boneList.toArray(new String[boneList.size()]), "boneList", new String[0]);
    oc.write(boneLinks.values().toArray(new PhysicsBoneLink[boneLinks.size()]), "boneLinks", new PhysicsBoneLink[0]);
    oc.write(modelPosition, "modelPosition", new Vector3f());
    oc.write(modelRotation, "modelRotation", new Quaternion());
    oc.write(targetModel, "targetModel", null);
    oc.write(skeleton, "skeleton", null);
    //        oc.write(preset, "preset", null);//TODO
    oc.write(initScale, "initScale", null);
    oc.write(mode, "mode", null);
    oc.write(blendedControl, "blendedControl", false);
    oc.write(weightThreshold, "weightThreshold", -1.0f);
    oc.write(blendStart, "blendStart", 0.0f);
    oc.write(blendTime, "blendTime", 1.0f);
    oc.write(eventDispatchImpulseThreshold, "eventDispatchImpulseThreshold", 10);
    oc.write(rootMass, "rootMass", 15);
    oc.write(totalMass, "totalMass", 0);
    oc.write(ikRotSpeed, "rotSpeed", 7f);
    oc.write(limbDampening, "limbDampening", 0.6f);
}
Also used : OutputCapsule(com.jme3.export.OutputCapsule) Quaternion(com.jme3.math.Quaternion) Vector3f(com.jme3.math.Vector3f)

Example 63 with Quaternion

use of com.jme3.math.Quaternion 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 64 with Quaternion

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

the class KinematicRagdollControl method read.

/**
     * de-serialize this control
     *
     * @param im
     * @throws IOException
     */
@Override
public void read(JmeImporter im) throws IOException {
    super.read(im);
    InputCapsule ic = im.getCapsule(this);
    String[] loadedBoneList = ic.readStringArray("boneList", new String[0]);
    boneList.addAll(Arrays.asList(loadedBoneList));
    PhysicsBoneLink[] loadedBoneLinks = (PhysicsBoneLink[]) ic.readSavableArray("boneList", new PhysicsBoneLink[0]);
    for (PhysicsBoneLink physicsBoneLink : loadedBoneLinks) {
        boneLinks.put(physicsBoneLink.bone.getName(), physicsBoneLink);
    }
    modelPosition.set((Vector3f) ic.readSavable("modelPosition", new Vector3f()));
    modelRotation.set((Quaternion) ic.readSavable("modelRotation", new Quaternion()));
    targetModel = (Spatial) ic.readSavable("targetModel", null);
    skeleton = (Skeleton) ic.readSavable("skeleton", null);
    //        preset //TODO
    initScale = (Vector3f) ic.readSavable("initScale", null);
    mode = ic.readEnum("mode", Mode.class, Mode.Kinematic);
    blendedControl = ic.readBoolean("blendedControl", false);
    weightThreshold = ic.readFloat("weightThreshold", -1.0f);
    blendStart = ic.readFloat("blendStart", 0.0f);
    blendTime = ic.readFloat("blendTime", 1.0f);
    eventDispatchImpulseThreshold = ic.readFloat("eventDispatchImpulseThreshold", 10);
    rootMass = ic.readFloat("rootMass", 15);
    totalMass = ic.readFloat("totalMass", 0);
}
Also used : Quaternion(com.jme3.math.Quaternion) InputCapsule(com.jme3.export.InputCapsule) Vector3f(com.jme3.math.Vector3f)

Example 65 with Quaternion

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

the class AbstractPhysicsDebugControl method applyPhysicsTransform.

protected void applyPhysicsTransform(Vector3f worldLocation, Quaternion worldRotation, Spatial spatial) {
    if (spatial != null) {
        Vector3f localLocation = spatial.getLocalTranslation();
        Quaternion localRotationQuat = spatial.getLocalRotation();
        if (spatial.getParent() != null) {
            localLocation.set(worldLocation).subtractLocal(spatial.getParent().getWorldTranslation());
            localLocation.divideLocal(spatial.getParent().getWorldScale());
            tmp_inverseWorldRotation.set(spatial.getParent().getWorldRotation()).inverseLocal().multLocal(localLocation);
            localRotationQuat.set(worldRotation);
            tmp_inverseWorldRotation.set(spatial.getParent().getWorldRotation()).inverseLocal().mult(localRotationQuat, localRotationQuat);
            spatial.setLocalTranslation(localLocation);
            spatial.setLocalRotation(localRotationQuat);
        } else {
            spatial.setLocalTranslation(worldLocation);
            spatial.setLocalRotation(worldRotation);
        }
    }
}
Also used : Quaternion(com.jme3.math.Quaternion) Vector3f(com.jme3.math.Vector3f)

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