Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package mygame;
- import com.jme3.app.SimpleApplication;
- import com.jme3.bounding.BoundingBox;
- import com.jme3.bullet.BulletAppState;
- import com.jme3.bullet.PhysicsSpace;
- import com.jme3.bullet.collision.shapes.BoxCollisionShape;
- import com.jme3.bullet.collision.shapes.CollisionShape;
- import com.jme3.bullet.control.RigidBodyControl;
- import com.jme3.bullet.control.VehicleControl;
- import com.jme3.bullet.objects.VehicleWheel;
- import com.jme3.bullet.util.CollisionShapeFactory;
- import com.jme3.font.BitmapText;
- import com.jme3.input.KeyInput;
- import com.jme3.input.controls.ActionListener;
- import com.jme3.input.controls.KeyTrigger;
- import com.jme3.light.AmbientLight;
- import com.jme3.light.DirectionalLight;
- import com.jme3.material.Material;
- import com.jme3.math.ColorRGBA;
- import com.jme3.math.FastMath;
- import com.jme3.math.Matrix3f;
- import com.jme3.math.Quaternion;
- import com.jme3.math.Vector2f;
- import com.jme3.math.Vector3f;
- import com.jme3.post.FilterPostProcessor;
- import com.jme3.post.ssao.SSAOFilter;
- import com.jme3.renderer.queue.RenderQueue.ShadowMode;
- import com.jme3.scene.CameraNode;
- import com.jme3.scene.Geometry;
- import com.jme3.scene.Node;
- import com.jme3.scene.Spatial;
- import com.jme3.scene.control.CameraControl.ControlDirection;
- import com.jme3.scene.shape.Box;
- import com.jme3.shadow.PssmShadowRenderer;
- import com.jme3.util.SkyFactory;
- import com.jme3.util.TangentBinormalGenerator;
- /**
- * Example 12 - how to give objects physical properties so they bounce and fall.
- * @author base code by double1984, updated by zathras
- */
- public class Main extends SimpleApplication implements ActionListener {
- private BulletAppState bulletAppState;
- private VehicleControl player;
- private VehicleWheel fr, fl, br, bl;
- private Node node_fr, node_fl, node_br, node_bl;
- private float wheelRadius;
- private float steeringValue = 0;
- private float accelerationValue = 0;
- private Node carNode;
- private RigidBodyControl floor_phy;
- private static Box floor;
- private RigidBodyControl floor1_phy;
- private static Box floor1;
- private RigidBodyControl floor2_phy;
- private static Box floor2;
- private BitmapText hudText;
- private static Box kamera;
- private static Geometry vcam;
- private BitmapText RPMText;
- private static Node camera_origin;
- private static Boolean isRotating;
- CameraNode camNode;
- static {
- /** Initialize the floor geometry */
- floor = new Box(200f, 0.01f, 200f);
- floor.scaleTextureCoordinates(new Vector2f(2f, 20f));
- }
- public static void main(String args[]) {
- Main app = new Main();
- app.start();
- }
- private void setupKeys() {
- inputManager.addMapping("Lefts", new KeyTrigger(KeyInput.KEY_A));
- inputManager.addMapping("Rights", new KeyTrigger(KeyInput.KEY_D));
- inputManager.addMapping("Ups", new KeyTrigger(KeyInput.KEY_W));
- inputManager.addMapping("Downs", new KeyTrigger(KeyInput.KEY_S));
- inputManager.addMapping("Space", new KeyTrigger(KeyInput.KEY_SPACE));
- inputManager.addMapping("Reset", new KeyTrigger(KeyInput.KEY_R));
- inputManager.addListener(this, "Lefts");
- inputManager.addListener(this, "Rights");
- inputManager.addListener(this, "Ups");
- inputManager.addListener(this, "Downs");
- inputManager.addListener(this, "Space");
- inputManager.addListener(this, "Reset");
- }
- @Override
- public void simpleInitApp() {
- bulletAppState = new BulletAppState();
- stateManager.attach(bulletAppState);
- rootNode.attachChild(SkyFactory.createSky(assetManager, "Textures/Sky/Bright/BrightSky.dds", false));
- //bulletAppState.getPhysicsSpace().enableDebug(assetManager);
- bulletAppState.getPhysicsSpace().setGravity(new Vector3f(0f, -20f, 0f));
- PssmShadowRenderer pssmr = new PssmShadowRenderer(assetManager, 4096, 7);
- pssmr.setDirection(new Vector3f(0.5f, -0.3f, 0.3f).normalizeLocal());
- pssmr.setLambda(0.55f);
- pssmr.setShadowIntensity(0.5f);
- pssmr.setEdgesThickness(10);
- pssmr.setCompareMode(PssmShadowRenderer.CompareMode.Software);
- pssmr.setFilterMode(PssmShadowRenderer.FilterMode.PCFPOISSON);
- viewPort.addProcessor(pssmr);
- /** A white, directional light source */
- DirectionalLight sun = new DirectionalLight();
- sun.setDirection((new Vector3f(.5f, -0.3f, 0.5f)).normalizeLocal());
- sun.setColor(ColorRGBA.White);
- rootNode.addLight(sun);
- AmbientLight al = new AmbientLight();
- al.setColor(ColorRGBA.White.mult(2f));
- rootNode.addLight(al);
- /** Ambient occlusion shadows */
- FilterPostProcessor fpp = new FilterPostProcessor(assetManager);
- float constantSSAO = 0.2f;
- SSAOFilter ssaoFilter = new SSAOFilter(12.94f*constantSSAO, 23.92f*constantSSAO, 1.33f*constantSSAO, 0.31f*constantSSAO);
- //ssaoFilter.setUseOnlyAo(true);
- fpp.addFilter(ssaoFilter);
- viewPort.addProcessor(fpp);
- setupKeys();
- initFloor();
- buildPlayer();
- initGUI();
- createVirtualCamera();
- initCamera();
- }
- private void initCamera(){
- //creating the camera Node
- camNode = new CameraNode("CamNode", cam);
- //Setting the direction to Spatial to camera, this means the camera will copy the movements of the Node
- camNode.setControlDir(ControlDirection.SpatialToCamera);
- //attaching the camNode to the teaNode
- camera_origin.attachChild(camNode);
- //setting the local translation of the cam node to move it away from the teanNode a bit
- camNode.setLocalTranslation(new Vector3f(0, 2.5f, 10));
- camNode.setLocalRotation(new Quaternion().fromAngleAxis(FastMath.DEG_TO_RAD*180, new Vector3f(0,1,0)));
- //setting the camNode to look at the teaNode
- //camNode.lookAt(new Vector3f(player.getPhysicsLocation().x, 1.5f, player.getPhysicsLocation().z), Vector3f.UNIT_Y);
- //disable the default 1st-person flyCam (don't forget this!!)
- flyCam.setEnabled(false);
- }
- private void initGUI(){
- hudText = new BitmapText(guiFont, false);
- hudText.setSize(guiFont.getCharSet().getRenderedSize()); // font size
- hudText.setColor(ColorRGBA.White); // font color
- hudText.setText("You can write any string here"); // the text
- hudText.setLocalTranslation(0, settings.getHeight(), 0); // position
- guiNode.attachChild(hudText);
- RPMText = new BitmapText(guiFont, false);
- RPMText.setSize(guiFont.getCharSet().getRenderedSize()); // font size
- RPMText.setColor(ColorRGBA.White); // font color
- RPMText.setText(1+". gear "+0+" RPM"); // the text
- RPMText.setLocalTranslation(0, settings.getHeight()-RPMText.getLineHeight(), 0); // position
- guiNode.attachChild(RPMText);
- }
- private PhysicsSpace getPhysicsSpace() {
- return bulletAppState.getPhysicsSpace();
- }
- private Geometry findGeom(Spatial spatial, String name) {
- if (spatial instanceof Node) {
- Node node = (Node) spatial;
- for (int i = 0; i < node.getQuantity(); i++) {
- Spatial child = node.getChild(i);
- Geometry result = findGeom(child, name);
- if (result != null) {
- return result;
- }
- }
- } else if (spatial instanceof Geometry) {
- if (spatial.getName().startsWith(name)) {
- return (Geometry) spatial;
- }
- }
- return null;
- }
- /** Make a solid floor and add it to the scene. */
- public void initFloor() {
- Material floor_mat = new Material(assetManager,
- "Common/MatDefs/Light/Lighting.j3md");
- floor_mat.setTexture("DiffuseMap",
- assetManager.loadTexture("Textures/Terrain/Pond/Pond.jpg"));
- floor_mat.setTexture("NormalMap",
- assetManager.loadTexture("Textures/Terrain/Pond/Pond_normal.png"));
- floor_mat.setBoolean("UseMaterialColors",true);
- floor_mat.setColor("Diffuse",ColorRGBA.White);
- floor_mat.setColor("Specular",ColorRGBA.White);
- floor_mat.setColor("Ambient", new ColorRGBA(.25f,.25f, .25f, 1));
- floor_mat.setFloat("Shininess", 64f); // [0,128]
- // BOX material
- Material box_mat = new Material(assetManager,
- "Common/MatDefs/Light/Lighting.j3md");
- box_mat.setTexture("DiffuseMap",
- assetManager.loadTexture("Textures/box-diffuse.dds"));
- box_mat.setTexture("NormalMap",
- assetManager.loadTexture("Textures/box-normal.dds"));
- box_mat.setTexture("SpecularMap",
- assetManager.loadTexture("Textures/box-specular"
- + ".dds"));
- box_mat.setBoolean("UseMaterialColors",true);
- box_mat.setColor("Diffuse",ColorRGBA.White);
- box_mat.setColor("Specular",ColorRGBA.White);
- box_mat.setColor("Ambient", new ColorRGBA(.25f,.25f, .25f, 1));
- box_mat.setFloat("Shininess", 64f); // [0,128]
- //END BOX MATERIAL
- Geometry floor_geo = new Geometry("Floor", floor);
- floor_geo.setShadowMode(ShadowMode.Receive);
- 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);
- floor1 = new Box(1f, 0.3f, 10f);
- // floor1.scaleTextureCoordinates(new Vector2f(3, 6));
- Geometry floor1_geo = new Geometry("Floor1", floor1);
- floor1_geo.setShadowMode(ShadowMode.CastAndReceive);
- floor1_geo.setMaterial(floor_mat);
- floor1_geo.setLocalTranslation(0, -0.1f, 0);
- this.rootNode.attachChild(floor1_geo);
- /* Make the floor physical with mass 0.0f! */
- floor1_phy = new RigidBodyControl(0.0f);
- floor1_geo.addControl(floor1_phy);
- bulletAppState.getPhysicsSpace().add(floor1_phy);
- for (int i=0; i<4; i++){
- Box floor4 = new Box(1f, 0.3f, 10f);
- Geometry skok = new Geometry("Floor4", floor4);
- skok.setShadowMode(ShadowMode.CastAndReceive);
- skok.setMaterial(floor_mat);
- skok.setLocalTranslation(-i*6f, -0.05f, 0);
- rootNode.attachChild(skok);
- /* Make the floor physical with mass 0.0f! */
- RigidBodyControl skok_phy = new RigidBodyControl(0.0f);
- skok.addControl(skok_phy);
- bulletAppState.getPhysicsSpace().add(skok_phy);
- }
- floor2 = new Box(5f, 0.3f, 10f);
- //floor2.scaleTextureCoordinates(new Vector2f(3, 6));
- Geometry floor2_geo = new Geometry("Floor2", floor2);
- floor2_geo.setShadowMode(ShadowMode.CastAndReceive);
- floor2_geo.setMaterial(floor_mat);
- floor2_geo.setLocalTranslation(5f, -0.1f, 0);
- floor2_geo.rotate(0,0 , 0.2f);
- this.rootNode.attachChild(floor2_geo);
- /* Make the floor physical with mass 0.0f! */
- floor2_phy = new RigidBodyControl(0.0f);
- floor2_geo.addControl(floor2_phy);
- bulletAppState.getPhysicsSpace().add(floor2_phy);
- for (int i=0; i<2; i++){
- for(int j = 0; j<3;j++){
- Box teapot = new Box(1f, 1f, 1f);
- Geometry teapot_g = new Geometry("pBox", teapot);
- teapot_g.setShadowMode(ShadowMode.CastAndReceive);
- TangentBinormalGenerator.generate(teapot_g.getMesh(), true);
- teapot_g.setMaterial(box_mat);
- rootNode.attachChild(teapot_g);
- teapot_g.setLocalTranslation(12f, i*3 +1.2f, j*2 +.1f);
- System.out.print(String.valueOf(j*2));
- teapot_g.setLocalScale(1);
- RigidBodyControl teapot_phy = new RigidBodyControl(1000.0f);
- teapot_g.addControl(teapot_phy);
- bulletAppState.getPhysicsSpace().add(teapot_phy);
- }
- }
- Spatial barrelNode = assetManager.loadModel("Models/barrel.obj");
- barrelNode.setShadowMode(ShadowMode.CastAndReceive);
- barrelNode.setLocalTranslation(0,2, -20);
- barrelNode.setLocalScale(0.5f);
- rootNode.attachChild(barrelNode);
- RigidBodyControl barrel_phy = new RigidBodyControl(1000.0f);
- barrelNode.addControl(barrel_phy);
- barrel_phy.setFriction(2);
- barrel_phy.setPhysicsRotation(new Quaternion(1f, 0f, 0f, 1f));
- bulletAppState.getPhysicsSpace().add(barrel_phy);
- Spatial felicia = assetManager.loadModel("Models/felicia.obj");
- felicia.setShadowMode(ShadowMode.CastAndReceive);
- felicia.setLocalTranslation(-4,2, 0);
- felicia.setLocalScale(0.5f);
- rootNode.attachChild(felicia);
- RigidBodyControl felicia_phy = new RigidBodyControl(1000.0f);
- felicia.addControl(felicia_phy);
- felicia_phy.setCollisionShape(new BoxCollisionShape(Vector3f.ZERO));
- felicia_phy.setFriction(2);
- bulletAppState.getPhysicsSpace().add(felicia_phy);
- }
- private void buildPlayer() {
- float stiffness = 60.0f;//200=f1 car
- float compValue = 0.05f; //(lower than damp!)
- float dampValue = 0.3f;
- final float mass = 2500;
- //Load model and get chassis Geometry
- carNode = (Node)assetManager.loadModel("Models/Ferrari/Car.scene");
- carNode.setShadowMode(ShadowMode.CastAndReceive);
- Geometry chasis = findGeom(carNode, "Car");
- BoundingBox box = (BoundingBox) chasis.getModelBound();
- //Create a hull collision shape for the chassis
- CollisionShape carHull = CollisionShapeFactory.createDynamicMeshShape(chasis);
- //Create a vehicle control
- player = new VehicleControl(carHull, mass);
- carNode.addControl(player);
- // player.setGravity(new Vector3f(0, 100f, 0)); //Setting default values for wheels
- player.setSuspensionCompression(compValue * 2.0f * FastMath.sqrt(stiffness));
- player.setSuspensionDamping(dampValue * 2.0f * FastMath.sqrt(stiffness));
- player.setSuspensionStiffness(stiffness);
- player.setMaxSuspensionForce(100000);
- //Create four wheels and add them at their locations
- //note that our fancy car actually goes backwards..
- Vector3f wheelDirection = new Vector3f(0, -1, 0);
- Vector3f wheelAxle = new Vector3f(-1, 0, 0);
- Geometry wheel_fr = findGeom(carNode, "WheelFrontRight");
- wheel_fr.setShadowMode(ShadowMode.CastAndReceive);
- wheel_fr.center();
- box = (BoundingBox) wheel_fr.getModelBound();
- wheelRadius = box.getYExtent();
- float back_wheel_h = (wheelRadius * 1.7f) - 1f;
- float front_wheel_h = (wheelRadius * 1.9f) - 1f;
- player.addWheel(wheel_fr.getParent(), box.getCenter().add(0, -front_wheel_h, 0),
- wheelDirection, wheelAxle, .5f, wheelRadius, true);
- Geometry wheel_fl = findGeom(carNode, "WheelFrontLeft");
- wheel_fl.setShadowMode(ShadowMode.CastAndReceive);
- wheel_fl.center();
- box = (BoundingBox) wheel_fl.getModelBound();
- player.addWheel(wheel_fl.getParent(), box.getCenter().add(0, -front_wheel_h, 0),
- wheelDirection, wheelAxle, .5f, wheelRadius, true);
- Geometry wheel_br = findGeom(carNode, "WheelBackRight");
- wheel_br.setShadowMode(ShadowMode.CastAndReceive);
- wheel_br.center();
- box = (BoundingBox) wheel_br.getModelBound();
- player.addWheel(wheel_br.getParent(), box.getCenter().add(0, -back_wheel_h, 0),
- wheelDirection, wheelAxle, .5f, wheelRadius, false);
- Geometry wheel_bl = findGeom(carNode, "WheelBackLeft");
- wheel_bl.setShadowMode(ShadowMode.CastAndReceive);
- wheel_bl.center();
- box = (BoundingBox) wheel_bl.getModelBound();
- player.addWheel(wheel_bl.getParent(), box.getCenter().add(0, -back_wheel_h, 0),
- wheelDirection, wheelAxle, .5f, wheelRadius, false);
- player.getWheel(0).setFrictionSlip(3f);
- player.getWheel(1).setFrictionSlip(3f);
- player.getWheel(2).setFrictionSlip(2f);
- player.getWheel(3).setFrictionSlip(2f);
- rootNode.attachChild(carNode);
- getPhysicsSpace().add(player);
- }
- public void onAction(String binding, boolean value, float tpf) {
- int power = 20000;
- if (binding.equals("Lefts")) {
- if (value) {
- steeringValue += .7f;
- } else {
- steeringValue += -.7f;
- }
- player.steer(steeringValue);
- } else if (binding.equals("Rights")) {
- if (value) {
- steeringValue += -.7f;
- } else {
- steeringValue += .7f;
- }
- player.steer(steeringValue);
- } //note that our fancy car actually goes backwards..
- else if (binding.equals("Ups")) {
- if (value) {
- accelerationValue -= power;
- } else {
- accelerationValue += power;
- }
- player.accelerate(2, accelerationValue);
- player.accelerate(3, accelerationValue);
- player.setCollisionShape(CollisionShapeFactory.createDynamicMeshShape(findGeom(carNode, "Car")));
- } else if (binding.equals("Downs")) {
- if (value) {
- accelerationValue += power;
- } else {
- accelerationValue -= power;
- }
- player.accelerate(2, accelerationValue);
- player.accelerate(3, accelerationValue);
- } else if (binding.equals("Reset")) {
- if (value) {
- System.out.println("Reset");
- player.setPhysicsLocation(Vector3f.ZERO);
- player.setPhysicsRotation(new Matrix3f());
- player.setLinearVelocity(Vector3f.ZERO);
- player.setAngularVelocity(Vector3f.ZERO);
- player.resetSuspension();
- } else {
- }
- }
- else if (binding.equals("Space")) {
- //createBox(carNode.getLocalTranslation(),carNode.getLocalRotation());
- if (value) {
- // camNode.lookAt(carNode.getLocalTranslation(), Vector3f.UNIT_Y);
- player.brake(2, 100000f);
- player.brake(3, 1000000f);
- } else {
- player.brake(2, 0f);
- player.brake(3, 0f);
- }
- }
- }
- public void createBox(Vector3f pos, Quaternion rot){
- Box b = new Box(1, 1, 1); // create a 1x1x1 box shape
- Geometry geom = new Geometry("Box", b); // create a cube geometry from the box shape
- Material mat = new Material(assetManager,
- "Common/MatDefs/Misc/Unshaded.j3md"); // create a simple material
- mat.setColor("Color", ColorRGBA.Blue); // set color of material to blue
- geom.setMaterial(mat); // set the cube geometry 's material
- geom.setLocalTranslation(pos.x,pos.y,pos.z+6f);
- geom.setLocalRotation(rot);
- rootNode.attachChild(geom); // make the cube geometry appear in the scene
- }
- public void createVirtualCamera(){
- kamera = new Box(.3f, .3f, .3f); // create a 1x1x1 box shape
- vcam = new Geometry("vCamera", kamera); // create a cube geometry from the box shape
- Material mat = new Material(assetManager,
- "Common/MatDefs/Misc/Unshaded.j3md"); // create a simple material
- mat.setColor("Color", ColorRGBA.Green); // set color of material to blue
- mat.getAdditionalRenderState().setWireframe(true);
- vcam.setMaterial(mat); // set the cube geometry 's material
- camera_origin = new Node("camer_origin");
- rootNode.attachChild(camera_origin);
- camera_origin.attachChild(vcam); // make the cube geometry appear in the scene
- }
- @Override
- public void simpleUpdate(float tpf) {
- //camNode.rotateUpTo(new Vector3f(-carNode.getWorldRotation().getX(), 1f, 0f));
- camera_origin.setLocalTranslation(carNode.getLocalTranslation());
- //camera_origin.setLocalRotation(new Quaternion(0f, carNode.getWorldRotation().getY(), 0f, carNode.getWorldRotation().getW() ));
- float velocity = FastMath.floor(Math.abs(player.getCurrentVehicleSpeedKmHour()));
- if(Math.abs(velocity) < 0.01f){
- //player.setLinearVelocity(Vector3f.ZERO);
- // player.brake(100f);
- }
- hudText.setText(String.valueOf(velocity) + "km/h");
- float cam_dir = camera_origin.getLocalRotation().getY();
- float car_dir = carNode.getLocalRotation().getY();
- String test= "";
- /*if(FastMath.abs(cam_dir - car_dir) > 0.2f){
- isRotating = Boolean.TRUE;
- }
- if(FastMath.abs(cam_dir - car_dir) < 0.01f){
- isRotating = Boolean.FALSE;
- }*/
- if(!(FastMath.abs(cam_dir - car_dir) < 0.00001f)){
- //camera_origin.setLocalRotation(new Quaternion(0f, camera_origin.getLocalRotation().getY()-(cam_dir - car_dir)*tpf, 0f, camera_origin.getWorldRotation().getW() ));
- Quaternion cama = camera_origin.getLocalRotation();
- Quaternion cara = carNode.getLocalRotation();
- Quaternion vysledek;
- cama.slerp(cara, tpf*3);
- camera_origin.setLocalRotation(new Quaternion(0f,cama.getY(),0f,cama.getW()).normalizeLocal());
- }
- test = String.format("%.4f",FastMath.abs(cam_dir - car_dir));
- RPMText.setText("Vektor kamery("+ String.format("%.4f", cam_dir) +") - vektor aute ("+ String.format("%.4f", car_dir) +"):"+test);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment