Search in sources :

Example 1 with CapsuleShapeComponent

use of org.terasology.engine.physics.components.shapes.CapsuleShapeComponent 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)

Example 2 with CapsuleShapeComponent

use of org.terasology.engine.physics.components.shapes.CapsuleShapeComponent in project Terasology by MovingBlocks.

the class BoundingBoxRenderer method renderOverlay.

@Override
public void renderOverlay() {
    if (config.getRendering().getDebug().isRenderEntityBoundingBoxes()) {
        GL33.glDepthFunc(GL33.GL_ALWAYS);
        Vector3f cameraPosition = worldRenderer.getActiveCamera().getPosition();
        Vector3f worldPos = new Vector3f();
        Vector3f worldPositionCameraSpace = new Vector3f();
        worldPos.sub(cameraPosition, worldPositionCameraSpace);
        Matrix4f matrixCameraSpace = new Matrix4f().translationRotateScale(worldPositionCameraSpace, new Quaternionf(), 1.0f);
        Matrix4f modelViewMatrix = new Matrix4f(worldRenderer.getActiveCamera().getViewMatrix()).mul(matrixCameraSpace);
        material.setMatrix4("projectionMatrix", worldRenderer.getActiveCamera().getProjectionMatrix());
        material.setMatrix4("modelViewMatrix", modelViewMatrix, true);
        int index = 0;
        meshData.reallocate(0, 0);
        meshData.indices.rewind();
        meshData.position.rewind();
        meshData.color0.rewind();
        Vector3f worldPosition = new Vector3f();
        Quaternionf worldRot = new Quaternionf();
        Matrix4f transform = new Matrix4f();
        AABBf bounds = new AABBf(0, 0, 0, 0, 0, 0);
        for (EntityRef entity : entityManager.getEntitiesWith(LocationComponent.class)) {
            LocationComponent location = entity.getComponent(LocationComponent.class);
            location.getWorldPosition(worldPosition);
            location.getWorldRotation(worldRot);
            BoxShapeComponent boxShapeComponent = entity.getComponent(BoxShapeComponent.class);
            if (boxShapeComponent != null) {
                bounds.set(0, 0, 0, 0, 0, 0);
                bounds.expand(new Vector3f(boxShapeComponent.extents).div(2.0f));
                transform.translationRotateScale(worldPosition, worldRot, location.getWorldScale());
                bounds.transform(transform);
                index = addRenderBound(meshData, bounds, index);
            }
            CapsuleShapeComponent capsuleComponent = entity.getComponent(CapsuleShapeComponent.class);
            if (capsuleComponent != null) {
                bounds.set(0, 0, 0, 0, 0, 0);
                bounds.expand(new Vector3f(capsuleComponent.radius, capsuleComponent.height / 2.0f, capsuleComponent.radius).div(2.0f));
                transform.translationRotateScale(worldPosition, worldRot, location.getWorldScale());
                bounds.transform(transform);
                index = addRenderBound(meshData, bounds, index);
            }
            CylinderShapeComponent cylinderShapeComponent = entity.getComponent(CylinderShapeComponent.class);
            if (cylinderShapeComponent != null) {
                bounds.set(0, 0, 0, 0, 0, 0);
                bounds.expand(new Vector3f(cylinderShapeComponent.radius, cylinderShapeComponent.height / 2.0f, cylinderShapeComponent.radius).div(2.0f));
                transform.translationRotateScale(worldPosition, worldRot, location.getWorldScale());
                bounds.transform(transform);
                index = addRenderBound(meshData, bounds, index);
            }
            SphereShapeComponent sphereShapeComponent = entity.getComponent(SphereShapeComponent.class);
            if (sphereShapeComponent != null) {
                bounds.set(0, 0, 0, 0, 0, 0);
                bounds.expand(new Vector3f(sphereShapeComponent.radius).div(2.0f));
                transform.translationRotateScale(worldPosition, worldRot, location.getWorldScale());
                bounds.transform(transform);
                index = addRenderBound(meshData, bounds, index);
            }
        }
        material.enable();
        mesh.reload(meshData);
        mesh.render();
        GL33.glDepthFunc(GL33.GL_LEQUAL);
    }
}
Also used : Matrix4f(org.joml.Matrix4f) AABBf(org.terasology.joml.geom.AABBf) SphereShapeComponent(org.terasology.engine.physics.components.shapes.SphereShapeComponent) Vector3f(org.joml.Vector3f) BoxShapeComponent(org.terasology.engine.physics.components.shapes.BoxShapeComponent) Quaternionf(org.joml.Quaternionf) CylinderShapeComponent(org.terasology.engine.physics.components.shapes.CylinderShapeComponent) EntityRef(org.terasology.engine.entitySystem.entity.EntityRef) LocationComponent(org.terasology.engine.logic.location.LocationComponent) CapsuleShapeComponent(org.terasology.engine.physics.components.shapes.CapsuleShapeComponent)

Aggregations

Vector3f (org.joml.Vector3f)2 BoxShapeComponent (org.terasology.engine.physics.components.shapes.BoxShapeComponent)2 CapsuleShapeComponent (org.terasology.engine.physics.components.shapes.CapsuleShapeComponent)2 CylinderShapeComponent (org.terasology.engine.physics.components.shapes.CylinderShapeComponent)2 SphereShapeComponent (org.terasology.engine.physics.components.shapes.SphereShapeComponent)2 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 Matrix4f (org.joml.Matrix4f)1 Quaternionf (org.joml.Quaternionf)1 Vector3fc (org.joml.Vector3fc)1 EntityRef (org.terasology.engine.entitySystem.entity.EntityRef)1 CharacterMovementComponent (org.terasology.engine.logic.characters.CharacterMovementComponent)1 LocationComponent (org.terasology.engine.logic.location.LocationComponent)1 HullShapeComponent (org.terasology.engine.physics.components.shapes.HullShapeComponent)1 AABBf (org.terasology.joml.geom.AABBf)1