[SOLVED] VehicleControl wheels digging into the ground

I’m having trouble with my VehicleControl wheels digging into the floor and inhibiting the movement of my vehicle. I’m trying to collide with an obj model using RigidBodyControl.

My car class:

package mygame;

import com.jme3.bullet.collision.shapes.BoxCollisionShape;
import com.jme3.bullet.collision.shapes.CompoundCollisionShape;
import com.jme3.bullet.control.VehicleControl;
import com.jme3.material.Material;
import com.jme3.math.FastMath;
import com.jme3.math.Quaternion;
import com.jme3.math.Vector3f;
import com.jme3.scene.Geometry;
import com.jme3.scene.Node;
import com.jme3.scene.Spatial;
import com.jme3.scene.shape.Cylinder;

/**
 *
 * @author codex
 */
public class Car {
	
	float x, y, z;
	Spatial model;
	Material mat;
	
	CompoundCollisionShape compoundShape;
	BoxCollisionShape box;
	Node vehicleNode = new Node("car");
	
	VehicleControl vehicle = new VehicleControl();
	float accel = 1.5f;
	float turn = 0f;
	Vector3f grav = new Vector3f(0, -1, 0);
	float friction = 0.7f;
	float mass = 0.8f;
	float stiffness = 60.0f;
	float compValue = .3f;
	float dampValue = .4f;
	
	Vector3f wheelDirection = new Vector3f(0, -1, 0);
	Vector3f wheelAxle = new Vector3f(-1, 0, 0);
	float radius = 1f;
	float restLength = 0.3f;
	float xOff = 3f;
	float yOff = -1f;
	float zOff = 2f;
	
	
	Car(float x, float y, float z, Spatial model, Material mat) {
		this.x = x;
		this.y = y;
		this.z = z;
		this.model = model;
		this.mat = mat;
	}
	
	protected Node createVehicle() {
		compoundShape = new CompoundCollisionShape();
		box = new BoxCollisionShape(new Vector3f(1.2f, 0.5f, 2.4f));
		compoundShape.addChildShape(box, new Vector3f(0, 1, 0));
		vehicle = new VehicleControl(compoundShape, 400);
		model.setMaterial(mat);
		Quaternion rotate = new Quaternion();
		rotate.fromAngleAxis(0.5f*FastMath.PI, new Vector3f(0, 1, 0));
		model.setLocalRotation(rotate);
		vehicle.setSpatial(model);
		buildWheels();
		//vehicle.setGravity(grav);
		//vehicle.setRollInfluence(0, turn);
		//vehicle.setFriction(friction);
		vehicle.setMass(mass);
		vehicle.setSuspensionCompression(compValue * 2.0f * FastMath.sqrt(stiffness));
		vehicle.setSuspensionDamping(dampValue * 2.0f * FastMath.sqrt(stiffness));
		vehicle.setSuspensionStiffness(stiffness);
		vehicle.setMaxSuspensionForce(10000.0f);
		//vehicle.setPhysicsLocation(new Vector3f(x, y, z));
		vehicleNode.attachChild(model);
		vehicleNode.addControl(vehicle);
		return vehicleNode;
	}
	private void buildWheels() {
		Node wbl = new Node("back left wheel");
		Node wbr = new Node("back right wheel");
		Node wfl = new Node("front left wheel");
		Node wfr = new Node("front right wheel");
		Cylinder wheelMesh = new Cylinder(16, 16, radius, radius * 0.6f, true);
		// connectionPoint, direction, axle, suspensionRestLength, wheelRadius, isFrontWheel
		// back wheel left
		Geometry wheels1 = new Geometry("wheel 1", wheelMesh);
		wfl.attachChild(wheels1);
		wheels1.rotate(0, FastMath.HALF_PI, 0);
		wheels1.setMaterial(mat);
		vehicle.addWheel(wfl, new Vector3f(-xOff, yOff, zOff),
				wheelDirection, wheelAxle, restLength, radius, true);
		// back wheel right
		Geometry wheels2 = new Geometry("wheel 2", wheelMesh);
		wfr.attachChild(wheels2);
		wheels2.rotate(0, FastMath.HALF_PI, 0);
		wheels2.setMaterial(mat);
		vehicle.addWheel(wfr, new Vector3f(xOff, yOff, zOff),
				wheelDirection, wheelAxle, restLength, radius, true);
		// front wheel left
		Geometry wheels3 = new Geometry("wheel 3", wheelMesh);
		wbl.attachChild(wheels3);
		wheels3.rotate(0, FastMath.HALF_PI, 0);
		wheels3.setMaterial(mat);
		vehicle.addWheel(wbl, new Vector3f(-xOff, yOff, -zOff),
				wheelDirection, wheelAxle, restLength, radius, false);
		// front wheel right
		Geometry wheels4 = new Geometry("wheel 4", wheelMesh);
		wbr.attachChild(wheels4);
		wheels4.rotate(0, FastMath.HALF_PI, 0);
		wheels4.setMaterial(mat);
		vehicle.addWheel(wbr, new Vector3f(xOff, yOff, -zOff),
				wheelDirection, wheelAxle, restLength, radius, false);
		vehicleNode.attachChild(wbl);
		vehicleNode.attachChild(wbr);
		vehicleNode.attachChild(wfl);
		vehicleNode.attachChild(wfr);
	}
	
	protected void forward() {
		vehicle.accelerate(accel);		
	}
	protected void steer(String binding, boolean isPressed) {
		if (binding.equals(Main.LEFT)) {
			if (isPressed) { turn += .5f; } else { turn += -.5f; }
		} else if (binding.equals(Main.RIGHT)) {
			if (isPressed) { turn += -.5f; } else { turn += .5f; }
		}
		vehicle.steer(turn);
	}
	protected void steerLeft() {
		vehicle.steer(-turn);
	}
	
	protected VehicleControl getPhysics() {
		return vehicle;
	}
	protected Vector3f getPos() {
		return vehicle.getPhysicsLocation();
	}
	protected Vector3f getDirection() {
		return vehicle.getForwardVector(grav);
	}
	
}
1 Like

If you didnot play too much the values the suspension(Spring compression ) , stiffness(rigidity) & damping (Spring release) , you wonot get this unless you are spawning the car at 0,0,0 & the ground at 0,2,0 & the ground can have thickness , so beware of your spawning code .

I havenot worked too much with those parameters but what I know that the damping should be higher than suspension value or compression & that’s true here

I copied the suspension, stiffness, and damping values from the VehicleControl tutorial. I’m spawning the car at {0, 0, 0} and my ground model at {0, -2, 0}.

Vehicle Tutorial

1 Like

If you havenot already added the car to the physics please check it , because , the wheels may be not added to the physics .

When I played around with the vehicle control stuff and things acted weird, I’d compare my code to the vehicle tests in jme3-examples… figure out what I was doing differently, and correct the issue.

The code in the tutorial is on a web page so is not guaranteed to work all the time.

1 Like

Try building wheels after setting car properties , because as suspected , physics would have applied effects if you call the buildwheels(); after the car spring properties which are the car wheels properties indeed so ,

{initializeWheels ->then-> apply wheels properties}

1 Like

Problem solved.
I was building my wheels before setting up other vehicle characteristics.

Thanks y’all!

PS: Is there a way to put the camera directly behind the car?

2 Likes
1 Like

There is also a tuning guide and some simple examples in Minie’s vehicle-physics tutorial:

2 Likes