Search in sources :

Example 11 with MeshCollisionShape

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

the class TestCollisionGroups 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
    Node physicsSphere2 = PhysicsTestHelper.createPhysicsTestNode(assetManager, new SphereCollisionShape(1), 1);
    physicsSphere2.getControl(RigidBodyControl.class).setPhysicsLocation(new Vector3f(4, 8, 0));
    physicsSphere2.getControl(RigidBodyControl.class).addCollideWithGroup(PhysicsCollisionObject.COLLISION_GROUP_02);
    rootNode.attachChild(physicsSphere2);
    getPhysicsSpace().add(physicsSphere2);
    // 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).setCollisionGroup(PhysicsCollisionObject.COLLISION_GROUP_02);
    node2.getControl(RigidBodyControl.class).setCollideWithGroups(PhysicsCollisionObject.COLLISION_GROUP_02);
    rootNode.attachChild(node2);
    getPhysicsSpace().add(node2);
    // the floor, does not move (mass=0)
    Node node3 = PhysicsTestHelper.createPhysicsTestNode(assetManager, new MeshCollisionShape(new Box(100f, 0.2f, 100f)), 0);
    node3.getControl(RigidBodyControl.class).setPhysicsLocation(new Vector3f(0f, -6, 0f));
    rootNode.attachChild(node3);
    getPhysicsSpace().add(node3);
}
Also used : MeshCollisionShape(com.jme3.bullet.collision.shapes.MeshCollisionShape) Sphere(com.jme3.scene.shape.Sphere) SphereCollisionShape(com.jme3.bullet.collision.shapes.SphereCollisionShape) BulletAppState(com.jme3.bullet.BulletAppState) Node(com.jme3.scene.Node) Vector3f(com.jme3.math.Vector3f) Box(com.jme3.scene.shape.Box) RigidBodyControl(com.jme3.bullet.control.RigidBodyControl)

Example 12 with MeshCollisionShape

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

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

the class WorldOfInception method simpleInitApp.

@Override
public void simpleInitApp() {
    //set far frustum only so we see the outer world longer
    cam.setFrustumFar(10000);
    cam.setLocation(Vector3f.ZERO);
    debugTools = new DebugTools(assetManager);
    rootNode.attachChild(debugTools.debugNode);
    poiMaterial = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
    poiMaterial.setTexture("ColorMap", assetManager.loadTexture("Interface/Logo/Monkey.jpg"));
    poiMesh = new Sphere(16, 16, 1f);
    ballMaterial = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
    ballMaterial.setTexture("ColorMap", assetManager.loadTexture("Interface/Logo/Monkey.jpg"));
    ballMaterial.setColor("Color", ColorRGBA.Red);
    ballMesh = new Sphere(16, 16, 1.0f);
    poiHorizonCollisionShape = new MeshCollisionShape(new Sphere(128, 128, poiRadius));
    poiCollisionShape = new SphereCollisionShape(1f);
    ballCollisionShape = new SphereCollisionShape(1f);
    setupKeys();
    setupDisplay();
    setupFog();
}
Also used : Sphere(com.jme3.scene.shape.Sphere) MeshCollisionShape(com.jme3.bullet.collision.shapes.MeshCollisionShape) SphereCollisionShape(com.jme3.bullet.collision.shapes.SphereCollisionShape) Material(com.jme3.material.Material) DebugTools(com.jme3.bullet.debug.DebugTools)

Example 14 with MeshCollisionShape

use of com.jme3.bullet.collision.shapes.MeshCollisionShape 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)

Example 15 with MeshCollisionShape

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

the class PhysicsRigidBody method rebuildRigidBody.

/**
     * Builds/rebuilds the phyiscs body when parameters have changed
     */
protected void rebuildRigidBody() {
    boolean removed = false;
    if (collisionShape instanceof MeshCollisionShape && mass != 0) {
        throw new IllegalStateException("Dynamic rigidbody can not have mesh collision shape!");
    }
    if (rBody != null) {
        if (rBody.isInWorld()) {
            PhysicsSpace.getPhysicsSpace().remove(this);
            removed = true;
        }
        rBody.destroy();
    }
    preRebuild();
    rBody = new RigidBody(constructionInfo);
    postRebuild();
    if (removed) {
        PhysicsSpace.getPhysicsSpace().add(this);
    }
}
Also used : MeshCollisionShape(com.jme3.bullet.collision.shapes.MeshCollisionShape) RigidBody(com.bulletphysics.dynamics.RigidBody)

Aggregations

RigidBodyControl (com.jme3.bullet.control.RigidBodyControl)11 Sphere (com.jme3.scene.shape.Sphere)10 MeshCollisionShape (com.jme3.bullet.collision.shapes.MeshCollisionShape)9 BulletAppState (com.jme3.bullet.BulletAppState)7 Vector3f (com.jme3.math.Vector3f)7 Node (com.jme3.scene.Node)7 Material (com.jme3.material.Material)6 SphereCollisionShape (com.jme3.bullet.collision.shapes.SphereCollisionShape)5 Geometry (com.jme3.scene.Geometry)5 Box (com.jme3.scene.shape.Box)5 Plane (com.jme3.math.Plane)4 AmbientLight (com.jme3.light.AmbientLight)3 RigidBody (com.bulletphysics.dynamics.RigidBody)1 TextureKey (com.jme3.asset.TextureKey)1 BoxCollisionShape (com.jme3.bullet.collision.shapes.BoxCollisionShape)1 PlaneCollisionShape (com.jme3.bullet.collision.shapes.PlaneCollisionShape)1 DebugTools (com.jme3.bullet.debug.DebugTools)1 HingeJoint (com.jme3.bullet.joints.HingeJoint)1 PhysicsCharacter (com.jme3.bullet.objects.PhysicsCharacter)1 BinaryImporter (com.jme3.export.binary.BinaryImporter)1