I have been trying to get the FancyCar physics/bullet example working, replacing the Ferrari with a different, relatively low-poly, model, but having trouble.
I scaled my own model to roughly about the same size as the original Ferrari, but the wheels tend to either go straight through the floor (with the car body resting on the floor), or the wheels get incredibly bouncy (bouncing through the bonnet) and the car slowly drifts of to the horizon uncontrollably.
I have been trying different values for suspension etc. for hours, but maybe the problem is somewhere else entirely. Anyway, I am lost.
For those interested, replacing/adding these files to the bullet fancy-car example should be enough to test:
Could the problem be with the wheel design? This is the wireframe of the wheels of the vehicle:
Or is there something wrong with my code. This is what I have so far:
private void buildPlayer() {
float stiffness = 60.0f;//200=f1 car
float compValue = 0.2f; //(lower than damp!)
float dampValue = 0.3f;
final float mass = 400;
//Load model and get chassis Geometry
carNode = (Node)assetManager.loadModel("Models/Cars/Coupe_Green.j3o");
carNode.setShadowMode(ShadowMode.Cast);
Geometry main = findGeometry(carNode, "main");
//Create a hull collision shape for the chassis
CollisionShape carHull = CollisionShapeFactory.createDynamicMeshShape(main);
//Create a vehicle control
player = new VehicleControl(carHull, mass);
carNode.addControl(player);
//Setting default values for wheels
player.setSuspensionCompression(compValue * 2.0f * FastMath.sqrt(stiffness));
player.setSuspensionDamping(dampValue * 2.0f * FastMath.sqrt(stiffness));
player.setSuspensionStiffness(stiffness);
player.setMaxSuspensionForce(10000);
//Create four wheels and add them at their locations
//note that our fancy car actually goes backwards..
Vector3f wheelDirection = new Vector3f(0, -1, 0);
Vector3f wheelAxle = new Vector3f(-1, 0, 0);
SetupWheel(carNode, "fl", true, wheelDirection, wheelAxle);
SetupWheel(carNode, "fr", true, wheelDirection, wheelAxle);
SetupWheel(carNode, "rl", false, wheelDirection, wheelAxle);
SetupWheel(carNode, "rr", false, wheelDirection, wheelAxle);
player.getWheel(2).setFrictionSlip(4);
player.getWheel(3).setFrictionSlip(4);
rootNode.attachChild(carNode);
getPhysicsSpace().add(player);
}
private void SetupWheel(Spatial carNode, String suffix, boolean isFrontWheel, Vector3f wheelDirection, Vector3f wheelAxle) {
Spatial wheel = findSpatial(carNode, "wheel_" + suffix);
BoundingBox box = (BoundingBox)wheel.getWorldBound();
float radius = box.getYExtent() / 2.0f;
float suspensionLength = radius * 1.1f;
Vector3f mountPoint = box.getCenter().add(0, -suspensionLength, 0);
Node node = (Node) wheel;
for (int i = 0; i < node.getQuantity(); i++) {
Spatial child = node.getChild(i);
if (child instanceof Geometry) {
child.center();
}
}
player.addWheel(wheel, mountPoint, wheelDirection, wheelAxle,
suspensionLength, radius, isFrontWheel);
}
public static Spatial findSpatial(Spatial spatial, String name) {
if (spatial instanceof Node) {
Node node = (Node) spatial;
for (int i = 0; i < node.getQuantity(); i++) {
Spatial child = node.getChild(i);
if (child.getName().startsWith(name)) {
return child;
}
else {
Spatial result = findSpatial(child, name);
if (result != null) {
return result;
}
}
}
}
return null;
}
public static Geometry findGeometry(Spatial spatial, String name) {
if (spatial instanceof Node) {
Node node = (Node) spatial;
for (int i = 0; i < node.getQuantity(); i++) {
Spatial child = node.getChild(i);
Geometry result = findGeometry(child, name);
if (result != null) {
return result;
}
}
} else if (spatial instanceof Geometry) {
if (spatial.getName().startsWith(name)) {
return (Geometry) spatial;
}
}
return null;
}
It looks pretty solid to me, but maybe I am missing something.
Thanks for any help,
Martin
Note: the model looks fine in the SceneComposer, but the texture or lighting is completely of in the game itself, but that is an other issue I suppose.