Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- package mygame.controls_states;
- import com.jme3.app.Application;
- import com.jme3.app.SimpleApplication;
- import com.jme3.app.state.AbstractAppState;
- import com.jme3.app.state.AppStateManager;
- import com.jme3.asset.AssetManager;
- import com.jme3.bullet.BulletAppState;
- import com.jme3.bullet.control.RigidBodyControl;
- import com.jme3.input.InputManager;
- import com.jme3.material.Material;
- import com.jme3.math.ColorRGBA;
- import com.jme3.math.Vector3f;
- import com.jme3.scene.Geometry;
- import com.jme3.scene.Node;
- import com.jme3.scene.Spatial;
- import com.jme3.scene.shape.Box;
- import java.util.Random;
- /**
- *
- */
- public class GameState extends AbstractAppState {
- Spatial terrain;
- SimpleApplication app;
- InputManager inputManager;
- AssetManager assetManager;
- Node rootNode;
- BulletAppState appState;
- Geometry vehicle;
- RigidBodyControl vehiclePhysics;
- Random random = new Random();
- @Override
- public void initialize(AppStateManager stateManager, Application app) {
- super.initialize(stateManager, app);
- this.app = (SimpleApplication) app;
- this.inputManager = this.app.getInputManager();
- this.assetManager = this.app.getAssetManager();
- this.rootNode = this.app.getRootNode();
- this.app.getFlyByCamera().setMoveSpeed(100);
- appState = new BulletAppState();
- stateManager.attach(appState);
- createTerrain();
- createVehicle();
- }
- private void createTerrain() {
- terrain = assetManager.loadModel("Textures/GameScene.j3o");
- terrain.setLocalTranslation(0, 0, 0);
- RigidBodyControl terrainControl = new RigidBodyControl(0);
- terrain.addControl(terrainControl);
- appState.getPhysicsSpace().add(terrainControl);
- rootNode.attachChild(terrain);
- }
- private void createVehicle() {
- Box vehicleBox = new Box(10, 2, 10);
- vehicle = new Geometry("Vehicle", vehicleBox);
- Material vehicleMaterial = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
- vehicleMaterial.setColor("Color", ColorRGBA.Blue);
- vehicleMaterial.getAdditionalRenderState().setWireframe(true);
- vehicle.setMaterial(vehicleMaterial);
- vehicle.setLocalTranslation(0, 50, 0);
- vehiclePhysics = new RigidBodyControl(50);
- vehicle.addControl(vehiclePhysics);
- vehiclePhysics.setKinematic(true);
- appState.getPhysicsSpace().add(vehiclePhysics);
- appState.getPhysicsSpace().setGravity(new Vector3f(0, -15, 0));
- Box collisionBox = new Box(10, 2, 10);
- Geometry collisionGeo = new Geometry("CollisonBox", collisionBox);
- vehicle.setLocalTranslation(0, 50, 0);
- RigidBodyControl collisionControl = new RigidBodyControl(50);
- collisionGeo.addControl(collisionControl);
- appState.getPhysicsSpace().add(collisionControl);
- VehicleController vehicleController = new VehicleController(inputManager, vehicle, terrain, vehiclePhysics, appState);
- vehicle.addControl(vehicleController);
- appState.getPhysicsSpace().addCollisionListener(vehicleController);
- rootNode.attachChild(vehicle);
- }
- @Override
- public void update(float tpf) {
- // if (VehicleController.signal == 1) {
- // vehiclePhysics.applyImpulse(new Vector3f(random.nextInt(30), random.nextInt(30), random.nextInt(30)), Vector3f.ZERO);
- // vehiclePhysics.applyCentralForce(new Vector3f(random.nextInt(50),random.nextInt(50), random.nextInt(50) ));
- // }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment