Stable velocity on NPC vehicle

How can I get a stable velocity for an NPC vehicle? I attemped vehicle.setLinearVelocity(vehicle.getForwardDirection(null))); but this slowly makes my vehicle sink through the floor until the collisionShape of the vehicle body is the only thing above the ground (the wheels sink under the floor).

Just don’t accelerate anymore when you reach a certain speed:

[java]if(vehicle.getCurrentVehicleSpeedKmHour()<60){

vehicle.accelerate(100);

}else{

vehicle.accelerate(0);

//maybe:

vehicle.brake(1);

}[/java]

You might also want to ramp the values so when reaching the threshold it isn’t all that sudden.

I keep getting odd results from this. When I drive the vehicle using the keyboard using the following code in my onAction method everything is great, but when I try to run anything that would turn or otherwise run the vehicle, the wheels slowly sink through the ground.

[java]else if (binding.equals(“Ups”)) {

if (value) {

vehicle.setLinearVelocity((vehicle.getForwardVector(null)));

}[/java]

If the suspension works fine for driving with the keyboard, but not so when doing it as a NPC. The NPC code is a very basic FSM sitting in the updateLoop() that makes the vehicle drive in a square. This sinking affect only occurs when I attempt to drive the vehicle from the simpleUpdate() loop. I have attempted to use setLinearVelocity() and setAngularVelocity() as well as other methods (setting acceleration of each wheel for instance). Here is the suspension values I am currently using (though I need to scale the entire vehicle down in size to fix in my world later).

[java] 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);

//Create four wheels and add them at their locations

Vector3f wheelDirection = new Vector3f(0, -1, 0); // was 0, -1, 0

Vector3f wheelAxle = new Vector3f(-1, 0, 0); // was -1, 0, 0

float restLength = 0.29f;[/java]

My vehicle is a cylinder sitting atop the wheels as shown here.http://i.imgur.com/VbzOn.png The arrangement of the wheels is that the two “forward” wheels are on the sides of the vehicle, and two to other wheels are only used to support the vehicle front and back. The red line shows the forwardDirection of the vehicle. Because of the differential drive-type, I can control each motor separately to turn as well, but this also produces the “wheels through the floor” problem.

I am using a vehicle object here instead of building the wheels out of hinges (as I was considering in this post) because I need to use the methods of getForwardDirection() and others that are vehicle-specific.

My ultimate goal is to be able to directly set the linear and angular velocities of the vehicle without having to ramp up and down the accelerations. The biggest problem so far is that the wheels sink through the floor only when I use a NPC code within the simpleUpdate() loop.

Any help would be greatly appreciated.

Sorry to bump this topic but I am still having the worst kind of trouble with this.

Look at the HoverControl in the tests templates, it does something similar.