Advertisement
Guest User

JME undetected overlap.

a guest
Jun 18th, 2019
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.53 KB | None | 0 0
  1. import com.jme3.app.SimpleApplication;
  2. import com.jme3.bullet.BulletAppState;
  3. import com.jme3.bullet.PhysicsSpace;
  4. import com.jme3.bullet.collision.PhysicsCollisionEvent;
  5. import com.jme3.bullet.collision.PhysicsCollisionListener;
  6. import com.jme3.bullet.control.RigidBodyControl;
  7. import com.jme3.material.Material;
  8. import com.jme3.math.ColorRGBA;
  9. import com.jme3.math.Quaternion;
  10. import com.jme3.math.Vector3f;
  11. import com.jme3.scene.Geometry;
  12. import com.jme3.scene.Mesh;
  13. import com.jme3.scene.Node;
  14. import com.jme3.scene.VertexBuffer;
  15. import com.jme3.scene.shape.Box;
  16.  
  17. public class Bug extends SimpleApplication implements PhysicsCollisionListener {
  18.   private int ticks = 0;
  19.  
  20.   public static void main(String[] args) {
  21.     Bug bug = new Bug();
  22.     bug.showSettings = false;
  23.     bug.start();
  24.   }
  25.  
  26.   @Override
  27.   public void simpleInitApp() {
  28.     BulletAppState bulletAppState = new BulletAppState(PhysicsSpace.BroadphaseType.SIMPLE);
  29.     this.stateManager.attach(bulletAppState);
  30.     bulletAppState.getPhysicsSpace().addCollisionListener(this);
  31.     bulletAppState.getPhysicsSpace().setBroadphaseType(PhysicsSpace.BroadphaseType.SIMPLE);
  32.  
  33.     /*
  34.      Here the original code builds the cube from coordinates.
  35.      The issue does not happen with JMonkey's Box at least for these particular values,
  36.      possibly because a btBoxShape is used for Bullet, which works different to a triangle mesh.
  37.      I need this to work fine with any convex mesh.
  38.      */
  39.     Mesh mesh = new Mesh();
  40.     Box q = new Box(2000,2000,2000);
  41.     for(VertexBuffer origin : q.getBufferList()) {
  42.       mesh.setBuffer(origin.getBufferType(), origin.getNumComponents(), origin.getFormat(), origin.getData());
  43.     }
  44.     mesh.updateBound();
  45.  
  46.     Material mat = new Material(assetManager,
  47.         "Common/MatDefs/Misc/Unshaded.j3md");
  48.     mat.setColor("Color", ColorRGBA.Blue);
  49.     final boolean isKinematic = true;
  50.  
  51.     // Create first box
  52.     Geometry spatial1 = new Geometry("Box", mesh);
  53.     RigidBodyControl control1 = new RigidBodyControl(1.0f);
  54.     spatial1.setMaterial(mat);
  55.     spatial1.addControl(control1);
  56.     control1.setKinematic(isKinematic);
  57.     Node node1 = new Node();
  58.     node1.attachChild(spatial1);
  59.     rootNode.attachChild(node1);
  60.     bulletAppState.getPhysicsSpace().add(control1);
  61.  
  62.     // Create second box
  63.     Geometry spatial2 = new Geometry("Box2", mesh);
  64.     RigidBodyControl control2 = new RigidBodyControl(1.0f);
  65.     spatial2.setMaterial(mat);
  66.     spatial2.addControl(control2);
  67.     control2.setKinematic(isKinematic);
  68.     Node node2 = new Node();
  69.     node2.attachChild(spatial2);
  70.     rootNode.attachChild(node2);
  71.     bulletAppState.getPhysicsSpace().add(control2);
  72.  
  73.     // Rotate the boxes some magic angles, if these values vary the boxes will be detected to collide.
  74.     // 8% randomly-generated angle pairs will trigger the problem.
  75.     final Quaternion q1 = new Quaternion().fromAngles(3.6280732f,5.2767496f,5.40851f);
  76.     final Quaternion q2 = new Quaternion().fromAngles(3.1379673f,5.9316964f,0.8267426f);
  77.     node1.setLocalRotation(q1);
  78.     node2.setLocalRotation(q2);
  79.     node1.setLocalTranslation(Vector3f.ZERO);
  80.     node2.setLocalTranslation(Vector3f.ZERO);
  81.     bulletAppState.startPhysics();
  82.   }
  83.  
  84.   @Override
  85.   public void simpleUpdate(float tpf) {
  86.     super.simpleUpdate(tpf);
  87.     ticks++;
  88.   }
  89.  
  90.   @Override
  91.   public void collision(PhysicsCollisionEvent event) {
  92.     System.out.println("Collision event lifetime: " + event.getLifeTime());
  93.     System.out.println("Number of ticks: " + ticks);
  94.     System.exit(0);
  95.   }
  96. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement