Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import com.jme3.app.SimpleApplication;
- import com.jme3.bullet.BulletAppState;
- import com.jme3.bullet.PhysicsSpace;
- import com.jme3.bullet.collision.PhysicsCollisionEvent;
- import com.jme3.bullet.collision.PhysicsCollisionListener;
- import com.jme3.bullet.control.RigidBodyControl;
- import com.jme3.material.Material;
- import com.jme3.math.ColorRGBA;
- import com.jme3.math.Quaternion;
- import com.jme3.math.Vector3f;
- import com.jme3.scene.Geometry;
- import com.jme3.scene.Mesh;
- import com.jme3.scene.Node;
- import com.jme3.scene.VertexBuffer;
- import com.jme3.scene.shape.Box;
- public class Bug extends SimpleApplication implements PhysicsCollisionListener {
- private int ticks = 0;
- public static void main(String[] args) {
- Bug bug = new Bug();
- bug.showSettings = false;
- bug.start();
- }
- @Override
- public void simpleInitApp() {
- BulletAppState bulletAppState = new BulletAppState(PhysicsSpace.BroadphaseType.SIMPLE);
- this.stateManager.attach(bulletAppState);
- bulletAppState.getPhysicsSpace().addCollisionListener(this);
- bulletAppState.getPhysicsSpace().setBroadphaseType(PhysicsSpace.BroadphaseType.SIMPLE);
- /*
- Here the original code builds the cube from coordinates.
- The issue does not happen with JMonkey's Box at least for these particular values,
- possibly because a btBoxShape is used for Bullet, which works different to a triangle mesh.
- I need this to work fine with any convex mesh.
- */
- Mesh mesh = new Mesh();
- Box q = new Box(2000,2000,2000);
- for(VertexBuffer origin : q.getBufferList()) {
- mesh.setBuffer(origin.getBufferType(), origin.getNumComponents(), origin.getFormat(), origin.getData());
- }
- mesh.updateBound();
- Material mat = new Material(assetManager,
- "Common/MatDefs/Misc/Unshaded.j3md");
- mat.setColor("Color", ColorRGBA.Blue);
- final boolean isKinematic = true;
- // Create first box
- Geometry spatial1 = new Geometry("Box", mesh);
- RigidBodyControl control1 = new RigidBodyControl(1.0f);
- spatial1.setMaterial(mat);
- spatial1.addControl(control1);
- control1.setKinematic(isKinematic);
- Node node1 = new Node();
- node1.attachChild(spatial1);
- rootNode.attachChild(node1);
- bulletAppState.getPhysicsSpace().add(control1);
- // Create second box
- Geometry spatial2 = new Geometry("Box2", mesh);
- RigidBodyControl control2 = new RigidBodyControl(1.0f);
- spatial2.setMaterial(mat);
- spatial2.addControl(control2);
- control2.setKinematic(isKinematic);
- Node node2 = new Node();
- node2.attachChild(spatial2);
- rootNode.attachChild(node2);
- bulletAppState.getPhysicsSpace().add(control2);
- // Rotate the boxes some magic angles, if these values vary the boxes will be detected to collide.
- // 8% randomly-generated angle pairs will trigger the problem.
- final Quaternion q1 = new Quaternion().fromAngles(3.6280732f,5.2767496f,5.40851f);
- final Quaternion q2 = new Quaternion().fromAngles(3.1379673f,5.9316964f,0.8267426f);
- node1.setLocalRotation(q1);
- node2.setLocalRotation(q2);
- node1.setLocalTranslation(Vector3f.ZERO);
- node2.setLocalTranslation(Vector3f.ZERO);
- bulletAppState.startPhysics();
- }
- @Override
- public void simpleUpdate(float tpf) {
- super.simpleUpdate(tpf);
- ticks++;
- }
- @Override
- public void collision(PhysicsCollisionEvent event) {
- System.out.println("Collision event lifetime: " + event.getLifeTime());
- System.out.println("Number of ticks: " + ticks);
- System.exit(0);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement