Search in sources :

Example 6 with LocationComponent

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);
    }
}
Also used : RigidBodyComponent(org.terasology.physics.components.RigidBodyComponent) Vector3f(javax.vecmath.Vector3f) ConvexShape(com.bulletphysics.collision.shapes.ConvexShape) LocationComponent(org.terasology.logic.location.LocationComponent) RigidBodyConstructionInfo(com.bulletphysics.dynamics.RigidBodyConstructionInfo)

Example 7 with LocationComponent

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);
}
Also used : Vector3f(javax.vecmath.Vector3f) ConvexShape(com.bulletphysics.collision.shapes.ConvexShape) CharacterMovementComponent(org.terasology.logic.characters.CharacterMovementComponent) LocationComponent(org.terasology.logic.location.LocationComponent) CapsuleShape(com.bulletphysics.collision.shapes.CapsuleShape)

Example 8 with LocationComponent

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
}
Also used : RigidBodyComponent(org.terasology.physics.components.RigidBodyComponent) Vector3f(javax.vecmath.Vector3f) LocationComponent(org.terasology.logic.location.LocationComponent)

Example 9 with LocationComponent

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;
    }
}
Also used : CollisionGroup(org.terasology.physics.CollisionGroup) StandardCollisionGroup(org.terasology.physics.StandardCollisionGroup) Vector3f(javax.vecmath.Vector3f) ConvexShape(com.bulletphysics.collision.shapes.ConvexShape) LocationComponent(org.terasology.logic.location.LocationComponent) PairCachingGhostObject(com.bulletphysics.collision.dispatch.PairCachingGhostObject) TriggerComponent(org.terasology.physics.components.TriggerComponent)

Example 10 with LocationComponent

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 + "'");
}
Also used : DisplayNameComponent(org.terasology.logic.common.DisplayNameComponent) CharacterTeleportEvent(org.terasology.logic.characters.CharacterTeleportEvent) Vector3f(org.terasology.math.geom.Vector3f) EntityRef(org.terasology.entitySystem.entity.EntityRef) ClientComponent(org.terasology.network.ClientComponent) LocationComponent(org.terasology.logic.location.LocationComponent) Command(org.terasology.logic.console.commandSystem.annotations.Command)

Aggregations

LocationComponent (org.terasology.logic.location.LocationComponent)69 EntityRef (org.terasology.entitySystem.entity.EntityRef)40 Vector3f (org.terasology.math.geom.Vector3f)39 ClientComponent (org.terasology.network.ClientComponent)17 Quat4f (org.terasology.math.geom.Quat4f)16 ReceiveEvent (org.terasology.entitySystem.event.ReceiveEvent)11 Command (org.terasology.logic.console.commandSystem.annotations.Command)10 Vector3i (org.terasology.math.geom.Vector3i)10 BlockComponent (org.terasology.world.block.BlockComponent)7 CharacterTeleportEvent (org.terasology.logic.characters.CharacterTeleportEvent)6 BaseQuat4f (org.terasology.math.geom.BaseQuat4f)6 Vector3f (javax.vecmath.Vector3f)5 Component (org.terasology.entitySystem.Component)5 BaseVector3f (org.terasology.math.geom.BaseVector3f)5 MeshComponent (org.terasology.rendering.logic.MeshComponent)5 EntityBuilder (org.terasology.entitySystem.entity.EntityBuilder)4 DisplayNameComponent (org.terasology.logic.common.DisplayNameComponent)4 Matrix4f (org.terasology.math.geom.Matrix4f)4 BlockFamily (org.terasology.world.block.family.BlockFamily)4 ConvexShape (com.bulletphysics.collision.shapes.ConvexShape)3