Search in sources :

Example 21 with Vector2f

use of org.joml.Vector2f in project Terasology by MovingBlocks.

the class BlockItemSystem method getSideHitPosition.

/**
 * Returns the position at which the block side was hit, relative to the side.
 * <p/>
 * The specified hit position is expected to be on the surface of the cubic block at the specified position.
 * Example: The front side was hit right in the center. The result will be (0.5, 0.5), representing the relative hit
 * position on the side's surface.
 *
 * @param hitPosition the hit position
 * @param blockPosition the block position relative to its center (block (0, 0, 0) has block position (0.5, 0.5,
 *     0.5))
 * @return the 2D hit position relative to the side that was hit
 */
private Vector2f getSideHitPosition(Vector3fc hitPosition, Vector3fc blockPosition) {
    float epsilon = 0.0001f;
    Vector3f relativeHitPosition = new Vector3f(hitPosition).sub(blockPosition);
    if (Math.abs(relativeHitPosition.x) > 0.5f - epsilon) {
        return new Vector2f(relativeHitPosition.z, relativeHitPosition.y).add(0.5f, 0.5f);
    } else if (Math.abs(relativeHitPosition.y) > 0.5f - epsilon) {
        return new Vector2f(relativeHitPosition.x, relativeHitPosition.z).add(0.5f, 0.5f);
    } else {
        return new Vector2f(relativeHitPosition.x, relativeHitPosition.y).add(0.5f, 0.5f);
    }
}
Also used : Vector2f(org.joml.Vector2f) Vector3f(org.joml.Vector3f)

Example 22 with Vector2f

use of org.joml.Vector2f in project Terasology by MovingBlocks.

the class BlockItemSystem method onPlaceBlock.

@ReceiveEvent(components = { BlockItemComponent.class, ItemComponent.class })
public void onPlaceBlock(ActivateEvent event, EntityRef item) {
    if (!event.getTarget().exists()) {
        event.consume();
        return;
    }
    BlockItemComponent blockItem = item.getComponent(BlockItemComponent.class);
    BlockFamily blockFamily = blockItem.blockFamily;
    Side surfaceSide = Side.inDirection(event.getHitNormal());
    BlockComponent blockComponent = event.getTarget().getComponent(BlockComponent.class);
    if (blockComponent == null) {
        // If there is no block there (i.e. it's a BlockGroup, we don't allow placing block, try somewhere else)
        event.consume();
        return;
    }
    Vector3i targetBlock = new Vector3i(blockComponent.getPosition());
    Vector3i placementPos = new Vector3i(targetBlock);
    placementPos.add(surfaceSide.direction());
    Vector2f relativeAttachmentPosition = getRelativeAttachmentPosition(event);
    Block block = blockFamily.getBlockForPlacement(new BlockPlacementData(placementPos, surfaceSide, event.getDirection(), relativeAttachmentPosition));
    if (canPlaceBlock(block, targetBlock, placementPos)) {
        // TODO: Fix this for changes.
        if (networkSystem.getMode().isAuthority()) {
            PlaceBlocks placeBlocks = new PlaceBlocks(placementPos, block, event.getInstigator());
            worldProvider.getWorldEntity().send(placeBlocks);
            if (!placeBlocks.isConsumed()) {
                item.send(new OnBlockItemPlaced(placementPos, blockEntityRegistry.getBlockEntityAt(placementPos), event.getInstigator()));
            } else {
                event.consume();
            }
        }
        recordBlockPlaced(event, blockFamily);
        event.getInstigator().send(new PlaySoundEvent(Assets.getSound("engine:PlaceBlock").get(), 0.5f));
    } else {
        event.consume();
    }
}
Also used : Side(org.terasology.engine.math.Side) BlockComponent(org.terasology.engine.world.block.BlockComponent) BlockPlacementData(org.terasology.engine.world.block.family.BlockPlacementData) Vector2f(org.joml.Vector2f) PlaySoundEvent(org.terasology.engine.audio.events.PlaySoundEvent) Vector3i(org.joml.Vector3i) Block(org.terasology.engine.world.block.Block) BlockFamily(org.terasology.engine.world.block.family.BlockFamily) PlaceBlocks(org.terasology.engine.world.block.entity.placement.PlaceBlocks) ReceiveEvent(org.terasology.engine.entitySystem.event.ReceiveEvent)

