Hello all,
I am trying to develop a simple racing game with the model/controls used in the fancycarmodeI, but I am having issues with the vehicle model I am using passing through the floor of the j3o scene.
My code is as follows:
package mygame;
import com.jme3.bullet.collision.shapes.BoxCollisionShape;
import com.jme3.bullet.collision.shapes.CompoundCollisionShape;
import com.jme3.scene.shape.Cylinder;
import com.jme3.app.SimpleApplication;
import com.jme3.bullet.BulletAppState;
import com.jme3.bullet.control.RigidBodyControl;
import com.jme3.bullet.control.VehicleControl;
import com.jme3.bullet.util.CollisionShapeFactory;
import com.jme3.input.KeyInput;
import com.jme3.input.controls.KeyTrigger;
import com.jme3.input.controls.ActionListener;
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.Vector3f;
import com.jme3.renderer.queue.RenderQueue;
import com.jme3.scene.Geometry;
import com.jme3.scene.Node;
import com.jme3.scene.Spatial;
import com.jme3.shadow.BasicShadowRenderer;
public class Main extends SimpleApplication implements ActionListener {
private Spatial sceneModel;
private RigidBodyControl landscape;
private RigidBodyControl player_phy;
private BulletAppState bulletAppState;
private Node carNode;
private VehicleControl player;
private static float steeringValue = 0f;
private static float accelerationValue = 0f;
private final float accelerationForce = 1000.0f;
private final float brakeForce = 100.0f;
private Vector3f jumpForce = new Vector3f(0, 3000, 0);
public static void main(String[] args) {
Main app = new Main();
app.start();
}
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));
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");
inputManager.addListener(this, "Space");
inputManager.addListener(this, "Reset");
}
@Override
public void simpleInitApp() {
setupKeys();
bulletAppState = new BulletAppState();
stateManager.attach(bulletAppState);
if (settings.getRenderer().startsWith("LWJGL")) {
BasicShadowRenderer bsr = new BasicShadowRenderer(assetManager, 512);
bsr.setDirection(new Vector3f(-0.5f, -0.3f, -0.3f).normalizeLocal());
viewPort.addProcessor(bsr);
}
cam.setFrustumFar(150f);
flyCam.setMoveSpeed(30f);
initScene();
initLight();
com.jme3.bullet.collision.shapes.CollisionShape sceneShape =
CollisionShapeFactory.createMeshShape((Node) sceneModel);
landscape = new RigidBodyControl(sceneShape, 0);
sceneModel.addControl(landscape);
buildPlayer();
}
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;
}
public void buildPlayer() {
float stiffness = 120.0f;//200=f1 car
float compValue = 0.2f; //(lower than damp!)
float dampValue = 0.3f;
final float mass = 400;
CompoundCollisionShape compoundShape = new CompoundCollisionShape();
BoxCollisionShape box = new BoxCollisionShape(new Vector3f(1.2f,0.5f,2.4f));
compoundShape.addChildShape(box, new Vector3f(0,1,0));
carNode = (Node)assetManager.loadModel("Models/Ferrari/Car.scene");
carNode.setShadowMode(RenderQueue.ShadowMode.Cast);
Geometry chasis = findGeom(carNode, "Car");
Cylinder wheelMesh = new Cylinder(16, 16, 0.5f, 0.5f * 0.6f, true);
player_phy = new RigidBodyControl();
chasis.addControl(player_phy);
com.jme3.bullet.collision.shapes.CollisionShape carHull = CollisionShapeFactory.createDynamicMeshShape(chasis);
player = new VehicleControl(carHull, mass);
carNode.addControl(player);
player.setSuspensionCompression(compValue * 2.0f * FastMath.sqrt(stiffness));
player.setSuspensionDamping(dampValue * 2.0f * FastMath.sqrt(stiffness));
player.setSuspensionStiffness(stiffness);
player.setMaxSuspensionForce(10000);
Material mat = new Material(getAssetManager (), "Common/MatDefs/Misc/ColoredTextured.j3md");
mat.setColor("Color", ColorRGBA.BlackNoAlpha);
Vector3f wheelDirection = new Vector3f(0, -1, 0);
Vector3f wheelAxle = new Vector3f(-1, 0, 0);
float radius = 0.5f;
float restLength = 0.3f;
float yOff = 0.5f;
float xOff = 1f;
float zOff = 2f;
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);
player.addWheel(node1, new Vector3f(-xOff, yOff, zOff),
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);
player.addWheel(node2, new Vector3f(xOff, yOff, zOff),
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);
player.addWheel(node3, new Vector3f(-xOff, yOff, -zOff),
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);
player.addWheel(node4, new Vector3f(xOff, yOff, -zOff),
wheelDirection, wheelAxle, restLength, radius, false);
carNode.attachChild(node1);
carNode.attachChild(node2);
carNode.attachChild(node3);
carNode.attachChild(node4);
player.getWheel(2).setFrictionSlip(4);
player.getWheel(3).setFrictionSlip(4);
bulletAppState.getPhysicsSpace().add(player);
rootNode.attachChild(carNode);
}
public void initScene(){
sceneModel = assetManager.loadModel("Scenes/Scene.j3o");
rootNode.attachChild(sceneModel);
}
public void initLight(){
DirectionalLight sun = new DirectionalLight();
sun.setDirection((new Vector3f(-0.5f, -0.5f, -0.5f)).normalizeLocal());
sun.setColor(ColorRGBA.White);
rootNode.addLight(sun);
}
public void onAction(String binding, boolean value, float tpf) {
if (binding.equals(“Lefts”)) {
if (value) { steeringValue += .5f; } else { steeringValue += -.5f; }
player.steer(steeringValue);
} else if (binding.equals(“Rights”)) {
if (value) { steeringValue += -.5f; } else { steeringValue += .5f; }
player.steer(steeringValue);
} else if (binding.equals(“Ups”)) {
if (value) {
accelerationValue += accelerationForce;
} else {
accelerationValue -= accelerationForce;
}
player.accelerate(accelerationValue);
} else if (binding.equals(“Downs”)) {
if (value) { player.brake(brakeForce); } else { player.brake(0f); }
} else if (binding.equals(“Space”)) {
if (value) {
player.applyImpulse(jumpForce, Vector3f.ZERO);
}
} 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 {
}
}
}
@Override
public void simpleUpdate(float tpf) {
cam.lookAt(carNode.getWorldTranslation(), Vector3f.UNIT_Y);
}
}