Search in sources :

Example 1 with Control

use of com.jme3.scene.control.Control in project jmonkeyengine by jMonkeyEngine.

the class AnimationEvent method read.

@Override
public void read(JmeImporter im) throws IOException {
    super.read(im);
    InputCapsule ic = im.getCapsule(this);
    if (im.getFormatVersion() == 0) {
        modelName = ic.readString("modelName", "");
    }
    //FIXME always the same issue, because of the clonning of assets, this won't work
    //we have to somehow store userdata in the spatial and then recurse the 
    //scene sub scenegraph to find the correct instance of the model
    //This brings a reflaxion about the cinematic being an appstate, 
    //shouldn't it be a control over the scene
    // this would allow to use the cloneForSpatial method and automatically 
    //rebind cloned references of original objects.
    //for now as nobody probably ever saved a cinematic, this is not a critical issue
    model = (Spatial) ic.readSavable("model", null);
    animationName = ic.readString("animationName", "");
    blendTime = ic.readFloat("blendTime", 0f);
    channelIndex = ic.readInt("channelIndex", 0);
}
Also used : InputCapsule(com.jme3.export.InputCapsule)

Example 2 with Control

use of com.jme3.scene.control.Control in project jmonkeyengine by jMonkeyEngine.

the class BoneTrack method setTime.

/**
     * 
     * Modify the bone which this track modifies in the skeleton to contain
     * the correct animation transforms for a given time.
     * The transforms can be interpolated in some method from the keyframes.
     *
     * @param time the current time of the animation
     * @param weight the weight of the animation
     * @param control
     * @param channel
     * @param vars
     */
public void setTime(float time, float weight, AnimControl control, AnimChannel channel, TempVars vars) {
    BitSet affectedBones = channel.getAffectedBones();
    if (affectedBones != null && !affectedBones.get(targetBoneIndex)) {
        return;
    }
    Bone target = control.getSkeleton().getBone(targetBoneIndex);
    Vector3f tempV = vars.vect1;
    Vector3f tempS = vars.vect2;
    Quaternion tempQ = vars.quat1;
    Vector3f tempV2 = vars.vect3;
    Vector3f tempS2 = vars.vect4;
    Quaternion tempQ2 = vars.quat2;
    int lastFrame = times.length - 1;
    if (time < 0 || lastFrame == 0) {
        rotations.get(0, tempQ);
        translations.get(0, tempV);
        if (scales != null) {
            scales.get(0, tempS);
        }
    } else if (time >= times[lastFrame]) {
        rotations.get(lastFrame, tempQ);
        translations.get(lastFrame, tempV);
        if (scales != null) {
            scales.get(lastFrame, tempS);
        }
    } else {
        int startFrame = 0;
        int endFrame = 1;
        // use lastFrame so we never overflow the array
        int i;
        for (i = 0; i < lastFrame && times[i] < time; i++) {
            startFrame = i;
            endFrame = i + 1;
        }
        float blend = (time - times[startFrame]) / (times[endFrame] - times[startFrame]);
        rotations.get(startFrame, tempQ);
        translations.get(startFrame, tempV);
        if (scales != null) {
            scales.get(startFrame, tempS);
        }
        rotations.get(endFrame, tempQ2);
        translations.get(endFrame, tempV2);
        if (scales != null) {
            scales.get(endFrame, tempS2);
        }
        tempQ.nlerp(tempQ2, blend);
        tempV.interpolateLocal(tempV2, blend);
        tempS.interpolateLocal(tempS2, blend);
    }
    //        if (weight != 1f) {
    target.blendAnimTransforms(tempV, tempQ, scales != null ? tempS : null, weight);
//        } else {
//            target.setAnimTransforms(tempV, tempQ, scales != null ? tempS : null);
//        }
}
Also used : Quaternion(com.jme3.math.Quaternion) Vector3f(com.jme3.math.Vector3f) BitSet(java.util.BitSet)

Example 3 with Control

use of com.jme3.scene.control.Control in project jmonkeyengine by jMonkeyEngine.

the class MotionPath method interpolatePath.

/**
     * interpolate the path giving the time since the beginning and the motionControl     
     * this methods sets the new localTranslation to the spatial of the MotionEvent control.
     * @param time the time since the animation started
     * @param control the control over the moving spatial
     */