Example 23 with Vector2f

use of org.joml.Vector2f in project chunkstories-core by Hugobros3.

the class EntityLivingImplementation method tick.

@Override
public void tick() {
    if (getWorld() == null)
        return;
    // Despawn counter is strictly a client matter
    if (getWorld() instanceof WorldMaster) {
        if (isDead()) {
            deathDespawnTimer--;
            if (deathDespawnTimer < 0) {
                world.removeEntity(this);
                return;
            }
        }
        // Fall damage
        if (isOnGround()) {
            if (!wasStandingLastTick && !Double.isNaN(lastStandingHeight)) {
                double fallDistance = lastStandingHeight - this.positionComponent.getLocation().y();
                if (fallDistance > 0) {
                    // System.out.println("Fell "+fallDistance+" meters");
                    if (fallDistance > 5) {
                        float fallDamage = (float) (fallDistance * fallDistance / 2);
                        System.out.println(this + "Took " + fallDamage + " hp of fall damage");
                        this.damage(DAMAGE_CAUSE_FALL, fallDamage);
                    }
                }
            }
            lastStandingHeight = this.positionComponent.getLocation().y();
        }
        this.wasStandingLastTick = isOnGround();
    }
    boolean shouldDoTick = false;
    if (this instanceof EntityControllable) {
        Controller controller = ((EntityControllable) this).getControllerComponent().getController();
        if (controller == null)
            shouldDoTick = (getWorld() instanceof WorldMaster);
        else if (getWorld() instanceof WorldClient && ((WorldClient) getWorld()).getClient().getPlayer().equals(controller))
            shouldDoTick = true;
    } else
        shouldDoTick = (getWorld() instanceof WorldMaster);
    if (shouldDoTick) {
        Vector3dc ogVelocity = getVelocityComponent().getVelocity();
        Vector3d velocity = new Vector3d(ogVelocity);
        Vector2f headRotationVelocity = this.getEntityRotationComponent().tickInpulse();
        getEntityRotationComponent().addRotation(headRotationVelocity.x(), headRotationVelocity.y());
        // voxelIn = VoxelsStore.get().getVoxelById(VoxelFormat.id(world.getVoxelData(positionComponent.getLocation())));
        // voxelIn.getType().isLiquid();
        boolean inWater = isInWater();
        // Gravity
        if (!(this instanceof EntityFlying && ((EntityFlying) this).getFlyingComponent().get())) {
            double terminalVelocity = inWater ? -0.05 : -0.5;
            if (velocity.y() > terminalVelocity)
                velocity.y = (velocity.y() - 0.008);
            if (velocity.y() < terminalVelocity)
                velocity.y = (terminalVelocity);
            // Water limits your overall movement
            double targetSpeedInWater = 0.02;
            if (inWater) {
                if (velocity.length() > targetSpeedInWater) {
                    double decelerationThen = Math.pow((velocity.length() - targetSpeedInWater), 1.0);
                    // System.out.println(decelerationThen);
                    double maxDeceleration = 0.006;
                    if (decelerationThen > maxDeceleration)
                        decelerationThen = maxDeceleration;
                    // System.out.println(decelerationThen);
                    acceleration.add(new Vector3d(velocity).normalize().negate().mul(decelerationThen));
                // acceleration.add(0.0, decelerationThen * (velocity.y() > 0.0 ? 1.0 : -1.0), 0.0);
                }
            }
        }
        // Acceleration
        velocity.x = (velocity.x() + acceleration.x());
        velocity.y = (velocity.y() + acceleration.y());
        velocity.z = (velocity.z() + acceleration.z());
        // TODO ugly
        if (!world.isChunkLoaded((int) (double) positionComponent.getLocation().x() / 32, (int) (double) positionComponent.getLocation().y() / 32, (int) (double) positionComponent.getLocation().z() / 32)) {
            velocity.set(0d, 0d, 0d);
        }
        // Eventually moves
        Vector3dc remainingToMove = moveWithCollisionRestrain(velocity.x(), velocity.y(), velocity.z());
        Vector2d remaining2d = new Vector2d(remainingToMove.x(), remainingToMove.z());
        // Auto-step logic
        if (remaining2d.length() > 0.001 && isOnGround()) {
            // Cap max speed we can get through the bump ?
            if (remaining2d.length() > 0.20d) {
                System.out.println("Too fast, capping");
                remaining2d.normalize();
                remaining2d.mul(0.20);
            }
            // Get whatever we are colliding with
            // Test if setting yourself on top would be ok
            // Do it if possible
            // TODO remake proper
            Vector3d blockedMomentum = new Vector3d(remaining2d.x(), 0, remaining2d.y());
            for (double d = 0.25; d < 0.5; d += 0.05) {
                // I don't want any of this to reflect on the object, because it causes ugly jumps in the animation
                Vector3dc canMoveUp = this.canMoveWithCollisionRestrain(new Vector3d(0.0, d, 0.0));
                // It can go up that bit
                if (canMoveUp.length() == 0.0f) {
                    // Would it help with being stuck ?
                    Vector3d tryFromHigher = new Vector3d(this.getLocation());
                    tryFromHigher.add(new Vector3d(0.0, d, 0.0));
                    Vector3dc blockedMomentumRemaining = this.canMoveWithCollisionRestrain(tryFromHigher, blockedMomentum);
                    // If length of remaining momentum < of what we requested it to do, that means it *did* go a bit further away
                    if (blockedMomentumRemaining.length() < blockedMomentum.length()) {
                        // Where would this land ?
                        Vector3d afterJump = new Vector3d(tryFromHigher);
                        afterJump.add(blockedMomentum);
                        afterJump.sub(blockedMomentumRemaining);
                        // land distance = whatever is left of our -0.55 delta when it hits the ground
                        Vector3dc landDistance = this.canMoveWithCollisionRestrain(afterJump, new Vector3d(0.0, -d, 0.0));
                        afterJump.add(new Vector3d(0.0, -d, 0.0));
                        afterJump.sub(landDistance);
                        this.setLocation(new Location(world, afterJump));
                        remaining2d = new Vector2d(blockedMomentumRemaining.x(), blockedMomentumRemaining.z());
                        break;
                    }
                }
            }
        }
        // Collisions, snap to axises
        if (Math.abs(remaining2d.x()) >= 0.001d)
            velocity.x = (0d);
        if (Math.abs(remaining2d.y()) >= 0.001d)
            velocity.z = (0d);
        // Stap it
        if (isOnGround() && velocity.y() < 0)
            velocity.y = (0d);
        else if (isOnGround())
            velocity.y = (0d);
        getVelocityComponent().setVelocity(velocity);
    }
}
Also used : EntityFlying(io.xol.chunkstories.api.entity.interfaces.EntityFlying) Controller(io.xol.chunkstories.api.entity.Controller) WorldClient(io.xol.chunkstories.api.world.WorldClient) Vector3dc(org.joml.Vector3dc) Vector2d(org.joml.Vector2d) Vector3d(org.joml.Vector3d) Vector2f(org.joml.Vector2f) EntityControllable(io.xol.chunkstories.api.entity.interfaces.EntityControllable) WorldMaster(io.xol.chunkstories.api.world.WorldMaster) Location(io.xol.chunkstories.api.Location)

