Search in sources :

Example 6 with Cylinder

use of com.jme3.scene.shape.Cylinder 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)

Example 7 with Cylinder

use of com.jme3.scene.shape.Cylinder in project jmonkeyengine by jMonkeyEngine.

the class TestLocalPhysics method simpleInitApp.

@Override
public void simpleInitApp() {
    bulletAppState = new BulletAppState();
    stateManager.attach(bulletAppState);
    bulletAppState.setDebugEnabled(true);
    // Add a physics sphere to the world
    Node physicsSphere = PhysicsTestHelper.createPhysicsTestNode(assetManager, new SphereCollisionShape(1), 1);
    physicsSphere.getControl(RigidBodyControl.class).setPhysicsLocation(new Vector3f(3, 6, 0));
    physicsSphere.getControl(RigidBodyControl.class).setApplyPhysicsLocal(true);
    rootNode.attachChild(physicsSphere);
    getPhysicsSpace().add(physicsSphere);
    // Add a physics sphere to the world using the collision shape from sphere one
    Node physicsSphere2 = PhysicsTestHelper.createPhysicsTestNode(assetManager, physicsSphere.getControl(RigidBodyControl.class).getCollisionShape(), 1);
    physicsSphere2.getControl(RigidBodyControl.class).setPhysicsLocation(new Vector3f(4, 8, 0));
    physicsSphere2.getControl(RigidBodyControl.class).setApplyPhysicsLocal(true);
    rootNode.attachChild(physicsSphere2);
    getPhysicsSpace().add(physicsSphere2);
    // Add a physics box to the world
    Node physicsBox = PhysicsTestHelper.createPhysicsTestNode(assetManager, new BoxCollisionShape(new Vector3f(1, 1, 1)), 1);
    physicsBox.getControl(RigidBodyControl.class).setFriction(0.1f);
    physicsBox.getControl(RigidBodyControl.class).setPhysicsLocation(new Vector3f(.6f, 4, .5f));
    physicsBox.getControl(RigidBodyControl.class).setApplyPhysicsLocal(true);
    rootNode.attachChild(physicsBox);
    getPhysicsSpace().add(physicsBox);
    // Add a physics cylinder to the world
    Node physicsCylinder = PhysicsTestHelper.createPhysicsTestNode(assetManager, new CylinderCollisionShape(new Vector3f(1f, 1f, 1.5f)), 1);
    physicsCylinder.getControl(RigidBodyControl.class).setPhysicsLocation(new Vector3f(2, 2, 0));
    physicsCylinder.getControl(RigidBodyControl.class).setApplyPhysicsLocal(true);
    rootNode.attachChild(physicsCylinder);
    getPhysicsSpace().add(physicsCylinder);
    // an obstacle mesh, does not move (mass=0)
    Node node2 = PhysicsTestHelper.createPhysicsTestNode(assetManager, new MeshCollisionShape(new Sphere(16, 16, 1.2f)), 0);
    node2.getControl(RigidBodyControl.class).setPhysicsLocation(new Vector3f(2.5f, -4, 0f));
    node2.getControl(RigidBodyControl.class).setApplyPhysicsLocal(true);
    rootNode.attachChild(node2);
    getPhysicsSpace().add(node2);
    // the floor mesh, does not move (mass=0)
    Node node3 = PhysicsTestHelper.createPhysicsTestNode(assetManager, new PlaneCollisionShape(new Plane(new Vector3f(0, 1, 0), 0)), 0);
    node3.getControl(RigidBodyControl.class).setPhysicsLocation(new Vector3f(0f, -6, 0f));
    node3.getControl(RigidBodyControl.class).setApplyPhysicsLocal(true);
    rootNode.attachChild(node3);
    getPhysicsSpace().add(node3);
// Join the physics objects with a Point2Point joint
//        PhysicsPoint2PointJoint joint=new PhysicsPoint2PointJoint(physicsSphere, physicsBox, new Vector3f(-2,0,0), new Vector3f(2,0,0));
//        PhysicsHingeJoint joint=new PhysicsHingeJoint(physicsSphere, physicsBox, new Vector3f(-2,0,0), new Vector3f(2,0,0), Vector3f.UNIT_Z,Vector3f.UNIT_Z);
//        getPhysicsSpace().add(joint);
}
Also used : Sphere(com.jme3.scene.shape.Sphere) Plane(com.jme3.math.Plane) BulletAppState(com.jme3.bullet.BulletAppState) Node(com.jme3.scene.Node) Vector3f(com.jme3.math.Vector3f) RigidBodyControl(com.jme3.bullet.control.RigidBodyControl)

