RigidBodyControl can either be static or dynamic, depending on mass.
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgr1R8FCf5zLNx5zrxcEHif4XPJK_8PKIzrvZV3Qi6Ptpg55FyohYl_oIJ5Sy0KAv4pG4pH6j-0lrb4E6iONDQADHlr_kKF_g5-vA56grXCDrIhELGwyfGwFQ-UeBdkoSDXfHU2DTFA35R-/s1600/java51+-+01.png)
The camera is moved and rotated, so it as y = 20, pointing to origin.
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgXfLUXEFKr75QDkxSAVHJv7dYMQQBoAt-KXywlBs2fdkZMXzGvZrlH0Pl3VR_VxZf31NGyhsIAVLf6pprA5JytnhG0gqOErqU5ezrf_PEMqsiNMbQccMIg_xLf-I2T5WSZlbYG9IaChGDL/s1600/java51+-+02.png)
// *** 1. Start (Camera +y looking down y) cam.setAxes(new Vector3f(-1,0,0), // left new Vector3f(0,0,-1), // up new Vector3f(0,-1,0)); // dir cam.setLocation(new Vector3f(0,20,0)); // above ground cam.update(); // *** 1. End
The Application has to use BulletAppState object to enable physics.
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhPKhLM9MuV7HUV9r-dphd00djZp0McRoEw6Ak_52dqUdI18Bia2RuDk5GtIOF32W0YPPOAO8K_0z2vKQY6ADJdMDmFRCTAWUDHeRsDSN4uxuW4IpAHGe6BrCWwLLuHuw5X7hmgyzhsDTqd/s1600/java51+-+03.png)
// *** 2. Start (Set Physics) bulletAppState = new BulletAppState(); stateManager.attach(bulletAppState); // *** 2. End
The floor is a static Box mesh object.
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjjzfzxCk-zIPewI5SAAMM8qay24okEXlIbkvx4l4V327MnP_OEioQZFW5e4dOrp7V3vsuTXV9CvZC3dL7H2gfA8zlFU9SUcHhAepOclC5F59ze_7y2sPYPrbzW0ilsQdC0YEP_-WnKaVRp/s1600/java51+-+04.png)
// *** 3. Start (Floor) Box bFloor = new Box(5, 1, 5); Geometry geomFloor = new Geometry("Floor", bFloor); Material matFloor = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md"); matFloor.setColor("Color", ColorRGBA.Blue); geomFloor.setMaterial(matFloor); rootNode.attachChild(geomFloor); RigidBodyControl floorPhy = new RigidBodyControl(0.0f); geomFloor.addControl(floorPhy); bulletAppState.getPhysicsSpace().add(floorPhy); // *** 3. End
The North Wall is also a static Box mesh object.
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEioU04xnK9iGn4zsUbJKh004c49SLWISz3tm9yOOZ4fa9vyaC6tcCFDizUD4qGIpPL9gLItP5M-QofbC6ymgJwBFxHmywTMs-p28oPxjMCFTStMWe0NYY8WllzI4DhNS54ZdLRLNC0xjtku/s1600/java51+-+05.png)
// *** 4. Start (North Wall) Box bWallN = new Box(5, 1, 1); Geometry geomWallN = new Geometry("WallN", bWallN); Material matWallN = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md"); matWallN.setColor("Color", ColorRGBA.Red); geomWallN.setLocalTranslation(0, 1, -6); geomWallN.setMaterial(matWallN); rootNode.attachChild(geomWallN); RigidBodyControl wallNPhy = new RigidBodyControl(0.0f); geomWallN.addControl(wallNPhy); bulletAppState.getPhysicsSpace().add(wallNPhy); // *** 4. End
The South Wall is also a static Box mesh object.
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgvYzLmma8JxSzjo_Szb9-wJKzCJimVHBY3_Ij-Iuj3gRcbjyBB3pFG1YD5uwXnI0hhXKV8ig6FZweVOv4hEJU5j_Ao-LGLE5NrjTBdx13olUjkJZo4xmdiO2UoSoEZszJWCnYK3JJKvqUx/s1600/java51+-+06.png)
// *** 5. Start (South Wall) Box bWallS = new Box(5, 1, 1); Geometry geomWallS = new Geometry("WallS", bWallS); Material matWallS = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md"); matWallS.setColor("Color", ColorRGBA.Yellow); geomWallS.setLocalTranslation(0, 1, 6); geomWallS.setMaterial(matWallS); rootNode.attachChild(geomWallS); RigidBodyControl wallSPhy = new RigidBodyControl(0.0f); geomWallS.addControl(wallSPhy); bulletAppState.getPhysicsSpace().add(wallSPhy); // *** 5. End
The Worth Wall is also a static Box mesh object.
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhmxrHz3L8ut3gnAmc_z-HMIelz97W5VN7wWJ0WufXC4mgkepdUHQzfTQLta3dNDSya-FT-d2G9h5yPXalWsaSEbrXxVi10UIc_Cc83e-y8Wl7jbDLzv-UIS5ASO6fysg4sQCg7UsLWlt6k/s1600/java51+-+07.png)
// *** 6. Start (West Wall) Box bWallW = new Box(1, 1, 5); Geometry geomWallW = new Geometry("WallW", bWallW); Material matWallW = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md"); matWallW.setColor("Color", ColorRGBA.Green); geomWallW.setLocalTranslation(-6, 1, 0); geomWallW.setMaterial(matWallW); rootNode.attachChild(geomWallW); RigidBodyControl wallWPhy = new RigidBodyControl(0.0f); geomWallW.addControl(wallWPhy); bulletAppState.getPhysicsSpace().add(wallWPhy); // *** 6. End
Finally, the East Wall is also a static Box mesh object.
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgjbCiwgfeqL7z52ZwNSb8OjfNZSoyoHV9FQRQ1ArDYnxtjc0h5CF-ZYJbqjdvE7OHNSGVsLGNIcA386p0fPM0ZBldurpxAXzDVR1wB5BRfckejfi447GNqbbD_D7fq__SzIZydVGOlMala/s1600/java51+-+08.png)
// *** 7. Start (East Wall) Box bWallE = new Box(1, 1, 5); Geometry geomWallE = new Geometry("WallE", bWallE); Material matWallE = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md"); matWallE.setColor("Color", ColorRGBA.Magenta); geomWallE.setLocalTranslation(6, 1, 0); geomWallE.setMaterial(matWallE); rootNode.attachChild(geomWallE); RigidBodyControl wallEPhy = new RigidBodyControl(0.0f); geomWallE.addControl(wallEPhy); bulletAppState.getPhysicsSpace().add(wallEPhy); // *** 7. End
ball1 is a Sphere mesh object and is a BetterCharacterControl object.
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg0wIEoAz6WHmifaCGVjOH7GBMP-HU1AxBR9YInIkcbkXc9Yl9sgHAWIUgn2QHASZy6SVO0dXShx7jeZz5mUM_DmnYpfpol2TAsOZPrypTA_eBWM9rgalG33m8R9iy9agp7Kz-nJGdryA7I/s1600/java51+-+09.png)
// *** 8. Start (ball1 - Player) Sphere ball1 = new Sphere (16,16,0.5f); geomBall1 = new Geometry("ball1", ball1); Material matBall1 = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md"); matBall1.setColor("Color", ColorRGBA.Cyan); geomBall1.setLocalTranslation(0, 2, 0); geomBall1.setMaterial(matBall1); rootNode.attachChild(geomBall1); ball1Phy = new BetterCharacterControl(0.5f,5f,30f); geomBall1.addControl(ball1Phy); bulletAppState.getPhysicsSpace().add(ball1Phy); // *** 8. End
ball2 is a Sphere mesh object. It is RigidBodyControl with nonzero mass.
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhcJvNOEZ1ldNAdLbXlztKBm8uwxg9CD_JA9v7EFd-j0MDFAC_hhQSnJ6CLOSCo7iOmHImSp6RZ4-81CnBxcgBbzmwQLU5Mphfbm_gSiBEr_-d1SlDVqn2mJxnI5yMAm19jE441j2_fVZwv/s1600/java51+-+10.png)
// *** 9. Start (ball2 - Can move) Sphere ball2 = new Sphere (16,16,0.5f); Geometry geomBall2 = new Geometry("ball2", ball2); Material matBall2 = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md"); matBall2.setColor("Color", ColorRGBA.Orange); geomBall2.setLocalTranslation(2, 2, 1.5f); geomBall2.setMaterial(matBall2); rootNode.attachChild(geomBall2); RigidBodyControl ball2Phy = new RigidBodyControl(1); geomBall2.addControl(ball2Phy); bulletAppState.getPhysicsSpace().add(ball2Phy); // *** 9. End
ball3 is a Sphere mesh object. It is RigidBodyControl with nonzero mass.
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhbgIhfh-Ls-maL3xDYQh0BLmwRsVhid-DJ9ox5kN4DdKNLMY0kon8G6aa3HpodyFNvGkm2Qr2oVetWvVE3gExTajw0rulPAPY0BvPCRx-ngDVfCsOTN3-WPuSAXzKho8W9v3JtXoLEDH6C/s1600/java51+-+11.png)
// *** 10. Start (ball2 - Can move) Sphere ball3 = new Sphere (16,16,0.5f); Geometry geomBall3 = new Geometry("ball3", ball3); Material matBall3 = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md"); matBall3.setColor("Color", ColorRGBA.Pink); geomBall3.setLocalTranslation(-2, 2, -1.5f); geomBall3.setMaterial(matBall3); rootNode.attachChild(geomBall3); RigidBodyControl ball3Phy = new RigidBodyControl(1); geomBall3.addControl(ball3Phy); bulletAppState.getPhysicsSpace().add(ball3Phy); // *** 10. End
// JMonkey51.java package mygame; import com.jme3.app.SimpleApplication; import com.jme3.bullet.BulletAppState; import com.jme3.bullet.control.BetterCharacterControl; import com.jme3.bullet.control.RigidBodyControl; import com.jme3.input.KeyInput; import com.jme3.input.controls.ActionListener; import com.jme3.input.controls.KeyTrigger; import com.jme3.material.Material; import com.jme3.math.ColorRGBA; import com.jme3.math.Vector3f; import com.jme3.renderer.RenderManager; import com.jme3.scene.Geometry; import com.jme3.scene.shape.Box; import com.jme3.scene.shape.Sphere; public class JMonkey51 extends SimpleApplication { public static void main(String[] args) { JMonkey51 app = new JMonkey51(); app.start(); } Geometry geomBall1; // Player Geometry private BulletAppState bulletAppState; // Physics private BetterCharacterControl ball1Phy; // Player private Vector3f walkDirection = new Vector3f(); private boolean left = false, right = false, up = false, down = false; @Override public void simpleInitApp() { flyCam.setEnabled(false); viewPort.setBackgroundColor(ColorRGBA.LightGray); setDisplayFps(false); setDisplayStatView(false); // *** 1. Start (Camera +y looking down y) cam.setAxes(new Vector3f(-1,0,0), // left new Vector3f(0,0,-1), // up new Vector3f(0,-1,0)); // dir cam.setLocation(new Vector3f(0,20,0)); // above ground cam.update(); // *** 1. End // *** 2. Start (Set Physics) bulletAppState = new BulletAppState(); stateManager.attach(bulletAppState); // *** 2. End // *** 3. Start (Floor) Box bFloor = new Box(5, 1, 5); Geometry geomFloor = new Geometry("Floor", bFloor); Material matFloor = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md"); matFloor.setColor("Color", ColorRGBA.Blue); geomFloor.setMaterial(matFloor); rootNode.attachChild(geomFloor); RigidBodyControl floorPhy = new RigidBodyControl(0.0f); geomFloor.addControl(floorPhy); bulletAppState.getPhysicsSpace().add(floorPhy); // *** 3. End // *** 4. Start (North Wall) Box bWallN = new Box(5, 1, 1); Geometry geomWallN = new Geometry("WallN", bWallN); Material matWallN = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md"); matWallN.setColor("Color", ColorRGBA.Red); geomWallN.setLocalTranslation(0, 1, -6); geomWallN.setMaterial(matWallN); rootNode.attachChild(geomWallN); RigidBodyControl wallNPhy = new RigidBodyControl(0.0f); geomWallN.addControl(wallNPhy); bulletAppState.getPhysicsSpace().add(wallNPhy); // *** 4. End // *** 5. Start (South Wall) Box bWallS = new Box(5, 1, 1); Geometry geomWallS = new Geometry("WallS", bWallS); Material matWallS = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md"); matWallS.setColor("Color", ColorRGBA.Yellow); geomWallS.setLocalTranslation(0, 1, 6); geomWallS.setMaterial(matWallS); rootNode.attachChild(geomWallS); RigidBodyControl wallSPhy = new RigidBodyControl(0.0f); geomWallS.addControl(wallSPhy); bulletAppState.getPhysicsSpace().add(wallSPhy); // *** 5. End // *** 6. Start (West Wall) Box bWallW = new Box(1, 1, 5); Geometry geomWallW = new Geometry("WallW", bWallW); Material matWallW = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md"); matWallW.setColor("Color", ColorRGBA.Green); geomWallW.setLocalTranslation(-6, 1, 0); geomWallW.setMaterial(matWallW); rootNode.attachChild(geomWallW); RigidBodyControl wallWPhy = new RigidBodyControl(0.0f); geomWallW.addControl(wallWPhy); bulletAppState.getPhysicsSpace().add(wallWPhy); // *** 6. End // *** 7. Start (East Wall) Box bWallE = new Box(1, 1, 5); Geometry geomWallE = new Geometry("WallE", bWallE); Material matWallE = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md"); matWallE.setColor("Color", ColorRGBA.Magenta); geomWallE.setLocalTranslation(6, 1, 0); geomWallE.setMaterial(matWallE); rootNode.attachChild(geomWallE); RigidBodyControl wallEPhy = new RigidBodyControl(0.0f); geomWallE.addControl(wallEPhy); bulletAppState.getPhysicsSpace().add(wallEPhy); // *** 7. End // *** 8. Start (ball1 - Player) Sphere ball1 = new Sphere (16,16,0.5f); geomBall1 = new Geometry("ball1", ball1); Material matBall1 = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md"); matBall1.setColor("Color", ColorRGBA.Cyan); geomBall1.setLocalTranslation(0, 2, 0); geomBall1.setMaterial(matBall1); rootNode.attachChild(geomBall1); ball1Phy = new BetterCharacterControl(0.5f,5f,30f); geomBall1.addControl(ball1Phy); bulletAppState.getPhysicsSpace().add(ball1Phy); // *** 8. End // *** 9. Start (ball2 - Can move) Sphere ball2 = new Sphere (16,16,0.5f); Geometry geomBall2 = new Geometry("ball2", ball2); Material matBall2 = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md"); matBall2.setColor("Color", ColorRGBA.Orange); geomBall2.setLocalTranslation(2, 2, 1.5f); geomBall2.setMaterial(matBall2); rootNode.attachChild(geomBall2); RigidBodyControl ball2Phy = new RigidBodyControl(1); geomBall2.addControl(ball2Phy); bulletAppState.getPhysicsSpace().add(ball2Phy); // *** 9. End // *** 10. Start (ball2 - Can move) Sphere ball3 = new Sphere (16,16,0.5f); Geometry geomBall3 = new Geometry("ball3", ball3); Material matBall3 = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md"); matBall3.setColor("Color", ColorRGBA.Pink); geomBall3.setLocalTranslation(-2, 2, -1.5f); geomBall3.setMaterial(matBall3); rootNode.attachChild(geomBall3); RigidBodyControl ball3Phy = new RigidBodyControl(1); geomBall3.addControl(ball3Phy); bulletAppState.getPhysicsSpace().add(ball3Phy); // *** 10. End setupKeys(); } private void setupKeys() { inputManager.addMapping("Left", new KeyTrigger(KeyInput.KEY_LEFT)); inputManager.addMapping("Right", new KeyTrigger(KeyInput.KEY_RIGHT)); inputManager.addMapping("Up", new KeyTrigger(KeyInput.KEY_UP)); inputManager.addMapping("Down", new KeyTrigger(KeyInput.KEY_DOWN)); inputManager.addListener(actionListener, "Left","Right","Up","Down"); } private ActionListener actionListener = new ActionListener() { public void onAction(String binding, boolean isPressed, float tpf) { if (binding.equals("Left")) { left = isPressed; } else if (binding.equals("Right")) { right= isPressed; } else if (binding.equals("Up")) { up = isPressed; } else if (binding.equals("Down")) { down = isPressed; } } }; @Override public void simpleUpdate(float tpf) { walkDirection.set(0, 0, 0); if (left) { walkDirection.addLocal(-10f,0,0); } else if (right) { walkDirection.addLocal(10f,0,0); } if (up) { walkDirection.addLocal(0,0,-10f); } else if (down) { walkDirection.addLocal(0,0,+10f); } ball1Phy.setWalkDirection(walkDirection); } @Override public void simpleRender(RenderManager rm) { } }
Output: