Search in sources :

Example 36 with Bone

use of com.jme3.animation.Bone in project jmonkeyengine by jMonkeyEngine.

the class SkeletonPoints method updateGeometry.

/**
     * The method updates the geometry according to the positions of the bones.
     */
public void updateGeometry() {
    VertexBuffer vb = this.getBuffer(Type.Position);
    FloatBuffer posBuf = this.getFloatBuffer(Type.Position);
    posBuf.clear();
    for (int i = 0; i < skeleton.getBoneCount(); ++i) {
        Bone bone = skeleton.getBone(i);
        Vector3f head = bone.getModelSpacePosition();
        posBuf.put(head.getX()).put(head.getY()).put(head.getZ());
        if (boneLengths != null) {
            Vector3f tail = head.add(bone.getModelSpaceRotation().mult(Vector3f.UNIT_Y.mult(boneLengths.get(i))));
            posBuf.put(tail.getX()).put(tail.getY()).put(tail.getZ());
        }
    }
    posBuf.flip();
    vb.updateData(posBuf);
    this.updateBound();
}
Also used : VertexBuffer(com.jme3.scene.VertexBuffer) Vector3f(com.jme3.math.Vector3f) FloatBuffer(java.nio.FloatBuffer) Bone(com.jme3.animation.Bone)

Example 37 with Bone

use of com.jme3.animation.Bone in project jmonkeyengine by jMonkeyEngine.

the class SkeletonWire method updateGeometry.

/**
     * The method updates the geometry according to the poitions of the bones.
     */
public void updateGeometry() {
    VertexBuffer vb = this.getBuffer(Type.Position);
    FloatBuffer posBuf = this.getFloatBuffer(Type.Position);
    posBuf.clear();
    for (int i = 0; i < skeleton.getBoneCount(); ++i) {
        Bone bone = skeleton.getBone(i);
        Vector3f head = bone.getModelSpacePosition();
        posBuf.put(head.getX()).put(head.getY()).put(head.getZ());
        if (boneLengths != null) {
            Vector3f tail = head.add(bone.getModelSpaceRotation().mult(Vector3f.UNIT_Y.mult(boneLengths.get(i))));
            posBuf.put(tail.getX()).put(tail.getY()).put(tail.getZ());
        }
    }
    posBuf.flip();
    vb.updateData(posBuf);
    this.updateBound();
}
Also used : VertexBuffer(com.jme3.scene.VertexBuffer) Vector3f(com.jme3.math.Vector3f) FloatBuffer(java.nio.FloatBuffer) Bone(com.jme3.animation.Bone)

Example 38 with Bone

use of com.jme3.animation.Bone in project jmonkeyengine by jMonkeyEngine.

the class SkeletonWire method writeConnections.

/**
     * The method writes the indexes for the connection vertices. Used in non-length mode.
     * @param indexBuf
     *            the index buffer
     * @param bone
     *            the bone
     */
private void writeConnections(ShortBuffer indexBuf, Bone bone) {
    for (Bone child : bone.getChildren()) {
        // write myself
        indexBuf.put((short) skeleton.getBoneIndex(bone));
        // write the child
        indexBuf.put((short) skeleton.getBoneIndex(child));
        this.writeConnections(indexBuf, child);
    }
}
Also used : Bone(com.jme3.animation.Bone)

Example 39 with Bone

use of com.jme3.animation.Bone in project jmonkeyengine by jMonkeyEngine.

the class ConstraintHelper method getTransform.

/**
     * The method retreives the transform from a feature in a given space.
     * 
     * @param oma
     *            the OMA of the feature (spatial or armature node)
     * @param subtargetName
     *            the feature's subtarget (bone in a case of armature's node)
     * @param space
     *            the space the transform is evaluated to
     * @return thensform of a feature in a given space
     */
