Search in sources :

Example 41 with Quat4f

use of org.terasology.math.geom.Quat4f in project Terasology by MovingBlocks.

the class FirstPersonClientSystem method update.

/**
 * modifies the held item mount point to move the held item in first person view
 */
@Override
public void update(float delta) {
    // ensure empty hand is shown if no item is hold at the moment
    if (!currentHeldItem.exists() && currentHeldItem != getHandEntity()) {
        linkHeldItemLocationForLocalPlayer(getHandEntity());
    }
    // ensure that there are no lingering items that are marked as still held. This situation happens with client side predicted items
    for (EntityRef entityRef : entityManager.getEntitiesWith(ItemIsHeldComponent.class)) {
        if (!entityRef.equals(currentHeldItem) && !entityRef.equals(handEntity)) {
            entityRef.destroy();
        }
    }
    // get the first person mount point and rotate it away from the camera
    CharacterHeldItemComponent characterHeldItemComponent = localPlayer.getCharacterEntity().getComponent(CharacterHeldItemComponent.class);
    FirstPersonHeldItemMountPointComponent mountPointComponent = localPlayer.getCameraEntity().getComponent(FirstPersonHeldItemMountPointComponent.class);
    if (characterHeldItemComponent == null || mountPointComponent == null) {
        return;
    }
    LocationComponent locationComponent = mountPointComponent.mountPointEntity.getComponent(LocationComponent.class);
    if (locationComponent == null) {
        return;
    }
    long timeElapsedSinceLastUsed = time.getGameTimeInMs() - characterHeldItemComponent.lastItemUsedTime;
    float animateAmount = 0f;
    if (timeElapsedSinceLastUsed < USEANIMATIONLENGTH) {
        // half way through the animation will be the maximum extent of rotation and translation
        animateAmount = 1f - Math.abs(((float) timeElapsedSinceLastUsed / (float) USEANIMATIONLENGTH) - 0.5f);
    }
    float addPitch = 15f * animateAmount;
    float addYaw = 10f * animateAmount;
    locationComponent.setLocalRotation(new Quat4f(TeraMath.DEG_TO_RAD * (mountPointComponent.rotateDegrees.y + addYaw), TeraMath.DEG_TO_RAD * (mountPointComponent.rotateDegrees.x + addPitch), TeraMath.DEG_TO_RAD * mountPointComponent.rotateDegrees.z));
    Vector3f offset = new Vector3f(0.25f * animateAmount, -0.12f * animateAmount, 0f);
    offset.add(mountPointComponent.translate);
    locationComponent.setLocalPosition(offset);
    mountPointComponent.mountPointEntity.saveComponent(locationComponent);
}
Also used : Vector3f(org.terasology.math.geom.Vector3f) CharacterHeldItemComponent(org.terasology.logic.characters.CharacterHeldItemComponent) EntityRef(org.terasology.entitySystem.entity.EntityRef) LocationComponent(org.terasology.logic.location.LocationComponent) Quat4f(org.terasology.math.geom.Quat4f)

Example 42 with Quat4f

use of org.terasology.math.geom.Quat4f in project Terasology by MovingBlocks.

the class LocalPlayer method getViewRotation.

public Quat4f getViewRotation() {
    ClientComponent clientComponent = getClientEntity().getComponent(ClientComponent.class);
    if (clientComponent == null) {
        return new Quat4f(Quat4f.IDENTITY);
    }
    LocationComponent location = clientComponent.camera.getComponent(LocationComponent.class);
    if (location == null) {
        return getRotation();
    }
    return location.getWorldRotation();
}
Also used : ClientComponent(org.terasology.network.ClientComponent) LocationComponent(org.terasology.logic.location.LocationComponent) Quat4f(org.terasology.math.geom.Quat4f)

Example 43 with Quat4f

use of org.terasology.math.geom.Quat4f in project Terasology by MovingBlocks.

the class PlayerFactory method newInstance.

/**
 * Creates a new player character entity. The desired spawning location is derived from
 * the {@link LocationComponent} of the controller.
 * @param controller the controlling client entity
 * @return a new player character entity
 */
