Search in sources :

Example 1 with Vector2d

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

the class InputSystem method processMouseInput.

/**
 * Processes the current input state of the mouse, sends input events and updates bind buttons.
 * <p>
 * Mouse position actions are handled here, while mouse button and mouse wheel actions are handled at {@link
 * #processMouseButtonInput(float, MouseAction)} and {@link #processMouseWheelInput(float, MouseAction)}
 * accordingly.
 *
 * @param delta The length of the current frame.
 */
private void processMouseInput(float delta) {
    if (!isCapturingMouse()) {
        return;
    }
    this.mouse.update();
    Vector2d deltaMouse = mouse.getDelta();
    // process mouse movement x axis
    if (deltaMouse.x != 0) {
        double xValue = deltaMouse.x * inputDeviceConfig.getMouseSensitivity();
        MouseAxisEvent event = MouseAxisEvent.create(MouseAxis.X, xValue, delta);
        send(event);
    }
    // process mouse movement y axis
    if (deltaMouse.y != 0) {
        double yMovement = inputDeviceConfig.isMouseYAxisInverted() ? deltaMouse.y * -1 : deltaMouse.y;
        double yValue = yMovement * inputDeviceConfig.getMouseSensitivity();
        MouseAxisEvent event = MouseAxisEvent.create(MouseAxis.Y, yValue, delta);
        send(event);
    }
    // process mouse clicks
    for (MouseAction action : mouse.getInputQueue()) {
        switch(action.getInput().getType()) {
            case MOUSE_BUTTON:
                processMouseButtonInput(delta, action);
                break;
            case MOUSE_WHEEL:
                processMouseWheelInput(delta, action);
                break;
            default:
                break;
        }
    }
}
Also used : MouseAction(org.terasology.input.device.MouseAction) Vector2d(org.joml.Vector2d) MouseAxisEvent(org.terasology.engine.input.events.MouseAxisEvent)

Example 2 with Vector2d

use of org.joml.Vector2d in project chunkstories by Hugobros3.

the class Lwjgl3Mouse method getMousePosition.

public Vector2d getMousePosition() {
    DoubleBuffer b1 = MemoryUtil.memAllocDouble(1);
    DoubleBuffer b2 = MemoryUtil.memAllocDouble(1);
    glfwGetCursorPos(im.gameWindow.glfwWindowHandle, b1, b2);
    Vector2d vec2 = new Vector2d(b1.get(), im.gameWindow.getHeight() - b2.get());
    MemoryUtil.memFree(b1);
    MemoryUtil.memFree(b2);
    return vec2;
}
Also used : DoubleBuffer(java.nio.DoubleBuffer) Vector2d(org.joml.Vector2d)

Example 3 with Vector2d

use of org.joml.Vector2d in project chunkstories by Hugobros3.

the class FarTerrainGSMeshRenderer method renderTerrain.