public float interpolatePath(float time, MotionEvent control, float tpf) {
    float traveledDistance = 0;
    TempVars vars = TempVars.get();
    Vector3f temp = vars.vect1;
    Vector3f tmpVector = vars.vect2;
    Vector2f v = vars.vect2d;
    //computing traveled distance according to new time
    traveledDistance = time * (getLength() / control.getInitialDuration());
    //getting waypoint index and current value from new traveled distance
    v = getWayPointIndexForDistance(traveledDistance, v);
    //setting values
    control.setCurrentWayPoint((int) v.x);
    control.setCurrentValue(v.y);
    //interpolating new position
    getSpline().interpolate(control.getCurrentValue(), control.getCurrentWayPoint(), temp);
    if (control.needsDirection()) {
        tmpVector.set(temp);
        control.setDirection(tmpVector.subtractLocal(control.getSpatial().getLocalTranslation()).normalizeLocal());
    }
    checkWayPoint(control, tpf);
    control.getSpatial().setLocalTranslation(temp);
    vars.release();
    return traveledDistance;
}
Also used : Vector2f(com.jme3.math.Vector2f) Vector3f(com.jme3.math.Vector3f) TempVars(com.jme3.util.TempVars)

Example 4 with Control

use of com.jme3.scene.control.Control in project jmonkeyengine by jMonkeyEngine.

the class FbxLoader method constructAnimations.

private void constructAnimations() {
    // In FBX, animation are not attached to any root.
    // They are implicitly global.
    // So, we need to use hueristics to find which node(s) 
    // an animation is associated with, so we can create the AnimControl
    // in the appropriate location in the scene.
    Map<FbxToJmeTrack, FbxToJmeTrack> pairs = new HashMap<FbxToJmeTrack, FbxToJmeTrack>();
    for (FbxAnimStack stack : animStacks) {
        for (FbxAnimLayer layer : stack.getLayers()) {
            for (FbxAnimCurveNode curveNode : layer.getAnimationCurveNodes()) {
                for (Map.Entry<FbxNode, String> nodePropertyEntry : curveNode.getInfluencedNodeProperties().entrySet()) {
                    FbxToJmeTrack lookupPair = new FbxToJmeTrack();
                    lookupPair.animStack = stack;
                    lookupPair.animLayer = layer;
                    lookupPair.node = nodePropertyEntry.getKey();
                    // Find if this pair is already stored
                    FbxToJmeTrack storedPair = pairs.get(lookupPair);
                    if (storedPair == null) {
                        // If not, store it.
                        storedPair = lookupPair;
                        pairs.put(storedPair, storedPair);
                    }
                    String property = nodePropertyEntry.getValue();
                    storedPair.animCurves.put(property, curveNode);
                }
            }
        }
    }
    // At this point we can construct the animation for all pairs ...
    for (FbxToJmeTrack pair : pairs.values()) {
        String animName = pair.animStack.getName();
        float duration = pair.animStack.getDuration();
        System.out.println("ANIMATION: " + animName + ", duration = " + duration);
        System.out.println("NODE: " + pair.node.getName());
        duration = pair.getDuration();
        if (pair.node instanceof FbxLimbNode) {
            // Find the spatial that has the skeleton for this limb.
            FbxLimbNode limbNode = (FbxLimbNode) pair.node;
            Bone bone = limbNode.getJmeBone();
            Spatial jmeSpatial = limbNode.getSkeletonHolder().getJmeObject();
            Skeleton skeleton = limbNode.getSkeletonHolder().getJmeSkeleton();
            // Get the animation control (create if missing).
            AnimControl animControl = jmeSpatial.getControl(AnimControl.class);
            if (animControl.getSkeleton() != skeleton) {
                throw new UnsupportedOperationException();
            }
            // Get the animation (create if missing).
            Animation anim = animControl.getAnim(animName);
            if (anim == null) {
                anim = new Animation(animName, duration);
                animControl.addAnim(anim);
            }
            // Find the bone index from the spatial's skeleton.
            int boneIndex = skeleton.getBoneIndex(bone);
            // Generate the bone track.
            BoneTrack bt = pair.toJmeBoneTrack(boneIndex, bone.getBindInverseTransform());
            // Add the bone track to the animation.
            anim.addTrack(bt);
        } else {
            // Create the spatial animation
            Animation anim = new Animation(animName, duration);
            anim.setTracks(new Track[] { pair.toJmeSpatialTrack() });
            // Get the animation control (create if missing).
            Spatial jmeSpatial = pair.node.getJmeObject();
            AnimControl animControl = jmeSpatial.getControl(AnimControl.class);
            if (animControl == null) {
                animControl = new AnimControl(null);
                jmeSpatial.addControl(animControl);
            }
            // Add the spatial animation
            animControl.addAnim(anim);
        }
    }
}
Also used : BoneTrack(com.jme3.animation.BoneTrack) HashMap(java.util.HashMap) FbxAnimCurveNode(com.jme3.scene.plugins.fbx.anim.FbxAnimCurveNode) AnimControl(com.jme3.animation.AnimControl) FbxNode(com.jme3.scene.plugins.fbx.node.FbxNode) Spatial(com.jme3.scene.Spatial) Animation(com.jme3.animation.Animation) FbxToJmeTrack(com.jme3.scene.plugins.fbx.anim.FbxToJmeTrack) Skeleton(com.jme3.animation.Skeleton) Bone(com.jme3.animation.Bone) FbxLimbNode(com.jme3.scene.plugins.fbx.anim.FbxLimbNode) FbxAnimLayer(com.jme3.scene.plugins.fbx.anim.FbxAnimLayer) HashMap(java.util.HashMap) Map(java.util.Map) FbxAnimStack(com.jme3.scene.plugins.fbx.anim.FbxAnimStack)

