Java Code Examples for com.jme3.bullet.control.RigidBodyControl

The following examples show how to use com.jme3.bullet.control.RigidBodyControl. These examples are extracted from open source projects. 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
/** This method creates one individual physical cannon ball.
 * By defaul, the ball is accelerated and flies
 * from the camera position in the camera direction.*/
 public void makeCannonBall() {
  /** Create a cannon ball geometry and attach to scene graph. */
  Geometry ball_geo = new Geometry("cannon ball", sphere);
  ball_geo.setMaterial(stone_mat);
  rootNode.attachChild(ball_geo);
  /** Position the cannon ball  */
  ball_geo.setLocalTranslation(cam.getLocation());
  /** Make the ball physical with a mass > 0.0f */
  ball_phy = new RigidBodyControl(1f);
  /** Add physical ball to physics space. */
  ball_geo.addControl(ball_phy);
  bulletAppState.getPhysicsSpace().add(ball_phy);
  /** Accelerate the physical ball to shoot it. */
  ball_phy.setLinearVelocity(cam.getDirection().mult(25));
}
 
Example 2
private void initializeNewTest() {
    bulletAppState = new BulletAppState();
    bulletAppState.setDebugEnabled(physicsDebug);
    stateManager.attach(bulletAppState);

    bulletAppState.setSpeed(bulletSpeed);

    dropTest();

    Geometry leftFloor = PhysicsTestHelper.createMeshTestFloor(assetManager, 20, new Vector3f(-11, -5, -10));
    addObject(leftFloor);

    //Hide physics debug visualization for floors
    if (physicsDebug) {
        BulletDebugAppState bulletDebugAppState = stateManager.getState(BulletDebugAppState.class);
        bulletDebugAppState.setFilter((Object obj) -> {
            return !(obj.equals(leftFloor.getControl(RigidBodyControl.class)));
        });
    }
}
 
Example 3
@Override
public void simpleInitApp() {
    stateManager.attach(bulletAppState);
    initCrossHair();

    Spatial s = assetManager.loadModel("Models/Elephant/Elephant.mesh.xml");
    s.setLocalScale(0.1f);

    CollisionShape collisionShape = CollisionShapeFactory.createMeshShape(s);
    Node n = new Node("elephant");
    n.addControl(new RigidBodyControl(collisionShape, 1));
    n.getControl(RigidBodyControl.class).setKinematic(true);
    bulletAppState.getPhysicsSpace().add(n);
    rootNode.attachChild(n);
    bulletAppState.setDebugEnabled(true);
}
 
Example 4
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 5
protected void attachPhysicsSelection(Spatial geom) {
    PhysicsCollisionObject control = geom.getControl(RigidBodyControl.class);
    if (control == null) {
        control = geom.getControl(VehicleControl.class);
    }
    if (control == null) {
        control = geom.getControl(GhostControl.class);
    }
    if (control == null) {
        control = geom.getControl(CharacterControl.class);
    }
    if (control == null) {
        return;
    }
    Spatial selectionGeometry = DebugShapeFactory.getDebugShape(control.getCollisionShape());
    if (selectionGeometry != null) {
        selectionGeometry.setMaterial(blueMat);
        selectionGeometry.setLocalTransform(geom.getWorldTransform());
        toolsNode.attachChild(selectionGeometry);
        selectionShape = selectionGeometry;
    }
}
 
Example 6
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 7
@Override
        public void onAction(String name, boolean keyPressed, float tpf) {
            if (name.equals("shoot") && !keyPressed) {
                Geometry bulletg = new Geometry("bullet", bullet);
                bulletg.setMaterial(mat2);
                bulletg.setShadowMode(ShadowMode.CastAndReceive);
                bulletg.setLocalTranslation(cam.getLocation());
                
                SphereCollisionShape bulletCollisionShape = new SphereCollisionShape(0.4f);
                RigidBodyControl bulletNode = new BombControl(assetManager, bulletCollisionShape, 1);
//                RigidBodyControl bulletNode = new RigidBodyControl(bulletCollisionShape, 1);
                bulletNode.setLinearVelocity(cam.getDirection().mult(25));
                bulletg.addControl(bulletNode);
                rootNode.attachChild(bulletg);
                getPhysicsSpace().add(bulletNode);
            }
            if (name.equals("gc") && !keyPressed) {
                System.gc();
            }
        }
 
