Search in sources :

Example 6 with HitResult

use of org.terasology.engine.physics.HitResult in project Terasology by MovingBlocks.

the class LocalPlayer method activateTargetOrOwnedEntity.

/**
 * @param usedOwnedEntity if it does not exist it is not an item usage.
 * @return true if an activation request got sent. Returns always true if usedItem exists.
 */
private boolean activateTargetOrOwnedEntity(EntityRef usedOwnedEntity) {
    EntityRef character = getCharacterEntity();
    CharacterComponent characterComponent = character.getComponent(CharacterComponent.class);
    Vector3f direction = getViewDirection(new Vector3f());
    Vector3f originPos = getViewPosition(new Vector3f());
    if (recordAndReplayCurrentStatus.getStatus() == RecordAndReplayStatus.RECORDING) {
        this.directionAndOriginPosRecorderList.getTargetOrOwnedEntityDirectionAndOriginPosRecorder().add(direction, originPos);
    } else if (recordAndReplayCurrentStatus.getStatus() == RecordAndReplayStatus.REPLAYING) {
        Vector3f[] data = this.directionAndOriginPosRecorderList.getTargetOrOwnedEntityDirectionAndOriginPosRecorder().poll();
        direction = data[0];
        originPos = data[1];
    }
    boolean ownedEntityUsage = usedOwnedEntity.exists();
    int activationId = nextActivationId++;
    Physics physics = CoreRegistry.get(Physics.class);
    // FIXME This is the same code as in CharacterSystem#isPredictionOfEventCorrect to derive the actual interaction range from the
    // player's character component and the used item's range component...
    float interactionRange;
    if (ownedEntityUsage && usedOwnedEntity.hasComponent(RangeComponent.class)) {
        interactionRange = Math.max(usedOwnedEntity.getComponent(RangeComponent.class).range, characterComponent.interactionRange);
    } else {
        interactionRange = characterComponent.interactionRange;
    }
    HitResult result = physics.rayTrace(originPos, direction, interactionRange, Sets.newHashSet(character), CharacterSystem.DEFAULTPHYSICSFILTER);
    boolean eventWithTarget = result.isHit();
    if (ownedEntityUsage || eventWithTarget) {
        EntityRef activatedObject = usedOwnedEntity.exists() ? usedOwnedEntity : result.getEntity();
        activatedObject.send(new ActivationPredicted(character, result.getEntity(), originPos, direction, result.getHitPoint(), result.getHitNormal(), activationId));
        character.send(new ActivationRequest(character, ownedEntityUsage, usedOwnedEntity, eventWithTarget, result.getEntity(), originPos, direction, result.getHitPoint(), result.getHitNormal(), activationId));
        return true;
    }
    return false;
}
Also used : HitResult(org.terasology.engine.physics.HitResult) ActivationPredicted(org.terasology.engine.logic.characters.events.ActivationPredicted) ActivationRequest(org.terasology.engine.logic.characters.events.ActivationRequest) RangeComponent(org.terasology.engine.logic.common.RangeComponent) Vector3f(org.joml.Vector3f) CharacterComponent(org.terasology.engine.logic.characters.CharacterComponent) EntityRef(org.terasology.engine.entitySystem.entity.EntityRef) Physics(org.terasology.engine.physics.Physics)

Example 7 with HitResult

use of org.terasology.engine.physics.HitResult in project Terasology by MovingBlocks.

the class CameraTargetSystem method update.

public void update(float delta) {
    // Repair lost target
    // TODO: Improvements to temporary chunk handling will remove the need for this
    boolean lostTarget = false;
    updateTarget();
    if (!target.exists()) {
        targetBlockPos = null;
        lostTarget = true;
    }
    HitResult hitInfo = physics.rayTrace(localPlayer.getViewPosition(new Vector3f()), localPlayer.getViewDirection(new Vector3f()), targetDistance, filter);
    updateFocalDistance(hitInfo, delta);
    Vector3i newBlockPos = null;
    EntityRef newTarget = EntityRef.NULL;
    if (hitInfo.isHit()) {
        newTarget = hitInfo.getEntity();
        hitPosition = hitInfo.getHitPoint();
        hitNormal = hitInfo.getHitNormal();
        if (hitInfo.isWorldHit()) {
            newBlockPos = new Vector3i(hitInfo.getBlockPosition());
        }
    }
    if (!Objects.equal(target, newTarget) || lostTarget) {
        EntityRef oldTarget = target;
        oldTarget.send(new CameraOutEvent());
        newTarget.send(new CameraOverEvent());
        localPlayer.getCharacterEntity().send(new CameraTargetChangedEvent(oldTarget, newTarget));
        // Set isBlock to false if the hit-entity does not have a BlockComponent
        isBlock = !(isTargetAvailable() && !newTarget.hasComponent(BlockComponent.class));
    }
    target = newTarget;
    targetBlockPos = newBlockPos;
}
Also used : HitResult(org.terasology.engine.physics.HitResult) BlockComponent(org.terasology.engine.world.block.BlockComponent) Vector3f(org.joml.Vector3f) Vector3i(org.joml.Vector3i) EntityRef(org.terasology.engine.entitySystem.entity.EntityRef)

