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

The following examples show how to use com.jme3.bullet.collision.shapes.BoxCollisionShape. 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: TestAttachGhostObject.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 6 votes vote down vote up
public void setupJoint() {
    Node holderNode = PhysicsTestHelper.createPhysicsTestNode(assetManager, new BoxCollisionShape(new Vector3f(.1f, .1f, .1f)), 0);
    holderNode.getControl(RigidBodyControl.class).setPhysicsLocation(new Vector3f(0f, 0, 0f));
    rootNode.attachChild(holderNode);
    getPhysicsSpace().add(holderNode);

    Node hammerNode = PhysicsTestHelper.createPhysicsTestNode(assetManager, new BoxCollisionShape(new Vector3f(.3f, .3f, .3f)), 1);
    hammerNode.getControl(RigidBodyControl.class).setPhysicsLocation(new Vector3f(0f, -1, 0f));
    rootNode.attachChild(hammerNode);
    getPhysicsSpace().add(hammerNode);

    //immovable
    collisionNode = PhysicsTestHelper.createPhysicsTestNode(assetManager, new BoxCollisionShape(new Vector3f(.3f, .3f, .3f)), 0);
    collisionNode.getControl(RigidBodyControl.class).setPhysicsLocation(new Vector3f(1.8f, 0, 0f));
    rootNode.attachChild(collisionNode);
    getPhysicsSpace().add(collisionNode);

    //ghost node
    ghostControl = new GhostControl(new SphereCollisionShape(0.7f));

    hammerNode.addControl(ghostControl);
    getPhysicsSpace().add(ghostControl);

    joint = new HingeJoint(holderNode.getControl(RigidBodyControl.class), hammerNode.getControl(RigidBodyControl.class), Vector3f.ZERO, new Vector3f(0f, -1, 0f), Vector3f.UNIT_Z, Vector3f.UNIT_Z);
    getPhysicsSpace().add(joint);
}
 
Example #2
Source File: TestAttachGhostObject.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
public void setupJoint() {
    Node holderNode = PhysicsTestHelper.createPhysicsTestNode(assetManager, new BoxCollisionShape(new Vector3f(.1f, .1f, .1f)), 0);
    holderNode.getControl(RigidBodyControl.class).setPhysicsLocation(new Vector3f(0f, 0, 0f));
    rootNode.attachChild(holderNode);
    getPhysicsSpace().add(holderNode);

    Node hammerNode = PhysicsTestHelper.createPhysicsTestNode(assetManager, new BoxCollisionShape(new Vector3f(.3f, .3f, .3f)), 1);
    hammerNode.getControl(RigidBodyControl.class).setPhysicsLocation(new Vector3f(0f, -1, 0f));
    rootNode.attachChild(hammerNode);
    getPhysicsSpace().add(hammerNode);

    //immovable
    collisionNode = PhysicsTestHelper.createPhysicsTestNode(assetManager, new BoxCollisionShape(new Vector3f(.3f, .3f, .3f)), 0);
    collisionNode.getControl(RigidBodyControl.class).setPhysicsLocation(new Vector3f(1.8f, 0, 0f));
    rootNode.attachChild(collisionNode);
    getPhysicsSpace().add(collisionNode);

    //ghost node
    ghostControl = new GhostControl(new SphereCollisionShape(0.7f));

    hammerNode.addControl(ghostControl);
    getPhysicsSpace().add(ghostControl);

    joint = new HingeJoint(holderNode.getControl(RigidBodyControl.class), hammerNode.getControl(RigidBodyControl.class), Vector3f.ZERO, new Vector3f(0f, -1, 0f), Vector3f.UNIT_Z, Vector3f.UNIT_Z);
    getPhysicsSpace().add(joint);
}
 
Example #3
Source File: KinematicRagdollControl.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 6 votes vote down vote up
private void scanSpatial(Spatial model) {
    AnimControl animControl = model.getControl(AnimControl.class);
    Map<Integer, List<Float>> pointsMap = null;
    if (weightThreshold == -1.0f) {
        pointsMap = RagdollUtils.buildPointMap(model);
    }

    skeleton = animControl.getSkeleton();
    skeleton.resetAndUpdate();
    for (int i = 0; i < skeleton.getRoots().length; i++) {
        Bone childBone = skeleton.getRoots()[i];
        if (childBone.getParent() == null) {
            logger.log(Level.INFO, "Found root bone in skeleton {0}", skeleton);
            baseRigidBody = new PhysicsRigidBody(new BoxCollisionShape(Vector3f.UNIT_XYZ.mult(0.1f)), 1);
            baseRigidBody.setKinematic(mode == Mode.Kinetmatic);
            boneRecursion(model, childBone, baseRigidBody, 1, pointsMap);
        }
    }
}
 
