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();
}
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;
}
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;
}
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;
}
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;
}
Aggregations