public Transform getTransform(Long oma, String subtargetName, Space space) {
    Spatial feature = (Spatial) blenderContext.getLoadedFeature(oma, LoadedDataType.FEATURE);
    boolean isArmature = blenderContext.getMarkerValue(ObjectHelper.ARMATURE_NODE_MARKER, feature) != null;
    if (isArmature) {
        blenderContext.getSkeleton(oma).updateWorldVectors();
        BoneContext targetBoneContext = blenderContext.getBoneByName(oma, subtargetName);
        Bone bone = targetBoneContext.getBone();
        if (bone.getParent() == null && (space == Space.CONSTRAINT_SPACE_LOCAL || space == Space.CONSTRAINT_SPACE_PARLOCAL)) {
            space = Space.CONSTRAINT_SPACE_POSE;
        }
        // use readable names of the matrices so that the code is more clear
        TempVars tempVars = TempVars.get();
        Transform result;
        switch(space) {
            case CONSTRAINT_SPACE_WORLD:
                Spatial model = (Spatial) blenderContext.getLoadedFeature(targetBoneContext.getSkeletonOwnerOma(), LoadedDataType.FEATURE);
                Matrix4f boneModelMatrix = this.toMatrix(bone.getModelSpacePosition(), bone.getModelSpaceRotation(), bone.getModelSpaceScale(), tempVars.tempMat4);
                Matrix4f modelWorldMatrix = this.toMatrix(model.getWorldTransform(), tempVars.tempMat42);
                Matrix4f boneMatrixInWorldSpace = modelWorldMatrix.multLocal(boneModelMatrix);
                result = new Transform(boneMatrixInWorldSpace.toTranslationVector(), boneMatrixInWorldSpace.toRotationQuat(), boneMatrixInWorldSpace.toScaleVector());
                break;
            case CONSTRAINT_SPACE_LOCAL:
                assert bone.getParent() != null : "CONSTRAINT_SPACE_LOCAL should be evaluated as CONSTRAINT_SPACE_POSE if the bone has no parent!";
                result = new Transform(bone.getLocalPosition(), bone.getLocalRotation(), bone.getLocalScale());
                break;
            case CONSTRAINT_SPACE_POSE:
                {
                    Matrix4f boneWorldMatrix = this.toMatrix(this.getTransform(oma, subtargetName, Space.CONSTRAINT_SPACE_WORLD), tempVars.tempMat4);
                    Matrix4f armatureInvertedWorldMatrix = this.toMatrix(feature.getWorldTransform(), tempVars.tempMat42).invertLocal();
                    Matrix4f bonePoseMatrix = armatureInvertedWorldMatrix.multLocal(boneWorldMatrix);
                    result = new Transform(bonePoseMatrix.toTranslationVector(), bonePoseMatrix.toRotationQuat(), bonePoseMatrix.toScaleVector());
                    break;
                }
            case CONSTRAINT_SPACE_PARLOCAL:
                {
                    Matrix4f boneWorldMatrix = this.toMatrix(this.getTransform(oma, subtargetName, Space.CONSTRAINT_SPACE_WORLD), tempVars.tempMat4);
                    Matrix4f armatureInvertedWorldMatrix = this.toMatrix(feature.getWorldTransform(), tempVars.tempMat42).invertLocal();
                    Matrix4f bonePoseMatrix = armatureInvertedWorldMatrix.multLocal(boneWorldMatrix);
                    result = new Transform(bonePoseMatrix.toTranslationVector(), bonePoseMatrix.toRotationQuat(), bonePoseMatrix.toScaleVector());
                    Bone parent = bone.getParent();
                    if (parent != null) {
                        BoneContext parentContext = blenderContext.getBoneContext(parent);
                        Vector3f head = parent.getModelSpacePosition();
                        Vector3f tail = head.add(bone.getModelSpaceRotation().mult(Vector3f.UNIT_Y.mult(parentContext.getLength())));
                        result.getTranslation().subtractLocal(tail);
                    }
                    break;
                }
            default:
                throw new IllegalStateException("Unknown space type: " + space);
        }
        tempVars.release();
        return result;
    } else {
        switch(space) {
            case CONSTRAINT_SPACE_LOCAL:
                return feature.getLocalTransform();
            case CONSTRAINT_SPACE_WORLD:
                return feature.getWorldTransform();
            case CONSTRAINT_SPACE_PARLOCAL:
            case CONSTRAINT_SPACE_POSE:
                throw new IllegalStateException("Nodes can have only Local and World spaces applied!");
            default:
                throw new IllegalStateException("Unknown space type: " + space);
        }
    }
}
Also used : Matrix4f(com.jme3.math.Matrix4f) Spatial(com.jme3.scene.Spatial) BoneContext(com.jme3.scene.plugins.blender.animations.BoneContext) Vector3f(com.jme3.math.Vector3f) Bone(com.jme3.animation.Bone) TempVars(com.jme3.util.TempVars) Transform(com.jme3.math.Transform)

