I am creating a vehicle. I am to add wheels to a spatial with vehicle control. Here is my code for this method:
bulletAppState = new BulletAppState();
bulletAppState.setThreadingType(BulletAppState.ThreadingType.PARALLEL);
stateManager.attach(bulletAppState);
terrain.setSupportMultipleCollisions(true);
terrain.addControl(new RigidBodyControl(0));
bulletAppState.getPhysicsSpace().addAll(terrain);
spaceShipShape = new CompoundCollisionShape();
spaceship.addControl(new RigidBodyControl(0));
bulletAppState.getPhysicsSpace().add(spaceship);
playerCapsule = new CapsuleCollisionShape(1.5f, 15f, 1);
character = new CharacterControl(playerCapsule, 0.2f);
character.setJumpSpeed(20);
character.setFallSpeed(50);
character.setGravity(new Vector3f(0,-40f,0));
character.setLinearVelocity(new Vector3f(400, 0, 400));
character.setPhysicsLocation(new Vector3f(-30f, 100f, -101f));
bulletAppState.getPhysicsSpace().add(character);
float stiffness = 120.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
Node carNode = (Node)assetManager.loadModel("Models/Ferrari/Car.scene");
Geometry chasis = findGeom(carNode, "Car");
BoundingBox box = (BoundingBox) chasis.getModelBound();
//Create a hull collision shape for the chassis
CollisionShape carHull = CollisionShapeFactory.createDynamicMeshShape(chasis);
//Create a vehicle control
plane = new VehicleControl(carHull, mass);
carNode.addControl(plane);
//Setting default values for wheels
plane.setSuspensionCompression(compValue * 2.0f * FastMath.sqrt(stiffness));
plane.setSuspensionDamping(dampValue * 2.0f * FastMath.sqrt(stiffness));
plane.setSuspensionStiffness(stiffness);
plane.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);
Geometry wheel_fr = findGeom(carNode, "WheelFrontRight");
wheel_fr.center();
box = (BoundingBox) wheel_fr.getModelBound();
float wheelRadius = box.getYExtent();
float back_wheel_h = (wheelRadius * 1.7f) - 1f;
float front_wheel_h = (wheelRadius * 1.9f) - 1f;
plane.addWheel(wheel_fr.getParent(), box.getCenter().add(0, -front_wheel_h, 0),
wheelDirection, wheelAxle, 0.2f, wheelRadius, true);
Geometry wheel_fl = findGeom(carNode, "WheelFrontLeft");
wheel_fl.center();
box = (BoundingBox) wheel_fl.getModelBound();
plane.addWheel(wheel_fl.getParent(), box.getCenter().add(0, -front_wheel_h, 0),
wheelDirection, wheelAxle, 0.2f, wheelRadius, true);
Geometry wheel_br = findGeom(carNode, "WheelBackRight");
wheel_br.center();
box = (BoundingBox) wheel_br.getModelBound();
plane.addWheel(wheel_br.getParent(), box.getCenter().add(0, -back_wheel_h, 0),
wheelDirection, wheelAxle, 0.2f, wheelRadius, false);
Geometry wheel_bl = findGeom(carNode, "WheelBackLeft");
wheel_bl.center();
box = (BoundingBox) wheel_bl.getModelBound();
plane.addWheel(wheel_bl.getParent(), box.getCenter().add(0, -back_wheel_h, 0),
wheelDirection, wheelAxle, 0.2f, wheelRadius, false);
plane.getWheel(2).setFrictionSlip(4);
plane.getWheel(3).setFrictionSlip(4);
rootNode.attachChild(carNode);
carNode.scale(10f);
bulletAppState.getPhysicsSpace().add(plane);
As you can see, alot of the code is the same as TestFancyCar.java, as I am trying to get a hang of the wheels/vehicle control.
However, I don’t want to use Car.scene – I have my own Spatial, that is loaded in w/ .obj, and I want to set the carNode to that. To do that, I need a Geometry chasis, and what I tried was (spaceship is my spatial):
1. Geometry chasis = (Geometry) spaceship;
2. Geometry chasis = (Geometry)((Node)spaceship).getChild("Spaceship"));
This didn’t work. I then tried setting my CollisionShape to:
CollisionShape carHull = CollisionShapeFactory.createMeshShape(spaceship); // without doing any Geometry stuff
None of these work. I get “cannot cast Node to Geometry” even though I am casting a spatial to geometry, and the third one doesn’t work well. How can I load in my Spatial??? I’ve been staring at the javadoc but can’t find anything. Thank you for your help!