Search in sources :

Example 6 with CompoundCollisionShape

use of com.jme3.bullet.collision.shapes.CompoundCollisionShape in project jmonkeyengine by jMonkeyEngine.

the class CollisionShapeFactory method shiftCompoundShapeContents.

/**
     * This method moves each child shape of a compound shape by the given vector
     * @param vector
     */
public static void shiftCompoundShapeContents(CompoundCollisionShape compoundShape, Vector3f vector) {
    for (Iterator<ChildCollisionShape> it = new LinkedList(compoundShape.getChildren()).iterator(); it.hasNext(); ) {
        ChildCollisionShape childCollisionShape = it.next();
        CollisionShape child = childCollisionShape.shape;
        Vector3f location = childCollisionShape.location;
        Matrix3f rotation = childCollisionShape.rotation;
        compoundShape.removeChildShape(child);
        compoundShape.addChildShape(child, location.add(vector), rotation);
    }
}
Also used : ChildCollisionShape(com.jme3.bullet.collision.shapes.infos.ChildCollisionShape) Matrix3f(com.jme3.math.Matrix3f) Vector3f(com.jme3.math.Vector3f) ChildCollisionShape(com.jme3.bullet.collision.shapes.infos.ChildCollisionShape) LinkedList(java.util.LinkedList)

Example 7 with CompoundCollisionShape

use of com.jme3.bullet.collision.shapes.CompoundCollisionShape 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 8 with CompoundCollisionShape

use of com.jme3.bullet.collision.shapes.CompoundCollisionShape in project jmonkeyengine by jMonkeyEngine.

the class DebugShapeFactory method getDebugShape.

/** The maximum corner for the aabb used for triangles to include in ConcaveShape processing.*/
//    private static final Vector3f aabbMax = new Vector3f(1e30f, 1e30f, 1e30f);
/** The minimum corner for the aabb used for triangles to include in ConcaveShape processing.*/
//    private static final Vector3f aabbMin = new Vector3f(-1e30f, -1e30f, -1e30f);
/**
     * Creates a debug shape from the given collision shape. This is mostly used internally.<br>
     * To attach a debug shape to a physics object, call <code>attachDebugShape(AssetManager manager);</code> on it.
     * @param collisionShape
     * @return
     */
public static Spatial getDebugShape(CollisionShape collisionShape) {
    if (collisionShape == null) {
        return null;
    }
    Spatial debugShape;
    if (collisionShape instanceof CompoundCollisionShape) {
        CompoundCollisionShape shape = (CompoundCollisionShape) collisionShape;
        List<ChildCollisionShape> children = shape.getChildren();
        Node node = new Node("DebugShapeNode");
        for (Iterator<ChildCollisionShape> it = children.iterator(); it.hasNext(); ) {
            ChildCollisionShape childCollisionShape = it.next();
            CollisionShape ccollisionShape = childCollisionShape.shape;
            Geometry geometry = createDebugShape(ccollisionShape);
            // apply translation
            geometry.setLocalTranslation(childCollisionShape.location);
            // apply rotation
            TempVars vars = TempVars.get();
            Matrix3f tempRot = vars.tempMat3;
            tempRot.set(geometry.getLocalRotation());
            childCollisionShape.rotation.mult(tempRot, tempRot);
            geometry.setLocalRotation(tempRot);
            vars.release();
            node.attachChild(geometry);
        }
        debugShape = node;
    } else {
        debugShape = createDebugShape(collisionShape);
    }
    if (debugShape == null) {
        return null;
    }
    debugShape.updateGeometricState();
    return debugShape;
}
Also used : Geometry(com.jme3.scene.Geometry) CompoundCollisionShape(com.jme3.bullet.collision.shapes.CompoundCollisionShape) ChildCollisionShape(com.jme3.bullet.collision.shapes.infos.ChildCollisionShape) CollisionShape(com.jme3.bullet.collision.shapes.CollisionShape) CompoundCollisionShape(com.jme3.bullet.collision.shapes.CompoundCollisionShape) Spatial(com.jme3.scene.Spatial) Matrix3f(com.jme3.math.Matrix3f) Node(com.jme3.scene.Node) ChildCollisionShape(com.jme3.bullet.collision.shapes.infos.ChildCollisionShape) TempVars(com.jme3.util.TempVars)

