Search in sources :

Example 1 with Physics

use of org.terasology.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);
        AABB blockBounds = block.getBounds(blockPos);
        Vector3f min = new Vector3f(blockBounds.getMin());
        Vector3f max = new Vector3f(blockBounds.getMax());
        /**
         * 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.
         */
        min.x += KinematicCharacterMover.HORIZONTAL_PENETRATION;
        max.x -= KinematicCharacterMover.HORIZONTAL_PENETRATION;
        min.y += KinematicCharacterMover.VERTICAL_PENETRATION;
        max.y -= KinematicCharacterMover.VERTICAL_PENETRATION;
        min.z += KinematicCharacterMover.HORIZONTAL_PENETRATION;
        max.z -= KinematicCharacterMover.HORIZONTAL_PENETRATION;
        /*
             * Calculations aren't exact and in the corner cases it is better to let the user place the block.
             */
        // ignore small rounding mistakes
        float additionalAllowedPenetration = 0.04f;
        min.add(ADDITIONAL_ALLOWED_PENETRATION, ADDITIONAL_ALLOWED_PENETRATION, ADDITIONAL_ALLOWED_PENETRATION);
        max.sub(ADDITIONAL_ALLOWED_PENETRATION, ADDITIONAL_ALLOWED_PENETRATION, ADDITIONAL_ALLOWED_PENETRATION);
        AABB newBounds = AABB.createMinMax(min, max);
        return physics.scanArea(newBounds, StandardCollisionGroup.DEFAULT, StandardCollisionGroup.CHARACTER).isEmpty();
    }
    return true;
}
Also used : Vector3f(org.terasology.math.geom.Vector3f) Block(org.terasology.world.block.Block) Physics(org.terasology.physics.Physics) AABB(org.terasology.math.AABB)

Example 2 with Physics

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

the class ParticleUpdaterImplTest method setUp.

@Before
public void setUp() throws Exception {
    Physics physics = mock(PhysicsEngine.class);
    particleUpdater = new ParticleUpdaterImpl(physics);
    registeredGeneratorFunctions = HashBiMap.create();
    registeredAffectorFunctions = HashBiMap.create();
}
Also used : Physics(org.terasology.physics.Physics) Before(org.junit.Before)

Example 3 with Physics

use of org.terasology.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();
    Vector3f originPos = getViewPosition();
    boolean ownedEntityUsage = usedOwnedEntity.exists();
    int activationId = nextActivationId++;
    Physics physics = CoreRegistry.get(Physics.class);
    HitResult result = physics.rayTrace(originPos, direction, characterComponent.interactionRange, Sets.newHashSet(character), CharacterSystem.DEFAULTPHYSICSFILTER);
    boolean eventWithTarget = result.isHit();
    if (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;
    } else if (ownedEntityUsage) {
        usedOwnedEntity.send(new ActivationPredicted(character, EntityRef.NULL, originPos, direction, originPos, new Vector3f(), activationId));
        character.send(new ActivationRequest(character, ownedEntityUsage, usedOwnedEntity, eventWithTarget, EntityRef.NULL, originPos, direction, originPos, new Vector3f(), activationId));
        return true;
    }
    return false;
}
Also used : HitResult(org.terasology.physics.HitResult) ActivationPredicted(org.terasology.logic.characters.events.ActivationPredicted) ActivationRequest(org.terasology.logic.characters.events.ActivationRequest) Vector3f(org.terasology.math.geom.Vector3f) CharacterComponent(org.terasology.logic.characters.CharacterComponent) EntityRef(org.terasology.entitySystem.entity.EntityRef) Physics(org.terasology.physics.Physics)

Aggregations

Physics (org.terasology.physics.Physics)3 Vector3f (org.terasology.math.geom.Vector3f)2 Before (org.junit.Before)1 EntityRef (org.terasology.entitySystem.entity.EntityRef)1 CharacterComponent (org.terasology.logic.characters.CharacterComponent)1 ActivationPredicted (org.terasology.logic.characters.events.ActivationPredicted)1 ActivationRequest (org.terasology.logic.characters.events.ActivationRequest)1 AABB (org.terasology.math.AABB)1 HitResult (org.terasology.physics.HitResult)1 Block (org.terasology.world.block.Block)1