Search in sources :

Example 1 with BoxShape

use of com.bulletphysics.collision.shapes.BoxShape in project jmonkeyengine by jMonkeyEngine.

the class BoxCollisionShape method createShape.

protected void createShape() {
    cShape = new BoxShape(Converter.convert(halfExtents));
    cShape.setLocalScaling(Converter.convert(getScale()));
    cShape.setMargin(margin);
}
Also used : BoxShape(com.bulletphysics.collision.shapes.BoxShape)

Example 2 with BoxShape

use of com.bulletphysics.collision.shapes.BoxShape in project Terasology by MovingBlocks.

the class BulletPhysics method getShapeFor.

/**
 * Returns the shape belonging to the given entity. It currently knows 4
 * different shapes: Sphere, Capsule, Cylinder or arbitrary.
 * The shape is determined based on the shape component of the given entity.
 * If the entity has somehow got multiple shapes, only one is picked. The
 * order of priority is: Sphere, Capsule, Cylinder, arbitrary.
 * <br><br>
 * TODO: Flyweight this (take scale as parameter)
 *
 * @param entity the entity to get the shape of.
 * @return the shape of the entity, ready to be used by Bullet.
 */
private ConvexShape getShapeFor(EntityRef entity) {
    BoxShapeComponent box = entity.getComponent(BoxShapeComponent.class);
    if (box != null) {
        Vector3f halfExtents = new Vector3f(VecMath.to(box.extents));
        halfExtents.scale(0.5f);
        return new BoxShape(halfExtents);
    }
    SphereShapeComponent sphere = entity.getComponent(SphereShapeComponent.class);
    if (sphere != null) {
        return new SphereShape(sphere.radius);
    }
    CapsuleShapeComponent capsule = entity.getComponent(CapsuleShapeComponent.class);
    if (capsule != null) {
        return new CapsuleShape(capsule.radius, capsule.height);
    }
    CylinderShapeComponent cylinder = entity.getComponent(CylinderShapeComponent.class);
    if (cylinder != null) {
        return new CylinderShape(new Vector3f(cylinder.radius, 0.5f * cylinder.height, cylinder.radius));
    }
    HullShapeComponent hull = entity.getComponent(HullShapeComponent.class);
    if (hull != null) {
        ObjectArrayList<Vector3f> verts = new ObjectArrayList<>();
        TFloatIterator iterator = hull.sourceMesh.getVertices().iterator();
        while (iterator.hasNext()) {
            Vector3f newVert = new Vector3f();
            newVert.x = iterator.next();
            newVert.y = iterator.next();
            newVert.z = iterator.next();
            verts.add(newVert);
        }
        return new ConvexHullShape(verts);
    }
    CharacterMovementComponent characterMovementComponent = entity.getComponent(CharacterMovementComponent.class);
    if (characterMovementComponent != null) {
        return new CapsuleShape(characterMovementComponent.radius, characterMovementComponent.height);
    }
    logger.error("Creating physics object that requires a ShapeComponent or CharacterMovementComponent, but has neither. Entity: {}", entity);
    throw new IllegalArgumentException("Creating physics object that requires a ShapeComponent or CharacterMovementComponent, but has neither. Entity: " + entity);
}
Also used : SphereShapeComponent(org.terasology.physics.components.shapes.SphereShapeComponent) ConvexHullShape(com.bulletphysics.collision.shapes.ConvexHullShape) CylinderShapeComponent(org.terasology.physics.components.shapes.CylinderShapeComponent) HullShapeComponent(org.terasology.physics.components.shapes.HullShapeComponent) CharacterMovementComponent(org.terasology.logic.characters.CharacterMovementComponent) CapsuleShapeComponent(org.terasology.physics.components.shapes.CapsuleShapeComponent) CylinderShape(com.bulletphysics.collision.shapes.CylinderShape) ObjectArrayList(com.bulletphysics.util.ObjectArrayList) Vector3f(javax.vecmath.Vector3f) TFloatIterator(gnu.trove.iterator.TFloatIterator) BoxShapeComponent(org.terasology.physics.components.shapes.BoxShapeComponent) SphereShape(com.bulletphysics.collision.shapes.SphereShape) BoxShape(com.bulletphysics.collision.shapes.BoxShape) CapsuleShape(com.bulletphysics.collision.shapes.CapsuleShape)

