use of org.terasology.logic.location.LocationComponent in project Terasology by MovingBlocks.
the class BulletPhysics method newRigidBody.
private RigidBody newRigidBody(EntityRef entity) {
LocationComponent location = entity.getComponent(LocationComponent.class);
RigidBodyComponent rigidBody = entity.getComponent(RigidBodyComponent.class);
ConvexShape shape = getShapeFor(entity);
if (location != null && rigidBody != null && shape != null) {
float scale = location.getWorldScale();
shape.setLocalScaling(new Vector3f(scale, scale, scale));
if (rigidBody.mass < 1) {
logger.warn("RigidBodyComponent.mass is set to less than 1.0, this can lead to strange behaviour, such as the objects moving through walls. " + "Entity: {}", entity);
}
Vector3f fallInertia = new Vector3f();
shape.calculateLocalInertia(rigidBody.mass, fallInertia);
RigidBodyConstructionInfo info = new RigidBodyConstructionInfo(rigidBody.mass, new EntityMotionState(entity), shape, fallInertia);
BulletRigidBody collider = new BulletRigidBody(info);
collider.rb.setUserPointer(entity);
collider.rb.setAngularFactor(VecMath.to(rigidBody.angularFactor));
collider.rb.setLinearFactor(VecMath.to(rigidBody.linearFactor));
collider.rb.setFriction(rigidBody.friction);
collider.rb.setRestitution(rigidBody.restitution);
collider.collidesWith = combineGroups(rigidBody.collidesWith);
updateKinematicSettings(rigidBody, collider);
BulletRigidBody oldBody = entityRigidBodies.put(entity, collider);
addRigidBody(collider, Lists.<CollisionGroup>newArrayList(rigidBody.collisionGroup), rigidBody.collidesWith);
if (oldBody != null) {
removeRigidBody(oldBody);
}
collider.setVelocity(rigidBody.velocity, rigidBody.angularVelocity);
collider.setTransform(location.getWorldPosition(), location.getWorldRotation());
return collider;
} else {
throw new IllegalArgumentException("Can only create a new rigid body for entities with a LocationComponent," + " RigidBodyComponent and ShapeComponent, this entity misses at least one: " + entity);
}
}
use of org.terasology.logic.location.LocationComponent in project Terasology by MovingBlocks.
the class BulletPhysics method createCharacterCollider.
/**
* Creates a Collider for the given entity based on the LocationComponent
* and CharacterMovementComponent.
* All collision flags are set right for a character movement component.
*
* @param owner the entity to create the collider for.
* @return
*/
private CharacterCollider createCharacterCollider(EntityRef owner) {
LocationComponent locComp = owner.getComponent(LocationComponent.class);
CharacterMovementComponent movementComp = owner.getComponent(CharacterMovementComponent.class);
if (locComp == null || movementComp == null) {
throw new IllegalArgumentException("Expected an entity with a Location component and CharacterMovementComponent.");
}
Vector3f pos = VecMath.to(locComp.getWorldPosition());
final float worldScale = locComp.getWorldScale();
final float height = (movementComp.height - 2 * movementComp.radius) * worldScale;
final float width = movementComp.radius * worldScale;
ConvexShape shape = new CapsuleShape(width, height);
shape.setMargin(0.1f);
return createCustomCollider(pos, shape, movementComp.collisionGroup.getFlag(), combineGroups(movementComp.collidesWith), CollisionFlags.CHARACTER_OBJECT, owner);
}
use of org.terasology.logic.location.LocationComponent in project Terasology by MovingBlocks.
the class BulletPhysics method updateRigidBody.
@Override
public boolean updateRigidBody(EntityRef entity) {
LocationComponent location = entity.getComponent(LocationComponent.class);
RigidBodyComponent rb = entity.getComponent(RigidBodyComponent.class);
BulletRigidBody rigidBody = entityRigidBodies.get(entity);
if (location == null) {
logger.warn("Updating rigid body of entity that has no " + "LocationComponent?! Nothing is done, except log this" + " warning instead. Entity: {}", entity);
return false;
} else if (rigidBody != null) {
float scale = location.getWorldScale();
if (Math.abs(rigidBody.rb.getCollisionShape().getLocalScaling(new Vector3f()).x - scale) > BulletGlobals.SIMD_EPSILON || rigidBody.collidesWith != combineGroups(rb.collidesWith)) {
removeRigidBody(rigidBody);
newRigidBody(entity);
} else {
rigidBody.rb.setAngularFactor(VecMath.to(rb.angularFactor));
rigidBody.rb.setLinearFactor(VecMath.to(rb.linearFactor));
rigidBody.rb.setFriction(rb.friction);
}
return true;
} else {
/*
* During the destruction of the entity it can happen that the rigged body is already destroyed while
* the location component changes.
* e.g. because another component that was attached via the LocationComponent gets removed.
*
* In such a situation it would be wrong to recreate the rigid body as it can't be updated properly after
* the destruction of the entity.
*
*/
return false;
}
// TODO: update if mass or collision groups change
}
use of org.terasology.logic.location.LocationComponent 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;
}
}
use of org.terasology.logic.location.LocationComponent in project Terasology by MovingBlocks.
the class MovementDebugCommands method teleportMeToPlayer.
@Command(shortDescription = "Teleport to player", runOnServer = true, requiredPermission = PermissionManager.CHEAT_PERMISSION)
public String teleportMeToPlayer(@Sender EntityRef sender, @CommandParam("username") String username) {
for (EntityRef clientEntity : entityManager.getEntitiesWith(ClientComponent.class)) {
EntityRef clientInfo = clientEntity.getComponent(ClientComponent.class).clientInfo;
DisplayNameComponent name = clientInfo.getComponent(DisplayNameComponent.class);
if (username.equalsIgnoreCase(name.name)) {
LocationComponent locationComponent = clientEntity.getComponent(LocationComponent.class);
if (locationComponent != null) {
Vector3f vLocation = locationComponent.getWorldPosition();
ClientComponent clientComp = sender.getComponent(ClientComponent.class);
if (clientComp != null) {
clientComp.character.send(new CharacterTeleportEvent(vLocation));
return "Teleporting you to " + username + " at " + vLocation.x + " " + vLocation.y + " " + vLocation.z;
}
}
}
}
throw new IllegalArgumentException("No such user '" + username + "'");
}
Aggregations