use of org.terasology.engine.physics.components.shapes.BoxShapeComponent 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);
}
use of org.terasology.engine.physics.components.shapes.BoxShapeComponent 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);
}
}
Aggregations