Example #4
Source File: KinematicRagdollControl.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 6 votes vote down vote up
private void scanSpatial(Spatial model) {
    AnimControl animControl = model.getControl(AnimControl.class);
    Map<Integer, List<Float>> pointsMap = null;
    if (weightThreshold == -1.0f) {
        pointsMap = RagdollUtils.buildPointMap(model);
    }

    skeleton = animControl.getSkeleton();
    skeleton.resetAndUpdate();
    for (int i = 0; i < skeleton.getRoots().length; i++) {
        Bone childBone = skeleton.getRoots()[i];
        if (childBone.getParent() == null) {
            logger.log(Level.INFO, "Found root bone in skeleton {0}", skeleton);
            baseRigidBody = new PhysicsRigidBody(new BoxCollisionShape(Vector3f.UNIT_XYZ.mult(0.1f)), 1);
            baseRigidBody.setKinematic(mode == Mode.Kinetmatic);
            boneRecursion(model, childBone, baseRigidBody, 1, pointsMap);
        }
    }
}
 
Example #5
Source File: VehicleEditorController.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 5 votes vote down vote up
public boolean doCheckVehicle(Node rootNode) {
    VehicleControl control = rootNode.getControl(VehicleControl.class);
    if (control == null) {
        vehicleControl = new VehicleControl(new BoxCollisionShape(Vector3f.UNIT_XYZ), 200);
        vehicleControl.createDebugShape(SceneApplication.getApplication().getAssetManager());
        rootNode.addControl(vehicleControl);
        return true;
    } else {
        vehicleControl = control;
        vehicleControl.createDebugShape(SceneApplication.getApplication().getAssetManager());
        return false;
    }
}
 
Example #6
Source File: TestPhysicsHingeJoint.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 5 votes vote down vote up
public void setupJoint() {
    Node holderNode=PhysicsTestHelper.createPhysicsTestNode(assetManager, new BoxCollisionShape(new Vector3f( .1f, .1f, .1f)),0);
    holderNode.getControl(RigidBodyControl.class).setPhysicsLocation(new Vector3f(0f,0,0f));
    rootNode.attachChild(holderNode);
    getPhysicsSpace().add(holderNode);

    Node hammerNode=PhysicsTestHelper.createPhysicsTestNode(assetManager, new BoxCollisionShape(new Vector3f( .3f, .3f, .3f)),1);
    hammerNode.getControl(RigidBodyControl.class).setPhysicsLocation(new Vector3f(0f,-1,0f));
    rootNode.attachChild(hammerNode);
    getPhysicsSpace().add(hammerNode);

    joint=new HingeJoint(holderNode.getControl(RigidBodyControl.class), hammerNode.getControl(RigidBodyControl.class), Vector3f.ZERO, new Vector3f(0f,-1,0f), Vector3f.UNIT_Z, Vector3f.UNIT_Z);
    getPhysicsSpace().add(joint);
}
 
Example #7
Source File: TestBrickWall.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 5 votes vote down vote up
public void initFloor() {
    Box floorBox = new Box(Vector3f.ZERO, 10f, 0.1f, 5f);
    floorBox.scaleTextureCoordinates(new Vector2f(3, 6));

    Geometry floor = new Geometry("floor", floorBox);
    floor.setMaterial(mat3);
    floor.setShadowMode(ShadowMode.Receive);
    floor.setLocalTranslation(0, -0.1f, 0);
    floor.addControl(new RigidBodyControl(new BoxCollisionShape(new Vector3f(10f, 0.1f, 5f)), 0));
    this.rootNode.attachChild(floor);
    this.getPhysicsSpace().add(floor);
}
 