Example 9 with CompoundCollisionShape

use of com.jme3.bullet.collision.shapes.CompoundCollisionShape in project jmonkeyengine by jMonkeyEngine.

the class TestAttachDriver method buildPlayer.

private void buildPlayer() {
    Material mat = new Material(getAssetManager(), "Common/MatDefs/Misc/Unshaded.j3md");
    mat.getAdditionalRenderState().setWireframe(true);
    mat.setColor("Color", ColorRGBA.Red);
    //create a compound shape and attach the BoxCollisionShape for the car body at 0,1,0
    //this shifts the effective center of mass of the BoxCollisionShape to 0,-1,0
    CompoundCollisionShape compoundShape = new CompoundCollisionShape();
    BoxCollisionShape box = new BoxCollisionShape(new Vector3f(1.2f, 0.5f, 2.4f));
    compoundShape.addChildShape(box, new Vector3f(0, 1, 0));
    //create vehicle node
    Node vehicleNode = new Node("vehicleNode");
    vehicle = new VehicleControl(compoundShape, 800);
    vehicleNode.addControl(vehicle);
    //setting suspension values for wheels, this can be a bit tricky
    //see also https://docs.google.com/Doc?docid=0AXVUZ5xw6XpKZGNuZG56a3FfMzU0Z2NyZnF4Zmo&hl=en
    //200=f1 car
    float stiffness = 60.0f;
    //(should be lower than damp)
    float compValue = .3f;
    float dampValue = .4f;
    vehicle.setSuspensionCompression(compValue * 2.0f * FastMath.sqrt(stiffness));
    vehicle.setSuspensionDamping(dampValue * 2.0f * FastMath.sqrt(stiffness));
    vehicle.setSuspensionStiffness(stiffness);
    vehicle.setMaxSuspensionForce(10000.0f);
    //Create four wheels and add them at their locations
    // was 0, -1, 0
    Vector3f wheelDirection = new Vector3f(0, -1, 0);
    // was -1, 0, 0
    Vector3f wheelAxle = new Vector3f(-1, 0, 0);
    float radius = 0.5f;
    float restLength = 0.3f;
    float yOff = 0.5f;
    float xOff = 1f;
    float zOff = 2f;
    Cylinder wheelMesh = new Cylinder(16, 16, radius, radius * 0.6f, true);
    Node node1 = new Node("wheel 1 node");
    Geometry wheels1 = new Geometry("wheel 1", wheelMesh);
    node1.attachChild(wheels1);
    wheels1.rotate(0, FastMath.HALF_PI, 0);
    wheels1.setMaterial(mat);
    vehicle.addWheel(node1, new Vector3f(-xOff, yOff, zOff), wheelDirection, wheelAxle, restLength, radius, true);
    Node node2 = new Node("wheel 2 node");
    Geometry wheels2 = new Geometry("wheel 2", wheelMesh);
    node2.attachChild(wheels2);
    wheels2.rotate(0, FastMath.HALF_PI, 0);
    wheels2.setMaterial(mat);
    vehicle.addWheel(node2, new Vector3f(xOff, yOff, zOff), wheelDirection, wheelAxle, restLength, radius, true);
    Node node3 = new Node("wheel 3 node");
    Geometry wheels3 = new Geometry("wheel 3", wheelMesh);
    node3.attachChild(wheels3);
    wheels3.rotate(0, FastMath.HALF_PI, 0);
    wheels3.setMaterial(mat);
    vehicle.addWheel(node3, new Vector3f(-xOff, yOff, -zOff), wheelDirection, wheelAxle, restLength, radius, false);
    Node node4 = new Node("wheel 4 node");
    Geometry wheels4 = new Geometry("wheel 4", wheelMesh);
    node4.attachChild(wheels4);
    wheels4.rotate(0, FastMath.HALF_PI, 0);
    wheels4.setMaterial(mat);
    vehicle.addWheel(node4, new Vector3f(xOff, yOff, -zOff), wheelDirection, wheelAxle, restLength, radius, false);
    vehicleNode.attachChild(node1);
    vehicleNode.attachChild(node2);
    vehicleNode.attachChild(node3);
    vehicleNode.attachChild(node4);
    rootNode.attachChild(vehicleNode);
    getPhysicsSpace().add(vehicle);
    //driver
    Node driverNode = new Node("driverNode");
    driverNode.setLocalTranslation(0, 2, 0);
    driver = new RigidBodyControl(new BoxCollisionShape(new Vector3f(0.2f, .5f, 0.2f)));
    driverNode.addControl(driver);
    rootNode.attachChild(driverNode);
    getPhysicsSpace().add(driver);
    //joint
    slider = new SliderJoint(driver, vehicle, Vector3f.UNIT_Y.negate(), Vector3f.UNIT_Y, true);
    slider.setUpperLinLimit(.1f);
    slider.setLowerLinLimit(-.1f);
    getPhysicsSpace().add(slider);
    Node pole1Node = new Node("pole1Node");
    Node pole2Node = new Node("pole1Node");
    Node bridgeNode = new Node("pole1Node");
    pole1Node.setLocalTranslation(new Vector3f(-2, -1, 4));
    pole2Node.setLocalTranslation(new Vector3f(2, -1, 4));
    bridgeNode.setLocalTranslation(new Vector3f(0, 1.4f, 4));
    RigidBodyControl pole1 = new RigidBodyControl(new BoxCollisionShape(new Vector3f(0.2f, 1.25f, 0.2f)), 0);
    pole1Node.addControl(pole1);
    RigidBodyControl pole2 = new RigidBodyControl(new BoxCollisionShape(new Vector3f(0.2f, 1.25f, 0.2f)), 0);
    pole2Node.addControl(pole2);
    bridge = new RigidBodyControl(new BoxCollisionShape(new Vector3f(2.5f, 0.2f, 0.2f)));
    bridgeNode.addControl(bridge);
    rootNode.attachChild(pole1Node);
    rootNode.attachChild(pole2Node);
    rootNode.attachChild(bridgeNode);
    getPhysicsSpace().add(pole1);
    getPhysicsSpace().add(pole2);
    getPhysicsSpace().add(bridge);
}
Also used : Geometry(com.jme3.scene.Geometry) Cylinder(com.jme3.scene.shape.Cylinder) CompoundCollisionShape(com.jme3.bullet.collision.shapes.CompoundCollisionShape) SliderJoint(com.jme3.bullet.joints.SliderJoint) Node(com.jme3.scene.Node) VehicleControl(com.jme3.bullet.control.VehicleControl) Material(com.jme3.material.Material) BoxCollisionShape(com.jme3.bullet.collision.shapes.BoxCollisionShape) RigidBodyControl(com.jme3.bullet.control.RigidBodyControl)