public EntityRef newInstance(EntityRef controller) {
    EntityBuilder builder = entityManager.newBuilder("engine:player");
    LocationComponent location = controller.getComponent(LocationComponent.class);
    Vector3f spawnPosition = findSpawnPositionFromLocationComponent(location);
    location.setWorldPosition(spawnPosition);
    controller.saveComponent(location);
    logger.debug("Spawing player at: {}", spawnPosition);
    builder.getComponent(LocationComponent.class).setWorldPosition(spawnPosition);
    builder.setOwner(controller);
    CharacterComponent playerComponent = builder.getComponent(CharacterComponent.class);
    playerComponent.controller = controller;
    EntityRef player = builder.build();
    Location.attachChild(player, controller, new Vector3f(), new Quat4f(0, 0, 0, 1));
    return player;
}
Also used : Vector3f(org.terasology.math.geom.Vector3f) CharacterComponent(org.terasology.logic.characters.CharacterComponent) EntityBuilder(org.terasology.entitySystem.entity.EntityBuilder) LocationComponent(org.terasology.logic.location.LocationComponent) EntityRef(org.terasology.entitySystem.entity.EntityRef) Quat4f(org.terasology.math.geom.Quat4f)

Example 44 with Quat4f

use of org.terasology.math.geom.Quat4f in project Terasology by MovingBlocks.

the class PlayerSystem method setSpawnLocationOnRespawnRequest.

@ReceiveEvent(priority = EventPriority.PRIORITY_CRITICAL, components = { ClientComponent.class })
public void setSpawnLocationOnRespawnRequest(RespawnRequestEvent event, EntityRef entity) {
    EntityRef clientInfo = entity.getComponent(ClientComponent.class).clientInfo;
    Vector3f spawnPosition;
    if (clientInfo.hasComponent(StaticSpawnLocationComponent.class)) {
        spawnPosition = clientInfo.getComponent(StaticSpawnLocationComponent.class).position;
    } else {
        spawnPosition = worldGenerator.getSpawnPosition(entity);
    }
    LocationComponent loc = entity.getComponent(LocationComponent.class);
    loc.setWorldPosition(spawnPosition);
    // reset rotation
    loc.setLocalRotation(new Quat4f());
    entity.saveComponent(loc);
}
Also used : Vector3f(org.terasology.math.geom.Vector3f) EntityRef(org.terasology.entitySystem.entity.EntityRef) ClientComponent(org.terasology.network.ClientComponent) LocationComponent(org.terasology.logic.location.LocationComponent) Quat4f(org.terasology.math.geom.Quat4f) ReceiveEvent(org.terasology.entitySystem.event.ReceiveEvent)

Example 45 with Quat4f

use of org.terasology.math.geom.Quat4f in project Terasology by MovingBlocks.

the class LocationComponentTest method setup.

@Before
public void setup() {
    loc = new LocationComponent();
    entity = createFakeEntityWith(loc);
    yawRotation = new Quat4f(TeraMath.DEG_TO_RAD * 90, 0, 0);
    pitchRotation = new Quat4f(0, TeraMath.DEG_TO_RAD * 45, 0);
    yawPitch = new Quat4f(TeraMath.DEG_TO_RAD * 90, TeraMath.DEG_TO_RAD * 45, 0);
}
Also used : Quat4f(org.terasology.math.geom.Quat4f) Before(org.junit.Before)

Aggregations

Quat4f (org.terasology.math.geom.Quat4f)50 Vector3f (org.terasology.math.geom.Vector3f)33 LocationComponent (org.terasology.logic.location.LocationComponent)16 EntityRef (org.terasology.entitySystem.entity.EntityRef)14 Matrix4f (org.terasology.math.geom.Matrix4f)9 ClientComponent (org.terasology.network.ClientComponent)7 BaseQuat4f (org.terasology.math.geom.BaseQuat4f)6 FloatBuffer (java.nio.FloatBuffer)4 Test (org.junit.Test)4 BaseVector3f (org.terasology.math.geom.BaseVector3f)4 ArrayList (java.util.ArrayList)3 EntityBuilder (org.terasology.entitySystem.entity.EntityBuilder)3 ReceiveEvent (org.terasology.entitySystem.event.ReceiveEvent)3 CharacterComponent (org.terasology.logic.characters.CharacterComponent)3 Lists (com.google.common.collect.Lists)2 TIntList (gnu.trove.list.TIntList)2 TIntArrayList (gnu.trove.list.array.TIntArrayList)2 IOException (java.io.IOException)2 InputStream (java.io.InputStream)2 Arrays (java.util.Arrays)2