Example #8
Source File: TestHoveringTank.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 5 votes vote down vote up
public void makeMissile() {
    Vector3f pos = spaceCraft.getWorldTranslation().clone();
    Quaternion rot = spaceCraft.getWorldRotation();
    Vector3f dir = rot.getRotationColumn(2);

    Spatial missile = assetManager.loadModel("Models/SpaceCraft/Rocket.mesh.xml");
    missile.scale(0.5f);
    missile.rotate(0, FastMath.PI, 0);
    missile.updateGeometricState();

    BoundingBox box = (BoundingBox) missile.getWorldBound();
    final Vector3f extent = box.getExtent(null);

    BoxCollisionShape boxShape = new BoxCollisionShape(extent);

    missile.setName("Missile");
    missile.rotate(rot);
    missile.setLocalTranslation(pos.addLocal(0, extent.y * 4.5f, 0));
    missile.setLocalRotation(hoverControl.getPhysicsRotation());
    missile.setShadowMode(ShadowMode.Cast);
    RigidBodyControl control = new BombControl(assetManager, boxShape, 20);
    control.setLinearVelocity(dir.mult(100));
    control.setCollisionGroup(PhysicsCollisionObject.COLLISION_GROUP_03);
    missile.addControl(control);


    rootNode.attachChild(missile);
    getPhysicsSpace().add(missile);
}
 
Example #9
Source File: TestGhostObject.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 5 votes vote down vote up
private void initGhostObject() {
    Vector3f halfExtents = new Vector3f(3, 4.2f, 1);
    ghostControl = new GhostControl(new BoxCollisionShape(halfExtents));
    Node node=new Node("Ghost Object");
    node.addControl(ghostControl);
    rootNode.attachChild(node);
    getPhysicsSpace().add(ghostControl);
}
 
Example #10
Source File: TestGhostObject.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 5 votes vote down vote up
@Override
public void simpleInitApp() {
    bulletAppState = new BulletAppState();
    stateManager.attach(bulletAppState);
    bulletAppState.getPhysicsSpace().enableDebug(assetManager);

    // Mesh to be shared across several boxes.
    Box boxGeom = new Box(Vector3f.ZERO, 1f, 1f, 1f);
    // CollisionShape to be shared across several boxes.
    CollisionShape shape = new BoxCollisionShape(new Vector3f(1, 1, 1));

    Node physicsBox = PhysicsTestHelper.createPhysicsTestNode(assetManager, shape, 1);
    physicsBox.setName("box0");
    physicsBox.getControl(RigidBodyControl.class).setPhysicsLocation(new Vector3f(.6f, 4, .5f));
    rootNode.attachChild(physicsBox);
    getPhysicsSpace().add(physicsBox);

    Node physicsBox1 = PhysicsTestHelper.createPhysicsTestNode(assetManager, shape, 1);
    physicsBox1.setName("box1");
    physicsBox1.getControl(RigidBodyControl.class).setPhysicsLocation(new Vector3f(0, 40, 0));
    rootNode.attachChild(physicsBox1);
    getPhysicsSpace().add(physicsBox1);

    Node physicsBox2 = PhysicsTestHelper.createPhysicsTestNode(assetManager, new BoxCollisionShape(new Vector3f(1, 1, 1)), 1);
    physicsBox2.setName("box0");
    physicsBox2.getControl(RigidBodyControl.class).setPhysicsLocation(new Vector3f(.5f, 80, -.8f));
    rootNode.attachChild(physicsBox2);
    getPhysicsSpace().add(physicsBox2);

    // the floor, does not move (mass=0)
    Node node = PhysicsTestHelper.createPhysicsTestNode(assetManager, new BoxCollisionShape(new Vector3f(100, 1, 100)), 0);
    node.setName("floor");
    node.getControl(RigidBodyControl.class).setPhysicsLocation(new Vector3f(0f, -6, 0f));
    rootNode.attachChild(node);
    getPhysicsSpace().add(node);

    initGhostObject();
}
 