Aggregations

CompoundCollisionShape (com.jme3.bullet.collision.shapes.CompoundCollisionShape)6 ChildCollisionShape (com.jme3.bullet.collision.shapes.infos.ChildCollisionShape)5 Geometry (com.jme3.scene.Geometry)5 Node (com.jme3.scene.Node)5 Vector3f (com.jme3.math.Vector3f)4 BoxCollisionShape (com.jme3.bullet.collision.shapes.BoxCollisionShape)3 Material (com.jme3.material.Material)3 Matrix3f (com.jme3.math.Matrix3f)3 CollisionShape (com.jme3.bullet.collision.shapes.CollisionShape)2 RigidBodyControl (com.jme3.bullet.control.RigidBodyControl)2 VehicleControl (com.jme3.bullet.control.VehicleControl)2 Spatial (com.jme3.scene.Spatial)2 Cylinder (com.jme3.scene.shape.Cylinder)2 TempVars (com.jme3.util.TempVars)2 CompoundShape (com.bulletphysics.collision.shapes.CompoundShape)1 Transform (com.bulletphysics.linearmath.Transform)1 BulletAppState (com.jme3.bullet.BulletAppState)1 CapsuleCollisionShape (com.jme3.bullet.collision.shapes.CapsuleCollisionShape)1 SphereCollisionShape (com.jme3.bullet.collision.shapes.SphereCollisionShape)1 GhostControl (com.jme3.bullet.control.GhostControl)1