Example 3 with BoxShape

use of com.bulletphysics.collision.shapes.BoxShape in project Terasology by MovingBlocks.

the class BulletPhysics method scanArea.

@Override
public List<EntityRef> scanArea(AABB area, Iterable<CollisionGroup> collisionFilter) {
    // TODO: Add the aabbTest method from newer versions of bullet to TeraBullet, use that instead
    BoxShape shape = new BoxShape(VecMath.to(area.getExtents()));
    GhostObject scanObject = createCollider(VecMath.to(area.getCenter()), shape, CollisionFilterGroups.SENSOR_TRIGGER, combineGroups(collisionFilter), CollisionFlags.NO_CONTACT_RESPONSE);
    // This in particular is overkill
    broadphase.calculateOverlappingPairs(dispatcher);
    List<EntityRef> result = Lists.newArrayList();
    for (int i = 0; i < scanObject.getNumOverlappingObjects(); ++i) {
        CollisionObject other = scanObject.getOverlappingObject(i);
        Object userObj = other.getUserPointer();
        if (userObj instanceof EntityRef) {
            result.add((EntityRef) userObj);
        }
    }
    removeCollider(scanObject);
    return result;
}
Also used : CollisionObject(com.bulletphysics.collision.dispatch.CollisionObject) GhostObject(com.bulletphysics.collision.dispatch.GhostObject) PairCachingGhostObject(com.bulletphysics.collision.dispatch.PairCachingGhostObject) GhostObject(com.bulletphysics.collision.dispatch.GhostObject) PairCachingGhostObject(com.bulletphysics.collision.dispatch.PairCachingGhostObject) CollisionObject(com.bulletphysics.collision.dispatch.CollisionObject) EntityRef(org.terasology.entitySystem.entity.EntityRef) ManifoldPoint(com.bulletphysics.collision.narrowphase.ManifoldPoint) BoxShape(com.bulletphysics.collision.shapes.BoxShape)

Aggregations

BoxShape (com.bulletphysics.collision.shapes.BoxShape)3 CollisionObject (com.bulletphysics.collision.dispatch.CollisionObject)1 GhostObject (com.bulletphysics.collision.dispatch.GhostObject)1 PairCachingGhostObject (com.bulletphysics.collision.dispatch.PairCachingGhostObject)1 ManifoldPoint (com.bulletphysics.collision.narrowphase.ManifoldPoint)1 CapsuleShape (com.bulletphysics.collision.shapes.CapsuleShape)1 ConvexHullShape (com.bulletphysics.collision.shapes.ConvexHullShape)1 CylinderShape (com.bulletphysics.collision.shapes.CylinderShape)1 SphereShape (com.bulletphysics.collision.shapes.SphereShape)1 ObjectArrayList (com.bulletphysics.util.ObjectArrayList)1 TFloatIterator (gnu.trove.iterator.TFloatIterator)1 Vector3f (javax.vecmath.Vector3f)1 EntityRef (org.terasology.entitySystem.entity.EntityRef)1 CharacterMovementComponent (org.terasology.logic.characters.CharacterMovementComponent)1 BoxShapeComponent (org.terasology.physics.components.shapes.BoxShapeComponent)1 CapsuleShapeComponent (org.terasology.physics.components.shapes.CapsuleShapeComponent)1 CylinderShapeComponent (org.terasology.physics.components.shapes.CylinderShapeComponent)1 HullShapeComponent (org.terasology.physics.components.shapes.HullShapeComponent)1 SphereShapeComponent (org.terasology.physics.components.shapes.SphereShapeComponent)1