2016-12-30 23:19:29 +00:00
|
|
|
package roadtrip;
|
|
|
|
|
|
|
|
import com.jme3.app.SimpleApplication;
|
2017-01-01 14:23:58 +00:00
|
|
|
import com.jme3.audio.AudioNode;
|
2017-01-03 00:14:35 +00:00
|
|
|
import com.jme3.audio.AudioSource;
|
|
|
|
import com.jme3.audio.AudioSource.Status;
|
2017-01-01 14:23:58 +00:00
|
|
|
import com.jme3.audio.Environment;
|
2016-12-30 23:19:29 +00:00
|
|
|
import com.jme3.bullet.BulletAppState;
|
|
|
|
import com.jme3.bullet.PhysicsSpace;
|
|
|
|
import com.jme3.bullet.collision.shapes.BoxCollisionShape;
|
|
|
|
import com.jme3.bullet.collision.shapes.CompoundCollisionShape;
|
2016-12-31 22:12:20 +00:00
|
|
|
import com.jme3.bullet.control.BetterCharacterControl;
|
|
|
|
import com.jme3.bullet.control.RigidBodyControl;
|
2016-12-30 23:19:29 +00:00
|
|
|
import com.jme3.bullet.control.VehicleControl;
|
|
|
|
import com.jme3.input.KeyInput;
|
|
|
|
import com.jme3.input.controls.ActionListener;
|
|
|
|
import com.jme3.input.controls.KeyTrigger;
|
2017-01-04 00:03:13 +00:00
|
|
|
import com.jme3.light.DirectionalLight;
|
2016-12-30 23:19:29 +00:00
|
|
|
import com.jme3.material.Material;
|
|
|
|
import com.jme3.math.ColorRGBA;
|
|
|
|
import com.jme3.math.FastMath;
|
2017-01-04 00:03:13 +00:00
|
|
|
import com.jme3.math.Quaternion;
|
2016-12-30 23:19:29 +00:00
|
|
|
import com.jme3.math.Vector3f;
|
|
|
|
import com.jme3.scene.Geometry;
|
|
|
|
import com.jme3.scene.Node;
|
|
|
|
import com.jme3.scene.Spatial;
|
2016-12-31 22:12:20 +00:00
|
|
|
import com.jme3.scene.shape.Box;
|
2016-12-30 23:19:29 +00:00
|
|
|
import com.jme3.scene.shape.Cylinder;
|
2017-01-03 00:14:35 +00:00
|
|
|
import java.util.LinkedList;
|
|
|
|
import java.util.List;
|
2017-01-01 14:23:58 +00:00
|
|
|
import org.lwjgl.openal.AL10;
|
|
|
|
import org.lwjgl.openal.AL11;
|
2016-12-30 23:19:29 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @author dejvino
|
|
|
|
*/
|
|
|
|
public class RoadTrip extends SimpleApplication implements ActionListener {
|
|
|
|
|
2016-12-31 22:12:20 +00:00
|
|
|
public static boolean DEBUG = false;//true;
|
2016-12-30 23:19:29 +00:00
|
|
|
|
|
|
|
private BulletAppState bulletAppState;
|
2017-01-04 00:03:13 +00:00
|
|
|
|
|
|
|
Spatial map;
|
2017-01-03 00:14:35 +00:00
|
|
|
private List<VehicleNode> vehicles = new LinkedList<>();
|
|
|
|
|
|
|
|
private Node playerNode;
|
|
|
|
private BetterCharacterControl playerPersonControl;
|
2016-12-30 23:19:29 +00:00
|
|
|
private Vector3f jumpForce = new Vector3f(0, 3000, 0);
|
2016-12-31 22:57:29 +00:00
|
|
|
private Vector3f walkDir = new Vector3f();
|
2017-01-04 00:03:13 +00:00
|
|
|
private VehicleNode playerVehicleNode;
|
|
|
|
|
2017-01-03 00:14:35 +00:00
|
|
|
public static void main(String[] args)
|
|
|
|
{
|
2016-12-30 23:19:29 +00:00
|
|
|
RoadTrip app = new RoadTrip();
|
|
|
|
app.start();
|
|
|
|
}
|
2017-01-04 00:03:13 +00:00
|
|
|
|
2016-12-30 23:19:29 +00:00
|
|
|
@Override
|
|
|
|
public void simpleInitApp() {
|
|
|
|
bulletAppState = new BulletAppState();
|
|
|
|
stateManager.attach(bulletAppState);
|
2016-12-31 22:12:20 +00:00
|
|
|
if (DEBUG) bulletAppState.getPhysicsSpace().enableDebug(assetManager);
|
2016-12-30 23:19:29 +00:00
|
|
|
PhysicsTestHelper.createPhysicsTestWorld(rootNode, assetManager, bulletAppState.getPhysicsSpace());
|
|
|
|
setupKeys();
|
|
|
|
|
2017-01-01 20:47:53 +00:00
|
|
|
//audioRenderer.setEnvironment(Environment.Dungeon);
|
|
|
|
//AL10.alDistanceModel(AL11.AL_EXPONENT_DISTANCE);
|
2017-01-01 14:23:58 +00:00
|
|
|
|
|
|
|
addMap();
|
2016-12-30 23:19:29 +00:00
|
|
|
|
2017-01-04 00:03:13 +00:00
|
|
|
DirectionalLight dl = new DirectionalLight();
|
|
|
|
dl.setColor(ColorRGBA.White);
|
|
|
|
dl.setDirection(new Vector3f(1, -1, 1));
|
|
|
|
rootNode.addLight(dl);
|
|
|
|
|
2017-01-03 00:14:35 +00:00
|
|
|
addPlayer();
|
2016-12-31 22:12:20 +00:00
|
|
|
|
2017-01-03 00:14:35 +00:00
|
|
|
addCar();
|
|
|
|
addCar();
|
|
|
|
addCar();
|
|
|
|
addCar();
|
|
|
|
addCar();
|
|
|
|
addPerson();
|
|
|
|
addPerson();
|
2016-12-31 22:12:20 +00:00
|
|
|
addPerson();
|
|
|
|
addPerson();
|
|
|
|
addPerson();
|
|
|
|
addPerson();
|
|
|
|
addPerson();
|
2016-12-30 23:19:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private PhysicsSpace getPhysicsSpace(){
|
|
|
|
return bulletAppState.getPhysicsSpace();
|
|
|
|
}
|
|
|
|
|
|
|
|
private void setupKeys() {
|
|
|
|
inputManager.addMapping("Lefts", new KeyTrigger(KeyInput.KEY_H));
|
|
|
|
inputManager.addMapping("Rights", new KeyTrigger(KeyInput.KEY_K));
|
|
|
|
inputManager.addMapping("Ups", new KeyTrigger(KeyInput.KEY_U));
|
|
|
|
inputManager.addMapping("Downs", new KeyTrigger(KeyInput.KEY_J));
|
2016-12-31 15:58:13 +00:00
|
|
|
inputManager.addMapping("Revs", new KeyTrigger(KeyInput.KEY_M));
|
2016-12-30 23:19:29 +00:00
|
|
|
inputManager.addMapping("Space", new KeyTrigger(KeyInput.KEY_SPACE));
|
|
|
|
inputManager.addMapping("Reset", new KeyTrigger(KeyInput.KEY_RETURN));
|
|
|
|
inputManager.addListener(this, "Lefts");
|
|
|
|
inputManager.addListener(this, "Rights");
|
|
|
|
inputManager.addListener(this, "Ups");
|
|
|
|
inputManager.addListener(this, "Downs");
|
2016-12-31 15:58:13 +00:00
|
|
|
inputManager.addListener(this, "Revs");
|
2016-12-30 23:19:29 +00:00
|
|
|
inputManager.addListener(this, "Space");
|
|
|
|
inputManager.addListener(this, "Reset");
|
|
|
|
}
|
|
|
|
|
2017-01-03 00:14:35 +00:00
|
|
|
private void addCar()
|
|
|
|
{
|
|
|
|
Node vehicleModel = new Node("VehicleModel");
|
2017-01-04 00:03:13 +00:00
|
|
|
VehicleInstance vehicleInstance = VehicleInstance.createVehicle(vehicles.size() % VehicleInstance.getVehicleTypesCount());
|
2017-01-03 00:14:35 +00:00
|
|
|
|
2016-12-30 23:19:29 +00:00
|
|
|
Material mat = new Material(getAssetManager(), "Common/MatDefs/Misc/Unshaded.j3md");
|
|
|
|
mat.getAdditionalRenderState().setWireframe(true);
|
2016-12-31 22:12:20 +00:00
|
|
|
mat.setColor("Color", ColorRGBA.Black);
|
2016-12-30 23:19:29 +00:00
|
|
|
|
2017-01-04 00:03:13 +00:00
|
|
|
//Create four wheels and add them at their locations
|
|
|
|
Vector3f wheelDirection = new Vector3f(0, -1, 0); // was 0, -1, 0
|
|
|
|
Vector3f wheelAxle = new Vector3f(-1, 0, 0); // was -1, 0, 0
|
|
|
|
float radius = 1.0f;
|
|
|
|
float restLength = 0.3f;
|
|
|
|
float yOff = 0.5f;
|
|
|
|
float xOff = 1.6f;
|
|
|
|
float zOff = 2f;
|
|
|
|
|
|
|
|
Geometry carBody = new Geometry("car body", new Box(new Vector3f(0.0f, 1f, 0.0f), 1.4f, 0.5f, 3.6f));
|
|
|
|
Material matBody = new Material(getAssetManager(), "Common/MatDefs/Light/Lighting.j3md");
|
|
|
|
matBody.setFloat("Shininess", 32f);
|
|
|
|
matBody.setBoolean("UseMaterialColors", true);
|
|
|
|
matBody.setColor("Ambient", ColorRGBA.Black);
|
|
|
|
matBody.setColor("Diffuse", ColorRGBA.Red);
|
|
|
|
matBody.setColor("Specular", ColorRGBA.White);
|
|
|
|
carBody.setMaterial(matBody);
|
|
|
|
vehicleModel.attachChild(carBody);
|
|
|
|
|
2016-12-30 23:19:29 +00:00
|
|
|
//create a compound shape and attach the BoxCollisionShape for the car body at 0,1,0
|
|
|
|
//this shifts the effective center of mass of the BoxCollisionShape to 0,-1,0
|
|
|
|
CompoundCollisionShape compoundShape = new CompoundCollisionShape();
|
|
|
|
BoxCollisionShape box = new BoxCollisionShape(new Vector3f(1.4f, 0.5f, 3.6f));
|
|
|
|
compoundShape.addChildShape(box, new Vector3f(0, 1, 0));
|
|
|
|
|
2017-01-03 00:14:35 +00:00
|
|
|
if (vehicleInstance.carType == VehicleInstance.TRUCK) {
|
2016-12-30 23:19:29 +00:00
|
|
|
BoxCollisionShape boxCabin = new BoxCollisionShape(new Vector3f(1.4f, 0.8f, 1.0f));
|
|
|
|
compoundShape.addChildShape(boxCabin, new Vector3f(0, 2, 2f));
|
2017-01-04 00:03:13 +00:00
|
|
|
|
|
|
|
Geometry carCabin = new Geometry("car cabin", new Box(new Vector3f(0, 2, 2f), 1.4f, 0.8f, 1.0f));
|
|
|
|
carCabin.setMaterial(matBody);
|
|
|
|
vehicleModel.attachChild(carCabin);
|
2017-01-03 00:14:35 +00:00
|
|
|
} else if (vehicleInstance.carType == VehicleInstance.SPORT) {
|
2016-12-30 23:19:29 +00:00
|
|
|
BoxCollisionShape boxCabin = new BoxCollisionShape(new Vector3f(1.2f, 0.6f, 2.0f));
|
|
|
|
compoundShape.addChildShape(boxCabin, new Vector3f(0, 2, -1f));
|
2017-01-04 00:03:13 +00:00
|
|
|
|
|
|
|
Geometry carCabin = new Geometry("car cabin", new Box(new Vector3f(0, 2, -1f), 1.2f, 0.6f, 2.0f));
|
|
|
|
carCabin.setMaterial(matBody);
|
|
|
|
vehicleModel.attachChild(carCabin);
|
2016-12-30 23:19:29 +00:00
|
|
|
}
|
2017-01-04 00:03:13 +00:00
|
|
|
|
2017-01-03 00:14:35 +00:00
|
|
|
VehicleControl vehicleControl = new VehicleControl(compoundShape, 500);
|
|
|
|
|
2016-12-30 23:19:29 +00:00
|
|
|
//setting suspension values for wheels, this can be a bit tricky
|
|
|
|
//see also https://docs.google.com/Doc?docid=0AXVUZ5xw6XpKZGNuZG56a3FfMzU0Z2NyZnF4Zmo&hl=en
|
|
|
|
float stiffness = 30.0f;//200=f1 car
|
|
|
|
float compValue = .1f; //(should be lower than damp)
|
|
|
|
float dampValue = .2f;
|
2017-01-03 00:14:35 +00:00
|
|
|
vehicleControl.setSuspensionCompression(compValue * 2.0f * FastMath.sqrt(stiffness));
|
|
|
|
vehicleControl.setSuspensionDamping(dampValue * 2.0f * FastMath.sqrt(stiffness));
|
|
|
|
vehicleControl.setSuspensionStiffness(stiffness);
|
|
|
|
vehicleControl.setMaxSuspensionForce(10000.0f);
|
2016-12-31 22:12:20 +00:00
|
|
|
|
2016-12-30 23:19:29 +00:00
|
|
|
Cylinder wheelMesh = new Cylinder(16, 16, radius, radius * 0.2f, true);
|
|
|
|
|
|
|
|
Node node1 = new Node("wheel 1 node");
|
|
|
|
Geometry wheels1 = new Geometry("wheel 1", wheelMesh);
|
|
|
|
node1.attachChild(wheels1);
|
|
|
|
wheels1.rotate(0, FastMath.HALF_PI, 0);
|
|
|
|
wheels1.setMaterial(mat);
|
2017-01-03 00:14:35 +00:00
|
|
|
vehicleControl.addWheel(node1, new Vector3f(-xOff, yOff, zOff),
|
2016-12-30 23:19:29 +00:00
|
|
|
wheelDirection, wheelAxle, restLength, radius, true);
|
|
|
|
|
|
|
|
Node node2 = new Node("wheel 2 node");
|
|
|
|
Geometry wheels2 = new Geometry("wheel 2", wheelMesh);
|
|
|
|
node2.attachChild(wheels2);
|
|
|
|
wheels2.rotate(0, FastMath.HALF_PI, 0);
|
|
|
|
wheels2.setMaterial(mat);
|
2017-01-03 00:14:35 +00:00
|
|
|
vehicleControl.addWheel(node2, new Vector3f(xOff, yOff, zOff),
|
2016-12-30 23:19:29 +00:00
|
|
|
wheelDirection, wheelAxle, restLength, radius, true);
|
|
|
|
|
|
|
|
Node node3 = new Node("wheel 3 node");
|
|
|
|
Geometry wheels3 = new Geometry("wheel 3", wheelMesh);
|
|
|
|
node3.attachChild(wheels3);
|
|
|
|
wheels3.rotate(0, FastMath.HALF_PI, 0);
|
|
|
|
wheels3.setMaterial(mat);
|
2017-01-03 00:14:35 +00:00
|
|
|
vehicleControl.addWheel(node3, new Vector3f(-xOff, yOff, -zOff),
|
2016-12-30 23:19:29 +00:00
|
|
|
wheelDirection, wheelAxle, restLength, radius, false);
|
|
|
|
|
|
|
|
Node node4 = new Node("wheel 4 node");
|
|
|
|
Geometry wheels4 = new Geometry("wheel 4", wheelMesh);
|
|
|
|
node4.attachChild(wheels4);
|
|
|
|
wheels4.rotate(0, FastMath.HALF_PI, 0);
|
|
|
|
wheels4.setMaterial(mat);
|
2017-01-03 00:14:35 +00:00
|
|
|
vehicleControl.addWheel(node4, new Vector3f(xOff, yOff, -zOff),
|
2016-12-30 23:19:29 +00:00
|
|
|
wheelDirection, wheelAxle, restLength, radius, false);
|
|
|
|
|
2017-01-03 00:14:35 +00:00
|
|
|
vehicleModel.attachChild(node1);
|
|
|
|
vehicleModel.attachChild(node2);
|
|
|
|
vehicleModel.attachChild(node3);
|
|
|
|
vehicleModel.attachChild(node4);
|
2016-12-30 23:19:29 +00:00
|
|
|
|
2017-01-03 00:14:35 +00:00
|
|
|
if (vehicleInstance.carType == VehicleInstance.TRUCK) {
|
2016-12-30 23:19:29 +00:00
|
|
|
Node node5 = new Node("wheel 5 node");
|
|
|
|
Geometry wheels5 = new Geometry("wheel 5", wheelMesh);
|
|
|
|
node5.attachChild(wheels5);
|
|
|
|
wheels5.rotate(0, FastMath.HALF_PI, 0);
|
|
|
|
wheels5.setMaterial(mat);
|
2017-01-03 00:14:35 +00:00
|
|
|
vehicleControl.addWheel(node5, new Vector3f(-xOff, yOff, 2.1f* -zOff),
|
2016-12-30 23:19:29 +00:00
|
|
|
wheelDirection, wheelAxle, restLength, radius, false);
|
|
|
|
|
|
|
|
Node node6 = new Node("wheel 6 node");
|
|
|
|
Geometry wheels6 = new Geometry("wheel 6", wheelMesh);
|
|
|
|
node6.attachChild(wheels6);
|
|
|
|
wheels6.rotate(0, FastMath.HALF_PI, 0);
|
|
|
|
wheels6.setMaterial(mat);
|
2017-01-03 00:14:35 +00:00
|
|
|
vehicleControl.addWheel(node6, new Vector3f(xOff, yOff, 2.1f* -zOff),
|
2016-12-30 23:19:29 +00:00
|
|
|
wheelDirection, wheelAxle, restLength, radius, false);
|
|
|
|
|
2017-01-03 00:14:35 +00:00
|
|
|
vehicleModel.attachChild(node5);
|
|
|
|
vehicleModel.attachChild(node6);
|
2016-12-30 23:19:29 +00:00
|
|
|
}
|
|
|
|
|
2017-01-03 00:14:35 +00:00
|
|
|
vehicleControl.getWheel(0).setFrictionSlip(0.8f);
|
|
|
|
vehicleControl.getWheel(1).setFrictionSlip(0.8f);
|
|
|
|
vehicleControl.getWheel(2).setFrictionSlip(0.6f);
|
|
|
|
vehicleControl.getWheel(3).setFrictionSlip(0.6f);
|
2016-12-30 23:19:29 +00:00
|
|
|
|
2017-01-03 00:14:35 +00:00
|
|
|
if (vehicleInstance.carType == VehicleInstance.TRUCK) {
|
|
|
|
vehicleControl.getWheel(4).setFrictionSlip(0.6f);
|
|
|
|
vehicleControl.getWheel(5).setFrictionSlip(0.6f);
|
2016-12-30 23:19:29 +00:00
|
|
|
}
|
2017-01-03 00:14:35 +00:00
|
|
|
vehicleControl.setPhysicsLocation(new Vector3f(5f, 30f, 5f));
|
2016-12-31 22:57:29 +00:00
|
|
|
|
2017-01-03 00:14:35 +00:00
|
|
|
VehicleNode vehicle = new VehicleNode("VehicleNode",
|
|
|
|
vehicleInstance, vehicleControl, vehicleModel);
|
2017-01-04 00:03:13 +00:00
|
|
|
vehicle.attachChild(vehicleModel);
|
2017-01-03 00:14:35 +00:00
|
|
|
|
|
|
|
vehicle.engineAudio = new AudioNode(assetManager, "Sounds/engine.ogg", false);
|
|
|
|
vehicle.engineAudio.setPositional(true);
|
|
|
|
vehicle.engineAudio.setLooping(true);
|
|
|
|
vehicle.engineAudio.setReverbEnabled(true);
|
2017-01-04 00:03:13 +00:00
|
|
|
vehicle.engineAudio.setRefDistance(5);
|
|
|
|
vehicle.engineAudio.setMaxDistance(1000000);
|
2017-01-03 00:14:35 +00:00
|
|
|
vehicle.attachChild(vehicle.engineAudio);
|
2017-01-01 14:23:58 +00:00
|
|
|
|
2017-01-03 00:14:35 +00:00
|
|
|
vehicle.wheelsAudio = new AudioNode(assetManager, "Sounds/wheels.ogg", false);
|
|
|
|
vehicle.wheelsAudio.setPositional(true);
|
|
|
|
vehicle.wheelsAudio.setLooping(true);
|
2017-01-01 20:47:53 +00:00
|
|
|
//wheelsAudio.setReverbEnabled(true);
|
2017-01-04 00:03:13 +00:00
|
|
|
vehicle.wheelsAudio.setRefDistance(1f);
|
|
|
|
vehicle.wheelsAudio.setMaxDistance(1000000f);
|
2017-01-03 00:14:35 +00:00
|
|
|
vehicle.wheelsAudio.play();
|
|
|
|
vehicle.attachChild(vehicle.wheelsAudio);
|
2017-01-01 20:47:53 +00:00
|
|
|
|
2017-01-03 00:14:35 +00:00
|
|
|
vehicle.wheelSlipAudio = new AudioNode(assetManager, "Sounds/wheel-slip.ogg", false);
|
|
|
|
vehicle.wheelSlipAudio.setPositional(true);
|
|
|
|
vehicle.wheelSlipAudio.setLooping(true);
|
2017-01-01 20:47:53 +00:00
|
|
|
//wheelsAudio.setReverbEnabled(true);
|
2017-01-04 00:03:13 +00:00
|
|
|
vehicle.wheelSlipAudio.setRefDistance(5);
|
|
|
|
vehicle.wheelSlipAudio.setMaxDistance(1000000);
|
2017-01-03 00:14:35 +00:00
|
|
|
vehicle.wheelSlipAudio.play();
|
|
|
|
vehicle.attachChild(vehicle.wheelSlipAudio);
|
2017-01-01 20:47:53 +00:00
|
|
|
|
2017-01-04 00:03:13 +00:00
|
|
|
vehicle.addControl(vehicleControl);
|
|
|
|
getPhysicsSpace().add(vehicleControl);
|
|
|
|
vehicleControl.setPhysicsLocation(new Vector3f(10f + (float)Math.random() * 20f, 30f, 12f + (float)Math.random() * 20f));
|
2017-01-03 00:14:35 +00:00
|
|
|
|
|
|
|
vehicles.add(vehicle);
|
2017-01-04 00:03:13 +00:00
|
|
|
rootNode.attachChild(vehicle);
|
2016-12-30 23:19:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void simpleUpdate(float tpf) {
|
2017-01-04 00:03:13 +00:00
|
|
|
Vector3f playerLocation = playerNode.getWorldTranslation();
|
2016-12-31 22:57:29 +00:00
|
|
|
Vector3f newLocation = new Vector3f(playerLocation).add(new Vector3f(-1f, 1.5f, 2.4f).mult(20f));
|
2016-12-31 00:17:11 +00:00
|
|
|
cam.setLocation(new Vector3f(cam.getLocation()).interpolate(newLocation, Math.min(tpf, 1f)));
|
2016-12-31 22:57:29 +00:00
|
|
|
cam.lookAt(playerLocation, Vector3f.UNIT_Y);
|
2017-01-01 14:23:58 +00:00
|
|
|
|
2017-01-03 00:14:35 +00:00
|
|
|
for (VehicleNode vehicle : vehicles) {
|
|
|
|
vehicle.vehicleInstance.accelerationSmooth = (vehicle.vehicleInstance.accelerationSmooth + vehicle.vehicleInstance.accelerationValue * (tpf * 10f)) / (1 + tpf * 10f);
|
|
|
|
//engineAudio.setVelocity(new Vector3f(0, 0, 0));
|
|
|
|
//engineAudio.setLocalTranslation(x, 0, z);
|
|
|
|
vehicle.engineAudio.updateGeometricState();
|
|
|
|
vehicle.engineAudio.setPitch(Math.max(0.5f, Math.min(vehicle.vehicleInstance.accelerationSmooth / vehicle.vehicleInstance.accelerationForce * 2f, 2.0f)));
|
|
|
|
boolean engineRunning = (vehicle.vehicleInstance.accelerationValue > 0.01f || vehicle.vehicleInstance.accelerationValue < -0.01f);
|
|
|
|
if ((vehicle.engineAudio.getStatus() == Status.Playing) && !engineRunning) {
|
|
|
|
vehicle.engineAudio.stop();
|
|
|
|
}
|
|
|
|
if ((vehicle.engineAudio.getStatus() != Status.Playing) && engineRunning) {
|
|
|
|
vehicle.engineAudio.play();
|
|
|
|
}
|
|
|
|
|
|
|
|
vehicle.wheelsAudio.updateGeometricState();
|
2017-01-04 00:03:13 +00:00
|
|
|
float wheelRot = Math.abs(vehicle.vehicleControl.getWheel(0).getDeltaRotation() + vehicle.vehicleControl.getWheel(1).getDeltaRotation()) / tpf / 100f;
|
2017-01-03 00:14:35 +00:00
|
|
|
// TODO: pitch
|
|
|
|
//System.out.println("wheel rot: " + wheelRot);
|
|
|
|
//wheelsAudio.setPitch(Math.max(0.5f, Math.min(wheelRot * 4f, 2.0f)));
|
|
|
|
vehicle.wheelsAudio.setVolume(Math.max(0.0001f, Math.min(wheelRot, 1.0f)) - 0.0001f);
|
2017-01-04 00:03:13 +00:00
|
|
|
if ((vehicle.engineAudio.getStatus() == Status.Playing) && wheelRot < 10f) {
|
|
|
|
vehicle.engineAudio.stop();
|
|
|
|
}
|
|
|
|
if ((vehicle.engineAudio.getStatus() != Status.Playing) && wheelRot > 10f) {
|
|
|
|
vehicle.engineAudio.play();
|
|
|
|
}
|
2017-01-03 00:14:35 +00:00
|
|
|
|
|
|
|
vehicle.wheelSlipAudio.updateGeometricState();
|
|
|
|
float slipAll = 0f;
|
|
|
|
for (int i = 0; i < vehicle.vehicleControl.getNumWheels(); i++) {
|
|
|
|
slipAll += vehicle.vehicleControl.getWheel(i).getSkidInfo();
|
|
|
|
}
|
|
|
|
float slip = 1f - (slipAll) / vehicle.vehicleControl.getNumWheels();
|
|
|
|
float wheelSlip = (slip * slip * slip * slip * slip * slip * slip) / tpf / 40f;
|
|
|
|
// TODO: pitch
|
|
|
|
//wheelsAudio.setPitch(Math.max(0.5f, Math.min(wheelRot * 4f, 2.0f)));
|
|
|
|
vehicle.wheelSlipAudio.setVolume(Math.max(0.0001f, Math.min(wheelSlip, 1.0f)) - 0.0001f);
|
2017-01-01 20:47:53 +00:00
|
|
|
}
|
2017-01-03 00:14:35 +00:00
|
|
|
|
|
|
|
listener.setLocation(cam.getLocation());
|
|
|
|
listener.setRotation(cam.getRotation());
|
2016-12-30 23:19:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public void onAction(String binding, boolean value, float tpf) {
|
2017-01-03 00:14:35 +00:00
|
|
|
if (playerVehicleNode == null) {
|
|
|
|
float walkSpeed = 6f;
|
2016-12-31 22:57:29 +00:00
|
|
|
if (binding.equals("Lefts")) {
|
|
|
|
if (value) {
|
|
|
|
walkDir.x -= walkSpeed;
|
|
|
|
} else {
|
|
|
|
walkDir.x += walkSpeed;
|
|
|
|
}
|
|
|
|
} else if (binding.equals("Rights")) {
|
|
|
|
if (value) {
|
|
|
|
walkDir.x += walkSpeed;
|
|
|
|
} else {
|
|
|
|
walkDir.x -= walkSpeed;
|
|
|
|
}
|
|
|
|
} else if (binding.equals("Ups")) {
|
|
|
|
if (value) {
|
|
|
|
walkDir.z -= walkSpeed;
|
|
|
|
} else {
|
|
|
|
walkDir.z += walkSpeed;
|
|
|
|
}
|
|
|
|
} else if (binding.equals("Downs")) {
|
|
|
|
if (value) {
|
|
|
|
walkDir.z += walkSpeed;
|
|
|
|
} else {
|
|
|
|
walkDir.z -= walkSpeed;
|
|
|
|
}
|
2017-01-04 00:03:13 +00:00
|
|
|
} else if (binding.equals("Reset")) {
|
|
|
|
if (value) {
|
|
|
|
System.out.println("Reset - to car");
|
|
|
|
Vector3f playerPos = playerNode.getWorldTranslation();
|
|
|
|
for (VehicleNode vehicle : vehicles) {
|
|
|
|
Vector3f vehiclePos = vehicle.getWorldTranslation();
|
|
|
|
float dist = playerPos.distance(vehiclePos);
|
|
|
|
System.out.println(" .. dist: " + dist);
|
|
|
|
if (dist < 5f) {
|
|
|
|
playerVehicleNode = vehicle;
|
|
|
|
playerNode.removeFromParent();
|
|
|
|
playerNode.setLocalTranslation(0f, 0f, 0f);
|
|
|
|
playerNode.setLocalRotation(Quaternion.DIRECTION_Z);
|
|
|
|
playerNode.removeControl(playerPersonControl);
|
|
|
|
playerVehicleNode.attachChild(playerNode);
|
|
|
|
playerVehicleNode.vehicleInstance.accelerationValue = 0;
|
|
|
|
playerVehicleNode.vehicleInstance.steeringValue = 0;
|
|
|
|
walkDir = new Vector3f();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-12-30 23:19:29 +00:00
|
|
|
}
|
2016-12-31 22:57:29 +00:00
|
|
|
playerPersonControl.setWalkDirection(walkDir);
|
|
|
|
playerPersonControl.setViewDirection(walkDir);
|
|
|
|
} else {
|
|
|
|
float steerMax = 0.5f;
|
2017-01-03 00:14:35 +00:00
|
|
|
if (playerVehicleNode.vehicleInstance.carType == VehicleInstance.TRUCK) {
|
2016-12-31 22:57:29 +00:00
|
|
|
steerMax = 0.7f;
|
2016-12-31 15:58:13 +00:00
|
|
|
}
|
2016-12-31 22:57:29 +00:00
|
|
|
if (binding.equals("Lefts")) {
|
|
|
|
if (value) {
|
2017-01-03 00:14:35 +00:00
|
|
|
playerVehicleNode.vehicleInstance.steeringValue += steerMax;
|
2016-12-31 22:57:29 +00:00
|
|
|
} else {
|
2017-01-03 00:14:35 +00:00
|
|
|
playerVehicleNode.vehicleInstance.steeringValue += -steerMax;
|
2016-12-31 22:57:29 +00:00
|
|
|
}
|
2017-01-03 00:14:35 +00:00
|
|
|
playerVehicleNode.vehicleControl.steer(playerVehicleNode.vehicleInstance.steeringValue);
|
2016-12-31 22:57:29 +00:00
|
|
|
} else if (binding.equals("Rights")) {
|
|
|
|
if (value) {
|
2017-01-03 00:14:35 +00:00
|
|
|
playerVehicleNode.vehicleInstance.steeringValue += -steerMax;
|
2016-12-31 22:57:29 +00:00
|
|
|
} else {
|
2017-01-03 00:14:35 +00:00
|
|
|
playerVehicleNode.vehicleInstance.steeringValue += steerMax;
|
2016-12-31 22:57:29 +00:00
|
|
|
}
|
2017-01-03 00:14:35 +00:00
|
|
|
playerVehicleNode.vehicleControl.steer(playerVehicleNode.vehicleInstance.steeringValue);
|
2016-12-31 22:57:29 +00:00
|
|
|
} else if (binding.equals("Ups")) {
|
|
|
|
if (value) {
|
2017-01-03 00:14:35 +00:00
|
|
|
playerVehicleNode.vehicleInstance.accelerationValue += playerVehicleNode.vehicleInstance.accelerationForce;
|
2016-12-31 22:57:29 +00:00
|
|
|
} else {
|
2017-01-03 00:14:35 +00:00
|
|
|
playerVehicleNode.vehicleInstance.accelerationValue -= playerVehicleNode.vehicleInstance.accelerationForce;
|
2016-12-31 22:57:29 +00:00
|
|
|
}
|
2017-01-03 00:14:35 +00:00
|
|
|
playerVehicleNode.vehicleControl.accelerate(2, playerVehicleNode.vehicleInstance.accelerationValue);
|
|
|
|
playerVehicleNode.vehicleControl.accelerate(3, playerVehicleNode.vehicleInstance.accelerationValue);
|
|
|
|
if (playerVehicleNode.vehicleInstance.carType == VehicleInstance.TRUCK) {
|
|
|
|
playerVehicleNode.vehicleControl.accelerate(4, playerVehicleNode.vehicleInstance.accelerationValue);
|
|
|
|
playerVehicleNode.vehicleControl.accelerate(5, playerVehicleNode.vehicleInstance.accelerationValue);
|
2016-12-31 22:57:29 +00:00
|
|
|
}
|
|
|
|
} else if (binding.equals("Downs")) {
|
|
|
|
float b;
|
|
|
|
if (value) {
|
2017-01-03 00:14:35 +00:00
|
|
|
b = playerVehicleNode.vehicleInstance.brakeForce;
|
2016-12-31 22:57:29 +00:00
|
|
|
} else {
|
|
|
|
b = 0f;
|
|
|
|
}
|
2017-01-03 00:14:35 +00:00
|
|
|
playerVehicleNode.vehicleControl.brake(0, b);
|
|
|
|
playerVehicleNode.vehicleControl.brake(1, b);
|
2016-12-31 22:57:29 +00:00
|
|
|
} else if (binding.equals("Revs")) {
|
|
|
|
if (value) {
|
2017-01-03 00:14:35 +00:00
|
|
|
playerVehicleNode.vehicleInstance.accelerationValue += playerVehicleNode.vehicleInstance.accelerationForce;
|
2016-12-31 22:57:29 +00:00
|
|
|
} else {
|
2017-01-03 00:14:35 +00:00
|
|
|
playerVehicleNode.vehicleInstance.accelerationValue -= playerVehicleNode.vehicleInstance.accelerationForce;
|
2016-12-31 22:57:29 +00:00
|
|
|
}
|
2017-01-03 00:14:35 +00:00
|
|
|
playerVehicleNode.vehicleControl.accelerate(2, -playerVehicleNode.vehicleInstance.accelerationValue);
|
|
|
|
playerVehicleNode.vehicleControl.accelerate(3, -playerVehicleNode.vehicleInstance.accelerationValue);
|
|
|
|
if (playerVehicleNode.vehicleInstance.carType == VehicleInstance.TRUCK) {
|
|
|
|
playerVehicleNode.vehicleControl.accelerate(4, -playerVehicleNode.vehicleInstance.accelerationValue);
|
|
|
|
playerVehicleNode.vehicleControl.accelerate(5, -playerVehicleNode.vehicleInstance.accelerationValue);
|
2016-12-31 22:57:29 +00:00
|
|
|
}
|
|
|
|
} else if (binding.equals("Space")) {
|
|
|
|
if (value) {
|
2017-01-03 00:14:35 +00:00
|
|
|
playerVehicleNode.vehicleControl.applyImpulse(jumpForce, Vector3f.ZERO);
|
2016-12-31 22:57:29 +00:00
|
|
|
}
|
|
|
|
} else if (binding.equals("Reset")) {
|
|
|
|
if (value) {
|
2017-01-04 00:03:13 +00:00
|
|
|
System.out.println("Reset - from car");
|
|
|
|
playerNode.removeFromParent();
|
|
|
|
playerNode.addControl(playerPersonControl);
|
|
|
|
playerPersonControl.warp(playerVehicleNode.getLocalTranslation());
|
|
|
|
rootNode.attachChild(playerNode);
|
|
|
|
playerVehicleNode = null;
|
|
|
|
walkDir = new Vector3f();
|
|
|
|
/*playerVehicleNode.vehicleControl.setPhysicsLocation(Vector3f.ZERO);
|
2017-01-03 00:14:35 +00:00
|
|
|
playerVehicleNode.vehicleControl.setPhysicsRotation(new Matrix3f());
|
|
|
|
playerVehicleNode.vehicleControl.setLinearVelocity(Vector3f.ZERO);
|
|
|
|
playerVehicleNode.vehicleControl.setAngularVelocity(Vector3f.ZERO);
|
2017-01-04 00:03:13 +00:00
|
|
|
playerVehicleNode.vehicleControl.resetSuspension();*/
|
2016-12-31 22:57:29 +00:00
|
|
|
} else {
|
|
|
|
}
|
2016-12-30 23:19:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-12-31 22:12:20 +00:00
|
|
|
|
2016-12-31 22:57:29 +00:00
|
|
|
private Node addPerson() {
|
2016-12-31 22:12:20 +00:00
|
|
|
Spatial personModel = assetManager.loadModel("Models/person.j3o");
|
|
|
|
Node person = new Node("person");
|
|
|
|
person.attachChild(personModel);
|
|
|
|
BetterCharacterControl personControl = new BetterCharacterControl(1f, 4f, 10f);
|
|
|
|
/*personModel.setLocalTranslation(0f, -1f, 0f);
|
|
|
|
BoxCollisionShape personShape = new BoxCollisionShape(new Vector3f(0.5f, 2f, 0.5f));
|
|
|
|
RigidBodyControl personControl = new RigidBodyControl(personShape, 80f);/**/
|
|
|
|
person.addControl(personControl);
|
|
|
|
/**/personControl.setJumpForce(new Vector3f(0,5f,0));
|
|
|
|
personControl.setGravity(new Vector3f(0,1f,0));
|
2017-01-03 00:14:35 +00:00
|
|
|
personControl.warp(new Vector3f(10f + (float)Math.random() * 20f, 30f, 12f + (float)Math.random() * 20f));/**/
|
2016-12-31 22:12:20 +00:00
|
|
|
//personControl.setPhysicsLocation(new Vector3f(10f, 30f, 12f));
|
|
|
|
getPhysicsSpace().add(personControl);
|
2017-01-04 00:03:13 +00:00
|
|
|
//getPhysicsSpace().addAll(person);
|
2016-12-31 22:12:20 +00:00
|
|
|
rootNode.attachChild(person);
|
2017-01-04 00:03:13 +00:00
|
|
|
|
2016-12-31 22:12:20 +00:00
|
|
|
Vector3f dir = new Vector3f((float)Math.random() * 2f - 1f, 0f, (float)Math.random() * 2f - 1f);
|
|
|
|
personControl.setViewDirection(dir);
|
|
|
|
personControl.setWalkDirection(dir);
|
2016-12-31 22:57:29 +00:00
|
|
|
|
|
|
|
return person;
|
|
|
|
}
|
|
|
|
|
2017-01-03 00:14:35 +00:00
|
|
|
private void addPlayer()
|
2016-12-31 22:57:29 +00:00
|
|
|
{
|
|
|
|
playerNode = addPerson();
|
|
|
|
playerPersonControl = playerNode.getControl(BetterCharacterControl.class);
|
2016-12-31 22:12:20 +00:00
|
|
|
}
|
2017-01-01 14:23:58 +00:00
|
|
|
|
|
|
|
private void addMap() {
|
|
|
|
map = assetManager.loadModel("Scenes/TestMap.j3o");
|
|
|
|
rootNode.attachChild(map);
|
|
|
|
getPhysicsSpace().addAll(map);
|
|
|
|
}
|
2016-12-30 23:19:29 +00:00
|
|
|
}
|
2017-01-04 00:03:13 +00:00
|
|
|
|