Search in sources :

Example 1 with Physics

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

the class ParticleUpdaterImplTest method setUp.

@BeforeEach
public void setUp() throws Exception {
    Physics physics = mock(PhysicsEngine.class);
    ModuleManager moduleManager = mock(ModuleManager.class);
    particleUpdater = new ParticleUpdaterImpl(physics, moduleManager);
}
Also used : ModuleManager(org.terasology.engine.core.module.ModuleManager) Physics(org.terasology.engine.physics.Physics) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 2 with Physics

use of org.terasology.engine.physics.Physics 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 3 with Physics

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

the class BlockItemSystem method canPlaceBlock.

private boolean canPlaceBlock(Block block, Vector3i targetBlock, Vector3i blockPos) {
    if (block == null) {
        return false;
    }
    Block centerBlock = worldProvider.getBlock(targetBlock.x, targetBlock.y, targetBlock.z);
    if (!centerBlock.isAttachmentAllowed()) {
        return false;
    }
    Block adjBlock = worldProvider.getBlock(blockPos.x, blockPos.y, blockPos.z);
    if (!adjBlock.isReplacementAllowed() || adjBlock.isTargetable()) {
        return false;
    }
    if (block.getBlockFamily().equals(adjBlock.getBlockFamily())) {
        return false;
    }
    // Prevent players from placing blocks inside their bounding boxes
    if (!block.isPenetrable()) {
        Physics physics = CoreRegistry.get(Physics.class);
        AABBf blockBounds = block.getBounds(blockPos);
        /**
         * Characters can enter other solid objects/blocks for certain amount. This is does to detect collsion
         * start and end without noise. So if the user walked as close to a block as possible it is only natural
         * to let it place a block exactly above it even if that technically would mean a collision start.
         */
        blockBounds.minX += KinematicCharacterMover.HORIZONTAL_PENETRATION;
        blockBounds.maxX -= KinematicCharacterMover.HORIZONTAL_PENETRATION;
        blockBounds.minY += KinematicCharacterMover.VERTICAL_PENETRATION;
        blockBounds.maxY -= KinematicCharacterMover.VERTICAL_PENETRATION;
        blockBounds.minZ += KinematicCharacterMover.HORIZONTAL_PENETRATION;
        blockBounds.maxZ -= KinematicCharacterMover.HORIZONTAL_PENETRATION;
        /*
             * Calculations aren't exact and in the corner cases it is better to let the user place the block.
             */
        blockBounds.minX += ADDITIONAL_ALLOWED_PENETRATION;
        blockBounds.minY += ADDITIONAL_ALLOWED_PENETRATION;
        blockBounds.minZ += ADDITIONAL_ALLOWED_PENETRATION;
        blockBounds.maxX -= ADDITIONAL_ALLOWED_PENETRATION;
        blockBounds.maxY -= ADDITIONAL_ALLOWED_PENETRATION;
        blockBounds.maxZ -= ADDITIONAL_ALLOWED_PENETRATION;
        return physics.scanArea(blockBounds, StandardCollisionGroup.DEFAULT, StandardCollisionGroup.CHARACTER).isEmpty();
    }
    return true;
}
Also used : AABBf(org.terasology.joml.geom.AABBf) Block(org.terasology.engine.world.block.Block) Physics(org.terasology.engine.physics.Physics)

Aggregations

Physics (org.terasology.engine.physics.Physics)3 Vector3f (org.joml.Vector3f)1 BeforeEach (org.junit.jupiter.api.BeforeEach)1 ModuleManager (org.terasology.engine.core.module.ModuleManager)1 EntityRef (org.terasology.engine.entitySystem.entity.EntityRef)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 RangeComponent (org.terasology.engine.logic.common.RangeComponent)1 HitResult (org.terasology.engine.physics.HitResult)1 Block (org.terasology.engine.world.block.Block)1 AABBf (org.terasology.joml.geom.AABBf)1