Example 8 with Cylinder

use of com.jme3.scene.shape.Cylinder in project jmonkeyengine by jMonkeyEngine.

the class TestCollisionShapeFactory method simpleInitApp.

@Override
public void simpleInitApp() {
    bulletAppState = new BulletAppState();
    stateManager.attach(bulletAppState);
    bulletAppState.setDebugEnabled(true);
    createMaterial();
    Node node = new Node("node1");
    attachRandomGeometry(node, mat1);
    randomizeTransform(node);
    Node node2 = new Node("node2");
    attachRandomGeometry(node2, mat2);
    randomizeTransform(node2);
    node.attachChild(node2);
    rootNode.attachChild(node);
    RigidBodyControl control = new RigidBodyControl(0);
    node.addControl(control);
    getPhysicsSpace().add(control);
    //test single geometry too
    Geometry myGeom = new Geometry("cylinder", new Cylinder(16, 16, 0.5f, 1));
    myGeom.setMaterial(mat3);
    randomizeTransform(myGeom);
    rootNode.attachChild(myGeom);
    RigidBodyControl control3 = new RigidBodyControl(0);
    myGeom.addControl(control3);
    getPhysicsSpace().add(control3);
}
Also used : Geometry(com.jme3.scene.Geometry) Cylinder(com.jme3.scene.shape.Cylinder) BulletAppState(com.jme3.bullet.BulletAppState) Node(com.jme3.scene.Node) RigidBodyControl(com.jme3.bullet.control.RigidBodyControl)

Example 9 with Cylinder

use of com.jme3.scene.shape.Cylinder in project jmonkeyengine by jMonkeyEngine.

the class TestSimplePhysics method simpleInitApp.

@Override
public void simpleInitApp() {
    bulletAppState = new BulletAppState();
    stateManager.attach(bulletAppState);
    bulletAppState.setDebugEnabled(true);
    // Add a physics sphere to the world
    Node physicsSphere = PhysicsTestHelper.createPhysicsTestNode(assetManager, new SphereCollisionShape(1), 1);
    physicsSphere.getControl(RigidBodyControl.class).setPhysicsLocation(new Vector3f(3, 6, 0));
    rootNode.attachChild(physicsSphere);
    getPhysicsSpace().add(physicsSphere);
    // Add a physics sphere to the world using the collision shape from sphere one
    Node physicsSphere2 = PhysicsTestHelper.createPhysicsTestNode(assetManager, physicsSphere.getControl(RigidBodyControl.class).getCollisionShape(), 1);
    physicsSphere2.getControl(RigidBodyControl.class).setPhysicsLocation(new Vector3f(4, 8, 0));
    rootNode.attachChild(physicsSphere2);
    getPhysicsSpace().add(physicsSphere2);
    // Add a physics box to the world
    Node physicsBox = PhysicsTestHelper.createPhysicsTestNode(assetManager, new BoxCollisionShape(new Vector3f(1, 1, 1)), 1);
    physicsBox.getControl(RigidBodyControl.class).setFriction(0.1f);
    physicsBox.getControl(RigidBodyControl.class).setPhysicsLocation(new Vector3f(.6f, 4, .5f));
    rootNode.attachChild(physicsBox);
    getPhysicsSpace().add(physicsBox);
    // Add a physics cylinder to the world
    Node physicsCylinder = PhysicsTestHelper.createPhysicsTestNode(assetManager, new CylinderCollisionShape(new Vector3f(1f, 1f, 1.5f)), 1);
    physicsCylinder.getControl(RigidBodyControl.class).setPhysicsLocation(new Vector3f(2, 2, 0));
    rootNode.attachChild(physicsCylinder);
    getPhysicsSpace().add(physicsCylinder);
    // an obstacle mesh, does not move (mass=0)
    Node node2 = PhysicsTestHelper.createPhysicsTestNode(assetManager, new MeshCollisionShape(new Sphere(16, 16, 1.2f)), 0);
    node2.getControl(RigidBodyControl.class).setPhysicsLocation(new Vector3f(2.5f, -4, 0f));
    rootNode.attachChild(node2);
    getPhysicsSpace().add(node2);
    // the floor mesh, does not move (mass=0)
    Node node3 = PhysicsTestHelper.createPhysicsTestNode(assetManager, new PlaneCollisionShape(new Plane(new Vector3f(0, 1, 0), 0)), 0);
    node3.getControl(RigidBodyControl.class).setPhysicsLocation(new Vector3f(0f, -6, 0f));
    rootNode.attachChild(node3);
    getPhysicsSpace().add(node3);
// Join the physics objects with a Point2Point joint
//        PhysicsPoint2PointJoint joint=new PhysicsPoint2PointJoint(physicsSphere, physicsBox, new Vector3f(-2,0,0), new Vector3f(2,0,0));
//        PhysicsHingeJoint joint=new PhysicsHingeJoint(physicsSphere, physicsBox, new Vector3f(-2,0,0), new Vector3f(2,0,0), Vector3f.UNIT_Z,Vector3f.UNIT_Z);
//        getPhysicsSpace().add(joint);
}
Also used : Sphere(com.jme3.scene.shape.Sphere) Plane(com.jme3.math.Plane) BulletAppState(com.jme3.bullet.BulletAppState) Node(com.jme3.scene.Node) Vector3f(com.jme3.math.Vector3f) RigidBodyControl(com.jme3.bullet.control.RigidBodyControl)

