Search in sources :

Example 51 with Transform

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

the class KinematicRagdollControl method ragDollUpdate.

protected void ragDollUpdate(float tpf) {
    TempVars vars = TempVars.get();
    Quaternion tmpRot1 = vars.quat1;
    Quaternion tmpRot2 = vars.quat2;
    for (PhysicsBoneLink link : boneLinks.values()) {
        Vector3f position = vars.vect1;
        //retrieving bone position in physic world space
        Vector3f p = link.rigidBody.getMotionState().getWorldLocation();
        //transforming this position with inverse transforms of the model
        targetModel.getWorldTransform().transformInverseVector(p, position);
        //retrieving bone rotation in physic world space
        Quaternion q = link.rigidBody.getMotionState().getWorldRotationQuat();
        //multiplying this rotation by the initialWorld rotation of the bone, 
        //then transforming it with the inverse world rotation of the model
        tmpRot1.set(q).multLocal(link.initalWorldRotation);
        tmpRot2.set(targetModel.getWorldRotation()).inverseLocal().mult(tmpRot1, tmpRot1);
        tmpRot1.normalizeLocal();
        //if the bone is the root bone, we apply the physic's transform to the model, so its position and rotation are correctly updated
        if (link.bone.getParent() == null) {
            //offsetting the physic's position/rotation by the root bone inverse model space position/rotaion
            modelPosition.set(p).subtractLocal(link.bone.getBindPosition());
            targetModel.getParent().getWorldTransform().transformInverseVector(modelPosition, modelPosition);
            modelRotation.set(q).multLocal(tmpRot2.set(link.bone.getBindRotation()).inverseLocal());
            //applying transforms to the model
            targetModel.setLocalTranslation(modelPosition);
            targetModel.setLocalRotation(modelRotation);
            //Applying computed transforms to the bone
            link.bone.setUserTransformsInModelSpace(position, tmpRot1);
        } else {
            //so we just update the bone position
            if (boneList.isEmpty()) {
                link.bone.setUserTransformsInModelSpace(position, tmpRot1);
            } else {
                //boneList is not empty, this means some bones of the skeleton might not be associated with a collision shape.
                //So we update them recusively
                RagdollUtils.setTransform(link.bone, position, tmpRot1, false, boneList);
            }
        }
    }
    vars.release();
}
Also used : Quaternion(com.jme3.math.Quaternion) Vector3f(com.jme3.math.Vector3f) TempVars(com.jme3.util.TempVars)

Example 52 with Transform

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

the class CollisionShapeFactory method getTransform.

/**
     * returns the correct transform for a collisionshape in relation
     * to the ancestor for which the collisionshape is generated
     * @param spat
     * @param parent
     * @return
     */
private static Transform getTransform(Spatial spat, Spatial parent) {
    Transform shapeTransform = new Transform();
    Spatial parentNode = spat.getParent() != null ? spat.getParent() : spat;
    Spatial currentSpatial = spat;
    //if we have parents combine their transforms
    while (parentNode != null) {
        if (parent == currentSpatial) {
            //real parent -> only apply scale, not transform
            Transform trans = new Transform();
            trans.setScale(currentSpatial.getLocalScale());
            shapeTransform.combineWithParent(trans);
            parentNode = null;
        } else {
            shapeTransform.combineWithParent(currentSpatial.getLocalTransform());
            parentNode = currentSpatial.getParent();
            currentSpatial = parentNode;
        }
    }
    return shapeTransform;
}
Also used : Transform(com.jme3.math.Transform)

Example 53 with Transform

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

the class CollisionShapeFactory method createCompoundShape.

