use of com.jme3.bullet.collision.shapes.HullCollisionShape in project jmonkeyengine by jMonkeyEngine.
the class KinematicRagdollControl method boneRecursion.
protected void boneRecursion(Spatial model, Bone bone, PhysicsRigidBody parent, int reccount, Map<Integer, List<Float>> pointsMap) {
PhysicsRigidBody parentShape = parent;
if (boneList.isEmpty() || boneList.contains(bone.getName())) {
PhysicsBoneLink link = new PhysicsBoneLink();
link.bone = bone;
//creating the collision shape
HullCollisionShape shape = null;
if (pointsMap != null) {
//build a shape for the bone, using the vertices that are most influenced by this bone
shape = RagdollUtils.makeShapeFromPointMap(pointsMap, RagdollUtils.getBoneIndices(link.bone, skeleton, boneList), initScale, link.bone.getModelSpacePosition());
} else {
//build a shape for the bone, using the vertices associated with this bone with a weight above the threshold
shape = RagdollUtils.makeShapeFromVerticeWeights(model, RagdollUtils.getBoneIndices(link.bone, skeleton, boneList), initScale, link.bone.getModelSpacePosition(), weightThreshold);
}
PhysicsRigidBody shapeNode = new PhysicsRigidBody(shape, rootMass / (float) reccount);
shapeNode.setKinematic(mode == Mode.Kinematic);
totalMass += rootMass / (float) reccount;
link.rigidBody = shapeNode;
link.initalWorldRotation = bone.getModelSpaceRotation().clone();
if (parent != null) {
//get joint position for parent
Vector3f posToParent = new Vector3f();
if (bone.getParent() != null) {
bone.getModelSpacePosition().subtract(bone.getParent().getModelSpacePosition(), posToParent).multLocal(initScale);
}
SixDofJoint joint = new SixDofJoint(parent, shapeNode, posToParent, new Vector3f(0, 0, 0f), true);
preset.setupJointForBone(bone.getName(), joint);
link.joint = joint;
joint.setCollisionBetweenLinkedBodys(false);
}
boneLinks.put(bone.getName(), link);
shapeNode.setUserObject(link);
parentShape = shapeNode;
}
for (Iterator<Bone> it = bone.getChildren().iterator(); it.hasNext(); ) {
Bone childBone = it.next();
boneRecursion(model, childBone, parentShape, reccount + 1, pointsMap);
}
}
use of com.jme3.bullet.collision.shapes.HullCollisionShape in project jmonkeyengine by jMonkeyEngine.
the class CollisionShapeFactory method createSingleDynamicMeshShape.
/**
* This method creates a hull collision shape for the given mesh.<br>
*/
private static HullCollisionShape createSingleDynamicMeshShape(Geometry geom, Spatial parent) {
Mesh mesh = geom.getMesh();
Transform trans = getTransform(geom, parent);
if (mesh != null) {
HullCollisionShape dynamicShape = new HullCollisionShape(mesh);
dynamicShape.setScale(trans.getScale());
return dynamicShape;
} else {
return null;
}
}
use of com.jme3.bullet.collision.shapes.HullCollisionShape in project jmonkeyengine by jMonkeyEngine.
the class RagdollUtils method makeShapeFromVerticeWeights.
/**
* Create a hull collision shape from linked vertices to this bone.
*
* @param model
* @param boneIndices
* @param initialScale
* @param initialPosition
* @param weightThreshold
* @return
*/
public static HullCollisionShape makeShapeFromVerticeWeights(Spatial model, List<Integer> boneIndices, Vector3f initialScale, Vector3f initialPosition, float weightThreshold) {
ArrayList<Float> points = new ArrayList<Float>();
if (model instanceof Geometry) {
Geometry g = (Geometry) model;
for (Integer index : boneIndices) {
points.addAll(getPoints(g.getMesh(), index, initialScale, initialPosition, weightThreshold));
}
} else if (model instanceof Node) {
Node node = (Node) model;
for (Spatial s : node.getChildren()) {
if (s instanceof Geometry) {
Geometry g = (Geometry) s;
for (Integer index : boneIndices) {
points.addAll(getPoints(g.getMesh(), index, initialScale, initialPosition, weightThreshold));
}
}
}
}
float[] p = new float[points.size()];
for (int i = 0; i < points.size(); i++) {
p[i] = points.get(i);
}
return new HullCollisionShape(p);
}
use of com.jme3.bullet.collision.shapes.HullCollisionShape in project jmonkeyengine by jMonkeyEngine.
the class RagdollUtils method makeShapeFromPointMap.
/**
* Create a hull collision shape from linked vertices to this bone.
* Vertices have to be previoulsly gathered in a map using buildPointMap method
*
* @param pointsMap
* @param boneIndices
* @param initialScale
* @param initialPosition
* @return
*/
public static HullCollisionShape makeShapeFromPointMap(Map<Integer, List<Float>> pointsMap, List<Integer> boneIndices, Vector3f initialScale, Vector3f initialPosition) {
ArrayList<Float> points = new ArrayList<Float>();
for (Integer index : boneIndices) {
List<Float> l = pointsMap.get(index);
if (l != null) {
for (int i = 0; i < l.size(); i += 3) {
Vector3f pos = new Vector3f();
pos.x = l.get(i);
pos.y = l.get(i + 1);
pos.z = l.get(i + 2);
pos.subtractLocal(initialPosition).multLocal(initialScale);
points.add(pos.x);
points.add(pos.y);
points.add(pos.z);
}
}
}
float[] p = new float[points.size()];
for (int i = 0; i < points.size(); i++) {
p[i] = points.get(i);
}
return new HullCollisionShape(p);
}
Aggregations