Example #11
Source File: TestCcd.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 5 votes vote down vote up
@Override
public void simpleInitApp() {
    bulletAppState = new BulletAppState();
    stateManager.attach(bulletAppState);
    bulletAppState.getPhysicsSpace().enableDebug(assetManager);
    bullet = new Sphere(32, 32, 0.4f, true, false);
    bullet.setTextureMode(TextureMode.Projected);
    bulletCollisionShape = new SphereCollisionShape(0.1f);
    setupKeys();

    mat = new Material(getAssetManager(), "Common/MatDefs/Misc/Unshaded.j3md");
    mat.getAdditionalRenderState().setWireframe(true);
    mat.setColor("Color", ColorRGBA.Green);

    mat2 = new Material(getAssetManager(), "Common/MatDefs/Misc/Unshaded.j3md");
    mat2.getAdditionalRenderState().setWireframe(true);
    mat2.setColor("Color", ColorRGBA.Red);

    // An obstacle mesh, does not move (mass=0)
    Node node2 = new Node();
    node2.setName("mesh");
    node2.setLocalTranslation(new Vector3f(2.5f, 0, 0f));
    node2.addControl(new RigidBodyControl(new MeshCollisionShape(new Box(Vector3f.ZERO, 4, 4, 0.1f)), 0));
    rootNode.attachChild(node2);
    getPhysicsSpace().add(node2);

    // The floor, does not move (mass=0)
    Node node3 = new Node();
    node3.setLocalTranslation(new Vector3f(0f, -6, 0f));
    node3.addControl(new RigidBodyControl(new BoxCollisionShape(new Vector3f(100, 1, 100)), 0));
    rootNode.attachChild(node3);
    getPhysicsSpace().add(node3);

}
 
Example #12
Source File: CollisionShapeFactory.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 5 votes vote down vote up
/**
 * Uses the bounding box of the supplied spatial to create a BoxCollisionShape
 * @param spatial
 * @return BoxCollisionShape with the size of the spatials BoundingBox
 */
private static BoxCollisionShape createSingleBoxShape(Spatial spatial, Spatial parent) {
    spatial.setModelBound(new BoundingBox());
    //TODO: using world bound here instead of "local world" bound...
    BoxCollisionShape shape = new BoxCollisionShape(
            ((BoundingBox) spatial.getWorldBound()).getExtent(new Vector3f()));
    return shape;
}
 
Example #13
Source File: CollisionShapeFactory.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 5 votes vote down vote up
/**
 * Uses the bounding box of the supplied spatial to create a BoxCollisionShape
 * @param spatial
 * @return BoxCollisionShape with the size of the spatials BoundingBox
 */
private static BoxCollisionShape createSingleBoxShape(Spatial spatial, Spatial parent) {
    spatial.setModelBound(new BoundingBox());
    //TODO: using world bound here instead of "local world" bound...
    BoxCollisionShape shape = new BoxCollisionShape(
            ((BoundingBox) spatial.getWorldBound()).getExtent(new Vector3f()));
    return shape;
}
 
Example #14
Source File: ChildCollisionShape.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * De-serialize this shape, for example when loading from a J3O file.
 *
 * @param im importer (not null)
 * @throws IOException from importer
 */
@Override
public void read(JmeImporter im) throws IOException {
    InputCapsule capsule = im.getCapsule(this);
    location = (Vector3f) capsule.readSavable("location", new Vector3f());
    rotation = (Matrix3f) capsule.readSavable("rotation", new Matrix3f());
    shape = (CollisionShape) capsule.readSavable("shape", new BoxCollisionShape(new Vector3f(1, 1, 1)));
}
 
Example #15
Source File: TestPhysicsHingeJoint.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public void setupJoint() {
    Node holderNode=PhysicsTestHelper.createPhysicsTestNode(assetManager, new BoxCollisionShape(new Vector3f( .1f, .1f, .1f)),0);
    holderNode.getControl(RigidBodyControl.class).setPhysicsLocation(new Vector3f(0f,0,0f));
    rootNode.attachChild(holderNode);
    getPhysicsSpace().add(holderNode);

    Node hammerNode=PhysicsTestHelper.createPhysicsTestNode(assetManager, new BoxCollisionShape(new Vector3f( .3f, .3f, .3f)),1);
    hammerNode.getControl(RigidBodyControl.class).setPhysicsLocation(new Vector3f(0f,-1,0f));
    rootNode.attachChild(hammerNode);
    getPhysicsSpace().add(hammerNode);

    joint=new HingeJoint(holderNode.getControl(RigidBodyControl.class), hammerNode.getControl(RigidBodyControl.class), Vector3f.ZERO, new Vector3f(0f,-1,0f), Vector3f.UNIT_Z, Vector3f.UNIT_Z);
    getPhysicsSpace().add(joint);
}
 