private static CompoundCollisionShape createCompoundShape(Node realRootNode, Node rootNode, CompoundCollisionShape shape, boolean meshAccurate, boolean dynamic) {
    for (Spatial spatial : rootNode.getChildren()) {
        if (spatial instanceof TerrainQuad) {
            Boolean bool = spatial.getUserData(UserData.JME_PHYSICSIGNORE);
            if (bool != null && bool.booleanValue()) {
                // go to the next child in the loop
                continue;
            }
            TerrainQuad terrain = (TerrainQuad) spatial;
            Transform trans = getTransform(spatial, realRootNode);
            shape.addChildShape(new HeightfieldCollisionShape(terrain.getHeightMap(), trans.getScale()), trans.getTranslation(), trans.getRotation().toRotationMatrix());
        } else if (spatial instanceof Node) {
            createCompoundShape(realRootNode, (Node) spatial, shape, meshAccurate, dynamic);
        } else if (spatial instanceof TerrainPatch) {
            Boolean bool = spatial.getUserData(UserData.JME_PHYSICSIGNORE);
            if (bool != null && bool.booleanValue()) {
                // go to the next child in the loop
                continue;
            }
            TerrainPatch terrain = (TerrainPatch) spatial;
            Transform trans = getTransform(spatial, realRootNode);
            shape.addChildShape(new HeightfieldCollisionShape(terrain.getHeightMap(), terrain.getLocalScale()), trans.getTranslation(), trans.getRotation().toRotationMatrix());
        } else if (spatial instanceof Geometry) {
            Boolean bool = spatial.getUserData(UserData.JME_PHYSICSIGNORE);
            if (bool != null && bool.booleanValue()) {
                // go to the next child in the loop
                continue;
            }
            if (meshAccurate) {
                CollisionShape childShape = dynamic ? createSingleDynamicMeshShape((Geometry) spatial, realRootNode) : createSingleMeshShape((Geometry) spatial, realRootNode);
                if (childShape != null) {
                    Transform trans = getTransform(spatial, realRootNode);
                    shape.addChildShape(childShape, trans.getTranslation(), trans.getRotation().toRotationMatrix());
                }
            } else {
                Transform trans = getTransform(spatial, realRootNode);
                shape.addChildShape(createSingleBoxShape(spatial, realRootNode), trans.getTranslation(), trans.getRotation().toRotationMatrix());
            }
        }
    }
    return shape;
}
Also used : ChildCollisionShape(com.jme3.bullet.collision.shapes.infos.ChildCollisionShape) Transform(com.jme3.math.Transform) TerrainQuad(com.jme3.terrain.geomipmap.TerrainQuad) TerrainPatch(com.jme3.terrain.geomipmap.TerrainPatch)

Example 54 with Transform

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

the class BoundingBox method transform.

public BoundingVolume transform(Matrix4f trans, BoundingVolume store) {
    BoundingBox box;
    if (store == null || store.getType() != Type.AABB) {
        box = new BoundingBox();
    } else {
        box = (BoundingBox) store;
    }
    TempVars vars = TempVars.get();
    float w = trans.multProj(center, box.center);
    box.center.divideLocal(w);
    Matrix3f transMatrix = vars.tempMat3;
    trans.toRotationMatrix(transMatrix);
    // Make the rotation matrix all positive to get the maximum x/y/z extent
    transMatrix.absoluteLocal();
    vars.vect1.set(xExtent, yExtent, zExtent);
    transMatrix.mult(vars.vect1, vars.vect1);
    // Assign the biggest rotations after scales.
    box.xExtent = FastMath.abs(vars.vect1.getX());
    box.yExtent = FastMath.abs(vars.vect1.getY());
    box.zExtent = FastMath.abs(vars.vect1.getZ());
    vars.release();
    return box;
}
Also used : TempVars(com.jme3.util.TempVars)

Example 55 with Transform

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

the class Skeleton method computeSkinningMatrices.

/**
     * Compute the skining matrices for each bone of the skeleton that would be used to transform vertices of associated meshes
     * @return 
     */
public Matrix4f[] computeSkinningMatrices() {
    TempVars vars = TempVars.get();
    for (int i = 0; i < boneList.length; i++) {
        boneList[i].getOffsetTransform(skinningMatrixes[i], vars.quat1, vars.vect1, vars.vect2, vars.tempMat3);
    }
    vars.release();
    return skinningMatrixes;
}
Also used : TempVars(com.jme3.util.TempVars)

Aggregations

Vector3f (com.jme3.math.Vector3f)29 Transform (com.jme3.math.Transform)26 TempVars (com.jme3.util.TempVars)24 Quaternion (com.jme3.math.Quaternion)11 Matrix4f (com.jme3.math.Matrix4f)10 Bone (com.jme3.animation.Bone)9 BoundingBox (com.jme3.bounding.BoundingBox)6 BoneContext (com.jme3.scene.plugins.blender.animations.BoneContext)5 PointLight (com.jme3.light.PointLight)4 Spatial (com.jme3.scene.Spatial)4 FloatBuffer (java.nio.FloatBuffer)4 Transform (com.bulletphysics.linearmath.Transform)3 ChildCollisionShape (com.jme3.bullet.collision.shapes.infos.ChildCollisionShape)3 DirectionalLight (com.jme3.light.DirectionalLight)3 Light (com.jme3.light.Light)3 SpotLight (com.jme3.light.SpotLight)3 CompoundShape (com.bulletphysics.collision.shapes.CompoundShape)2 BoneTrack (com.jme3.animation.BoneTrack)2 SpatialTrack (com.jme3.animation.SpatialTrack)2 BoundingVolume (com.jme3.bounding.BoundingVolume)2