Example 24 with Vector2f

use of org.joml.Vector2f in project chunkstories-core by Hugobros3.

the class GenericHumanoidAI method makeEntityLookAt.

private void makeEntityLookAt(EntityHumanoid entity, Vector3d delta) {
    Vector2f deltaHorizontal = new Vector2f((float) (double) delta.x(), (float) (double) delta.z());
    Vector2f deltaVertical = new Vector2f(deltaHorizontal.length(), (float) (double) delta.y());
    deltaHorizontal.normalize();
    deltaVertical.normalize();
    double targetH = Math.acos(deltaHorizontal.y()) * 180.0 / Math.PI;
    double targetV = Math.asin(deltaVertical.y()) * 180.0 / Math.PI;
    if (deltaHorizontal.x() > 0.0)
        targetH *= -1;
    if (targetV > 90f)
        targetV = 90f;
    if (targetV < -90f)
        targetV = -90f;
    while (targetH < 0.0) targetH += 360.0;
    double diffH = targetH - entity.getEntityRotationComponent().getHorizontalRotation();
    // Ensures we always take the fastest route
    if (Math.abs(diffH + 360) < Math.abs(diffH))
        diffH = diffH + 360;
    else if (Math.abs(diffH - 360) < Math.abs(diffH))
        diffH = diffH - 360;
    double diffV = targetV - entity.getEntityRotationComponent().getVerticalRotation();
    if (Double.isNaN(diffH))
        diffH = 0;
    if (Double.isNaN(diffV))
        diffV = 0;
    entity.getEntityRotationComponent().addRotation(diffH / 15f, diffV / 15f);
}
Also used : Vector2f(org.joml.Vector2f)