@Override
public void renderTerrain(RenderingInterface renderer, ReadyVoxelMeshesMask mask) {
    Shader terrainShader = renderer.useShader("terrain_blocky");
    renderer.setBlendMode(BlendMode.DISABLED);
    renderer.getCamera().setupShader(terrainShader);
    worldRenderer.getSkyRenderer().setupShader(terrainShader);
    terrainShader.setUniform1f("viewDistance", world.getClient().getConfiguration().getIntOption("client.rendering.viewDistance"));
    Texture2D waterTexture = renderer.textures().getTexture("./textures/water/shallow.png");
    waterTexture.setLinearFiltering(true);
    waterTexture.setMipMapping(true);
    Texture2D waterTexture2 = renderer.textures().getTexture("./textures/water/deep.png");
    waterTexture2.setLinearFiltering(true);
    waterTexture2.setMipMapping(true);
    // renderer.bindCubemap("environmentCubemap", worldRenderer.renderBuffers.rbEnvironmentMap);
    renderer.bindTexture2D("blockLightmap", TexturesHandler.getTexture("./textures/environement/light.png"));
    Texture2D lightColors = TexturesHandler.getTexture("./textures/environement/lightcolors.png");
    renderer.bindTexture2D("lightColors", lightColors);
    renderer.bindTexture2D("waterNormalShallow", waterTexture);
    renderer.bindTexture2D("waterNormalDeep", waterTexture2);
    world.getGenerator().getEnvironment().setupShadowColors(renderer, terrainShader);
    renderer.bindTexture2D("vegetationColorTexture", world.getGenerator().getEnvironment().getGrassTexture(renderer));
    terrainShader.setUniform1f("mapSize", worldRenderer.getWorld().getSizeInChunks() * 32);
    terrainShader.setUniform1f("animationTimer", worldRenderer.getAnimationTimer());
    renderer.bindArrayTexture("heights", worldRenderer.getSummariesTexturesHolder().getHeightsArrayTexture());
    renderer.bindArrayTexture("topVoxels", worldRenderer.getSummariesTexturesHolder().getTopVoxelsArrayTexture());
    renderer.bindTexture1D("blocksTexturesSummary", colours.get());
    if (renderer.getClient().getInputsManager().getInputByName("wireframeFarTerrain").isPressed() && ClientLimitations.isDebugAllowed)
        renderer.setPolygonFillMode(PolygonFillMode.WIREFRAME);
    if (!renderer.getClient().getInputsManager().getInputByName("hideFarTerrain").isPressed() && ClientLimitations.isDebugAllowed) {
        renderer.setCullingMode(CullingMode.DISABLED);
        // renderer.setCullingMode(CullingMode.COUNTERCLOCKWISE);
        renderer.setDepthTestMode(DepthTestMode.LESS_OR_EQUAL);
        Player player = Client.getInstance().getPlayer();
        Location playerPosition = player.getLocation();
        if (playerPosition == null)
            // We won't do shit with that going on
            return;
        Vector2d playerCenter = new Vector2d(playerPosition.x, playerPosition.z);
        int chunkX = (int) Math.floor(playerPosition.x / 32.0);
        int chunkZ = (int) Math.floor(playerPosition.z / 32.0);
        int regionX = chunkX / 8;
        int regionZ = chunkZ / 8;
        int[] lodInstanceCount = new int[detailLevels.length];
        ByteBuffer[] lodByteBuffer = new ByteBuffer[detailLevels.length];
        // ByteBuffer summariesAttributes = MemoryUtil.memAlloc(9 * 9 * (4 + 2 * 4));
        // MemFreeByteBuffer auto_free_summariesAttributes = new MemFreeByteBuffer(summariesAttributes);
        // int count = 0;
        double lodBias = -0.2;
        double lodExponent = 0.35;
        if (!world.getClient().getConfiguration().getBooleanOption("client.rendering.hqTerrain")) {
            lodBias = 0.3;
            lodExponent = 0.35;
        } else {
            lodBias = 0.0;
            lodExponent = 0.45;
        }
        if (mask == null) {
            lodExponent = 1.0;
            lodBias = 0.6;
        }
        Vector2d center = new Vector2d();
        for (int i = -4; i <= 4; i++) for (int j = -4; j <= 4; j++) {
            int regionI = regionX + i;
            int regionJ = regionZ + j;
            int index = worldRenderer.getSummariesTexturesHolder().getSummaryIndex(regionI, regionJ);
            if (index == -1)
                continue;
            // For the extra row of triangles to mask the seams
            int index10 = worldRenderer.getSummariesTexturesHolder().getSummaryIndex(regionI + 1, regionJ);
            int index01 = worldRenderer.getSummariesTexturesHolder().getSummaryIndex(regionI, regionJ + 1);
            int index11 = worldRenderer.getSummariesTexturesHolder().getSummaryIndex(regionI + 1, regionJ + 1);
            if (index10 == -1 || index01 == -1 || index11 == -1)
                continue;
            HeightmapImplementation sum = (HeightmapImplementation) world.getRegionsSummariesHolder().getHeightmap(regionI, regionJ);
            // Early out
            if (sum == null || !sum.isLoaded())
                continue;
            if (!renderer.getCamera().isBoxInFrustrum(new CollisionBox(regionI * 256, 0, regionJ * 256, 256, 1024, /*+ sum.getHeightMipmapped(0, 0, 9)*/
            256)))
                continue;
            for (int l = 0; l < 8; l++) for (int m = 0; m < 8; m++) {
                if (mask != null) {
                    if (mask.shouldMaskSlab(regionI * 8 + l, regionJ * 8 + m, sum.min[l][m], sum.max[l][m]))
                        continue;
                }
                center.set(regionI * 256 + l * 32 + 16, regionJ * 256 + m * 32 + 16);
                int lod = detailLevels.length - (int) ((lodBias + Math.pow(Math.min(1024, center.distance(playerCenter)) / 1024, lodExponent)) * detailLevels.length);
                // System.out.println(center.distance(playerCenter));
                if (lod <= 0)
                    lod = 0;
                if (lod >= detailLevels.length)
                    lod = detailLevels.length - 1;
                // lod = 2;
                // lod = 2;
                // System.out.println("lod:"+lod);
                // lod = Math.random() > 0.5 ? 1 : 2;
                ByteBuffer summariesAttributes = lodByteBuffer[lod];
                if (summariesAttributes == null) {
                    summariesAttributes = MemoryUtil.memAlloc(9 * 9 * 8 * 8 * (4 + 2 * 4));
                    lodByteBuffer[lod] = summariesAttributes;
                }
                summariesAttributes.putFloat(regionI * 256 + l * 32);
                summariesAttributes.putFloat(regionJ * 256 + m * 32);
                summariesAttributes.put((byte) index);
                summariesAttributes.put((byte) index10);
                summariesAttributes.put((byte) index01);
                summariesAttributes.put((byte) index11);
                lodInstanceCount[lod]++;
            // Always add both so lod 1 is drew under
            /*if(lod != 0) {
								lod = 0;
								
								summariesAttributes = lodByteBuffer[lod];
								
								if(summariesAttributes == null) {
									summariesAttributes = MemoryUtil.memAlloc(9 * 9 * 8 * 8 * (4 + 2 * 4));
									lodByteBuffer[lod] = summariesAttributes;
								}
								
								summariesAttributes.putFloat(regionI * 256 + l * 32);
								summariesAttributes.putFloat(regionJ * 256 + m * 32);
								summariesAttributes.put((byte)index);
								summariesAttributes.put((byte)index10);
								summariesAttributes.put((byte)index01);
								summariesAttributes.put((byte)index11);
								lodInstanceCount[lod]++;
							}*/
            }
        }
        // for(int lod = 0; lod < detailLevels.length; lod++) {
        for (int lod = detailLevels.length - 1; lod >= 0; lod--) {
            // Check if anything was supposed to be drew at this lod
            ByteBuffer summariesAttributes = lodByteBuffer[lod];
            if (summariesAttributes == null)
                continue;
            if (lod < 0) {
                MemoryUtil.memFree(summariesAttributes);
                continue;
            }
            // Flip buffer, box it for autodeletion, upload it
            summariesAttributes.flip();
            MemFreeByteBuffer auto_free_summariesAttributes = new MemFreeByteBuffer(summariesAttributes);
            gridAttributes.uploadData(auto_free_summariesAttributes);
            terrainShader.setUniform1i("lodLevel", lod);
            terrainShader.setUniform1f("textureLodLevel", lod - 5);
            terrainShader.setUniform1i("maskPresence", mask == null ? 0 : 1);
            renderer.bindAttribute("vertexIn", grids[lod].asAttributeSource(VertexFormat.FLOAT, 3, 0, 0L));
            renderer.bindAttribute("displacementIn", gridAttributes.asAttributeSource(VertexFormat.FLOAT, 2, (4 + 2 * 4), 0L, 1));
            renderer.bindAttribute("indexIn", gridAttributes.asIntegerAttributeSource(VertexFormat.BYTE, 4, (4 + 2 * 4), 8L, 1));
            renderer.draw(Primitive.POINT, 0, (detailLevels[lod] + 1) * (detailLevels[lod] + 1) * 1, lodInstanceCount[lod]);
        }
    }
    renderer.setPolygonFillMode(PolygonFillMode.FILL);
}
Also used : Texture2D(io.xol.chunkstories.api.rendering.textures.Texture2D) Player(io.xol.chunkstories.api.player.Player) Shader(io.xol.chunkstories.api.rendering.shader.Shader) MemFreeByteBuffer(io.xol.chunkstories.client.util.MemFreeByteBuffer) ByteBuffer(java.nio.ByteBuffer) MemFreeByteBuffer(io.xol.chunkstories.client.util.MemFreeByteBuffer) HeightmapImplementation(io.xol.chunkstories.world.summary.HeightmapImplementation) Vector2d(org.joml.Vector2d) Location(io.xol.chunkstories.api.Location) CollisionBox(io.xol.chunkstories.api.physics.CollisionBox)