Example #16
Source File: TestBrickWall.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public void initFloor() {
    Box floorBox = new Box(10f, 0.1f, 5f);
    floorBox.scaleTextureCoordinates(new Vector2f(3, 6));

    Geometry floor = new Geometry("floor", floorBox);
    floor.setMaterial(mat3);
    floor.setShadowMode(ShadowMode.Receive);
    floor.setLocalTranslation(0, -0.1f, 0);
    floor.addControl(new RigidBodyControl(new BoxCollisionShape(new Vector3f(10f, 0.1f, 5f)), 0));
    this.rootNode.attachChild(floor);
    this.getPhysicsSpace().add(floor);
}
 
Example #17
Source File: TestIssue877.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
Node createTestNode(float mass, Vector3f location) {
    float size = 0.1f;
    Vector3f halfExtents = new Vector3f(size, size, size);
    CollisionShape shape = new BoxCollisionShape(halfExtents);
    RigidBodyControl control = new RigidBodyControl(shape, mass);
    Node node = new Node();
    node.addControl(control);
    rootNode.attachChild(node);
    bulletAppState.getPhysicsSpace().add(node);
    control.setPhysicsLocation(location);

    return node;
}
 
Example #18
Source File: TestHoveringTank.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public void makeMissile() {
    Vector3f pos = spaceCraft.getWorldTranslation().clone();
    Quaternion rot = spaceCraft.getWorldRotation();
    Vector3f dir = rot.getRotationColumn(2);

    Spatial missile = assetManager.loadModel("Models/SpaceCraft/Rocket.mesh.xml");
    missile.scale(0.5f);
    missile.rotate(0, FastMath.PI, 0);
    missile.updateGeometricState();

    BoundingBox box = (BoundingBox) missile.getWorldBound();
    final Vector3f extent = box.getExtent(null);

    BoxCollisionShape boxShape = new BoxCollisionShape(extent);

    missile.setName("Missile");
    missile.rotate(rot);
    missile.setLocalTranslation(pos.addLocal(0, extent.y * 4.5f, 0));
    missile.setLocalRotation(hoverControl.getPhysicsRotation());
    missile.setShadowMode(ShadowMode.Cast);
    RigidBodyControl control = new BombControl(assetManager, boxShape, 20);
    control.setLinearVelocity(dir.mult(100));
    control.setCollisionGroup(PhysicsCollisionObject.COLLISION_GROUP_03);
    missile.addControl(control);


    rootNode.attachChild(missile);
    getPhysicsSpace().add(missile);
}
 
Example #19
Source File: TestIssue889.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public void simpleInitApp() {
    flyCam.setEnabled(false);

    BulletAppState bulletAppState = new BulletAppState();
    bulletAppState.setDebugEnabled(true);
    bulletAppState.setSpeed(0f);
    stateManager.attach(bulletAppState);
    PhysicsSpace space = bulletAppState.getPhysicsSpace();

    float radius = 1f;
    CollisionShape sphere = new SphereCollisionShape(radius);
    CollisionShape box = new BoxCollisionShape(Vector3f.UNIT_XYZ);

    RigidBodyControl rbc = new RigidBodyControl(box);
    rbc.setEnabled(false);
    rbc.setPhysicsSpace(space);
    rootNode.addControl(rbc);

    BetterCharacterControl bcc = new BetterCharacterControl(radius, 4f, 1f);
    bcc.setEnabled(false);
    bcc.setPhysicsSpace(space);
    rootNode.addControl(bcc);

    GhostControl gc = new GhostControl(sphere);
    gc.setEnabled(false);
    gc.setPhysicsSpace(space);
    rootNode.addControl(gc);
}
 
Example #20
Source File: ChildCollisionShape.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Serialize this shape, for example when saving to a J3O file.
 *
 * @param ex exporter (not null)
 * @throws IOException from exporter
 */
@Override
public void write(JmeExporter ex) throws IOException {
    OutputCapsule capsule = ex.getCapsule(this);
    capsule.write(location, "location", new Vector3f());
    capsule.write(rotation, "rotation", new Matrix3f());
    capsule.write(shape, "shape", new BoxCollisionShape(new Vector3f(1, 1, 1)));
}
 