Example 8
public void setupFloor() {
    Material mat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
    TextureKey key = new TextureKey("Interface/Logo/Monkey.jpg", true);
    key.setGenerateMips(true);
    Texture tex = assetManager.loadTexture(key);
    tex.setMinFilter(Texture.MinFilter.Trilinear);
    mat.setTexture("ColorMap", tex);

    Box floor = new Box(100, 1f, 100);
    Geometry floorGeom = new Geometry("Floor", floor);
    floorGeom.setMaterial(mat);
    floorGeom.setLocalTranslation(new Vector3f(0f, -3, 0f));

    floorGeom.addControl(new RigidBodyControl(new MeshCollisionShape(floorGeom.getMesh()), 0));
    rootNode.attachChild(floorGeom);
    getPhysicsSpace().add(floorGeom);
}
 
Example 9
private RigidBodyControl drop(Vector3f offset, String model, float scale, float mass) {
    scale *= scaleMod;
    Node n = (Node) assetManager.loadModel(model);
    n.setLocalTranslation(offset);
    n.rotate(0, 0, -FastMath.HALF_PI);

    Geometry tp = ((Geometry) n.getChild(0));
    tp.scale(scale);
    Material mat = new Material(assetManager, "Common/MatDefs/Light/Lighting.j3md");
    tp.setMaterial(mat);

    Mesh mesh = tp.getMesh();
    GImpactCollisionShape shape = new GImpactCollisionShape(mesh);
    shape.setScale(new Vector3f(scale, scale, scale));

    RigidBodyControl control = new RigidBodyControl(shape, mass);
    n.addControl(control);
    addObject(n);
    return control;
}
 
Example 10
private Geometry getRandomBall(Vector3f location) {
    Vector3f localLocation = new Vector3f();
    localLocation.set(location);
    localLocation.addLocal(new Vector3f(random.nextFloat() - 0.5f, random.nextFloat() - 0.5f, random.nextFloat() - 0.5f).normalize().mult(3));
    Geometry poiGeom = new Geometry("ball", ballMesh);
    poiGeom.setLocalTranslation(localLocation);
    poiGeom.setMaterial(ballMaterial);
    RigidBodyControl control = new RigidBodyControl(ballCollisionShape, 1);
    //!!! Important
    control.setApplyPhysicsLocal(true);
    poiGeom.addControl(control);
    float x = (random.nextFloat() - 0.5f) * 100;
    float y = (random.nextFloat() - 0.5f) * 100;
    float z = (random.nextFloat() - 0.5f) * 100;
    control.setLinearVelocity(new Vector3f(x, y, z));
    return poiGeom;
}
 
Example 11
/**
 * Add all collision objects and joints in the specified subtree of the
 * scene graph to this space (e.g. after loading from disk). Note:
 * recursive!
 *
 * @param spatial the root of the subtree (not null)
 */
public void addAll(Spatial spatial) {
    add(spatial);

    if (spatial.getControl(RigidBodyControl.class) != null) {
        RigidBodyControl physicsNode = spatial.getControl(RigidBodyControl.class);
        //add joints with physicsNode as BodyA
        List<PhysicsJoint> joints = physicsNode.getJoints();
        for (Iterator<PhysicsJoint> it1 = joints.iterator(); it1.hasNext();) {
            PhysicsJoint physicsJoint = it1.next();
            if (physicsNode.equals(physicsJoint.getBodyA())) {
                //add(physicsJoint.getBodyB());
                add(physicsJoint);
            }
        }
    }
    //recursion
    if (spatial instanceof Node) {
        List<Spatial> children = ((Node) spatial).getChildren();
        for (Iterator<Spatial> it = children.iterator(); it.hasNext();) {
            Spatial spat = it.next();
            addAll(spat);
        }
    }
}
 
Example 12
/**
 * Remove all physics controls and joints in the specified subtree of the
 * scene graph from the physics space (e.g. before saving to disk) Note:
 * recursive!
 *
 * @param spatial the root of the subtree (not null)
 */
public void removeAll(Spatial spatial) {
    if (spatial.getControl(RigidBodyControl.class) != null) {
        RigidBodyControl physicsNode = spatial.getControl(RigidBodyControl.class);
        //remove joints with physicsNode as BodyA
        List<PhysicsJoint> joints = physicsNode.getJoints();
        for (Iterator<PhysicsJoint> it1 = joints.iterator(); it1.hasNext();) {
            PhysicsJoint physicsJoint = it1.next();
            if (physicsNode.equals(physicsJoint.getBodyA())) {
                removeJoint(physicsJoint);
                //remove(physicsJoint.getBodyB());
            }
        }
    }
        
    remove(spatial);
    //recursion
    if (spatial instanceof Node) {
        List<Spatial> children = ((Node) spatial).getChildren();
        for (Iterator<Spatial> it = children.iterator(); it.hasNext();) {
            Spatial spat = it.next();
            removeAll(spat);
        }
    }
}
 
