From 6ee17375a6b47ecb4303d2f5cb9fe7177cc05f26 Mon Sep 17 00:00:00 2001 From: jsrobson10 Date: Fri, 7 Aug 2020 16:40:31 +1000 Subject: [PATCH] Made solid tile collision correction velocity based --- src/projectzombie/entity/Entity.java | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/src/projectzombie/entity/Entity.java b/src/projectzombie/entity/Entity.java index 27874a7..29a0b00 100755 --- a/src/projectzombie/entity/Entity.java +++ b/src/projectzombie/entity/Entity.java @@ -170,6 +170,7 @@ public abstract class Entity implements IBdfClassManager this.chunk = chunk; Vec2i tpos = pos.xz().toInt(); + boolean pos_is_legal = moveIsLegal(pos.xz()); tile_back = chunk.getBackTile(tpos); tile_front = chunk.getFrontTile(tpos); @@ -184,13 +185,13 @@ public abstract class Entity implements IBdfClassManager if(isSolid) { - if(moveIsLegal(new Vec2d(new_pos.x, pos.z))) { + if(moveIsLegal(new Vec2d(new_pos.x, pos.z)) || !pos_is_legal) { pos.x = new_pos.x; } else { velocity.x *= -0.25; } - if(moveIsLegal(new Vec2d(pos.x, new_pos.z))) { + if(moveIsLegal(new Vec2d(pos.x, new_pos.z)) || !pos_is_legal) { pos.z = new_pos.z; } else { velocity.z *= -0.25; @@ -225,11 +226,19 @@ public abstract class Entity implements IBdfClassManager } // Move the player out of a solid area - while(!moveIsLegal(pos)) + if(!pos_is_legal) { - Vec2i tpos = pos.xz().toInt(); - Vec2d direction = pos.xz().subtract(tpos.toDouble()).subtract(0.5).normalize(); - pos = pos.add(direction.multiply(0.01).xny()); + Vec2i collision_pos = pos.xz().toInt(); + Vec2d direction = pos.xz().subtract(collision_pos.toDouble()).subtract(0.5); + + // Do this to prevent division by zero, just move in an arbitrary direction + if(direction.x == 0 && direction.y == 0) { + push(new Vec3d(0.1, 0, 0)); + } + + else { + push(direction.multiply(0.1).xny()); + } } } }