Example 10 with Cylinder

use of com.jme3.scene.shape.Cylinder in project jmonkeyengine by jMonkeyEngine.

the class TestPhysicsReadWrite method simpleInitApp.

@Override
public void simpleInitApp() {
    bulletAppState = new BulletAppState();
    stateManager.attach(bulletAppState);
    bulletAppState.setDebugEnabled(true);
    physicsRootNode = new Node("PhysicsRootNode");
    rootNode.attachChild(physicsRootNode);
    // Add a physics sphere to the world
    Node physicsSphere = PhysicsTestHelper.createPhysicsTestNode(assetManager, new SphereCollisionShape(1), 1);
    physicsSphere.getControl(RigidBodyControl.class).setPhysicsLocation(new Vector3f(3, 6, 0));
    rootNode.attachChild(physicsSphere);
    getPhysicsSpace().add(physicsSphere);
    // Add a physics sphere to the world using the collision shape from sphere one
    Node physicsSphere2 = PhysicsTestHelper.createPhysicsTestNode(assetManager, physicsSphere.getControl(RigidBodyControl.class).getCollisionShape(), 1);
    physicsSphere2.getControl(RigidBodyControl.class).setPhysicsLocation(new Vector3f(4, 8, 0));
    rootNode.attachChild(physicsSphere2);
    getPhysicsSpace().add(physicsSphere2);
    // Add a physics box to the world
    Node physicsBox = PhysicsTestHelper.createPhysicsTestNode(assetManager, new BoxCollisionShape(new Vector3f(1, 1, 1)), 1);
    physicsBox.getControl(RigidBodyControl.class).setFriction(0.1f);
    physicsBox.getControl(RigidBodyControl.class).setPhysicsLocation(new Vector3f(.6f, 4, .5f));
    rootNode.attachChild(physicsBox);
    getPhysicsSpace().add(physicsBox);
    // Add a physics cylinder to the world
    Node physicsCylinder = PhysicsTestHelper.createPhysicsTestNode(assetManager, new CylinderCollisionShape(new Vector3f(1f, 1f, 1.5f)), 1);
    physicsCylinder.getControl(RigidBodyControl.class).setPhysicsLocation(new Vector3f(2, 2, 0));
    rootNode.attachChild(physicsCylinder);
    getPhysicsSpace().add(physicsCylinder);
    // an obstacle mesh, does not move (mass=0)
    Node node2 = PhysicsTestHelper.createPhysicsTestNode(assetManager, new MeshCollisionShape(new Sphere(16, 16, 1.2f)), 0);
    node2.getControl(RigidBodyControl.class).setPhysicsLocation(new Vector3f(2.5f, -4, 0f));
    rootNode.attachChild(node2);
    getPhysicsSpace().add(node2);
    // the floor mesh, does not move (mass=0)
    Node node3 = PhysicsTestHelper.createPhysicsTestNode(assetManager, new PlaneCollisionShape(new Plane(new Vector3f(0, 1, 0), 0)), 0);
    node3.getControl(RigidBodyControl.class).setPhysicsLocation(new Vector3f(0f, -6, 0f));
    rootNode.attachChild(node3);
    getPhysicsSpace().add(node3);
    // Join the physics objects with a Point2Point joint
    HingeJoint joint = new HingeJoint(physicsSphere.getControl(RigidBodyControl.class), physicsBox.getControl(RigidBodyControl.class), new Vector3f(-2, 0, 0), new Vector3f(2, 0, 0), Vector3f.UNIT_Z, Vector3f.UNIT_Z);
    getPhysicsSpace().add(joint);
    //save and load the physicsRootNode
    try {
        //remove all physics objects from physics space
        getPhysicsSpace().removeAll(physicsRootNode);
        physicsRootNode.removeFromParent();
        //export to byte array
        ByteArrayOutputStream bout = new ByteArrayOutputStream();
        BinaryExporter.getInstance().save(physicsRootNode, bout);
        //import from byte array
        ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
        BinaryImporter imp = BinaryImporter.getInstance();
        imp.setAssetManager(assetManager);
        Node newPhysicsRootNode = (Node) imp.load(bin);
        //add all physics objects to physics space
        getPhysicsSpace().addAll(newPhysicsRootNode);
        rootNode.attachChild(newPhysicsRootNode);
    } catch (IOException ex) {
        Logger.getLogger(TestPhysicsReadWrite.class.getName()).log(Level.SEVERE, null, ex);
    }
}
Also used : HingeJoint(com.jme3.bullet.joints.HingeJoint) Plane(com.jme3.math.Plane) Node(com.jme3.scene.Node) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) RigidBodyControl(com.jme3.bullet.control.RigidBodyControl) Sphere(com.jme3.scene.shape.Sphere) BinaryImporter(com.jme3.export.binary.BinaryImporter) ByteArrayInputStream(java.io.ByteArrayInputStream) BulletAppState(com.jme3.bullet.BulletAppState) Vector3f(com.jme3.math.Vector3f)