Example 4 with Vector2d

use of org.joml.Vector2d in project chunkstories by Hugobros3.

the class FarTerrainNoMeshRenderer method renderTerrain.

@Override
public void renderTerrain(RenderingInterface renderer, ReadyVoxelMeshesMask mask) {
    Shader terrainShader = renderer.useShader("terrain");
    renderer.setBlendMode(BlendMode.DISABLED);
    renderer.getCamera().setupShader(terrainShader);
    worldRenderer.getSkyRenderer().setupShader(terrainShader);
    terrainShader.setUniform1f("viewDistance", world.getClient().getConfiguration().getIntOption("client.rendering.viewDistance"));
    Texture2D waterTexture = renderer.textures().getTexture("./textures/water/shallow.png");
    waterTexture.setLinearFiltering(true);
    waterTexture.setMipMapping(true);
    // renderer.bindCubemap("environmentCubemap", worldRenderer.renderBuffers.rbEnvironmentMap);
    renderer.bindTexture2D("blockLightmap", TexturesHandler.getTexture("./textures/environement/light.png"));
    Texture2D lightColors = TexturesHandler.getTexture("./textures/environement/lightcolors.png");
    renderer.bindTexture2D("lightColors", lightColors);
    renderer.bindTexture2D("normalTexture", waterTexture);
    world.getGenerator().getEnvironment().setupShadowColors(renderer, terrainShader);
    // worldRenderer.setupShadowColors(terrainShader);
    renderer.bindTexture2D("vegetationColorTexture", world.getGenerator().getEnvironment().getGrassTexture(renderer));
    terrainShader.setUniform1f("mapSize", worldRenderer.getWorld().getSizeInChunks() * 32);
    renderer.bindArrayTexture("heights", worldRenderer.getSummariesTexturesHolder().getHeightsArrayTexture());
    renderer.bindArrayTexture("topVoxels", worldRenderer.getSummariesTexturesHolder().getTopVoxelsArrayTexture());
    renderer.bindTexture1D("blocksTexturesSummary", colours.get());
    if (renderer.getClient().getInputsManager().getInputByName("wireframeFarTerrain").isPressed() && ClientLimitations.isDebugAllowed)
        renderer.setPolygonFillMode(PolygonFillMode.WIREFRAME);
    if (!renderer.getClient().getInputsManager().getInputByName("hideFarTerrain").isPressed() && ClientLimitations.isDebugAllowed) {
        renderer.setCullingMode(CullingMode.COUNTERCLOCKWISE);
        renderer.setDepthTestMode(DepthTestMode.LESS_OR_EQUAL);
        Player player = Client.getInstance().getPlayer();
        Location playerPosition = player.getLocation();
        if (playerPosition == null)
            // We won't do shit with that going on
            return;
        Vector2d playerCenter = new Vector2d(playerPosition.x, playerPosition.z);
        int chunkX = (int) Math.floor(playerPosition.x / 32.0);
        int chunkZ = (int) Math.floor(playerPosition.z / 32.0);
        int regionX = chunkX / 8;
        int regionZ = chunkZ / 8;
        int[] lodInstanceCount = new int[detailLevels.length];
        ByteBuffer[] lodByteBuffer = new ByteBuffer[detailLevels.length];
        // ByteBuffer summariesAttributes = MemoryUtil.memAlloc(9 * 9 * (4 + 2 * 4));
        // MemFreeByteBuffer auto_free_summariesAttributes = new MemFreeByteBuffer(summariesAttributes);
        // int count = 0;
        double lodBias = -0.0;
        double lodExponent = 0.35;
        if (!world.getClient().getConfiguration().getBooleanOption("client.rendering.hqTerrain")) {
            lodBias = 0.4;
            lodExponent = 0.35;
        }
        if (mask == null) {
            lodExponent = 1.0;
            lodBias = 0.6;
        }
        Vector2d center = new Vector2d();
        for (int i = -4; i <= 4; i++) for (int j = -4; j <= 4; j++) {
            int regionI = regionX + i;
            int regionJ = regionZ + j;
            int index = worldRenderer.getSummariesTexturesHolder().getSummaryIndex(regionI, regionJ);
            if (index == -1)
                continue;
            // For the extra row of triangles to mask the seams
            int index10 = worldRenderer.getSummariesTexturesHolder().getSummaryIndex(regionI + 1, regionJ);
            int index01 = worldRenderer.getSummariesTexturesHolder().getSummaryIndex(regionI, regionJ + 1);
            int index11 = worldRenderer.getSummariesTexturesHolder().getSummaryIndex(regionI + 1, regionJ + 1);
            if (i < 4 && index10 == -1)
                continue;
            if (j < 4 && index01 == -1)
                continue;
            if (i < 4 && j < 4 && index11 == -1)
                continue;
            HeightmapImplementation sum = (HeightmapImplementation) world.getRegionsSummariesHolder().getHeightmap(regionI, regionJ);
            // Early out
            if (sum == null || !sum.isLoaded())
                continue;
            if (!renderer.getCamera().isBoxInFrustrum(new CollisionBox(regionI * 256, 0, regionJ * 256, 256, 1024, /*+ sum.getHeightMipmapped(0, 0, 9)*/
            256)))
                continue;
            for (int l = 0; l < 8; l++) for (int m = 0; m < 8; m++) {
                if (mask != null) {
                    if (mask.shouldMaskSlab(regionI * 8 + l, regionJ * 8 + m, sum.min[l][m], sum.max[l][m]))
                        continue;
                }
                center.set(regionI * 256 + l * 32 + 16, regionJ * 256 + m * 32 + 16);
                int lod = detailLevels.length - (int) ((lodBias + Math.pow(Math.min(1024, center.distance(playerCenter)) / 1024, lodExponent)) * detailLevels.length);
                // System.out.println(center.distance(playerCenter));
                if (lod <= 0)
                    lod = 0;
                if (lod >= detailLevels.length)
                    lod = detailLevels.length - 1;
                // lod = 2;
                // System.out.println("lod:"+lod);
                // lod = Math.random() > 0.5 ? 1 : 2;
                ByteBuffer summariesAttributes = lodByteBuffer[lod];
                if (summariesAttributes == null) {
                    summariesAttributes = MemoryUtil.memAlloc(9 * 9 * 8 * 8 * (4 + 2 * 4));
                    lodByteBuffer[lod] = summariesAttributes;
                }
                summariesAttributes.putFloat(regionI * 256 + l * 32);
                summariesAttributes.putFloat(regionJ * 256 + m * 32);
                summariesAttributes.put((byte) index);
                summariesAttributes.put((byte) index10);
                summariesAttributes.put((byte) index01);
                summariesAttributes.put((byte) index11);
                lodInstanceCount[lod]++;
                // Always add both so lod 1 is drew under
                if (lod != 0) {
                    lod = 0;
                    summariesAttributes = lodByteBuffer[lod];
                    if (summariesAttributes == null) {
                        summariesAttributes = MemoryUtil.memAlloc(9 * 9 * 8 * 8 * (4 + 2 * 4));
                        lodByteBuffer[lod] = summariesAttributes;
                    }
                    summariesAttributes.putFloat(regionI * 256 + l * 32);
                    summariesAttributes.putFloat(regionJ * 256 + m * 32);
                    summariesAttributes.put((byte) index);
                    summariesAttributes.put((byte) index10);
                    summariesAttributes.put((byte) index01);
                    summariesAttributes.put((byte) index11);
                    lodInstanceCount[lod]++;
                }
            }
        }
        // for(int lod = 0; lod < detailLevels.length; lod++) {
        for (int lod = detailLevels.length - 1; lod >= 0; lod--) {
            // Check if anything was supposed to be drew at this lod
            ByteBuffer summariesAttributes = lodByteBuffer[lod];
            if (summariesAttributes == null)
                continue;
            if (lod < 0) {
                MemoryUtil.memFree(summariesAttributes);
                continue;
            }
            // Flip buffer, box it for autodeletion, upload it
            summariesAttributes.flip();
            MemFreeByteBuffer auto_free_summariesAttributes = new MemFreeByteBuffer(summariesAttributes);
            gridAttributes.uploadData(auto_free_summariesAttributes);
            terrainShader.setUniform1i("lodLevel", lod);
            terrainShader.setUniform1f("textureLodLevel", lod - 5);
            terrainShader.setUniform1i("maskPresence", mask == null ? 0 : 1);
            renderer.bindAttribute("vertexIn", grids[lod].asAttributeSource(VertexFormat.FLOAT, 3, 0, 0L));
            renderer.bindAttribute("displacementIn", gridAttributes.asAttributeSource(VertexFormat.FLOAT, 2, (4 + 2 * 4), 0L, 1));
            renderer.bindAttribute("indexIn", gridAttributes.asIntegerAttributeSource(VertexFormat.BYTE, 4, (4 + 2 * 4), 8L, 1));
            renderer.draw(Primitive.TRIANGLE, 0, (detailLevels[lod] + 1) * (detailLevels[lod] + 1) * 2 * 3, lodInstanceCount[lod]);
        }
    }
    renderer.setPolygonFillMode(PolygonFillMode.FILL);
}
Also used : Texture2D(io.xol.chunkstories.api.rendering.textures.Texture2D) Player(io.xol.chunkstories.api.player.Player) Shader(io.xol.chunkstories.api.rendering.shader.Shader) MemFreeByteBuffer(io.xol.chunkstories.client.util.MemFreeByteBuffer) ByteBuffer(java.nio.ByteBuffer) MemFreeByteBuffer(io.xol.chunkstories.client.util.MemFreeByteBuffer) HeightmapImplementation(io.xol.chunkstories.world.summary.HeightmapImplementation) Vector2d(org.joml.Vector2d) Location(io.xol.chunkstories.api.Location) CollisionBox(io.xol.chunkstories.api.physics.CollisionBox)