Example #21
Source File: TestGhostObject.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private void initGhostObject() {
    Vector3f halfExtents = new Vector3f(3, 4.2f, 1);
    ghostControl = new GhostControl(new BoxCollisionShape(halfExtents));
    Node node=new Node("Ghost Object");
    node.addControl(ghostControl);
    rootNode.attachChild(node);
    getPhysicsSpace().add(ghostControl);
}
 
Example #22
Source File: TestGhostObject.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public void simpleInitApp() {
    bulletAppState = new BulletAppState();
    stateManager.attach(bulletAppState);
    bulletAppState.setDebugEnabled(true);

    // Mesh to be shared across several boxes.
    Box boxGeom = new Box(1f, 1f, 1f);
    // CollisionShape to be shared across several boxes.
    CollisionShape shape = new BoxCollisionShape(new Vector3f(1, 1, 1));

    Node physicsBox = PhysicsTestHelper.createPhysicsTestNode(assetManager, shape, 1);
    physicsBox.setName("box0");
    physicsBox.getControl(RigidBodyControl.class).setPhysicsLocation(new Vector3f(.6f, 4, .5f));
    rootNode.attachChild(physicsBox);
    getPhysicsSpace().add(physicsBox);

    Node physicsBox1 = PhysicsTestHelper.createPhysicsTestNode(assetManager, shape, 1);
    physicsBox1.setName("box1");
    physicsBox1.getControl(RigidBodyControl.class).setPhysicsLocation(new Vector3f(0, 40, 0));
    rootNode.attachChild(physicsBox1);
    getPhysicsSpace().add(physicsBox1);

    Node physicsBox2 = PhysicsTestHelper.createPhysicsTestNode(assetManager, new BoxCollisionShape(new Vector3f(1, 1, 1)), 1);
    physicsBox2.setName("box0");
    physicsBox2.getControl(RigidBodyControl.class).setPhysicsLocation(new Vector3f(.5f, 80, -.8f));
    rootNode.attachChild(physicsBox2);
    getPhysicsSpace().add(physicsBox2);

    // the floor, does not move (mass=0)
    Node node = PhysicsTestHelper.createPhysicsTestNode(assetManager, new BoxCollisionShape(new Vector3f(100, 1, 100)), 0);
    node.setName("floor");
    node.getControl(RigidBodyControl.class).setPhysicsLocation(new Vector3f(0f, -6, 0f));
    rootNode.attachChild(node);
    getPhysicsSpace().add(node);

    initGhostObject();
}
 
Example #23
Source File: TestCcd.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public void simpleInitApp() {
    bulletAppState = new BulletAppState();
    stateManager.attach(bulletAppState);
    bulletAppState.setDebugEnabled(true);
    bullet = new Sphere(32, 32, 0.4f, true, false);
    bullet.setTextureMode(TextureMode.Projected);
    bulletCollisionShape = new SphereCollisionShape(0.1f);
    setupKeys();

    mat = new Material(getAssetManager(), "Common/MatDefs/Misc/Unshaded.j3md");
    mat.getAdditionalRenderState().setWireframe(true);
    mat.setColor("Color", ColorRGBA.Green);

    mat2 = new Material(getAssetManager(), "Common/MatDefs/Misc/Unshaded.j3md");
    mat2.getAdditionalRenderState().setWireframe(true);
    mat2.setColor("Color", ColorRGBA.Red);

    // An obstacle mesh, does not move (mass=0)
    Node node2 = new Node();
    node2.setName("mesh");
    node2.setLocalTranslation(new Vector3f(2.5f, 0, 0f));
    node2.addControl(new RigidBodyControl(new MeshCollisionShape(new Box(4, 4, 0.1f)), 0));
    rootNode.attachChild(node2);
    getPhysicsSpace().add(node2);

    // The floor, does not move (mass=0)
    Node node3 = new Node();
    node3.setLocalTranslation(new Vector3f(0f, -6, 0f));
    node3.addControl(new RigidBodyControl(new BoxCollisionShape(new Vector3f(100, 1, 100)), 0));
    rootNode.attachChild(node3);
    getPhysicsSpace().add(node3);

}
 
