Search in sources :

Example 1 with HullShapeComponent

use of org.terasology.engine.physics.components.shapes.HullShapeComponent 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 entityRef the entity to get the shape of.
 * @return the shape of the entity, ready to be used by Bullet.
 */
private btCollisionShape getShapeFor(EntityRef entityRef) {
    BoxShapeComponent box = entityRef.getComponent(BoxShapeComponent.class);
    if (box != null) {
        Vector3f halfExtents = new Vector3f(box.extents);
        return new btBoxShape(halfExtents.mul(.5f));
    }
    SphereShapeComponent sphere = entityRef.getComponent(SphereShapeComponent.class);
    if (sphere != null) {
        return new btSphereShape(sphere.radius);
    }
    CapsuleShapeComponent capsule = entityRef.getComponent(CapsuleShapeComponent.class);
    if (capsule != null) {
        return new btCapsuleShape(capsule.radius, capsule.height);
    }
    CylinderShapeComponent cylinder = entityRef.getComponent(CylinderShapeComponent.class);
    if (cylinder != null) {
        return new btCylinderShape(new Vector3f(cylinder.radius, 0.5f * cylinder.height, cylinder.radius));
    }
    HullShapeComponent hull = entityRef.getComponent(HullShapeComponent.class);
    if (hull != null) {
        VertexAttributeBinding<Vector3fc, Vector3f> positions = hull.sourceMesh.vertices();
        final int numVertices = hull.sourceMesh.elementCount();
        FloatBuffer buffer = BufferUtils.createFloatBuffer(numVertices * 3);
        Vector3f pos = new Vector3f();
        for (int i = 0; i < numVertices; i++) {
            positions.get(i, pos);
            buffer.put(pos.x);
            buffer.put(pos.y);
            buffer.put(pos.z);
        }
        return new btConvexHullShape(buffer, numVertices, 3 * Float.BYTES);
    }
    CharacterMovementComponent characterMovementComponent = entityRef.getComponent(CharacterMovementComponent.class);
    if (characterMovementComponent != null) {
        return new btCapsuleShape(characterMovementComponent.pickupRadius, characterMovementComponent.height - 2 * characterMovementComponent.radius);
    }
    logger.error("Creating physics object that requires a ShapeComponent or CharacterMovementComponent, but has " + "neither. Entity: {}", entityRef);
    throw new IllegalArgumentException("Creating physics object that requires a ShapeComponent or " + "CharacterMovementComponent, but has neither. Entity: " + entityRef);
}
Also used : com.badlogic.gdx.physics.bullet.collision.btBoxShape(com.badlogic.gdx.physics.bullet.collision.btBoxShape) com.badlogic.gdx.physics.bullet.collision.btCylinderShape(com.badlogic.gdx.physics.bullet.collision.btCylinderShape) SphereShapeComponent(org.terasology.engine.physics.components.shapes.SphereShapeComponent) CylinderShapeComponent(org.terasology.engine.physics.components.shapes.CylinderShapeComponent) HullShapeComponent(org.terasology.engine.physics.components.shapes.HullShapeComponent) CharacterMovementComponent(org.terasology.engine.logic.characters.CharacterMovementComponent) FloatBuffer(java.nio.FloatBuffer) com.badlogic.gdx.physics.bullet.collision.btManifoldPoint(com.badlogic.gdx.physics.bullet.collision.btManifoldPoint) CapsuleShapeComponent(org.terasology.engine.physics.components.shapes.CapsuleShapeComponent) Vector3fc(org.joml.Vector3fc) com.badlogic.gdx.physics.bullet.collision.btSphereShape(com.badlogic.gdx.physics.bullet.collision.btSphereShape) Vector3f(org.joml.Vector3f) com.badlogic.gdx.physics.bullet.collision.btCapsuleShape(com.badlogic.gdx.physics.bullet.collision.btCapsuleShape) BoxShapeComponent(org.terasology.engine.physics.components.shapes.BoxShapeComponent) com.badlogic.gdx.physics.bullet.collision.btConvexHullShape(com.badlogic.gdx.physics.bullet.collision.btConvexHullShape)

Aggregations

com.badlogic.gdx.physics.bullet.collision.btBoxShape (com.badlogic.gdx.physics.bullet.collision.btBoxShape)1 com.badlogic.gdx.physics.bullet.collision.btCapsuleShape (com.badlogic.gdx.physics.bullet.collision.btCapsuleShape)1 com.badlogic.gdx.physics.bullet.collision.btConvexHullShape (com.badlogic.gdx.physics.bullet.collision.btConvexHullShape)1 com.badlogic.gdx.physics.bullet.collision.btCylinderShape (com.badlogic.gdx.physics.bullet.collision.btCylinderShape)1 com.badlogic.gdx.physics.bullet.collision.btManifoldPoint (com.badlogic.gdx.physics.bullet.collision.btManifoldPoint)1 com.badlogic.gdx.physics.bullet.collision.btSphereShape (com.badlogic.gdx.physics.bullet.collision.btSphereShape)1 FloatBuffer (java.nio.FloatBuffer)1 Vector3f (org.joml.Vector3f)1 Vector3fc (org.joml.Vector3fc)1 CharacterMovementComponent (org.terasology.engine.logic.characters.CharacterMovementComponent)1 BoxShapeComponent (org.terasology.engine.physics.components.shapes.BoxShapeComponent)1 CapsuleShapeComponent (org.terasology.engine.physics.components.shapes.CapsuleShapeComponent)1 CylinderShapeComponent (org.terasology.engine.physics.components.shapes.CylinderShapeComponent)1 HullShapeComponent (org.terasology.engine.physics.components.shapes.HullShapeComponent)1 SphereShapeComponent (org.terasology.engine.physics.components.shapes.SphereShapeComponent)1