Made solid tile collision correction velocity based

This commit is contained in:
jsrobson10 2020-08-07 16:40:31 +10:00
parent 20b1dac4a6
commit 6ee17375a6
1 changed files with 15 additions and 6 deletions

View File

@ -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());
}
}
}
}