package lecture18; import com.jme3.app.SimpleApplication; import com.jme3.bullet.BulletAppState; import com.jme3.bullet.control.RigidBodyControl; import com.jme3.light.DirectionalLight; import com.jme3.material.Material; import com.jme3.math.ColorRGBA; import com.jme3.math.Quaternion; import com.jme3.math.Vector3f; import com.jme3.scene.Geometry; import com.jme3.scene.Node; import com.jme3.scene.shape.Box; import com.jme3.scene.shape.PQTorus; import com.jme3.scene.shape.Sphere; public class Example02 extends SimpleApplication { Geometry floor1, floor2, floor3; Node boxes; Geometry ball; Vector3f floor2Normal = new Vector3f(-1f, 2f, 0).normalize(); Vector3f floor3Normal = new Vector3f(1f, 2f, 0).normalize(); private BulletAppState bulletAppState; protected Geometry boxFromNormal(String name, Vector3f n) { Box b = new Box(10f, 1f, 30f); Geometry bg = new Geometry(name, b); Material mat = new Material(assetManager, "Common/MatDefs/Light/Lighting.j3md"); mat.setBoolean("UseMaterialColors", true); mat.setColor("Ambient", ColorRGBA.Gray); mat.setColor("Diffuse", ColorRGBA.Gray); bg.setMaterial(mat); Quaternion q = new Quaternion(); q.fromAxes(n.cross(Vector3f.UNIT_Z), n, Vector3f.UNIT_Z); bg.setLocalRotation(q); return bg; } public void simpleInitApp() { bulletAppState = new BulletAppState(); stateManager.attach(bulletAppState); DirectionalLight sun = new DirectionalLight(); sun.setDirection(new Vector3f(1, -5, -7)); sun.setColor(ColorRGBA.White); rootNode.addLight(sun); boxes = new Node("boxes"); floor1 = boxFromNormal("floor1", Vector3f.UNIT_Y); boxes.attachChild(floor1); floor2 = boxFromNormal("floor2", floor2Normal); floor2.setLocalTranslation(18.5f, 4.5f, 0); boxes.attachChild(floor2); floor3 = boxFromNormal("floor3", floor3Normal); floor3.setLocalTranslation(-18.5f, 4.5f, 0); boxes.attachChild(floor3); rootNode.attachChild(boxes); RigidBodyControl scenePhy = new RigidBodyControl(0f); boxes.addControl(scenePhy); scenePhy.setRestitution(1); scenePhy.setFriction(2); bulletAppState.getPhysicsSpace().add(scenePhy); Material mat = new Material(assetManager, "Common/MatDefs/Light/Lighting.j3md"); mat.setBoolean("UseMaterialColors", true); mat.setColor("Ambient", ColorRGBA.Red); mat.setColor("Diffuse", ColorRGBA.Red); Sphere s = new Sphere(60, 60, 1.5f); //Box s = new Box(1,1,1); //PQTorus s = new PQTorus(5,3, 1f, 0.5f, 32, 32); ball = new Geometry("Sphere", s); ball.setMaterial(mat); ball.move(15, 30, 0); RigidBodyControl myControl = new RigidBodyControl(1f); ball.addControl(myControl); myControl.setRestitution(.8f); myControl.setFriction(2); myControl.setDamping(0, 0.1f); //myControl.setAngularVelocity(new Vector3f(0,0,-5)); bulletAppState.getPhysicsSpace().add(myControl); rootNode.attachChild(ball); cam.setLocation(new Vector3f(0, 10, 100)); cam.lookAt(Vector3f.ZERO, Vector3f.UNIT_Y); } @Override public void simpleUpdate(float tpf) { } public static void main(String[] args) { Example02 app = new Example02(); app.start(); } }