Hi everyone! First, i’m french so i hope you’ll understand what i says. I’m pretty new on jmonkey and i try to learn how it work progressively. I’m trying to make an arcade car, i mean the car will not leave the ground (except for jump) and will not drift.
I can’t do this, my car bouce and turn around if i turn at “too high speed”.
I’m asking your help, does anyone ever tried to do same?
Here’s a video of what i mean:
http://www.youtube.com/watch?v=-pUPBYske5E
And there's my java code:
[java]package mygame;
import com.jme3.app.SimpleApplication;
import com.jme3.math.Vector3f;
import com.jme3.math.FastMath;
import com.jme3.renderer.RenderManager;
import com.jme3.light.AmbientLight;
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.ActionListener;
import com.jme3.input.controls.KeyTrigger;
import com.jme3.math.ColorRGBA;
import com.jme3.scene.Node;
import com.jme3.scene.Spatial;
import com.jme3.scene.CameraNode;
import com.jme3.scene.control.CameraControl.ControlDirection;
/**
* test
* @author normenhansen
*/
public class Main extends SimpleApplication implements ActionListener{
private int speed = 0, accelerate = 500,maxSpeed = 100,minSpeed = 0;
private float angle = 0,rotation = 0.5f,maxAngle = 1.0f,minAngle = -1.0f,wheelRadius;
private boolean isRunning = true;
private RigidBodyControl trackControl;
private VehicleControl carControl;
private BulletAppState bulletAppState;
private Node trackNode;
private Node player;
private Spatial carNode;
private CameraNode camNode;
public static void main(String[] args) {
Main app = new Main();
app.start();
}
@Override
public void simpleInitApp() {
//BulletAppSpace collision
bulletAppState = new BulletAppState();
stateManager.attach(bulletAppState);
bulletAppState.getPhysicsSpace().enableDebug(assetManager);
flyCam.setEnabled(false);
camNode = new CameraNode("Camera Node", cam);
camNode.setControlDir(ControlDirection.SpatialToCamera);
buildTrack();
buildCar();
Vector3f test = new Vector3f();
test.set(carNode.getLocalTranslation());
camNode.setLocalTranslation(new Vector3f(test.x,test.y +10,test.z - 50));
initKeys();
addLight();
}
@Override
public void simpleUpdate(float tpf) {
updateCam();
}
@Override
public void simpleRender(RenderManager rm) {
//TODO: add render code
}
public void updateCam(){
Vector3f test = new Vector3f();
test.set(carNode.getLocalTranslation());
}
public void buildTrack() {
trackNode = (Node) assetManager.loadModel("Scenes/track/MountainValley_Track.j3o");
trackControl = new RigidBodyControl(CollisionShapeFactory.createMeshShape(trackNode),0);
trackNode.addControl(trackControl);
rootNode.attachChild(trackNode);
}
public void addLight() {
AmbientLight light = new AmbientLight();
light.setColor(ColorRGBA.White.mult(1.3f));
rootNode.addLight(light);
}
public void buildCar() {
float stiffness = 120.0f;
float compValue = 0.2f;
float dampValue = 0.3f;
final float mass = 400;
player = new Node("player");
carNode = assetManager.loadModel("Models/kart/kart_test_1.j3o");
carControl = carNode.getControl(VehicleControl.class);
carControl.setMass(mass);
carControl.setSuspensionCompression(compValue * 2.0f * FastMath.sqrt(stiffness));
carControl.setSuspensionDamping(dampValue * 2.0f * FastMath.sqrt(stiffness));
carControl.setSuspensionStiffness(stiffness);
carControl.setFriction(20);
carControl.setMaxSuspensionForce(10000);
player.attachChild(carNode);
player.attachChild(camNode);
player.addControl(carControl);
carControl.setPhysicsLocation(new Vector3f(-95,10,120));
rootNode.attachChild(player);
bulletAppState.getPhysicsSpace().add(carControl);
bulletAppState.getPhysicsSpace().add(trackNode);
}
private void initKeys(){
//Mappage des touches
inputManager.addMapping("Pause", new KeyTrigger(KeyInput.KEY_P));
inputManager.addMapping("Up", new KeyTrigger(KeyInput.KEY_UP));
inputManager.addMapping("Down", new KeyTrigger(KeyInput.KEY_DOWN));
inputManager.addMapping("Left", new KeyTrigger(KeyInput.KEY_LEFT));
inputManager.addMapping("Right", new KeyTrigger(KeyInput.KEY_RIGHT));
//On ajoute les nom au listenner
inputManager.addListener(this,"Pause");
inputManager.addListener(this,"Up");
inputManager.addListener(this,"Down");
inputManager.addListener(this,"Left");
inputManager.addListener(this,"Right");
}
public void onAction(String name, boolean keyPressed, float tpf){
if(isRunning){
if(name.equals("Pause") && !keyPressed) {
}else if(name.equals("Right")){
if(keyPressed){
angle += -rotation;
}else {
angle += rotation;
}
carControl.steer(angle);
}else if(name.equals("Left")){
if(keyPressed){
angle += rotation;
}else{
angle += -rotation;
}
carControl.steer(angle);
}else if(name.equals("Up")){
if(keyPressed){
speed += accelerate;
}else{
speed += -accelerate;
}
}
carControl.accelerate(speed);
}else if(name.equals("Down")){
if(keyPressed){
}else{
}
}
}
}[/java]