1
\$\begingroup\$

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,

\$\endgroup\$
3
  • \$\begingroup\$ That internalTick was probably from my answer here. In your situation there are too many things that can go wrong that we could give the answer right away. You need to narrow down the problem. 1) Replace meshes with primitives. 2) Try with rigid bodies that are moved by the physics engine instead of you. Apply some initial impulses / gravity to make them move. \$\endgroup\$
    – msell
    Sep 8, 2015 at 7:23
  • \$\begingroup\$ Maybe it is, although I thought it was from a c++ code, i could be wrong though. I do not know what is wrong with it exactly, that is why I am asking for possible problems. The meshes are now primitive boxes, but I do not want to move the object with the physics engine, because I want to specify what to do with the objects. when I update my entity position, I use this: transform.origin.set(position); I thought that would move it, maybe not. But I do not want gravity from the engine. \$\endgroup\$
    – thor625
    Sep 8, 2015 at 23:25
  • \$\begingroup\$ The original C++ code is in Bullet Physics Wiki. To move the object yourself you might need kinematic objects. See my other answer here for more information. However I was suggesting to try to let Bullet move your objects as a debugging step first. That way you can isolate if the problem is in the collision checking or if the whole physics world has been setup incorrectly. \$\endgroup\$
    – msell
    Sep 9, 2015 at 4:58

0

You must log in to answer this question.

Browse other questions tagged .