[SOLVED] VehicleControl accelerate

Hello, i tried making a car using vehicleControl. When i put all wheels together the vehicleControl doesnt react to .accelerate()
this is the first time im using vehicleControl and i couldnt find much on this issue anywhere.
here are some screens(to show that the wheels are actually physically connected + the terrain is also a physical body with mass 0) and code

package mygame;

import com.jme3.app.SimpleApplication;
import com.jme3.bullet.BulletAppState;
import static com.jme3.bullet.PhysicsSpace.getPhysicsSpace;
import com.jme3.bullet.collision.shapes.BoxCollisionShape;
import com.jme3.bullet.collision.shapes.CompoundCollisionShape;
import com.jme3.bullet.control.BetterCharacterControl;
import com.jme3.bullet.control.GhostControl;
import com.jme3.bullet.control.RigidBodyControl;
import com.jme3.bullet.control.VehicleControl;
import com.jme3.input.ChaseCamera;
import com.jme3.input.KeyInput;
import com.jme3.input.MouseInput;
import com.jme3.input.controls.ActionListener;
import com.jme3.input.controls.KeyTrigger;
import com.jme3.input.controls.MouseAxisTrigger;
import com.jme3.input.controls.MouseButtonTrigger;
import com.jme3.light.DirectionalLight;
import com.jme3.material.Material;
import com.jme3.math.ColorRGBA;
import com.jme3.math.FastMath;
import com.jme3.math.Vector3f;
import com.jme3.renderer.RenderManager;
import com.jme3.scene.Geometry;
import com.jme3.scene.Node;
import com.jme3.scene.Spatial;
import com.jme3.scene.shape.Box;

/**
 * This is the Main Class of your Game. You should only do initialization here.
 * Move your Logic into AppStates or Controls
 * @author normenhansen
 */
public class Main extends SimpleApplication {
private BulletAppState bulletAppState;
private RigidBodyControl terrainRigidBody;
private ChaseCamera chaseCam;

private Spatial playerSpatial;
private Node playerNode;
private Boolean playerIsMoving = false;
private BetterCharacterControl playerCharacterControl;
private GhostControl playerGhostControl;
private VehicleControl vehicle;
private Spatial wheelFrontLeft;
private Spatial wheelFrontRight;
private Spatial wheelBackLeft;
private Spatial wheelBackRight;

    public static void main(String[] args) {
        Main app = new Main();
        app.start();
    }

    @Override
    public void simpleInitApp() {
        flyCam.setEnabled(true);

        
        Box b = new Box(10, 0.01f, 10); // create cube shape
        Geometry geom = new Geometry("Box", b);  // create cube geometry from the shape
        Material mat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");  // create a simple material
        mat.setTexture("ColorMap", assetManager.loadTexture("Textures/roadTEST.jpg"));
        geom.setMaterial(mat);                   // set the cube's material
        rootNode.attachChild(geom); 
        
        DirectionalLight sun = new DirectionalLight();
        sun.setDirection(new Vector3f(-0.1f, -0.7f, -1.0f));
        rootNode.addLight(sun);
        
        

        bulletAppState = new BulletAppState();
        bulletAppState.setThreadingType(BulletAppState.ThreadingType.PARALLEL);
        stateManager.attach(bulletAppState);
        bulletAppState.setDebugEnabled(false);
        
        terrainRigidBody = new RigidBodyControl(0); // static terrain physics
        geom.addControl(terrainRigidBody);
        getPhysicsSpace().add(geom);
        
        
   
        playerSpatial = assetManager.loadModel("Models/auto/auto.j3o");
     

          
        
        
        
        bulletAppState.setDebugEnabled(true);


        initPlayerVehicle();
        initKeys();
        
    }
   