Example 13
/**
 * Removes all physics controls and joints in the given spatial from the physics space
 * (e.g. before saving to disk) - recursive if node
 * @param spatial the rootnode containing the physics objects
 */
public void removeAll(Spatial spatial) {
    if (spatial.getControl(RigidBodyControl.class) != null) {
        RigidBodyControl physicsNode = spatial.getControl(RigidBodyControl.class);
        //remove joints with physicsNode as BodyA
        List<PhysicsJoint> joints = physicsNode.getJoints();
        for (Iterator<PhysicsJoint> it1 = joints.iterator(); it1.hasNext();) {
            PhysicsJoint physicsJoint = it1.next();
            if (physicsNode.equals(physicsJoint.getBodyA())) {
                removeJoint(physicsJoint);
                //remove(physicsJoint.getBodyB());
            }
        }
    }
    
    remove(spatial);
    //recursion
    if (spatial instanceof Node) {
        List<Spatial> children = ((Node) spatial).getChildren();
        for (Iterator<Spatial> it = children.iterator(); it.hasNext();) {
            Spatial spat = it.next();
            removeAll(spat);
        }
    }
}
 
Example 14
public void onAction(String name, boolean keyPressed, float tpf) {
            if (name.equals("shoot") && !keyPressed) {
                Geometry bulletg = new Geometry("bullet", bullet);
                bulletg.setMaterial(mat2);
                bulletg.setShadowMode(ShadowMode.CastAndReceive);
                bulletg.setLocalTranslation(cam.getLocation());
                
                SphereCollisionShape bulletCollisionShape = new SphereCollisionShape(0.4f);
                RigidBodyControl bulletNode = new BombControl(assetManager, bulletCollisionShape, 1);
//                RigidBodyControl bulletNode = new RigidBodyControl(bulletCollisionShape, 1);
                bulletNode.setLinearVelocity(cam.getDirection().mult(25));
                bulletg.addControl(bulletNode);
                rootNode.attachChild(bulletg);
                getPhysicsSpace().add(bulletNode);
            }
            if (name.equals("gc") && !keyPressed) {
                System.gc();
            }
        }
 
Example 15
/** This method creates one individual physical cannon ball.
 * By defaul, the ball is accelerated and flies
 * from the camera position in the camera direction.*/
 public void makeCannonBall() {
  /** Create a cannon ball geometry and attach to scene graph. */
  Geometry ball_geo = new Geometry("cannon ball", sphere);
  ball_geo.setMaterial(stone_mat);
  rootNode.attachChild(ball_geo);
  /** Position the cannon ball  */
  ball_geo.setLocalTranslation(cam.getLocation());
  /** Make the ball physcial with a mass > 0.0f */
  ball_phy = new RigidBodyControl(1f);
  /** Add physical ball to physics space. */
  ball_geo.addControl(ball_phy);
  bulletAppState.getPhysicsSpace().add(ball_phy);
  /** Accelerate the physcial ball to shoot it. */
  ball_phy.setLinearVelocity(cam.getDirection().mult(25));
}
 
Example 16
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, 0);
    floor.addControl(new RigidBodyControl(0));
    this.rootNode.attachChild(floor);
    this.getPhysicsSpace().add(floor);
}
 
Example 17
Source Project: jmonkeybuilder   Source File: RigidBodyControlTreeNode.java    License: Apache License 2.0 5 votes vote down vote up
@Override
@FxThread
public @Nullable Image getIcon() {

    final RigidBodyControl element = getElement();

    if (element.getMass() == 0F) {
        return Icons.STATIC_RIGID_BODY_16;
    }

    return Icons.RIGID_BODY_16;
}
 
Example 18
@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));
    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(Vector3f.ZERO, 100f, 0.2f, 100f)), 0);
    node3.getControl(RigidBodyControl.class).setPhysicsLocation(new Vector3f(0f, -6, 0f));
    rootNode.attachChild(node3);
    getPhysicsSpace().add(node3);
}
 
Example 19
Source Project: jmonkeybuilder   Source File: RigidBodyControlTreeNode.java    License: Apache License 2.0 5 votes vote down vote up
@Override
@FxThread
public void fillContextMenu(@NotNull final NodeTree<?> nodeTree,
                            @NotNull final ObservableList<MenuItem> items) {

    final RigidBodyControl element = getElement();

    if (!element.isActive()) {
        items.add(new ReactivatePhysicsControlAction(nodeTree, this));
    }

    super.fillContextMenu(nodeTree, items);
}
 
