com.jme3.bullet.collision.shapes.HullCollisionShape Java Examples

The following examples show how to use com.jme3.bullet.collision.shapes.HullCollisionShape. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: RagdollUtils.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * Create a hull collision shape from linked vertices to this bone.
 *
 * @param model the model on which to base the shape
 * @param boneIndices indices of relevant bones (not null, unaffected)
 * @param initialScale scale factors
 * @param initialPosition location
 * @param weightThreshold minimum weight for inclusion
 * @return a new shape
 */
public static HullCollisionShape makeShapeFromVerticeWeights(Spatial model,
        List<Integer> boneIndices, Vector3f initialScale,
        Vector3f initialPosition, float weightThreshold) {
    List<Float> points = new ArrayList<>(100);

    SkeletonControl skeletonCtrl = model.getControl(SkeletonControl.class);
    Mesh[] targetMeshes = skeletonCtrl.getTargets();
    for (Mesh mesh : targetMeshes) {
        for (Integer index : boneIndices) {
            List<Float> bonePoints = getPoints(mesh, index, initialScale,
                    initialPosition, weightThreshold);
            points.addAll(bonePoints);
        }
    }

    assert !points.isEmpty();
    float[] p = new float[points.size()];
    for (int i = 0; i < points.size(); i++) {
        p[i] = points.get(i);
    }

    return new HullCollisionShape(p);
}
 
Example #2
Source File: RagdollUtils.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Create a hull collision shape from linked vertices to this bone. Vertices
 * must have previously been gathered using buildPointMap().
 *
 * @param pointsMap map from bone indices to coordinates (not null,
 * unaffected)
 * @param boneIndices (not null, unaffected)
 * @param initialScale scale factors (not null, unaffected)
 * @param initialPosition location (not null, unaffected)
 * @return a new shape (not null)
 */
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);
            }
        }
    }

    assert !points.isEmpty();
    float[] p = new float[points.size()];
    for (int i = 0; i < points.size(); i++) {
        p[i] = points.get(i);
    }

    return new HullCollisionShape(p);
}
 
Example #3
Source File: CollisionTester.java    From OpenRTS with MIT License 5 votes vote down vote up
private static CompoundCollisionShape getCollisionShape(Asset asset){
		Spatial s = models.get(asset.modelPath);
//		if(!shapes.containsKey(asset.modelPath)){
			CompoundCollisionShape res = new CompoundCollisionShape();
			if(s instanceof Node){
				for(Spatial child : ((Node)s).getChildren())
					if(child instanceof Geometry){
						HullCollisionShape hull = new HullCollisionShape(((Geometry)child).getMesh());
						float scale = (float)asset.scale;
						Vector3f vScale = new Vector3f(scale, scale, scale) ;
						hull.setScale(vScale);
						res.addChildShape(hull, Vector3f.ZERO);
					}
			} else {
				logger.info("houston on a un probleme");
			}
			
						
			shapes.put(asset.modelPath, res);
//		}
//		CompoundCollisionShape res = shapes.get(asset.modelPath);
//		float scale = (float)asset.scale;
//		Vector3f vScale = new Vector3f(scale, scale, scale) ;
//		for(ChildCollisionShape hull : res.getChildren())
//			hull.shape.setScale(vScale);

		return res;
	}
 
Example #4
Source File: RagdollUtils.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 5 votes vote down vote up
/**
 * Create a hull collision shape from linked vertices to this bone.
 * Vertices have to be previoulsly gathered in a map using buildPointMap method
 * @param link
 * @param model
 * @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);
}
 
Example #5
Source File: CollisionShapeFactory.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 5 votes vote down vote up
/**
 * 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;
    }
}
 
Example #6
Source File: RagdollUtils.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 5 votes vote down vote up
/**
 * Create a hull collision shape from linked vertices to this bone.
 * Vertices have to be previoulsly gathered in a map using buildPointMap method
 * @param link
 * @param model
 * @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);
}
 
Example #7
Source File: CollisionShapeFactory.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 5 votes vote down vote up
/**
 * 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;
    }
}
 
Example #8
Source File: HullCollisionShapeTreeNode.java    From jmonkeybuilder with Apache License 2.0 4 votes vote down vote up
public HullCollisionShapeTreeNode(@NotNull final HullCollisionShape element, final long objectId) {
    super(element, objectId);
}
 
Example #9
Source File: KinematicRagdollControl.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
/**
 * Generate a physics shape and bone links for the specified bone. Note:
 * recursive!
 *
 * @param model the spatial with the model's SkeletonControl (not null)
 * @param bone the bone to be linked (not null)
 * @param parent the body linked to the parent bone (not null)
 * @param reccount depth of the recursion (&ge;1)
 * @param pointsMap (not null)
 */
protected void boneRecursion(Spatial model, Bone bone, PhysicsRigidBody parent, int reccount, Map<Integer, List<Float>> pointsMap) {
    PhysicsRigidBody parentShape = parent;
    if (boneList.contains(bone.getName())) {

        PhysicsBoneLink link = new PhysicsBoneLink();
        link.bone = bone;

        //create 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 / reccount);

        shapeNode.setKinematic(mode == Mode.Kinematic);
        totalMass += rootMass / 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);
    }
}
 
Example #10
Source File: KinematicRagdollControl.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 4 votes vote down vote up
private 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.Kinetmatic);
        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);
    }
}
 
Example #11
Source File: KinematicRagdollControl.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 4 votes vote down vote up
private 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.Kinetmatic);
        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);
    }
}