Example 8 with HitResult

use of org.terasology.engine.physics.HitResult in project Terasology by MovingBlocks.

the class TargetSystem method updateTarget.

public boolean updateTarget(Vector3f pos, Vector3f dir, float maxDist) {
    if (targetBlockPos != null && !target.exists()) {
        target = blockRegistry.getEntityAt(targetBlockPos);
    }
    HitResult hitInfo = physics.rayTrace(pos, dir, maxDist, filter);
    EntityRef newTarget = hitInfo.getEntity();
    if (hitInfo.isWorldHit()) {
        if (targetBlockPos != null) {
            if (targetBlockPos.equals(hitInfo.getBlockPosition())) {
                return false;
            }
        }
        targetBlockPos = hitInfo.getBlockPosition();
    } else {
        if (target.equals(newTarget)) {
            return false;
        }
        targetBlockPos = null;
    }
    prevTarget = target;
    target = newTarget;
    LocationComponent location = target.getComponent(LocationComponent.class);
    if (location != null && targetBlockPos != null) {
        location.setLocalPosition(new Vector3f(targetBlockPos));
    }
    return true;
}
Also used : HitResult(org.terasology.engine.physics.HitResult) Vector3f(org.joml.Vector3f) EntityRef(org.terasology.engine.entitySystem.entity.EntityRef) LocationComponent(org.terasology.engine.logic.location.LocationComponent)

Example 9 with HitResult

use of org.terasology.engine.physics.HitResult in project Terasology by MovingBlocks.

the class ParticleUpdaterImpl method checkCollision.

private void checkCollision(final ParticlePool pool, final int offset) {
    final Vector3f vel = new Vector3f();
    final Vector3f halfVelDir = new Vector3f();
    final Vector3f curr = new Vector3f();
    for (int i = offset; i < pool.livingParticles(); i += PHYSICS_SKIP_NR) {
        int i3 = i * 3;
        curr.set(pool.position[i3 + 0], pool.position[i3 + 1], pool.position[i3 + 2]);
        vel.set(pool.velocity[i3 + 0], pool.velocity[i3 + 1], pool.velocity[i3 + 2]);
        halfVelDir.set(0).add(vel).normalize().mul(0.5f);
        curr.sub(halfVelDir);
        float dist = (vel.length() + 0.5f) * movingAvgDelta * PHYSICS_SKIP_NR * 1.5f;
        vel.normalize();
        HitResult hitResult = physics.rayTrace(curr, vel, dist, StandardCollisionGroup.WORLD);
        if (hitResult.isHit()) {
            pool.energy[i] = 0;
        }
    }
}
Also used : HitResult(org.terasology.engine.physics.HitResult) Vector3f(org.joml.Vector3f)

Aggregations

Vector3f (org.joml.Vector3f)9 HitResult (org.terasology.engine.physics.HitResult)9 EntityRef (org.terasology.engine.entitySystem.entity.EntityRef)8 Vector3i (org.joml.Vector3i)3 LocationComponent (org.terasology.engine.logic.location.LocationComponent)3 com.badlogic.gdx.physics.bullet.collision.btCollisionObject (com.badlogic.gdx.physics.bullet.collision.btCollisionObject)2 com.badlogic.gdx.physics.bullet.collision.btVector3i (com.badlogic.gdx.physics.bullet.collision.btVector3i)2 RangeComponent (org.terasology.engine.logic.common.RangeComponent)2 BlockComponent (org.terasology.engine.world.block.BlockComponent)2 AllHitsRayResultCallback (com.badlogic.gdx.physics.bullet.collision.AllHitsRayResultCallback)1 ClosestRayResultCallback (com.badlogic.gdx.physics.bullet.collision.ClosestRayResultCallback)1 com.badlogic.gdx.physics.bullet.collision.btCollisionObjectConstArray (com.badlogic.gdx.physics.bullet.collision.btCollisionObjectConstArray)1 com.badlogic.gdx.physics.bullet.collision.btManifoldPoint (com.badlogic.gdx.physics.bullet.collision.btManifoldPoint)1 ReceiveEvent (org.terasology.engine.entitySystem.event.ReceiveEvent)1 CharacterComponent (org.terasology.engine.logic.characters.CharacterComponent)1 ActivationPredicted (org.terasology.engine.logic.characters.events.ActivationPredicted)1 ActivationRequest (org.terasology.engine.logic.characters.events.ActivationRequest)1 AttackEvent (org.terasology.engine.logic.characters.events.AttackEvent)1 OnItemUseEvent (org.terasology.engine.logic.characters.events.OnItemUseEvent)1 PlayerCharacterComponent (org.terasology.engine.logic.players.PlayerCharacterComponent)1