Search in sources :

Example 1 with HullCollisionShape

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);
    }
}
Also used : SixDofJoint(com.jme3.bullet.joints.SixDofJoint) Vector3f(com.jme3.math.Vector3f) Bone(com.jme3.animation.Bone) PhysicsRigidBody(com.jme3.bullet.objects.PhysicsRigidBody) HullCollisionShape(com.jme3.bullet.collision.shapes.HullCollisionShape)

Example 2 with HullCollisionShape

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;
    }
}
Also used : Transform(com.jme3.math.Transform)

Example 3 with HullCollisionShape

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);
}
Also used : Geometry(com.jme3.scene.Geometry) Spatial(com.jme3.scene.Spatial) Node(com.jme3.scene.Node) HullCollisionShape(com.jme3.bullet.collision.shapes.HullCollisionShape) SixDofJoint(com.jme3.bullet.joints.SixDofJoint)

Example 4 with HullCollisionShape

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);
}
Also used : Vector3f(com.jme3.math.Vector3f) HullCollisionShape(com.jme3.bullet.collision.shapes.HullCollisionShape) SixDofJoint(com.jme3.bullet.joints.SixDofJoint)

Aggregations

HullCollisionShape (com.jme3.bullet.collision.shapes.HullCollisionShape)3 SixDofJoint (com.jme3.bullet.joints.SixDofJoint)3 Vector3f (com.jme3.math.Vector3f)2 Bone (com.jme3.animation.Bone)1 PhysicsRigidBody (com.jme3.bullet.objects.PhysicsRigidBody)1 Transform (com.jme3.math.Transform)1 Geometry (com.jme3.scene.Geometry)1 Node (com.jme3.scene.Node)1 Spatial (com.jme3.scene.Spatial)1