Example #24
Source File: TestPhysicsReadWrite.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 4 votes vote down vote up
@Override
public void simpleInitApp() {
    bulletAppState = new BulletAppState();
    stateManager.attach(bulletAppState);
    bulletAppState.getPhysicsSpace().enableDebug(assetManager);
    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);
    }

}
 
Example #25
Source File: BoxCollisionShapeTreeNode.java    From jmonkeybuilder with Apache License 2.0 4 votes vote down vote up
public BoxCollisionShapeTreeNode(@NotNull final BoxCollisionShape element, final long objectId) {
    super(element, objectId);
}
 
Example #26
Source File: TestBatchedTower.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 4 votes vote down vote up
@Override
public void simpleInitApp() {
    bulletAppState = new BulletAppState();
    bulletAppState.setThreadingType(BulletAppState.ThreadingType.PARALLEL);
   // bulletAppState.setEnabled(false);
    stateManager.attach(bulletAppState);
    bullet = new Sphere(32, 32, 0.4f, true, false);
    bullet.setTextureMode(TextureMode.Projected);
    bulletCollisionShape = new SphereCollisionShape(0.4f);

    brick = new Box(Vector3f.ZERO, brickWidth, brickHeight, brickDepth);
    brick.scaleTextureCoordinates(new Vector2f(1f, .5f));
  //  bulletAppState.getPhysicsSpace().enableDebug(assetManager);
    initMaterial();
    initTower();
    initFloor();
    initCrossHairs();

    List<BatchedGeometry> bgs = batch.batch(geoms);
    for (BatchedGeometry bg : bgs) {
        RigidBodyControl rb = new RigidBodyControl(1.5f);
        rb.setCollisionShape(new BoxCollisionShape(new Vector3f(brick.getXExtent(), brick.getYExtent(), brick.getZExtent())));
        rb.setFriction(1.6f);
        bg.addControl(rb);
        this.getPhysicsSpace().add(bg);
    }

    batch.setMaterial(mat);
    batch.setShadowMode(ShadowMode.CastAndReceive);

    this.cam.setLocation(new Vector3f(0, 25f, 8f));
    cam.lookAt(Vector3f.ZERO, new Vector3f(0, 1, 0));
    cam.setFrustumFar(80);
    inputManager.addMapping("shoot", new MouseButtonTrigger(MouseInput.BUTTON_LEFT));
    inputManager.addListener(actionListener, "shoot");
    rootNode.setShadowMode(ShadowMode.Off);
    bsr = new PssmShadowRenderer(assetManager, 1024, 2);
    bsr.setDirection(new Vector3f(-1, -1, -1).normalizeLocal());
    bsr.setLambda(0.50f);
    bsr.setShadowIntensity(0.6f);
    bsr.setCompareMode(CompareMode.Hardware);
    bsr.setFilterMode(FilterMode.PCF4);
    viewPort.addProcessor(bsr);
    flyCam.setMoveSpeed(50);
    rootNode.attachChild(batch);
}
 
Example #27
Source File: CreateVehicleControlAction.java    From jmonkeybuilder with Apache License 2.0 4 votes vote down vote up
@Override
@FxThread
protected @NotNull Control createControl(@NotNull final Spatial parent) {
    return new VehicleControl(new BoxCollisionShape(new Vector3f(1F, 1F, 1F)), 1F);
}
 
Example #28
Source File: CreateBoxCollisionShapeAction.java    From jmonkeybuilder with Apache License 2.0 4 votes vote down vote up
@Override
@FxThread
protected @NotNull CollisionShape createShape(@NotNull final VarTable vars) {
    final Vector3f halfExtents = vars.get(PROPERTY_HALF_EXTENTS);
    return new BoxCollisionShape(halfExtents);
}
 
Example #29
Source File: TestLocalPhysics.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 4 votes vote down vote up
@Override
    public void simpleInitApp() {
        bulletAppState = new BulletAppState();
        stateManager.attach(bulletAppState);
        bulletAppState.getPhysicsSpace().enableDebug(assetManager);

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

    }
 
Example #30
Source File: TestPhysicsCar.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
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, 400);
    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
    float stiffness = 60.0f;//200=f1 car
    float compValue = .3f; //(should be lower than damp)
    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
    Vector3f wheelDirection = new Vector3f(0, -1, 0); // was 0, -1, 0
    Vector3f wheelAxle = new Vector3f(-1, 0, 0); // was -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);
}