Aggregations

Node (com.jme3.scene.Node)7 Vector3f (com.jme3.math.Vector3f)6 RigidBodyControl (com.jme3.bullet.control.RigidBodyControl)5 Geometry (com.jme3.scene.Geometry)5 Cylinder (com.jme3.scene.shape.Cylinder)5 BulletAppState (com.jme3.bullet.BulletAppState)4 Material (com.jme3.material.Material)4 Plane (com.jme3.math.Plane)3 Sphere (com.jme3.scene.shape.Sphere)3 BoxCollisionShape (com.jme3.bullet.collision.shapes.BoxCollisionShape)2 CompoundCollisionShape (com.jme3.bullet.collision.shapes.CompoundCollisionShape)2 VehicleControl (com.jme3.bullet.control.VehicleControl)2 Texture (com.jme3.texture.Texture)2 FloatBuffer (java.nio.FloatBuffer)2 ChaseCameraAppState (com.jme3.app.ChaseCameraAppState)1 TextureKey (com.jme3.asset.TextureKey)1 HingeJoint (com.jme3.bullet.joints.HingeJoint)1 SliderJoint (com.jme3.bullet.joints.SliderJoint)1 BinaryImporter (com.jme3.export.binary.BinaryImporter)1 ActionListener (com.jme3.input.controls.ActionListener)1