Example 5 with Control

use of com.jme3.scene.control.Control in project jmonkeyengine by jMonkeyEngine.

the class KinematicRagdollControl method ikUpdate.

private void ikUpdate(float tpf) {
    TempVars vars = TempVars.get();
    Quaternion tmpRot1 = vars.quat1;
    Quaternion[] tmpRot2 = new Quaternion[] { vars.quat2, new Quaternion() };
    Iterator<String> it = ikTargets.keySet().iterator();
    float distance;
    Bone bone;
    String boneName;
    while (it.hasNext()) {
        boneName = it.next();
        bone = (Bone) boneLinks.get(boneName).bone;
        if (!bone.hasUserControl()) {
            Logger.getLogger(KinematicRagdollControl.class.getSimpleName()).log(Level.FINE, "{0} doesn't have user control", boneName);
            continue;
        }
        distance = bone.getModelSpacePosition().distance(ikTargets.get(boneName));
        if (distance < IKThreshold) {
            Logger.getLogger(KinematicRagdollControl.class.getSimpleName()).log(Level.FINE, "Distance is close enough");
            continue;
        }
        int depth = 0;
        int maxDepth = ikChainDepth.get(bone.getName());
        updateBone(boneLinks.get(bone.getName()), tpf * (float) FastMath.sqrt(distance), vars, tmpRot1, tmpRot2, bone, ikTargets.get(boneName), depth, maxDepth);
        Vector3f position = vars.vect1;
        for (PhysicsBoneLink link : boneLinks.values()) {
            matchPhysicObjectToBone(link, position, tmpRot1);
        }
    }
    vars.release();
}
Also used : Quaternion(com.jme3.math.Quaternion) Vector3f(com.jme3.math.Vector3f) TempVars(com.jme3.util.TempVars) Bone(com.jme3.animation.Bone) SixDofJoint(com.jme3.bullet.joints.SixDofJoint)

Aggregations

Vector3f (com.jme3.math.Vector3f)51 DirectionalLight (com.jme3.light.DirectionalLight)24 Material (com.jme3.material.Material)23 Quaternion (com.jme3.math.Quaternion)21 Node (com.jme3.scene.Node)19 TerrainLodControl (com.jme3.terrain.geomipmap.TerrainLodControl)18 Texture (com.jme3.texture.Texture)16 AnimControl (com.jme3.animation.AnimControl)15 ColorRGBA (com.jme3.math.ColorRGBA)15 TerrainQuad (com.jme3.terrain.geomipmap.TerrainQuad)15 RigidBodyControl (com.jme3.bullet.control.RigidBodyControl)14 Geometry (com.jme3.scene.Geometry)14 Spatial (com.jme3.scene.Spatial)13 DistanceLodCalculator (com.jme3.terrain.geomipmap.lodcalc.DistanceLodCalculator)13 BulletAppState (com.jme3.bullet.BulletAppState)12 AbstractHeightMap (com.jme3.terrain.heightmap.AbstractHeightMap)11 ImageBasedHeightMap (com.jme3.terrain.heightmap.ImageBasedHeightMap)11 ArrayList (java.util.ArrayList)10 Box (com.jme3.scene.shape.Box)7 Animation (com.jme3.animation.Animation)5