So I have spent a long time trying to work on a collision detection system from scratch for own game engine and came up fruitless due to lack of time. Finally I decided to try to use Jbullet to try to make things quicker. Now the documentation is basically useless, and I am having somewhat of a difficulty trying to transfer over the bullet code to java (or what I transfer over is not working). I have been ripping out my hair trying to search through the library code, but then time-saving that I was hoping for has almost been useless. So, I am going to explain what I am doing, maybe you guys can help me out. I am only looking for simple collision detection, like you hit something then just print a line for now. The rest I can probably work out on my own.
So I create my world:
BroadphaseInterface broadphase = new DbvtBroadphase();
CollisionConfiguration collisionConfig = new DefaultCollisionConfiguration();
Dispatcher dispatcher = new CollisionDispatcher(collisionConfig);
ConstraintSolver solver = new SequentialImpulseConstraintSolver();
DynamicsWorld dynamicsWorld = new DiscreteDynamicsWorld(dispatcher, broadphase, solver, collisionConfig);
return dynamicsWorld;
So I have my entity Class and in there I have another class that stores all the info for the physics object attached to that entity. This allows me to simply do: entity.getPhysics().getCollisionObject()/.setPosition() etc...
Then I create my CollisionObject in that class:
List<org.lwjgl.util.vector.Vector3f> mesh = model.getModel().getVertices();
ObjectArrayList<javax.vecmath.Vector3f> vertices = new ObjectArrayList<javax.vecmath.Vector3f>();
for(org.lwjgl.util.vector.Vector3f vertex:mesh){
javax.vecmath.Vector3f v = new javax.vecmath.Vector3f(vertex.x, vertex.y, vertex.z);
vertices.add(v);
}
ConvexHullShape shape = new ConvexHullShape(vertices);
ShapeHull hull = new ShapeHull(shape);
hull.buildHull(shape.getMargin());
ConvexHullShape newShape = new ConvexHullShape(hull.getVertexPointer());
CollisionObject result = newShape;
I believe this converts already made mesh, which I use to render my entity, from Vector3f of the LWJGL library, and Jbullets Vector3f. Then it creates a ConvexHullShape out of those vertices in the mesh, and I believe that the:
hull.buildHull(shape.getMargin());
is supposed to kind of simplify the mesh (from the documentation). Then I just create the Collision Object. Pretty simple, I think...
I create my rigidbody (although I am not sure I need a rigid body or just a collision Object, and If someone could let me know if this is true, that would be great):
//mass = 0, so that there is not any gravity application?
float mass = 0;
Transform transform = new Transform(new Matrix4f(new Quat4f(rotation.x, rotation.y, rotation.z, 1), position, scale));
this.transform = transform;
MotionState state = new DefaultMotionState(transform);
RigidBodyConstructionInfo info = new RigidBodyConstructionInfo(mass, state, shape);
RigidBody body = new RigidBody(info);
Then I go through my game loop:
dynamicsWorld.stepSimulation(DisplayManager.getFrameTimeSeconds(), 7);
dynamicsWorld.performDiscreteCollisionDetection();
dynamicsWorld.setInternalTickCallback(new InternalTickCallback(){
@Override
public void internalTick(DynamicsWorld world, float delta) {
Dispatcher dispatcher = world.getDispatcher();
int manifoldCount = dispatcher.getNumManifolds();
for(int i = 0; i < manifoldCount; i ++){
PersistentManifold manifold = dispatcher.getManifoldByIndexInternal(i);
RigidBody object1 = (RigidBody)manifold.getBody0();
RigidBody object2 = (RigidBody)manifold.getBody1();
CollisionObject physicsObject1 = (CollisionObject)object1.getUserPointer();
CollisionObject physicsObject2 = (CollisionObject)object2.getUserPointer();
boolean contact = false;
javax.vecmath.Vector3f normal = null;
for (int j = 0; j < manifold.getNumContacts(); j++) {
ManifoldPoint contactPoint = manifold.getContactPoint(j);
if (contactPoint.getDistance() < 0.0f) {
contact = true;
normal = contactPoint.normalWorldOnB;
break;
}
}
if (contact) {
System.out.println("hit");
}
}
}
}, null);
I got this from someone... I forgot where though. So, basically nothing is happening... I am not sure but maybe I have to add the objects to the manifold, or something like that. Don't know how to do so. Any help?
Thank you,