Skip to content

Commit

Permalink
Add block corner collisions
Browse files Browse the repository at this point in the history
  • Loading branch information
Nixinova committed Aug 15, 2024
1 parent fa6ffcb commit a341478
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 37 deletions.
129 changes: 92 additions & 37 deletions src/com/nixinova/input/ControlsTick.java
Original file line number Diff line number Diff line change
Expand Up @@ -150,43 +150,7 @@ private void tickInputted(InputHandler input, boolean onGround) {
Coord3 nextMove = resultFromMove(xMove, yMove, zMove).pos;
CornersList collisionPoints = Hitbox.getCollisionPoints(this.game.world, nextMove);
if (collisionPoints.list.size() > 0) {
Vector3<Double> shove = new Vector3<Double>(0d, 0d, 0d);

// Collision with foot
// along all planes at foot
if (collisionPoints.containsAll(Corner.FOOT_xz, Corner.FOOT_xZ, Corner.FOOT_Xz, Corner.FOOT_XZ))
shove.y = Options.gravity;
// along negative X plane at foot
else if (collisionPoints.containsAll(Corner.FOOT_xz, Corner.FOOT_xZ))
shove.x = mvChange;
// along positive X plane at foot
else if (collisionPoints.containsAll(Corner.FOOT_Xz, Corner.FOOT_XZ))
shove.x = -mvChange;
// along negative Z plane at foot
else if (collisionPoints.containsAll(Corner.FOOT_xz, Corner.FOOT_Xz))
shove.z = mvChange;
// along positive Z plane at foot
else if (collisionPoints.containsAll(Corner.FOOT_xZ, Corner.FOOT_XZ))
shove.z = -mvChange;

// Collision with head
// along all planes at head
else if (collisionPoints.containsAll(Corner.HEAD_xz, Corner.HEAD_xZ, Corner.HEAD_Xz, Corner.HEAD_XZ)) {
this.isJumping = false;
shove.y = -Options.gravity;
}
// along negative X plane at head
else if (collisionPoints.containsAll(Corner.HEAD_xz, Corner.HEAD_xZ))
shove.x = mvChange;
// along positive X plane at head
else if (collisionPoints.containsAll(Corner.HEAD_Xz, Corner.HEAD_XZ))
shove.x = -mvChange;
// along negative Z plane at head
else if (collisionPoints.containsAll(Corner.HEAD_xz, Corner.HEAD_Xz))
shove.z = mvChange;
// along positive Z plane at head
else if (collisionPoints.containsAll(Corner.HEAD_xZ, Corner.HEAD_XZ))
shove.z = -mvChange;
Vector3<Double> shove = calculateShoveFactor(collisionPoints, mvChange, Options.gravity);

// Shove player in the given direction
PxCoord curPos = controls.pos.toPx();
Expand Down Expand Up @@ -302,4 +266,95 @@ private boolean isBelowGround() {
return !aboveTxIsAir;
}

/**
* Calculates an opposing vector to the direction the player is moving in,
* as apparent by which (combination of) vertices of their hitbox are in collision with a solid block.
*/
private Vector3<Double> calculateShoveFactor(CornersList collisionPoints, double horizMove, double vertMove) {
Vector3<Double> shove = new Vector3<Double>(0d, 0d, 0d);

// Collision with foot
// along all planes at foot
if (collisionPoints.containsAll(Corner.FOOT_xz, Corner.FOOT_xZ, Corner.FOOT_Xz, Corner.FOOT_XZ)) {
shove.y = vertMove;
}
// along negative X plane at foot
else if (collisionPoints.containsAll(Corner.FOOT_xz, Corner.FOOT_xZ)) {
shove.x = horizMove;
}
// along positive X plane at foot
else if (collisionPoints.containsAll(Corner.FOOT_Xz, Corner.FOOT_XZ)) {
shove.x = -horizMove;
}
// along negative Z plane at foot
else if (collisionPoints.containsAll(Corner.FOOT_xz, Corner.FOOT_Xz)) {
shove.z = horizMove;
}
// along positive Z plane at foot
else if (collisionPoints.containsAll(Corner.FOOT_xZ, Corner.FOOT_XZ)) {
shove.z = -horizMove;
}
// with a single point
else if (collisionPoints.contains(Corner.FOOT_xz)) {
// collision with minX,minZ so push +x,+z
shove.x = horizMove;
shove.z = horizMove;
} else if (collisionPoints.contains(Corner.FOOT_xZ)) {
// collision with minX,maxZ so push +x,-z
shove.x = horizMove;
shove.z = -horizMove;
} else if (collisionPoints.contains(Corner.FOOT_Xz)) {
// collision with maxX,minZ so push -x,+z
shove.x = -horizMove;
shove.z = horizMove;
} else if (collisionPoints.contains(Corner.FOOT_XZ)) {
// collision with maxX,maxZ so push -x,-z
shove.x = -horizMove;
shove.z = -horizMove;
}

// Collision with head
// along all planes at head
else if (collisionPoints.containsAll(Corner.HEAD_xz, Corner.HEAD_xZ, Corner.HEAD_Xz, Corner.HEAD_XZ)) {
this.isJumping = false;
shove.y = -vertMove;
}
// along negative X plane at head
else if (collisionPoints.containsAll(Corner.HEAD_xz, Corner.HEAD_xZ)) {
shove.x = horizMove;
}
// along positive X plane at head
else if (collisionPoints.containsAll(Corner.HEAD_Xz, Corner.HEAD_XZ)) {
shove.x = -horizMove;
}
// along negative Z plane at head
else if (collisionPoints.containsAll(Corner.HEAD_xz, Corner.HEAD_Xz)) {
shove.z = horizMove;
}
// along positive Z plane at head
else if (collisionPoints.containsAll(Corner.HEAD_xZ, Corner.HEAD_XZ)) {
shove.z = -horizMove;
}
// with a single point
else if (collisionPoints.contains(Corner.HEAD_xz)) {
// collision with minX,minZ so push +x,+z
shove.x = horizMove;
shove.z = horizMove;
} else if (collisionPoints.contains(Corner.HEAD_xZ)) {
// collision with minX,maxZ so push +x,-z
shove.x = horizMove;
shove.z = -horizMove;
} else if (collisionPoints.contains(Corner.HEAD_Xz)) {
// collision with maxX,minZ so push -x,+z
shove.x = -horizMove;
shove.z = horizMove;
} else if (collisionPoints.contains(Corner.HEAD_XZ)) {
// collision with maxX,maxZ so push -x,-z
shove.x = -horizMove;
shove.z = -horizMove;
}

return shove;
}

}
4 changes: 4 additions & 0 deletions src/com/nixinova/player/Hitbox.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ public static enum Corner {
public static class CornersList {
public List<Corner> list = new ArrayList<>();

public boolean contains(Corner corner) {
return list.contains(corner);
}

public boolean containsAll(Corner... providedCorners) {
for (Corner corner : providedCorners) {
if (!list.contains(corner))
Expand Down

0 comments on commit a341478

Please sign in to comment.