Search in sources :

Example 1 with DynamicsWorld

use of com.bulletphysics.dynamics.DynamicsWorld in project jmonkeyengine by jMonkeyEngine.

the class PhysicsSpace method setTickCallback.

private void setTickCallback() {
    final PhysicsSpace space = this;
    InternalTickCallback callback2 = new InternalTickCallback() {

        @Override
        public void internalTick(DynamicsWorld dw, float f) {
            //execute task list
            AppTask task = pQueue.poll();
            task = pQueue.poll();
            while (task != null) {
                while (task.isCancelled()) {
                    task = pQueue.poll();
                }
                try {
                    task.invoke();
                } catch (Exception ex) {
                    logger.log(Level.SEVERE, null, ex);
                }
                task = pQueue.poll();
            }
            for (Iterator<PhysicsTickListener> it = tickListeners.iterator(); it.hasNext(); ) {
                PhysicsTickListener physicsTickCallback = it.next();
                physicsTickCallback.prePhysicsTick(space, f);
            }
        }
    };
    dynamicsWorld.setPreTickCallback(callback2);
    InternalTickCallback callback = new InternalTickCallback() {

        @Override
        public void internalTick(DynamicsWorld dw, float f) {
            for (Iterator<PhysicsTickListener> it = tickListeners.iterator(); it.hasNext(); ) {
                PhysicsTickListener physicsTickCallback = it.next();
                physicsTickCallback.physicsTick(space, f);
            }
        }
    };
    dynamicsWorld.setInternalTickCallback(callback, this);
}
Also used : AppTask(com.jme3.app.AppTask) InternalTickCallback(com.bulletphysics.dynamics.InternalTickCallback) DiscreteDynamicsWorld(com.bulletphysics.dynamics.DiscreteDynamicsWorld) DynamicsWorld(com.bulletphysics.dynamics.DynamicsWorld)

Example 2 with DynamicsWorld

use of com.bulletphysics.dynamics.DynamicsWorld in project Terasology by MovingBlocks.

the class BulletPhysics method getNewCollisionPairs.

private Collection<? extends PhysicsSystem.CollisionPair> getNewCollisionPairs() {
    List<PhysicsSystem.CollisionPair> collisionPairs = Lists.newArrayList();
    DynamicsWorld world = discreteDynamicsWorld;
    ObjectArrayList<PersistentManifold> manifolds = new ObjectArrayList<>();
    for (PairCachingGhostObject trigger : entityTriggers.values()) {
        EntityRef entity = (EntityRef) trigger.getUserPointer();
        for (BroadphasePair initialPair : trigger.getOverlappingPairCache().getOverlappingPairArray()) {
            EntityRef otherEntity = null;
            if (initialPair.pProxy0.clientObject == trigger) {
                if (((CollisionObject) initialPair.pProxy1.clientObject).getUserPointer() instanceof EntityRef) {
                    otherEntity = (EntityRef) ((CollisionObject) initialPair.pProxy1.clientObject).getUserPointer();
                }
            } else {
                if (((CollisionObject) initialPair.pProxy0.clientObject).getUserPointer() instanceof EntityRef) {
                    otherEntity = (EntityRef) ((CollisionObject) initialPair.pProxy0.clientObject).getUserPointer();
                }
            }
            if (otherEntity == null || otherEntity == EntityRef.NULL) {
                continue;
            }
            BroadphasePair pair = world.getPairCache().findPair(initialPair.pProxy0, initialPair.pProxy1);
            if (pair == null) {
                continue;
            }
            manifolds.clear();
            if (pair.algorithm != null) {
                pair.algorithm.getAllContactManifolds(manifolds);
            }
            for (PersistentManifold manifold : manifolds) {
                for (int point = 0; point < manifold.getNumContacts(); ++point) {
                    ManifoldPoint manifoldPoint = manifold.getContactPoint(point);
                    if (manifoldPoint.getDistance() < 0) {
                        collisionPairs.add(new PhysicsSystem.CollisionPair(entity, otherEntity, VecMath.from(manifoldPoint.positionWorldOnA), VecMath.from(manifoldPoint.positionWorldOnB), manifoldPoint.getDistance(), VecMath.from(manifoldPoint.normalWorldOnB)));
                        break;
                    }
                }
            }
        }
    }
    return collisionPairs;
}
Also used : ManifoldPoint(com.bulletphysics.collision.narrowphase.ManifoldPoint) PersistentManifold(com.bulletphysics.collision.narrowphase.PersistentManifold) BroadphasePair(com.bulletphysics.collision.broadphase.BroadphasePair) PairCachingGhostObject(com.bulletphysics.collision.dispatch.PairCachingGhostObject) ManifoldPoint(com.bulletphysics.collision.narrowphase.ManifoldPoint) CollisionObject(com.bulletphysics.collision.dispatch.CollisionObject) PhysicsSystem(org.terasology.physics.engine.PhysicsSystem) ObjectArrayList(com.bulletphysics.util.ObjectArrayList) EntityRef(org.terasology.entitySystem.entity.EntityRef) DiscreteDynamicsWorld(com.bulletphysics.dynamics.DiscreteDynamicsWorld) DynamicsWorld(com.bulletphysics.dynamics.DynamicsWorld)

Aggregations

DiscreteDynamicsWorld (com.bulletphysics.dynamics.DiscreteDynamicsWorld)2 DynamicsWorld (com.bulletphysics.dynamics.DynamicsWorld)2 BroadphasePair (com.bulletphysics.collision.broadphase.BroadphasePair)1 CollisionObject (com.bulletphysics.collision.dispatch.CollisionObject)1 PairCachingGhostObject (com.bulletphysics.collision.dispatch.PairCachingGhostObject)1 ManifoldPoint (com.bulletphysics.collision.narrowphase.ManifoldPoint)1 PersistentManifold (com.bulletphysics.collision.narrowphase.PersistentManifold)1 InternalTickCallback (com.bulletphysics.dynamics.InternalTickCallback)1 ObjectArrayList (com.bulletphysics.util.ObjectArrayList)1 AppTask (com.jme3.app.AppTask)1 EntityRef (org.terasology.entitySystem.entity.EntityRef)1 PhysicsSystem (org.terasology.physics.engine.PhysicsSystem)1