Example 20
public void addBrick(Vector3f ori) {
    Geometry reBoxg = new Geometry("brick", brick);
    reBoxg.setMaterial(mat);
    reBoxg.setLocalTranslation(ori);
    reBoxg.rotate(0f, (float)Math.toRadians(angle) , 0f );
    reBoxg.addControl(new RigidBodyControl(1.5f));
    reBoxg.setShadowMode(ShadowMode.CastAndReceive);
    reBoxg.getControl(RigidBodyControl.class).setFriction(1.6f);
    this.rootNode.attachChild(reBoxg);
    this.getPhysicsSpace().add(reBoxg);
}
 
Example 21
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, 0);
    floor.addControl(new RigidBodyControl(0));
    this.rootNode.attachChild(floor);
    this.getPhysicsSpace().add(floor);
}
 
Example 22
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 23
/** Make a solid floor and add it to the scene. */
public void initFloor() {
  Geometry floor_geo = new Geometry("Floor", floor);
  floor_geo.setMaterial(floor_mat);
  floor_geo.setLocalTranslation(0, -0.1f, 0);
  this.rootNode.attachChild(floor_geo);
  /* Make the floor physical with mass 0.0f! */
  floor_phy = new RigidBodyControl(0.0f);
  floor_geo.addControl(floor_phy);
  bulletAppState.getPhysicsSpace().add(floor_phy);
}
 
Example 24
/** This method creates one individual physical brick. */
public void makeBrick(Vector3f loc) {
  /** Create a brick geometry and attach to scene graph. */
  Geometry brick_geo = new Geometry("brick", box);
  brick_geo.setMaterial(wall_mat);
  rootNode.attachChild(brick_geo);
  /** Position the brick geometry  */
  brick_geo.setLocalTranslation(loc);
  /** Make brick physical with a mass > 0.0f. */
  brick_phy = new RigidBodyControl(2f);
  /** Add physical brick to physics space. */
  brick_geo.addControl(brick_phy);
  bulletAppState.getPhysicsSpace().add(brick_phy);
}
 
Example 25
private void attachTestObject(Mesh mesh, Vector3f position, float mass) {
    Material material = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
    Geometry g = new Geometry("mesh", mesh);
    g.setLocalTranslation(position);
    g.setMaterial(material);

    RigidBodyControl control = new RigidBodyControl(new GImpactCollisionShape(mesh), mass);
    g.addControl(control);
    addObject(g);
}
 
Example 26
@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 27
@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);
}
 
Example 28
private void initWall(float bLength, float bWidth, float bHeight) {
    Box brick = new Box(bLength, bHeight, bWidth);
    brick.scaleTextureCoordinates(new Vector2f(1f, 0.5f));
    Material mat2 = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
    TextureKey key = new TextureKey("Textures/Terrain/BrickWall/BrickWall.jpg");
    key.setGenerateMips(true);
    Texture tex = assetManager.loadTexture(key);
    mat2.setTexture("ColorMap", tex);

    float startpt = bLength / 4f;
    float height = -5f;
    for (int j = 0; j < 15; j++) {
        for (int i = 0; i < 4; i++) {
            Vector3f ori = new Vector3f(i * bLength * 2f + startpt, bHeight + height, -10f);
            Geometry reBoxg = new Geometry("brick", brick);
            reBoxg.setMaterial(mat2);
            reBoxg.setLocalTranslation(ori);
            //for geometry with sphere mesh the physics system automatically uses a sphere collision shape
            reBoxg.addControl(new RigidBodyControl(1.5f));
            reBoxg.setShadowMode(ShadowMode.CastAndReceive);
            reBoxg.getControl(RigidBodyControl.class).setFriction(0.6f);
            this.rootNode.attachChild(reBoxg);
            physicsSpace.add(reBoxg);
        }
        startpt = -startpt;
        height += 2f * bHeight;
    }
}
 
Example 29
public void onAction(String name, boolean keyPressed, float tpf) {
            if (name.equals("shoot") && !keyPressed) {
                Geometry bulletg = new Geometry("bullet", bullet);
                bulletg.setMaterial(mat2);
                bulletg.setShadowMode(ShadowMode.CastAndReceive);
                bulletg.setLocalTranslation(cam.getLocation());
                RigidBodyControl bulletNode = new BombControl(assetManager, bulletCollisionShape, 1);
//                RigidBodyControl bulletNode = new RigidBodyControl(bulletCollisionShape, 1);
                bulletNode.setLinearVelocity(cam.getDirection().mult(25));
                bulletg.addControl(bulletNode);
                rootNode.attachChild(bulletg);
                getPhysicsSpace().add(bulletNode);
            }
        }
 
Example 30
@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);

}