VehicleControl not removed from PhysicsSpace when using addCollisionObject()
Hi @sgold ,
I’m experimenting with VehicleControl and noticed a difference in behavior depending on how I add it to the PhysicsSpace.
If I add the vehicle like this:
getPhysicsSpace().add(vehicle);
then disabling the control works as expected:
vehicle.setEnabled(false);
The VehicleControl is automatically removed from the PhysicsSpace.
However, if I add it like this:
getPhysicsSpace().addCollisionObject(vehicle);
and then disable it:
vehicle.setEnabled(false);
the vehicle/rigidBody remains inside the PhysicsSpace and is not removed.
Here is the relevant part of my test code:
@Override
public void onAction(String name, boolean isPressed, float tpf) {
if (name.equals("ToggleVehicleEnabled") && isPressed) {
boolean enabled = vehicle.isEnabled();
vehicle.setEnabled(!enabled);
}
}
Question
Is this difference in behavior expected? Does addCollisionObject() bypass the enable/disable lifecycle of PhysicsControl objects? And if so, is the correct approach to always use add() for controls like VehicleControl?
Below is the full test class that I rewrote in a more compact and readable form. You can also find it on GitHub TestFancyCar
Thanks
Edit: I’m using the latest version of Minie (9.0.3+big4)
package jme3test.bullet;
import com.jme3.app.SimpleApplication;
import com.jme3.bounding.BoundingBox;
import com.jme3.bullet.BulletAppState;
import com.jme3.bullet.PhysicsSpace;
import com.jme3.bullet.collision.shapes.CollisionShape;
import com.jme3.bullet.control.VehicleControl;
import com.jme3.bullet.objects.VehicleWheel;
import com.jme3.bullet.util.CollisionShapeFactory;
import com.jme3.input.KeyInput;
import com.jme3.input.controls.ActionListener;
import com.jme3.input.controls.KeyTrigger;
import com.jme3.input.controls.Trigger;
import com.jme3.light.DirectionalLight;
import com.jme3.math.FastMath;
import com.jme3.math.Matrix3f;
import com.jme3.math.Vector3f;
import com.jme3.renderer.queue.RenderQueue.ShadowMode;
import com.jme3.scene.Geometry;
import com.jme3.scene.Node;
import com.jme3.scene.Spatial;
public class TestFancyCar extends SimpleApplication implements ActionListener {
private BulletAppState bulletAppState;
private VehicleControl vehicle;
private float steeringValue = 0;
private float accelerationValue = 0;
private Node carNode;
private float stiffness = 120.0f;//200=f1 car
private float compValue = 0.2f; //(lower than damp!)
private float dampValue = 0.3f;
private float suspensionRestLength = 0.2f;
private float frictionSlip = 4f;
private final float mass = 400;
private Vector3f wheelDirection = new Vector3f(0, -1, 0);
private Vector3f wheelAxle = new Vector3f(-1, 0, 0);
public static void main(String[] args) {
TestFancyCar app = new TestFancyCar();
app.setPauseOnLostFocus(false);
app.start();
}
@Override
public void simpleInitApp() {
bulletAppState = new BulletAppState();
stateManager.attach(bulletAppState);
bulletAppState.setDebugEnabled(true);
configureCamera();
DirectionalLight dl = new DirectionalLight();
dl.setDirection(new Vector3f(-0.5f, -1f, -0.3f).normalizeLocal());
rootNode.addLight(dl);
PhysicsTestHelper.createPhysicsTestWorld(rootNode, assetManager, getPhysicsSpace());
buildPlayer();
setupKeys();
}
private void configureCamera() {
flyCam.setDragToRotate(true);
flyCam.setMoveSpeed(15f);
// Adjust to near frustum to a very close amount.
float aspect = (float) cam.getWidth() / cam.getHeight();
cam.setFrustumPerspective(45, aspect, 0.1f, 1000);
cam.lookAtDirection(Vector3f.UNIT_Z, Vector3f.UNIT_Y);
}
private PhysicsSpace getPhysicsSpace() {
return bulletAppState.getPhysicsSpace();
}
private void buildPlayer() {
// Load model and get chassis Geometry
carNode = (Node) assetManager.loadModel("Models/Ferrari/Car.scene");
carNode.setShadowMode(ShadowMode.Cast);
Geometry chassis = findGeom(carNode, "Car");
// Create a hull collision shape for the chassis
// CollisionShape collShape = CollisionShapeFactory.createDynamicMeshShape(chassis);
CollisionShape collShape = CollisionShapeFactory.createBoxShape(chassis);
// Create a vehicle control
vehicle = new VehicleControl(collShape, mass);
carNode.addControl(vehicle);
// Setting default values for wheels
vehicle.setSuspensionCompression(compValue * 2.0f * FastMath.sqrt(stiffness));
vehicle.setSuspensionDamping(dampValue * 2.0f * FastMath.sqrt(stiffness));
vehicle.setSuspensionStiffness(stiffness);
vehicle.setMaxSuspensionForce(10000);
// Create four wheels and add them at their locations.
// Note that our fancy car actually goes backward.
addWheel(vehicle, findGeom(carNode, "WheelFrontRight"), true);
addWheel(vehicle, findGeom(carNode, "WheelFrontLeft"), true);
addWheel(vehicle, findGeom(carNode, "WheelBackRight"), false);
addWheel(vehicle, findGeom(carNode, "WheelBackLeft"), false);
// Apply friction to rear wheels
vehicle.getWheel(2).setFrictionSlip(frictionSlip);
vehicle.getWheel(3).setFrictionSlip(frictionSlip);
rootNode.attachChild(carNode);
getPhysicsSpace().add(vehicle);
//getPhysicsSpace().addCollisionObject(vehicle); <----
}
private VehicleWheel addWheel(VehicleControl vehicle, Geometry wheel, boolean isFrontWheel) {
wheel.center();
BoundingBox box = (BoundingBox) wheel.getModelBound();
float wheelRadius = box.getYExtent();
float k = (isFrontWheel) ? 1.9f : 1.7f;
float h = (wheelRadius * k) - 1f;
Vector3f connectionPoint = box.getCenter().add(0, -h, 0);
return vehicle.addWheel(wheel.getParent(), connectionPoint,
wheelDirection, wheelAxle, suspensionRestLength, wheelRadius, isFrontWheel);
}
private Geometry findGeom(Spatial spatial, String name) {
if (spatial instanceof Node) {
for (Spatial child : ((Node) spatial).getChildren()) {
Geometry result = findGeom(child, name);
if (result != null) {
return result;
}
}
} else if (spatial instanceof Geometry) {
if (spatial.getName().startsWith(name)) {
return (Geometry) spatial;
}
}
return null;
}
private void setupKeys() {
addMapping("ToggleVehicleEnabled", new KeyTrigger(KeyInput.KEY_P));
addMapping("Lefts", new KeyTrigger(KeyInput.KEY_H));
addMapping("Rights", new KeyTrigger(KeyInput.KEY_K));
addMapping("Ups", new KeyTrigger(KeyInput.KEY_U));
addMapping("Downs", new KeyTrigger(KeyInput.KEY_J));
addMapping("Reset", new KeyTrigger(KeyInput.KEY_RETURN));
}
private void addMapping(String mappingName, Trigger... triggers) {
inputManager.addMapping(mappingName, triggers);
inputManager.addListener(this, mappingName);
}
@Override
public void onAction(String name, boolean isPressed, float tpf) {
if (name.equals("ToggleVehicleEnabled") && isPressed) {
boolean enabled = vehicle.isEnabled();
vehicle.setEnabled(!enabled);
}
if (name.equals("Lefts")) {
if (isPressed) {
steeringValue += .5f;
} else {
steeringValue -= .5f;
}
vehicle.steer(steeringValue);
} else if (name.equals("Rights")) {
if (isPressed) {
steeringValue -= .5f;
} else {
steeringValue += .5f;
}
vehicle.steer(steeringValue);
} // Note that our fancy car actually goes backward.
else if (name.equals("Ups")) {
if (isPressed) {
accelerationValue -= 800;
} else {
accelerationValue += 800;
}
vehicle.accelerate(accelerationValue);
} else if (name.equals("Downs")) {
if (isPressed) {
vehicle.brake(40f);
} else {
vehicle.brake(0f);
}
} else if (name.equals("Reset")) {
if (isPressed) {
System.out.println("Reset");
vehicle.setPhysicsLocation(Vector3f.ZERO);
vehicle.setPhysicsRotation(new Matrix3f());
vehicle.setLinearVelocity(Vector3f.ZERO);
vehicle.setAngularVelocity(Vector3f.ZERO);
vehicle.resetSuspension();
}
}
}
@Override
public void simpleUpdate(float tpf) {
cam.lookAt(carNode.getWorldTranslation(), Vector3f.UNIT_Y);
}
}
Discussion in the ATmosphere