Example 5 with Vector2d

use of org.joml.Vector2d 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)

Aggregations

Vector2d (org.joml.Vector2d)5 Location (io.xol.chunkstories.api.Location)3 CollisionBox (io.xol.chunkstories.api.physics.CollisionBox)2 Player (io.xol.chunkstories.api.player.Player)2 Shader (io.xol.chunkstories.api.rendering.shader.Shader)2 Texture2D (io.xol.chunkstories.api.rendering.textures.Texture2D)2 MemFreeByteBuffer (io.xol.chunkstories.client.util.MemFreeByteBuffer)2 HeightmapImplementation (io.xol.chunkstories.world.summary.HeightmapImplementation)2 ByteBuffer (java.nio.ByteBuffer)2 Controller (io.xol.chunkstories.api.entity.Controller)1 EntityControllable (io.xol.chunkstories.api.entity.interfaces.EntityControllable)1 EntityFlying (io.xol.chunkstories.api.entity.interfaces.EntityFlying)1 WorldClient (io.xol.chunkstories.api.world.WorldClient)1 WorldMaster (io.xol.chunkstories.api.world.WorldMaster)1 DoubleBuffer (java.nio.DoubleBuffer)1 Vector2f (org.joml.Vector2f)1 Vector3d (org.joml.Vector3d)1 Vector3dc (org.joml.Vector3dc)1 MouseAxisEvent (org.terasology.engine.input.events.MouseAxisEvent)1 MouseAction (org.terasology.input.device.MouseAction)1