1
0
mirror of https://github.com/Dejvino/roadtrip synced 2024-11-21 23:42:35 +00:00

Refactor: major moving around, trying to minimize RoadTrip.java

(TODO: enable vehicles back)
This commit is contained in:
Dejvino 2017-01-14 03:01:01 +01:00
parent 33f2e768cc
commit a2af1e02bb
8 changed files with 254 additions and 179 deletions

View File

@ -25,33 +25,18 @@ import com.jme3.math.FastMath;
import com.jme3.math.Quaternion; import com.jme3.math.Quaternion;
import com.jme3.math.Vector3f; import com.jme3.math.Vector3f;
import com.jme3.renderer.queue.RenderQueue; import com.jme3.renderer.queue.RenderQueue;
import com.jme3.scene.CameraNode;
import com.jme3.scene.Geometry; import com.jme3.scene.Geometry;
import com.jme3.scene.Node; import com.jme3.scene.Node;
import com.jme3.scene.Spatial; import com.jme3.scene.Spatial;
import com.jme3.scene.control.CameraControl.ControlDirection;
import com.jme3.scene.debug.Arrow; import com.jme3.scene.debug.Arrow;
import com.jme3.scene.shape.Box; import com.jme3.scene.shape.Box;
import com.jme3.scene.shape.Cylinder; import com.jme3.scene.shape.Cylinder;
import com.jme3.scene.shape.Quad;
import com.jme3.terrain.geomipmap.TerrainGrid;
import com.jme3.terrain.geomipmap.TerrainGridListener; import com.jme3.terrain.geomipmap.TerrainGridListener;
import com.jme3.terrain.geomipmap.TerrainGridLodControl;
import com.jme3.terrain.geomipmap.TerrainLodControl;
import com.jme3.terrain.geomipmap.TerrainQuad; import com.jme3.terrain.geomipmap.TerrainQuad;
import com.jme3.terrain.geomipmap.grid.FractalTileLoader; import roadtrip.model.VehicleInstance;
import com.jme3.terrain.geomipmap.lodcalc.DistanceLodCalculator; import roadtrip.view.GameWorldView;
import com.jme3.terrain.noise.ShaderUtils; import roadtrip.view.VehicleNode;
import com.jme3.terrain.noise.basis.FilteredBasis; import roadtrip.view.model.Player;
import com.jme3.terrain.noise.filter.IterativeFilter;
import com.jme3.terrain.noise.filter.OptimizedErode;
import com.jme3.terrain.noise.filter.PerturbFilter;
import com.jme3.terrain.noise.filter.SmoothFilter;
import com.jme3.terrain.noise.fractal.FractalSum;
import com.jme3.terrain.noise.modulator.NoiseModulator;
import com.jme3.texture.Texture;
import java.util.LinkedList;
import java.util.List;
/** /**
* *
@ -59,44 +44,21 @@ import java.util.List;
*/ */
public class RoadTrip extends SimpleApplication implements ActionListener { public class RoadTrip extends SimpleApplication implements ActionListener {
public static void main(String[] args) public static void main(String[] args) {
{
RoadTrip app = new RoadTrip(); RoadTrip app = new RoadTrip();
app.start(); app.start();
} }
public static boolean DEBUG = false;//true; public static boolean DEBUG = false;//true;
private BulletAppState bulletAppState; private BulletAppState bulletAppState;
private GameWorldView gameWorldView;
private ChaseCamera chaseCam; private ChaseCamera chaseCam;
// START Terrain
private Material mat_terrain;
private TerrainGrid terrain;
private float grassScale = 64;
private float dirtScale = 64;
private float rockScale = 64;
private FractalSum base;
private PerturbFilter perturb;
private OptimizedErode therm;
private SmoothFilter smooth;
private IterativeFilter iterate;
// END Terrain
private List<VehicleNode> vehicles = new LinkedList<>();
private static class Player
{
Node node;
BetterCharacterControl characterControl;
Vector3f jumpForce = new Vector3f(0, 3000, 0);
Vector3f walkDir = new Vector3f();
VehicleNode vehicleNode;
}
private Player player = new Player(); private Player player = new Player();
private Vector3f journeyTarget = new Vector3f(50, 0f, 50f); private Vector3f journeyTarget = new Vector3f(50, 0f, 50f);
private Node targetNode; private Node targetNode;
private Node compassNode; private Node compassNode;
@ -126,13 +88,37 @@ public class RoadTrip extends SimpleApplication implements ActionListener {
dl.setDirection(new Vector3f(1, -1, 1)); dl.setDirection(new Vector3f(1, -1, 1));
rootNode.addLight(dl); rootNode.addLight(dl);
addMap(); gameWorldView = GameWorldView.create(assetManager, cam, rootNode);
gameWorldView.terrain.terrainGrid.addListener(new TerrainGridListener() {
addCar();
addCar(); @Override
public void gridMoved(Vector3f newCenter) {
}
@Override
public void tileAttached(Vector3f cell, TerrainQuad quad) {
while(quad.getControl(RigidBodyControl.class)!=null){
quad.removeControl(RigidBodyControl.class);
}
quad.addControl(new RigidBodyControl(new HeightfieldCollisionShape(quad.getHeightMap(), gameWorldView.terrain.terrainGrid.getLocalScale()), 0));
bulletAppState.getPhysicsSpace().add(quad);
}
@Override
public void tileDetached(Vector3f cell, TerrainQuad quad) {
if (quad.getControl(RigidBodyControl.class) != null) {
bulletAppState.getPhysicsSpace().remove(quad);
quad.removeControl(RigidBodyControl.class);
}
}
});
/*addCar();
addCar(); addCar();
addCar(); addCar();
addCar(); addCar();
addCar();*/
addPerson(); addPerson();
addPerson(); addPerson();
addPerson(); addPerson();
@ -140,7 +126,7 @@ public class RoadTrip extends SimpleApplication implements ActionListener {
addPerson(); addPerson();
addPerson(); addPerson();
addPerson(); addPerson();
addPlayer(); addPlayer();
addTarget(); addTarget();
@ -171,7 +157,7 @@ public class RoadTrip extends SimpleApplication implements ActionListener {
inputManager.addListener(this, "Esc"); inputManager.addListener(this, "Esc");
} }
private void addCar() /*private void addCar()
{ {
Node vehicleModel = new Node("VehicleModel"); Node vehicleModel = new Node("VehicleModel");
VehicleInstance vehicleInstance = VehicleInstance.createVehicle(vehicles.size() % VehicleInstance.getVehicleTypesCount()); VehicleInstance vehicleInstance = VehicleInstance.createVehicle(vehicles.size() % VehicleInstance.getVehicleTypesCount());
@ -351,7 +337,7 @@ public class RoadTrip extends SimpleApplication implements ActionListener {
vehicles.add(vehicle); vehicles.add(vehicle);
rootNode.attachChild(vehicle); rootNode.attachChild(vehicle);
} }*/
private Node addPerson() { private Node addPerson() {
Spatial personModel = assetManager.loadModel("Models/person.j3o"); Spatial personModel = assetManager.loadModel("Models/person.j3o");
@ -435,117 +421,7 @@ public class RoadTrip extends SimpleApplication implements ActionListener {
rootNode.attachChild(compassNode); rootNode.attachChild(compassNode);
} }
private void addMap() {
// TERRAIN TEXTURE material
this.mat_terrain = new Material(this.assetManager, "Common/MatDefs/Terrain/HeightBasedTerrain.j3md");
// Parameters to material:
// regionXColorMap: X = 1..4 the texture that should be appliad to state X
// regionX: a Vector3f containing the following information:
// regionX.x: the start height of the region
// regionX.y: the end height of the region
// regionX.z: the texture scale for the region
// it might not be the most elegant way for storing these 3 values, but it packs the data nicely :)
// slopeColorMap: the texture to be used for cliffs, and steep mountain sites
// slopeTileFactor: the texture scale for slopes
// terrainSize: the total size of the terrain (used for scaling the texture)
// GRASS texture
Texture grass = this.assetManager.loadTexture("Textures/solid-grass.png");
grass.setWrap(Texture.WrapMode.Repeat);
Texture dirt = this.assetManager.loadTexture("Textures/solid-road.png");
dirt.setWrap(Texture.WrapMode.Repeat);
Texture rock = this.assetManager.loadTexture("Textures/solid-stone.png");
rock.setWrap(Texture.WrapMode.Repeat);
this.mat_terrain.setTexture("region1ColorMap", dirt);
this.mat_terrain.setVector3("region1", new Vector3f(0, 80, this.dirtScale));
this.mat_terrain.setTexture("region2ColorMap", grass);
this.mat_terrain.setVector3("region2", new Vector3f(100, 160, this.grassScale));
this.mat_terrain.setTexture("region3ColorMap", rock);
this.mat_terrain.setVector3("region3", new Vector3f(190, 240, this.rockScale));
this.mat_terrain.setTexture("region4ColorMap", dirt);
this.mat_terrain.setVector3("region4", new Vector3f(250, 350, this.dirtScale));
this.mat_terrain.setTexture("slopeColorMap", rock);
this.mat_terrain.setFloat("slopeTileFactor", 32);
this.mat_terrain.setFloat("terrainSize", 513);
this.base = new FractalSum();
this.base.setRoughness(0.7f);
this.base.setFrequency(1.0f);
this.base.setAmplitude(1.0f);
this.base.setLacunarity(2.12f);
this.base.setOctaves(8);
this.base.setScale(0.02125f);
this.base.addModulator(new NoiseModulator() {
@Override
public float value(float... in) {
return ShaderUtils.clamp(in[0] * 0.5f + 0.5f, 0, 1);
}
});
FilteredBasis ground = new FilteredBasis(this.base);
this.perturb = new PerturbFilter();
this.perturb.setMagnitude(0.119f);
this.therm = new OptimizedErode();
this.therm.setRadius(5);
this.therm.setTalus(0.011f);
this.smooth = new SmoothFilter();
this.smooth.setRadius(1);
this.smooth.setEffect(0.7f);
this.iterate = new IterativeFilter();
this.iterate.addPreFilter(this.perturb);
this.iterate.addPostFilter(this.smooth);
this.iterate.setFilter(this.therm);
this.iterate.setIterations(2);
ground.addPreFilter(this.iterate);
this.terrain = new TerrainGrid("terrain", 64 + 1, 256 + 1, new FractalTileLoader(ground, 300f));
this.terrain.setMaterial(this.mat_terrain);
this.terrain.setLocalTranslation(0, -200, 0);
this.terrain.setLocalScale(2f, 1f, 2f);
this.rootNode.attachChild(this.terrain);
TerrainLodControl control = new TerrainGridLodControl(this.terrain, this.getCamera());
control.setLodCalculator(new DistanceLodCalculator(64 + 1, 2.7f)); // patch size, and a multiplier
this.terrain.addControl(control);
terrain.addListener(new TerrainGridListener() {
@Override
public void gridMoved(Vector3f newCenter) {
}
@Override
public void tileAttached(Vector3f cell, TerrainQuad quad) {
while(quad.getControl(RigidBodyControl.class)!=null){
quad.removeControl(RigidBodyControl.class);
}
quad.addControl(new RigidBodyControl(new HeightfieldCollisionShape(quad.getHeightMap(), terrain.getLocalScale()), 0));
bulletAppState.getPhysicsSpace().add(quad);
}
@Override
public void tileDetached(Vector3f cell, TerrainQuad quad) {
if (quad.getControl(RigidBodyControl.class) != null) {
bulletAppState.getPhysicsSpace().remove(quad);
quad.removeControl(RigidBodyControl.class);
}
}
});
}
@Override @Override
public void simpleUpdate(float tpf) { public void simpleUpdate(float tpf) {
@ -554,7 +430,7 @@ public class RoadTrip extends SimpleApplication implements ActionListener {
/*cam.setLocation(new Vector3f(cam.getLocation()).interpolate(newLocation, Math.min(tpf, 1f))); /*cam.setLocation(new Vector3f(cam.getLocation()).interpolate(newLocation, Math.min(tpf, 1f)));
cam.lookAt(playerLocation, Vector3f.UNIT_Y);*/ cam.lookAt(playerLocation, Vector3f.UNIT_Y);*/
for (VehicleNode vehicle : vehicles) { /*for (VehicleNode vehicle : gameWorldState.vehicles) {
vehicle.vehicleInstance.accelerationSmooth = (vehicle.vehicleInstance.accelerationSmooth + vehicle.vehicleInstance.accelerationValue * (tpf * 10f)) / (1 + tpf * 10f); vehicle.vehicleInstance.accelerationSmooth = (vehicle.vehicleInstance.accelerationSmooth + vehicle.vehicleInstance.accelerationValue * (tpf * 10f)) / (1 + tpf * 10f);
vehicle.engineAudio.setVelocity(new Vector3f(0, 0, 0)); vehicle.engineAudio.setVelocity(new Vector3f(0, 0, 0));
vehicle.engineAudio.updateGeometricState(); vehicle.engineAudio.updateGeometricState();
@ -590,7 +466,7 @@ public class RoadTrip extends SimpleApplication implements ActionListener {
// TODO: pitch // TODO: pitch
//wheelsAudio.setPitch(Math.max(0.5f, Math.min(wheelRot * 4f, 2.0f))); //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); vehicle.wheelSlipAudio.setVolume(Math.max(0.0001f, Math.min(wheelSlip, 1.0f)) - 0.0001f);
} }*/
listener.setLocation(cam.getLocation()); listener.setLocation(cam.getLocation());
listener.setRotation(cam.getRotation()); listener.setRotation(cam.getRotation());
@ -651,7 +527,7 @@ public class RoadTrip extends SimpleApplication implements ActionListener {
if (value) { if (value) {
System.out.println("Reset - to car"); System.out.println("Reset - to car");
Vector3f playerPos = player.node.getWorldTranslation(); Vector3f playerPos = player.node.getWorldTranslation();
for (VehicleNode vehicle : vehicles) { /*for (VehicleNode vehicle : vehicles) {
Vector3f vehiclePos = vehicle.getWorldTranslation(); Vector3f vehiclePos = vehicle.getWorldTranslation();
float dist = playerPos.distance(vehiclePos); float dist = playerPos.distance(vehiclePos);
System.out.println(" .. dist: " + dist); System.out.println(" .. dist: " + dist);
@ -668,11 +544,11 @@ public class RoadTrip extends SimpleApplication implements ActionListener {
player.walkDir = new Vector3f(); player.walkDir = new Vector3f();
break; break;
} }
} }*/
} }
} }
} else { } else {
VehicleInstance playerVehicle = player.vehicleNode.vehicleInstance; /*VehicleInstance playerVehicle = player.vehicleNode.vehicleInstance;
VehicleControl playerVehicleControl = player.vehicleNode.vehicleControl; VehicleControl playerVehicleControl = player.vehicleNode.vehicleControl;
int playerCarType = playerVehicle.carType; int playerCarType = playerVehicle.carType;
float steerMax = 0.5f; float steerMax = 0.5f;
@ -739,14 +615,9 @@ public class RoadTrip extends SimpleApplication implements ActionListener {
rootNode.attachChild(player.node); rootNode.attachChild(player.node);
player.vehicleNode = null; player.vehicleNode = null;
player.walkDir = new Vector3f(); player.walkDir = new Vector3f();
/*playerVehicleControl.setPhysicsLocation(Vector3f.ZERO);
playerVehicleControl.setPhysicsRotation(new Matrix3f());
playerVehicleControl.setLinearVelocity(Vector3f.ZERO);
playerVehicleControl.setAngularVelocity(Vector3f.ZERO);
playerVehicleControl.resetSuspension();*/
} else { } else {
} }
} }*/
} }
if (binding.equals("Esc")) { if (binding.equals("Esc")) {
stop(); stop();

View File

@ -0,0 +1,18 @@
package roadtrip.model;
import com.jme3.terrain.noise.filter.IterativeFilter;
import com.jme3.terrain.noise.filter.OptimizedErode;
import com.jme3.terrain.noise.filter.PerturbFilter;
import com.jme3.terrain.noise.filter.SmoothFilter;
import com.jme3.terrain.noise.fractal.FractalSum;
/**
* Created by dejvino on 14.01.2017.
*/
public class TerrainDataProvider {
public FractalSum base;
public PerturbFilter perturb;
public OptimizedErode therm;
public SmoothFilter smooth;
public IterativeFilter iterate;
}

View File

@ -1,6 +1,6 @@
/* /*
*/ */
package roadtrip; package roadtrip.model;
/** /**
* *

View File

@ -0,0 +1,134 @@
package roadtrip.view;
import com.jme3.asset.AssetManager;
import com.jme3.bullet.collision.shapes.HeightfieldCollisionShape;
import com.jme3.bullet.control.RigidBodyControl;
import com.jme3.material.Material;
import com.jme3.math.Vector3f;
import com.jme3.renderer.Camera;
import com.jme3.scene.Node;
import com.jme3.terrain.geomipmap.*;
import com.jme3.terrain.geomipmap.grid.FractalTileLoader;
import com.jme3.terrain.geomipmap.lodcalc.DistanceLodCalculator;
import com.jme3.terrain.noise.ShaderUtils;
import com.jme3.terrain.noise.basis.FilteredBasis;
import com.jme3.terrain.noise.filter.IterativeFilter;
import com.jme3.terrain.noise.filter.OptimizedErode;
import com.jme3.terrain.noise.filter.PerturbFilter;
import com.jme3.terrain.noise.filter.SmoothFilter;
import com.jme3.terrain.noise.fractal.FractalSum;
import com.jme3.terrain.noise.modulator.NoiseModulator;
import com.jme3.texture.Texture;
import roadtrip.model.TerrainDataProvider;
import roadtrip.view.model.GameWorldState;
/**
* Created by dejvino on 14.01.2017.
*/
public class GameWorldView {
private GameWorldState state = new GameWorldState();
private AssetManager assetManager;
private Camera camera;
private Node rootNode;
public TerrainView terrain = new TerrainView(new TerrainDataProvider());
public static GameWorldView create(AssetManager assetManager, Camera camera, Node rootNode) {
GameWorldView gameWorldView = new GameWorldView();
gameWorldView.assetManager = assetManager;
gameWorldView.camera = camera;
gameWorldView.rootNode = rootNode;
gameWorldView.initialize();
return gameWorldView;
}
private void initialize()
{
// TERRAIN TEXTURE material
terrain.mat_terrain = new Material(assetManager, "Common/MatDefs/Terrain/HeightBasedTerrain.j3md");
// Parameters to material:
// regionXColorMap: X = 1..4 the texture that should be appliad to state X
// regionX: a Vector3f containing the following information:
// regionX.x: the start height of the region
// regionX.y: the end height of the region
// regionX.z: the texture scale for the region
// it might not be the most elegant way for storing these 3 values, but it packs the data nicely :)
// slopeColorMap: the texture to be used for cliffs, and steep mountain sites
// slopeTileFactor: the texture scale for slopes
// terrainSize: the total size of the terrain (used for scaling the texture)
// GRASS texture
Texture grass = this.assetManager.loadTexture("Textures/solid-grass.png");
grass.setWrap(Texture.WrapMode.Repeat);
Texture dirt = this.assetManager.loadTexture("Textures/solid-road.png");
dirt.setWrap(Texture.WrapMode.Repeat);
Texture rock = this.assetManager.loadTexture("Textures/solid-stone.png");
rock.setWrap(Texture.WrapMode.Repeat);
terrain.mat_terrain.setTexture("region1ColorMap", dirt);
terrain.mat_terrain.setVector3("region1", new Vector3f(0, 80, terrain.dirtScale));
terrain.mat_terrain.setTexture("region2ColorMap", grass);
terrain.mat_terrain.setVector3("region2", new Vector3f(100, 160, terrain.grassScale));
terrain.mat_terrain.setTexture("region3ColorMap", rock);
terrain.mat_terrain.setVector3("region3", new Vector3f(190, 240, terrain.rockScale));
terrain.mat_terrain.setTexture("region4ColorMap", dirt);
terrain.mat_terrain.setVector3("region4", new Vector3f(250, 350, terrain.dirtScale));
terrain.mat_terrain.setTexture("slopeColorMap", rock);
terrain.mat_terrain.setFloat("slopeTileFactor", 32);
terrain.mat_terrain.setFloat("terrainSize", 513);
terrain.terrainDataProvider.base = new FractalSum();
terrain.terrainDataProvider.base.setRoughness(0.7f);
terrain.terrainDataProvider.base.setFrequency(1.0f);
terrain.terrainDataProvider.base.setAmplitude(1.0f);
terrain.terrainDataProvider.base.setLacunarity(2.12f);
terrain.terrainDataProvider.base.setOctaves(8);
terrain.terrainDataProvider.base.setScale(0.02125f);
terrain.terrainDataProvider.base.addModulator(new NoiseModulator() {
@Override
public float value(float... in) {
return ShaderUtils.clamp(in[0] * 0.5f + 0.5f, 0, 1);
}
});
FilteredBasis ground = new FilteredBasis(terrain.terrainDataProvider.base);
terrain.terrainDataProvider.perturb = new PerturbFilter();
terrain.terrainDataProvider.perturb.setMagnitude(0.119f);
terrain.terrainDataProvider.therm = new OptimizedErode();
terrain.terrainDataProvider.therm.setRadius(5);
terrain.terrainDataProvider.therm.setTalus(0.011f);
terrain.terrainDataProvider.smooth = new SmoothFilter();
terrain.terrainDataProvider.smooth.setRadius(1);
terrain.terrainDataProvider.smooth.setEffect(0.7f);
terrain.terrainDataProvider.iterate = new IterativeFilter();
terrain.terrainDataProvider.iterate.addPreFilter(terrain.terrainDataProvider.perturb);
terrain.terrainDataProvider.iterate.addPostFilter(terrain.terrainDataProvider.smooth);
terrain.terrainDataProvider.iterate.setFilter(terrain.terrainDataProvider.therm);
terrain.terrainDataProvider.iterate.setIterations(2);
ground.addPreFilter(terrain.terrainDataProvider.iterate);
terrain.terrainGrid = new TerrainGrid("terrain", 64 + 1, 256 + 1, new FractalTileLoader(ground, 300f));
terrain.terrainGrid.setMaterial(terrain.mat_terrain);
terrain.terrainGrid.setLocalTranslation(0, -200, 0);
terrain.terrainGrid.setLocalScale(2f, 1f, 2f);
this.rootNode.attachChild(terrain.terrainGrid);
TerrainLodControl control = new TerrainGridLodControl(terrain.terrainGrid, camera);
control.setLodCalculator(new DistanceLodCalculator(64 + 1, 2.7f)); // patch size, and a multiplier
terrain.terrainGrid.addControl(control);
}
}

View File

@ -0,0 +1,21 @@
package roadtrip.view;
import com.jme3.material.Material;
import com.jme3.terrain.geomipmap.TerrainGrid;
import roadtrip.model.TerrainDataProvider;
/**
* Created by dejvino on 14.01.2017.
*/
public class TerrainView {
public Material mat_terrain;
public TerrainGrid terrainGrid;
public float grassScale = 64;
public float dirtScale = 64;
public float rockScale = 64;
public TerrainDataProvider terrainDataProvider;
public TerrainView(TerrainDataProvider terrainDataProvider) {
this.terrainDataProvider = terrainDataProvider;
}
}

View File

@ -1,11 +1,12 @@
/* /*
*/ */
package roadtrip; package roadtrip.view;
import com.jme3.audio.AudioNode; import com.jme3.audio.AudioNode;
import com.jme3.bullet.control.VehicleControl; import com.jme3.bullet.control.VehicleControl;
import com.jme3.scene.Node; import com.jme3.scene.Node;
import com.jme3.scene.Spatial; import com.jme3.scene.Spatial;
import roadtrip.model.VehicleInstance;
/** /**
* *

View File

@ -0,0 +1,13 @@
package roadtrip.view.model;
import roadtrip.view.VehicleNode;
import java.util.LinkedList;
import java.util.List;
/**
* Created by dejvino on 14.01.2017.
*/
public class GameWorldState {
private List<VehicleNode> vehicles = new LinkedList<>();
}

View File

@ -0,0 +1,17 @@
package roadtrip.view.model;
import com.jme3.bullet.control.BetterCharacterControl;
import com.jme3.math.Vector3f;
import com.jme3.scene.Node;
import roadtrip.view.VehicleNode;
/**
* Created by dejvino on 14.01.2017.
*/
public class Player {
public Node node;
public BetterCharacterControl characterControl;
public Vector3f jumpForce = new Vector3f(0, 3000, 0);
public Vector3f walkDir = new Vector3f();
public VehicleNode vehicleNode;
}