Search in sources :

Example 1 with PairCachingGhostObject

use of com.bulletphysics.collision.dispatch.PairCachingGhostObject in project Terasology by MovingBlocks.

the class BulletPhysics method createCollider.

private PairCachingGhostObject createCollider(Vector3f pos, ConvexShape shape, short groups, short filters, int collisionFlags) {
    Transform startTransform = new Transform(new Matrix4f(new Quat4f(0, 0, 0, 1), pos, 1.0f));
    PairCachingGhostObject result = new PairCachingGhostObject();
    result.setWorldTransform(startTransform);
    result.setCollisionShape(shape);
    result.setCollisionFlags(collisionFlags);
    discreteDynamicsWorld.addCollisionObject(result, groups, filters);
    return result;
}
Also used : Matrix4f(javax.vecmath.Matrix4f) Transform(com.bulletphysics.linearmath.Transform) PairCachingGhostObject(com.bulletphysics.collision.dispatch.PairCachingGhostObject) Quat4f(javax.vecmath.Quat4f)

Example 2 with PairCachingGhostObject

use of com.bulletphysics.collision.dispatch.PairCachingGhostObject in project Terasology by MovingBlocks.

the class BulletPhysics method newTrigger.

// *******************Private helper methods**************************\\
/**
 * Creates a new trigger.
 *
 * @param entity the entity to create a trigger for.
 */
private boolean newTrigger(EntityRef entity) {
    LocationComponent location = entity.getComponent(LocationComponent.class);
    TriggerComponent trigger = entity.getComponent(TriggerComponent.class);
    ConvexShape shape = getShapeFor(entity);
    if (shape != null && location != null && trigger != null) {
        float scale = location.getWorldScale();
        shape.setLocalScaling(new Vector3f(scale, scale, scale));
        List<CollisionGroup> detectGroups = Lists.newArrayList(trigger.detectGroups);
        CollisionGroup collisionGroup = trigger.collisionGroup;
        PairCachingGhostObject triggerObj = createCollider(VecMath.to(location.getWorldPosition()), shape, collisionGroup.getFlag(), combineGroups(detectGroups), CollisionFlags.NO_CONTACT_RESPONSE);
        triggerObj.setUserPointer(entity);
        PairCachingGhostObject oldTrigger = entityTriggers.put(entity, triggerObj);
        if (oldTrigger != null) {
            logger.warn("Creating a trigger for an entity that already has a trigger. " + "Multiple trigger pre entity are not supported. Removing old one. Entity: {}", entity);
            removeCollider(oldTrigger);
            return false;
        } else {
            return true;
        }
    } else {
        logger.warn("Trying to create trigger for entity without ShapeComponent or without LocationComponent or without TriggerComponent. Entity: {}", entity);
        return false;
    }
}
Also used : CollisionGroup(org.terasology.physics.CollisionGroup) StandardCollisionGroup(org.terasology.physics.StandardCollisionGroup) Vector3f(javax.vecmath.Vector3f) ConvexShape(com.bulletphysics.collision.shapes.ConvexShape) LocationComponent(org.terasology.logic.location.LocationComponent) PairCachingGhostObject(com.bulletphysics.collision.dispatch.PairCachingGhostObject) TriggerComponent(org.terasology.physics.components.TriggerComponent)

Example 3 with PairCachingGhostObject

use of com.bulletphysics.collision.dispatch.PairCachingGhostObject in project jmonkeyengine by jMonkeyEngine.

the class PhysicsCharacter method buildObject.

protected void buildObject() {
    if (gObject == null) {
        gObject = new PairCachingGhostObject();
    }
    gObject.setCollisionFlags(CollisionFlags.CHARACTER_OBJECT);
    gObject.setCollisionFlags(gObject.getCollisionFlags() & ~CollisionFlags.NO_CONTACT_RESPONSE);
    gObject.setCollisionShape(collisionShape.getCShape());
    gObject.setUserPointer(this);
    character = new KinematicCharacterController(gObject, (ConvexShape) collisionShape.getCShape(), stepHeight);
}
Also used : ConvexShape(com.bulletphysics.collision.shapes.ConvexShape) PairCachingGhostObject(com.bulletphysics.collision.dispatch.PairCachingGhostObject) KinematicCharacterController(com.bulletphysics.dynamics.character.KinematicCharacterController)

Example 4 with PairCachingGhostObject

use of com.bulletphysics.collision.dispatch.PairCachingGhostObject in project jmonkeyengine by jMonkeyEngine.

the class PhysicsGhostObject method buildObject.

protected void buildObject() {
    if (gObject == null) {
        gObject = new PairCachingGhostObject();
        gObject.setCollisionFlags(gObject.getCollisionFlags() | CollisionFlags.NO_CONTACT_RESPONSE);
    }
    gObject.setCollisionShape(collisionShape.getCShape());
    gObject.setUserPointer(this);
}
Also used : PairCachingGhostObject(com.bulletphysics.collision.dispatch.PairCachingGhostObject)

Example 5 with PairCachingGhostObject

use of com.bulletphysics.collision.dispatch.PairCachingGhostObject 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

PairCachingGhostObject (com.bulletphysics.collision.dispatch.PairCachingGhostObject)6 ConvexShape (com.bulletphysics.collision.shapes.ConvexShape)2 Transform (com.bulletphysics.linearmath.Transform)2 Matrix4f (javax.vecmath.Matrix4f)2 Quat4f (javax.vecmath.Quat4f)2 Vector3f (javax.vecmath.Vector3f)2 LocationComponent (org.terasology.logic.location.LocationComponent)2 BroadphasePair (com.bulletphysics.collision.broadphase.BroadphasePair)1 CollisionObject (com.bulletphysics.collision.dispatch.CollisionObject)1 ManifoldPoint (com.bulletphysics.collision.narrowphase.ManifoldPoint)1 PersistentManifold (com.bulletphysics.collision.narrowphase.PersistentManifold)1 DiscreteDynamicsWorld (com.bulletphysics.dynamics.DiscreteDynamicsWorld)1 DynamicsWorld (com.bulletphysics.dynamics.DynamicsWorld)1 KinematicCharacterController (com.bulletphysics.dynamics.character.KinematicCharacterController)1 ObjectArrayList (com.bulletphysics.util.ObjectArrayList)1 EntityRef (org.terasology.entitySystem.entity.EntityRef)1 CollisionGroup (org.terasology.physics.CollisionGroup)1 StandardCollisionGroup (org.terasology.physics.StandardCollisionGroup)1 TriggerComponent (org.terasology.physics.components.TriggerComponent)1 PhysicsSystem (org.terasology.physics.engine.PhysicsSystem)1