I’m going to try bullet physics. By this, I will build a simple car game. This is my simple car in Blender.
I start to first initialize the bullet physics.
// Bullet BulletAppState bulletAppState = new BulletAppState(); bulletAppState.setDebugEnabled(true); stateManager.attach(bulletAppState);
Then I import the car model and connect it to the nodes.
Spatial Car = assetManager.loadModel("Models/Car.j3o"); Node carNode = new Node(); rootNode.attachChild(carNode); carNode.attachChild(Car);
Then I compute the collision shape volume for my car.
BoundingBox boundingBox = (BoundingBox) carNode.getWorldBound(); float width = boundingBox.getXExtent(); float length = boundingBox.getYExtent(); CapsuleCollisionShape capsuleCollisionShape = new CapsuleCollisionShape(length, width, 2);
Then I create the control for the car and attach it to the carNode.
CharacterControl carControl = new CharacterControl(capsuleCollisionShape, 1f); // 1f > 0 mass carNode.addControl(carControl);
Then I use bullet appstate to say that the physics should take account to the control for the car.
Also I need a floor for the car to stand on too.
// Load scene and control Spatial scene = assetManager.loadModel("Scenes/carScene.j3o"); rootNode.attachChild(scene); Spatial floor = rootNode.getChild("floor"); bulletAppState.getPhysicsSpace().add(floor.getControl(RigidBodyControl.class));
So the result was that the car is flying on the collision shape round thing.
Then I was because the center of the car was placed under the car in Blender(first picture). Then I was thinking “Why not have a transparent rectangular box over the car and then set the collision shape over that box and then connect the car to that box?”.
So I created a invisible box and imported to JME and set the material Materials/Geom/SimpleGeom.j3m I also lower the box a little bit so the center would be in the middle of the box.
Then I import it that box and set the nodes.
Spatial Block = assetManager.loadModel("Models/Block.j3o"); // Transperency block Node blockNode = new Node();
Lifting the block up and attach it.
blockNode.setLocalTranslation(0, 2.60319f, 0);
Create the collision shape and physics for that box.
BoundingBox boundingBox = (BoundingBox) blockNode.getWorldBound(); float width = boundingBox.getXExtent(); float length = boundingBox.getYExtent(); CapsuleCollisionShape capsuleCollisionShape = new CapsuleCollisionShape(length, width, 2); CharacterControl carControl = new CharacterControl(capsuleCollisionShape, 1f); // 1f > 0 mass blockNode.addControl(carControl); bulletAppState.getPhysicsSpace().add(carControl);
Then connect blockNode to carNode.
And the result is no car available inside this collision shape but the good thing is that the collision shape is not cutting in the floor.
How can I attach the car inside this box? I have some kinda solve this problem by moving the car center in JME a little bit lower.
Instead of this height.
This gave me the result, but I don’t really like to do this. I pretty sure that I is a better way to move the collision shape and not the car.
I remove the grid.
Looks good. Not sure if this is the correct way to do this.