Vehicle max speed?

I was curious as to what would be a way to control the max speed of a jME vehicle using the vehicle controller?



Thanks for any tips on this!

I think what you are looking for is the acceleration value. Other than that there is no such thing as “max speed”, if your car gets accelerated by an explosion it wouldn’t make sense to cap the speed at say 180km/h. If you want to emulate an engine that cannot give more acceleration above a certain speed, add a check for the vehicle speed and cap the acceleration with that, something like this:

[java]

float maxSpeed=180;

public void accelerateVehicle(float amount){

float speedFactor=(maxSpeed-vehicle.getCurrentVehicleSpeedKmHour())/maxSpeed;

vehicle.accelerate(amount * speedFactor);

}

[/java]

1 Like

Thanks normen! this seems to work well. I added this to tpf:



[java]

float curSpeed = truck.getCurrentVehicleSpeedKmHour();

accelValue = accelerationValue;

if(curSpeed >= (maxSpeed)) {

accelerateVehicle(accelValue);

}

System.out.println("Speed = " + curSpeed);

}



[/java]



In my case I had to set the max speed to a - number, i think was because of the -z facing direction of the vehicle :slight_smile:

Hard way to do the acceleration control



I used information from this site and made my car more close to real one. VehicleControl only applies forward force and in real life there are opposite forces of drag and rolling resistance. So, maximum speed is achieved when summed up drag and rolling resistance forces are equal to force set to wheels in VehicleControl. Drag and rolling resistance depend on speed, so when you stop accelerating your car will slow down by itself and eventually stop (if there are no other forces involved).



My implementation of drag and rolling resistance calculation and adding them to total force is way too long to add here (used material based modifiers to mix things up), but basic principle is that there is always acceleration when car is moving, so I call acceleration for vehicle wheels on simpleUpdate every time, except when braking. Total acceleration force is acceleration force set by player minus drag force and rolling resistance force, so it can be negative if player is not accelerating. It took one to two days for get that working how I wanted. In that way you can set drag and rolling resistance values so you get maximum speed wanted, but I haven’t made a correct formula for that. I just set some forces and look how fast my car goes and then modify forces. So, some coding with little math and you can make quite real car. This is the long way if just fixed maxSpeed value is not enough.



If rolling resistance calculation is done, it can also be used for other purposed. For example I use different rolling resistance multipliers for different surfaces and high rolling resistance multiplier for sand. My car will slow down very fast when drove from road to sand.

You can just use the brake value instead of complicated calculations to simulate motor and axis resistance.

Yeah, I set a simple resistance in the onAction tpf settings, if no keys are being pressed:



if (!value){

truck.brake(40f);

}



Was simple to do, and prevents the vehicle rolling around when not being controlled :slight_smile: