How to handle collision between static and kinematic object?

Post Reply
Chaz
Posts: 44
Joined: Mon Jan 12, 2015 1:36 pm

How to handle collision between static and kinematic object?

Post by Chaz »

Hello. I try to use a KinematicCharacterController with a HeightfieldTerrainShape and my char goes through that terrain. But I can detect collision between them using a collision detection method.

Code: Select all

 private static void DetectCollision()
    {
        int numManifolds = dynamicsWorld.getDispatcher().getNumManifolds();
        for (int i=0;i<numManifolds;i++)
        {
            PersistentManifold contactManifold =  dynamicsWorld.getDispatcher().getManifoldByIndexInternal(i);
            CollisionObject obA = (CollisionObject)contactManifold.getBody0();
            CollisionObject obB = (CollisionObject)contactManifold.getBody1();

            int numContacts = contactManifold.getNumContacts();
            for (int j=0;j<numContacts;j++)
            {
                ManifoldPoint pt = contactManifold.getContactPoint(j);
                if (pt.getDistance()<0.f)
                {
                    Vector3f ptA = new Vector3f();
                    Vector3f ptB = new Vector3f();
                    pt.getPositionWorldOnA(ptA);
                    pt.getPositionWorldOnB(ptB);
                    Vector3f normalOnB = pt.normalWorldOnB;
                    System.out.println("Collision!");
                }
            }
        }
    }
I call it after step simulation

Code: Select all

 private void update()
    {
        if(active)
        {
            dynamicsWorld.stepSimulation(1.0f / 60.0f);
            DetectCollision();
        }
    }
So how to make character not goes through the terrain?
p.s. using jbullet
p.s.s. sry for my bad english
Chaz
Posts: 44
Joined: Mon Jan 12, 2015 1:36 pm

Re: How to handle collision between static and kinematic obj

Post by Chaz »

I have decided that code of world and character creating will be usefull for some person who want to help me

Code: Select all

    private static void setUpPhysics() throws Exception
    {
        broadphase = new DbvtBroadphase();
        CollisionConfiguration collisionConfiguration = new DefaultCollisionConfiguration();
        CollisionDispatcher dispatcher = new CollisionDispatcher(collisionConfiguration);
        ConstraintSolver solver = new SequentialImpulseConstraintSolver();
        dynamicsWorld=new DiscreteDynamicsWorld(dispatcher, broadphase, solver, collisionConfiguration);
        dynamicsWorld.setGravity(new Vector3f(0,-9.81f,0));
        
        //Terrain
        terrain=new Terrain(false);
        HeightfieldTerrainShape terrainShape = terrain.getTerrainShape();
        Vector3f scaling = terrain.getScaling();
        int heightStick = terrain.getHeightStickLength();
        int widthStick = terrain.getWidthStickLength();
        float maxHeight = terrain.getMaxHeight();
        startPos = new Vector3f(-((heightStick-1)/2f)*scaling.getX(),(maxHeight/2f)*scaling.getY(),((widthStick-1)/2f)*scaling.getZ());
        MotionState terrainMotionState = new DefaultMotionState(new Transform(new Matrix4f(new Quat4f(0,0,0,1),startPos,1)));
        RigidBodyConstructionInfo terrainBodyConstructionInfo = new RigidBodyConstructionInfo(0, terrainMotionState, terrainShape, new Vector3f(0,0,0));
        terrainBodyConstructionInfo.restitution=0.2f;
        terrainRigidBody = new RigidBody(terrainBodyConstructionInfo);
        dynamicsWorld.addCollisionObject(terrainRigidBody);
        //End terrain


        Transform startTransform = new Transform();
        startTransform.setIdentity();
        startTransform.origin.set(-10f, 5f, 10f);

        Vector3f worldMin = new Vector3f(-10000f,-10000f,-10000f);
        Vector3f worldMax = new Vector3f(10000f,10000f,10000f);
        AxisSweep3 sweepBP = new AxisSweep3(worldMin, worldMax);

        ghostObject = new PairCachingGhostObject();
        ghostObject.setWorldTransform(startTransform);
        sweepBP.getOverlappingPairCache().setInternalGhostPairCallback(new GhostPairCallback());
        ConvexShape capsule = new CapsuleShape(1,5);
        ghostObject.setCollisionShape(capsule);
        ghostObject.setCollisionFlags(CollisionFlags.CHARACTER_OBJECT);

        float stepHeight = 1;
        character = new KinematicCharacterController(ghostObject, capsule, stepHeight);
        character.setGravity(0.2f);
        character.setJumpSpeed(1);
        character.setMaxJumpHeight(10);
        dynamicsWorld.addCollisionObject(ghostObject, CollisionFilterGroups.CHARACTER_FILTER, (CollisionFilterGroups.ALL_FILTER));
        dynamicsWorld.addAction(character);
    }
Chaz
Posts: 44
Joined: Mon Jan 12, 2015 1:36 pm

Re: How to handle collision between static and kinematic obj

Post by Chaz »

I decide that better would be to create my own character controller using ghost object and ray test. What you think about that?
Post Reply