    private void initPlayerVehicle(){
          CompoundCollisionShape compoundShape = new CompoundCollisionShape();
          BoxCollisionShape box = new BoxCollisionShape(new Vector3f(1.5f, 0.5f, 0.5f));
          compoundShape.addChildShape(box, new Vector3f(0, 1, 0));
          Node vehicleNode= new Node("vehicleNode");
          vehicleNode.attachChild(playerSpatial);
          playerSpatial.move(0,0.5f,0);
          vehicle = new VehicleControl(compoundShape, 400);
          vehicleNode.addControl(vehicle);
          float stiffness = 60.0f;//200=f1 car
          float compValue = .3f; //(should be lower than damp)
          float dampValue = .4f;
          vehicle.setSuspensionCompression(compValue * 2.0f * FastMath.sqrt(stiffness));
          vehicle.setSuspensionDamping(dampValue * 2.0f * FastMath.sqrt(stiffness));
          vehicle.setSuspensionStiffness(stiffness);
          vehicle.setMaxSuspensionForce(10000.0f);
          
          wheelFrontLeft = assetManager.loadModel("Models/kolo/kolo.j3o");
          wheelFrontRight =assetManager.loadModel("Models/kolo/kolo.j3o");
          wheelBackLeft = assetManager.loadModel("Models/kolo/kolo.j3o");
          wheelBackRight = assetManager.loadModel("Models/kolo/kolo.j3o");
          
          // front left wheel
          wheelFrontLeft = assetManager.loadModel("Models/kolo/kolo.j3o");
          Node node1 = new Node("wheel 1 node");
          node1.attachChild(wheelFrontLeft);
          vehicle.addWheel(node1, new Vector3f(-0.90f,0.5f,0.5f),
          new Vector3f(0,1,0), new Vector3f(-1,0,0), 0.2f, 0.25f, true);   
          vehicleNode.attachChild(node1);
          
          // front right wheel
          Node node2 = new Node("wheel 2 node");
          node2.attachChild(wheelFrontRight);
          vehicle.addWheel(node2, new Vector3f(-0.90f,0.5f,-0.45f),
          new Vector3f(0,1,0), new Vector3f(-1,0,0), 0.2f, 0.25f, true);   
          vehicleNode.attachChild(node2);
          
          // back left wheel
          Node node3 = new Node("wheel 3 node");
          node3.attachChild(wheelBackLeft);
          vehicle.addWheel(node3, new Vector3f(0.65f,0.5f,0.5f),
          new Vector3f(0,1,0), new Vector3f(-1,0,0), 0.2f, 0.25f, false);   
          vehicleNode.attachChild(node3);
          
          // back right wheel
          Node node4 = new Node("wheel 4 node");
          node4.attachChild(wheelBackRight);
          vehicle.addWheel(node4, new Vector3f(0.65f,0.5f,-0.45f),
          new Vector3f(0,1,0), new Vector3f(-1,0,0), 0.2f, 0.25f, false);   
          vehicleNode.attachChild(node4);
          
          

          
          rootNode.attachChild(vehicleNode);
          getPhysicsSpace().add(vehicle);


    }
    public void initKeys(){
    inputManager.addMapping("Attack", new MouseButtonTrigger(MouseInput.BUTTON_LEFT));
    inputManager.addMapping("Aim", new MouseButtonTrigger(MouseInput.BUTTON_RIGHT));
    
    //test
    inputManager.addMapping("Die", new KeyTrigger(KeyInput.KEY_K));
    //test

    
    
    inputManager.addMapping("Forward",new KeyTrigger(KeyInput.KEY_W));
    inputManager.addMapping("Backward", new KeyTrigger(KeyInput.KEY_S));
    inputManager.addMapping("Left",new KeyTrigger(KeyInput.KEY_A));
    inputManager.addMapping("Right",new KeyTrigger(KeyInput.KEY_D));
    inputManager.addMapping("Sprint",new KeyTrigger(KeyInput.KEY_LSHIFT));
    inputManager.addMapping("Roll",new KeyTrigger(KeyInput.KEY_SPACE));





    
    inputManager.addListener(actionListener, "Attack", "Forward", "Backward", "Left", "Right","Aim", "Die","Roll","Sprint");
//    inputManager.addListener(analogListener, "Forward", "Backward", "Left", "Right","Sneak");
            }
    
    private final ActionListener actionListener = new ActionListener() {
        @Override
        public void onAction(String name, boolean keyPressed, float tpf) {
            if(name.equals("Forward")){
             if(keyPressed){
                 playerIsMoving = true;
             } else{
                  playerIsMoving = false;

             }
             }
           
        }
    };

    @Override
    public void simpleUpdate(float tpf) {
        //TODO: add update code
        if(playerIsMoving){
        vehicle.accelerate(805);
        System.out.println("W- pressed");
        }
    }

    @Override
    public void simpleRender(RenderManager rm) {
        //TODO: add render code
    }
}

1 Like

Without access to the model, it’s difficult to determine the issue. However, the vehicle’s center of mass appears to be underground. Perhaps the undercarriage is scraping the ground, or the suspension is bottomed out, or the chassis has intersected the ground body. You might try “dropping” the vehicle from a moderate height to see how it behaves.

Also, in case you’re unaware, the Minie tutorials provide some useful hints on tuning VehicleControl:
An introduction to vehicle physics :: The Minie project

1 Like

First of all , could the car be moved by gravity or any other physics related stuff (like inclined ground )?

Try FastMath.pow(20,5) instead of 805 then adjust to your need , using accelerate on low values donot work , I donot know why,may be the mass, but try it out !

If you cannot move , then use the accelerate() method directly inside onAction() actually you donot need to refer to that using update(tpf)

EDIT: TestCases from jme :slight_smile:

1 Like

Thank you! i will give it a try right now

1 Like

Any news?

yeah, totally forgot to post the reply. The wheels had wrong axes, my bad.
Thanks!

2 Likes