public void playerCollision(Player player, WorldManager worldManager, float delta) { Vector2 playerProjection = new Vector2(player.getPlayerPosition()); Vector2 playerAcc = new Vector2(player.getPlayerAcceleration()); playerAcc.x = 0; playerProjection.add(playerAcc.tmp().mul(delta)); Chunk collidingChunk = worldManager.getWorld().getChunkAt(playerProjection.x, playerProjection.y); if (collidingChunk != null) { for (int x = 0; x < chunkSizeX; x++) { for (int y = 0; y < chunkSizeY; y++) { if (playerProjection.x >= (collidingChunk.getChunkX() + (x * blockSizeX)) && playerProjection.x <= (collidingChunk.getChunkX() + (x * blockSizeX) + blockSizeX)) { if ((playerProjection.y >= (collidingChunk.getChunkY() + (y * blockSizeY)) && playerProjection.y <= (collidingChunk.getChunkY() + (y * blockSizeY) + blockSizeY)) || (playerProjection.y + 32 >= (collidingChunk.getChunkY() + (y * blockSizeY)) && playerProjection.y + 32 <= (collidingChunk.getChunkY() + (y * blockSizeY) + blockSizeY))) { Byte[][] chunkData = collidingChunk.getChunkData(); if (blockTypes.isVisible(chunkData[x][y])) { if (player.getVelocityY() < player.getFallDamageThreshold()) { player.setHealth( player.getHealth() - Math.round( (Math.abs(player.getVelocityY()) / player.getFallDamageMultiplier()))); } player.setVelocityY(0); player.setOnFloor(true); } } } else if (playerProjection.x + 16 >= (collidingChunk.getChunkX() + (x * blockSizeX)) && playerProjection.x + 16 <= (collidingChunk.getChunkX() + (x * blockSizeX) + blockSizeX)) { if ((playerProjection.y >= (collidingChunk.getChunkY() + (y * blockSizeY)) && playerProjection.y <= (collidingChunk.getChunkY() + (y * blockSizeY) + blockSizeY)) || (playerProjection.y + 32 >= (collidingChunk.getChunkY() + (y * blockSizeY)) && playerProjection.y + 32 <= (collidingChunk.getChunkY() + (y * blockSizeY) + blockSizeY))) { Byte[][] chunkData = collidingChunk.getChunkData(); if (blockTypes.isVisible(chunkData[x][y])) { if (player.getVelocityY() < player.getFallDamageThreshold()) { player.setHealth( player.getHealth() - Math.round( (Math.abs(player.getVelocityY()) / player.getFallDamageMultiplier()))); } player.setVelocityY(0); player.setOnFloor(true); } } } } } Vector2 playerProjectionY = new Vector2(player.getPlayerPosition()); Vector2 playerAccY = new Vector2(player.getPlayerAcceleration()); playerAccY.y = 0; playerProjectionY.add(playerAccY.tmp().mul(delta)); for (int x = 0; x < chunkSizeX; x++) { for (int y = 0; y < chunkSizeY; y++) { if (playerProjectionY.x >= (collidingChunk.getChunkX() + (x * blockSizeX)) && playerProjectionY.x <= (collidingChunk.getChunkX() + (x * blockSizeX) + blockSizeX)) { if ((playerProjectionY.y >= (collidingChunk.getChunkY() + (y * blockSizeY)) && playerProjectionY.y <= (collidingChunk.getChunkY() + (y * blockSizeY) + blockSizeY)) || (playerProjectionY.y + 32 >= (collidingChunk.getChunkY() + (y * blockSizeY)) && playerProjectionY.y + 32 <= (collidingChunk.getChunkY() + (y * blockSizeY) + blockSizeY))) { Byte[][] chunkData = collidingChunk.getChunkData(); if (blockTypes.isVisible(chunkData[x][y])) { player.setVelocityX(0); } } } else if (playerProjectionY.x + 16 >= (collidingChunk.getChunkX() + (x * blockSizeX)) && playerProjectionY.x + 16 <= (collidingChunk.getChunkX() + (x * blockSizeX) + blockSizeX)) { if ((playerProjectionY.y >= (collidingChunk.getChunkY() + (y * blockSizeY)) && playerProjectionY.y <= (collidingChunk.getChunkY() + (y * blockSizeY) + blockSizeY)) || (playerProjectionY.y + 32 >= (collidingChunk.getChunkY() + (y * blockSizeY)) && playerProjectionY.y + 32 <= (collidingChunk.getChunkY() + (y * blockSizeY) + blockSizeY))) { Byte[][] chunkData = collidingChunk.getChunkData(); if (blockTypes.isVisible(chunkData[x][y])) { player.setVelocityX(0); } } } } } } }
/** * Linearly interpolates between this vector and the target vector by alpha which is in the range * [0,1]. The result is stored in this vector. * * @param target The target vector * @param alpha The interpolation coefficient * @return This vector for chaining. */ public Vector2 lerp(Vector2 target, float alpha) { Vector2 r = this.mul(1.0f - alpha); r.add(target.tmp().mul(alpha)); return r; }