Example 25 with Vector2f

use of org.joml.Vector2f in project Terasology by MovingBlocks.

the class VectorTypeSerializerTest method testSerializationConstant.

@Test
void testSerializationConstant() {
    TestObject2 a = new TestObject2();
    a.v1 = new Vector3f(1.0f, 2.0f, 3.0f);
    a.v2 = new Vector4f(1.0f, 2.0f, 3.0f, 5.0f);
    a.v3 = new Vector2f(1.0f, 2.0f);
    byte[] data = gsonSerializer.serialize(a, new TypeInfo<TestObject2>() {
    }).get();
    TestObject2 o = gsonSerializer.deserialize(new TypeInfo<TestObject2>() {
    }, data).get();
    assertEquals(new Vector3f(1.0f, 2.0f, 3.0f), o.v1, .00001f);
    assertEquals(new Vector4f(1.0f, 2.0f, 3.0f, 5.0f), o.v2, .00001f);
    assertEquals(new Vector2f(1.0f, 2.0f), o.v3, .00001f);
}
Also used : Vector4f(org.joml.Vector4f) Vector2f(org.joml.Vector2f) Vector3f(org.joml.Vector3f) TypeInfo(org.terasology.reflection.TypeInfo) Test(org.junit.jupiter.api.Test) ModuleEnvironmentTest(org.terasology.engine.ModuleEnvironmentTest)

Aggregations

Vector2f (org.joml.Vector2f)47 Vector3f (org.joml.Vector3f)13 Test (org.junit.jupiter.api.Test)6 Vector2i (org.joml.Vector2i)5 Vector4f (org.joml.Vector4f)5 Rectanglei (org.terasology.joml.geom.Rectanglei)5 Rectanglef (org.terasology.joml.geom.Rectanglef)4 IOException (java.io.IOException)3 ByteBuffer (java.nio.ByteBuffer)3 Vector3i (org.joml.Vector3i)3 SubtextureData (org.terasology.engine.rendering.assets.texture.subtexture.SubtextureData)3 Name (org.terasology.gestalt.naming.Name)3 Vector2fc (org.joml.Vector2fc)2 Vector3d (org.joml.Vector3d)2 MeshBuilder (org.terasology.engine.rendering.assets.mesh.MeshBuilder)2 TFloatList (gnu.trove.list.TFloatList)1 TObjectIntMap (gnu.trove.map.TObjectIntMap)1 TObjectIntHashMap (gnu.trove.map.hash.TObjectIntHashMap)1 Location (io.xol.chunkstories.api.Location)1 Controller (io.xol.chunkstories.api.entity.Controller)1