123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162 |
- /*
- * To change this template, choose Tools | Templates
- * and open the template in the editor.
- */
- package jme3test.bullet;
- import com.jme3.app.Application;
- import com.jme3.asset.AssetManager;
- import com.jme3.asset.TextureKey;
- import com.jme3.bullet.PhysicsSpace;
- import com.jme3.bullet.collision.shapes.CollisionShape;
- import com.jme3.bullet.collision.shapes.MeshCollisionShape;
- import com.jme3.bullet.control.RigidBodyControl;
- import com.jme3.input.MouseInput;
- import com.jme3.input.controls.ActionListener;
- import com.jme3.input.controls.MouseButtonTrigger;
- import com.jme3.light.AmbientLight;
- import com.jme3.material.Material;
- import com.jme3.math.ColorRGBA;
- import com.jme3.renderer.queue.RenderQueue.ShadowMode;
- import com.jme3.scene.Geometry;
- import com.jme3.scene.Node;
- import com.jme3.scene.shape.Box;
- import com.jme3.scene.shape.Sphere;
- import com.jme3.scene.shape.Sphere.TextureMode;
- import com.jme3.texture.Texture;
- /**
- *
- * @author normenhansen
- */
- public class PhysicsTestHelper {
- /**
- * creates a simple physics test world with a floor, an obstacle and some test boxes
- * @param rootNode
- * @param assetManager
- * @param space
- */
- public static void createPhysicsTestWorld(Node rootNode, AssetManager assetManager, PhysicsSpace space) {
- AmbientLight light = new AmbientLight();
- light.setColor(ColorRGBA.LightGray);
- rootNode.addLight(light);
- Material material = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
- material.setTexture("ColorMap", assetManager.loadTexture("Interface/Logo/Monkey.jpg"));
- Box floorBox = new Box(140, 0.25f, 140);
- Geometry floorGeometry = new Geometry("Floor", floorBox);
- floorGeometry.setMaterial(material);
- floorGeometry.setLocalTranslation(0, -5, 0);
- // Plane plane = new Plane();
- // plane.setOriginNormal(new Vector3f(0, 0.25f, 0), Vector3f.UNIT_Y);
- // floorGeometry.addControl(new RigidBodyControl(new PlaneCollisionShape(plane), 0));
- floorGeometry.addControl(new RigidBodyControl(0));
- rootNode.attachChild(floorGeometry);
- space.add(floorGeometry);
- //movable boxes
- for (int i = 0; i < 12; i++) {
- Box box = new Box(0.25f, 0.25f, 0.25f);
- Geometry boxGeometry = new Geometry("Box", box);
- boxGeometry.setMaterial(material);
- boxGeometry.setLocalTranslation(i, 5, -3);
- //RigidBodyControl automatically uses box collision shapes when attached to single geometry with box mesh
- boxGeometry.addControl(new RigidBodyControl(2));
- rootNode.attachChild(boxGeometry);
- space.add(boxGeometry);
- }
- //immovable sphere with mesh collision shape
- Sphere sphere = new Sphere(8, 8, 1);
- Geometry sphereGeometry = new Geometry("Sphere", sphere);
- sphereGeometry.setMaterial(material);
- sphereGeometry.setLocalTranslation(4, -4, 2);
- sphereGeometry.addControl(new RigidBodyControl(new MeshCollisionShape(sphere), 0));
- rootNode.attachChild(sphereGeometry);
- space.add(sphereGeometry);
- }
- /**
- * creates a box geometry with a RigidBodyControl
- * @param assetManager
- * @return
- */
- public static Geometry createPhysicsTestBox(AssetManager assetManager) {
- Material material = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
- material.setTexture("ColorMap", assetManager.loadTexture("Interface/Logo/Monkey.jpg"));
- Box box = new Box(0.25f, 0.25f, 0.25f);
- Geometry boxGeometry = new Geometry("Box", box);
- boxGeometry.setMaterial(material);
- //RigidBodyControl automatically uses box collision shapes when attached to single geometry with box mesh
- boxGeometry.addControl(new RigidBodyControl(2));
- return boxGeometry;
- }
- /**
- * creates a sphere geometry with a RigidBodyControl
- * @param assetManager
- * @return
- */
- public static Geometry createPhysicsTestSphere(AssetManager assetManager) {
- Material material = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
- material.setTexture("ColorMap", assetManager.loadTexture("Interface/Logo/Monkey.jpg"));
- Sphere sphere = new Sphere(8, 8, 0.25f);
- Geometry boxGeometry = new Geometry("Sphere", sphere);
- boxGeometry.setMaterial(material);
- //RigidBodyControl automatically uses sphere collision shapes when attached to single geometry with sphere mesh
- boxGeometry.addControl(new RigidBodyControl(2));
- return boxGeometry;
- }
- /**
- * creates an empty node with a RigidBodyControl
- * @param manager
- * @param shape
- * @param mass
- * @return
- */
- public static Node createPhysicsTestNode(AssetManager manager, CollisionShape shape, float mass) {
- Node node = new Node("PhysicsNode");
- RigidBodyControl control = new RigidBodyControl(shape, mass);
- node.addControl(control);
- return node;
- }
- /**
- * creates the necessary inputlistener and action to shoot balls from teh camera
- * @param app
- * @param rootNode
- * @param space
- */
- public static void createBallShooter(final Application app, final Node rootNode, final PhysicsSpace space) {
- ActionListener actionListener = new ActionListener() {
- public void onAction(String name, boolean keyPressed, float tpf) {
- Sphere bullet = new Sphere(32, 32, 0.4f, true, false);
- bullet.setTextureMode(TextureMode.Projected);
- Material mat2 = new Material(app.getAssetManager(), "Common/MatDefs/Misc/Unshaded.j3md");
- TextureKey key2 = new TextureKey("Textures/Terrain/Rock/Rock.PNG");
- key2.setGenerateMips(true);
- Texture tex2 = app.getAssetManager().loadTexture(key2);
- mat2.setTexture("ColorMap", tex2);
- if (name.equals("shoot") && !keyPressed) {
- Geometry bulletg = new Geometry("bullet", bullet);
- bulletg.setMaterial(mat2);
- bulletg.setShadowMode(ShadowMode.CastAndReceive);
- bulletg.setLocalTranslation(app.getCamera().getLocation());
- RigidBodyControl bulletControl = new RigidBodyControl(1);
- bulletg.addControl(bulletControl);
- bulletControl.setLinearVelocity(app.getCamera().getDirection().mult(25));
- bulletg.addControl(bulletControl);
- rootNode.attachChild(bulletg);
- space.add(bulletControl);
- }
- }
- };
- app.getInputManager().addMapping("shoot", new MouseButtonTrigger(MouseInput.BUTTON_LEFT));
- app.getInputManager().addListener(actionListener, "shoot");
- }
- }
|