Example 40 with Bone

use of com.jme3.animation.Bone in project jmonkeyengine by jMonkeyEngine.

the class SimulationNode method applyConstraints.

/**
     * Applies constraints to the given bone and its children.
     * The goal is to apply constraint from root bone to the last child.
     * @param bone
     *            the bone whose constraints will be applied
     * @param alteredOmas
     *            the set of OMAS of the altered bones (is populated if necessary)
     * @param frame
     *            the current frame of the animation
     * @param bonesStack
     *            the stack of bones used to avoid infinite loops while applying constraints
     */
private void applyConstraints(Bone bone, Set<Long> alteredOmas, Set<Long> applied, int frame, Stack<Bone> bonesStack) {
    if (!bonesStack.contains(bone)) {
        bonesStack.push(bone);
        BoneContext boneContext = blenderContext.getBoneContext(bone);
        if (!applied.contains(boneContext.getBoneOma())) {
            List<Constraint> constraints = this.findConstraints(boneContext.getBoneOma(), blenderContext);
            if (constraints != null && constraints.size() > 0) {
                for (Constraint constraint : constraints) {
                    if (constraint.getTargetOMA() != null && constraint.getTargetOMA() > 0L) {
                        // first apply constraints of the target bone
                        BoneContext targetBone = blenderContext.getBoneContext(constraint.getTargetOMA());
                        this.applyConstraints(targetBone.getBone(), alteredOmas, applied, frame, bonesStack);
                    }
                    constraint.apply(frame);
                    if (constraint.getAlteredOmas() != null) {
                        alteredOmas.addAll(constraint.getAlteredOmas());
                    }
                    alteredOmas.add(boneContext.getBoneOma());
                }
            }
            applied.add(boneContext.getBoneOma());
        }
        List<Bone> children = bone.getChildren();
        if (children != null && children.size() > 0) {
            for (Bone child : bone.getChildren()) {
                this.applyConstraints(child, alteredOmas, applied, frame, bonesStack);
            }
        }
        bonesStack.pop();
    }
}
Also used : BoneContext(com.jme3.scene.plugins.blender.animations.BoneContext) Bone(com.jme3.animation.Bone)

Aggregations

Bone (com.jme3.animation.Bone)35 Vector3f (com.jme3.math.Vector3f)25 Quaternion (com.jme3.math.Quaternion)17 TempVars (com.jme3.util.TempVars)13 SixDofJoint (com.jme3.bullet.joints.SixDofJoint)10 FloatBuffer (java.nio.FloatBuffer)10 VertexBuffer (com.jme3.scene.VertexBuffer)8 HashMap (java.util.HashMap)8 Skeleton (com.jme3.animation.Skeleton)7 Matrix4f (com.jme3.math.Matrix4f)7 Transform (com.jme3.math.Transform)7 BoneContext (com.jme3.scene.plugins.blender.animations.BoneContext)7 ByteBuffer (java.nio.ByteBuffer)7 BoneTrack (com.jme3.animation.BoneTrack)6 Structure (com.jme3.scene.plugins.blender.file.Structure)6 ArrayList (java.util.ArrayList)6 AnimControl (com.jme3.animation.AnimControl)5 Spatial (com.jme3.scene.Spatial)5 Map (java.util.Map)5 Animation (com.jme3.animation.Animation)4