I have 2 spheres and 1 box polygons in my simulation software and I map them into JBullet DiscreteDynamicsWorld for collision detection. The 2 spheres are dynamic and the box is static. Gravity is applied so the spheres are falling nicely but they do not collide with the box. They just fall through the box. Then I realise the Aabb of the box is not bounding around it, but concentrated in the middle of the polygon.
Is there something that I set wrongly? I am attaching my code below:
Code: Select all
// CollisionConfiguration class
defaultCC = new DefaultCollisionConfiguration();
// Dispatcher class
dispatcher = new CollisionDispatcher(defaultCC);
// BroadphaseInterface class
Vector3f worldaabbmin = new Vector3f(0f, 0f, -200f);
Vector3f worldaabbmax = new Vector3f(200f, 200f, 200f);
broadphase = new AxisSweep3(worldaabbmin, worldaabbmax, 10);
// ConstraintSolver class
sics = new SequentialImpulseConstraintSolver();
// Dynamics world initialisation
dynamicsWorld = new DiscreteDynamicsWorld(dispatcher, broadphase, sics, defaultCC);
RigidBody newRigidBody = null;
// ================ ball1
// Mapping ball1 into JBullet environment
newRigidBody = mapMeshObjects(ball1, 50.0f, false);
dynamicsWorld.addRigidBody(newRigidBody, CollisionFilterGroups.DEFAULT_FILTER, CollisionFilterGroups.ALL_FILTER);
// ==================== ball1
// ==================== ball2
// Mapping ball2 into JBullet environment
newRigidBody = mapMeshObjects(ball2, 50.0f, false);
dynamicsWorld.addRigidBody(newRigidBody, CollisionFilterGroups.DEFAULT_FILTER, CollisionFilterGroups.ALL_FILTER);
// ==================== ball2
// ==================== plate
// Mapping plate into JBullet environment
newRigidBody = mapMeshObjects(plate, 0.0f, false);
dynamicsWorld.addRigidBody(newRigidBody, CollisionFilterGroups.STATIC_FILTER, CollisionFilterGroups.ALL_FILTER);
// ==================== plate
// Set gravity
dynamicsWorld.setGravity(new Vector3f(0.0f, -50.0f, 0.0f));
Code: Select all
public RigidBody mapMeshObjects(GlsTriMesh trimesh, float mass, boolean kinematic)
{
// Setting origin of DisplayObject
Transform startTransform = new Transform();
startTransform.origin.set(trimesh.X(), trimesh.Y(), trimesh.Z());
DefaultMotionState motionState = new DefaultMotionState(startTransform);
TriangleIndexVertexArray triIndexVertArr = new TriangleIndexVertexArray();
// Creating an object to reference to a GlsTrimesh.
IndexedMesh trimeshRef = new IndexedMesh();
trimeshRef.triangleIndexBase = ByteBuffer.allocate(trimesh.NumberOfFaces() * 3 * 4);
trimeshRef.vertexBase = ByteBuffer.allocate(trimesh.NumberOfVertices() * 3 * 4);
// Settle triangle referencing
GlsTriMesh.GlsMeshFace[] trimeshFacesArr = trimesh.Faces();
trimeshRef.numTriangles = trimeshFacesArr.length;
trimeshRef.triangleIndexStride = 12;
for(int i = 0; i < trimeshFacesArr.length; i++)
{
trimeshRef.triangleIndexBase.putInt(trimeshFacesArr[i].a);
trimeshRef.triangleIndexBase.putInt(trimeshFacesArr[i].b);
trimeshRef.triangleIndexBase.putInt(trimeshFacesArr[i].c);
}
// Settle vertex referencing
Vertex[] trimeshVertexArr = trimesh.Vertices();
trimeshRef.numVertices = trimeshVertexArr.length;
trimeshRef.vertexStride = 12;
for(int i = 0; i < trimeshVertexArr.length; i++)
{
trimeshRef.vertexBase.putFloat(trimeshVertexArr[i].x);
trimeshRef.vertexBase.putFloat(trimeshVertexArr[i].y);
trimeshRef.vertexBase.putFloat(trimeshVertexArr[i].z);
}
triIndexVertArr.addIndexedMesh(trimeshRef);
BvhTriangleMeshShape bvhTrimesh = new BvhTriangleMeshShape(triIndexVertArr, false, true);
Vector3f localInertia = new Vector3f(0.0f, 0.0f, 0.0f);
// Calculate local inertia if mass is more than 0
if(mass > 0.0f)
{
bvhTrimesh.calculateLocalInertia(mass, localInertia);
}
RigidBody newRigidBody = new RigidBody(mass, motionState, bvhTrimesh, localInertia);
if(mass == 0.0f && kinematic == true)
{
newRigidBody.setCollisionFlags(CollisionFlags.KINEMATIC_OBJECT);
newRigidBody.setActivationState(CollisionObject.DISABLE_DEACTIVATION);
}
return newRigidBody;
}
Regards,
Aaron