Skip to content

Commit

Permalink
Add head collision too
Browse files Browse the repository at this point in the history
  • Loading branch information
Nixinova committed Aug 15, 2024
1 parent 0f7c79c commit 22aa029
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 6 deletions.
1 change: 1 addition & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
- Added collision boundary box to the player
- Changed renderer to only render blocks within view of the player
- Changed player spawn point to centre of world
- Changed player height to 1.6 blocks
- Changed default jumpHeight to 1.5
- Fixed jumping not pertaining to the specified jumpHeight

Expand Down
20 changes: 15 additions & 5 deletions src/com/nixinova/input/ControlsTick.java
Original file line number Diff line number Diff line change
Expand Up @@ -142,21 +142,31 @@ private void tickInputted(InputHandler input, boolean onGround) {
CornersList collisionPoints = Hitbox.getCollisionPoints(this.game.world, nextMove);
if (collisionPoints.list.size() > 0) {
Vector3<Double> shove = new Vector3<Double>(0d, 0d, 0d);
// Along all planes at foot

// 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
// 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
// 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
// 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
// along positive Z plane at foot
else if (collisionPoints.containsAll(Corner.FOOT_xZ, Corner.FOOT_XZ))
shove.z = -mvChange;

// Collision with head
// along any plane at head
if (collisionPoints.containsAny(Corner.HEAD_xz, Corner.HEAD_xZ, Corner.HEAD_Xz, Corner.HEAD_XZ)) {
this.isJumping = false;
shove.y = -Options.gravity;
}

// Shove player in the given direction
PxCoord curPos = controls.pos.toPx();
controls.pos = Coord3.fromPx(curPos.x + shove.x, curPos.y + shove.y, curPos.z + shove.z);
Expand Down
9 changes: 9 additions & 0 deletions src/com/nixinova/player/Hitbox.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,14 @@ public static enum Corner {
public static class CornersList {
public List<Corner> list = new ArrayList<>();

public boolean containsAny(Corner... providedCorners) {
for (Corner corner : providedCorners) {
if (list.contains(corner))
return true;
}
return false;
}

public boolean containsAll(Corner... providedCorners) {
for (Corner corner : providedCorners) {
if (!list.contains(corner))
Expand Down Expand Up @@ -50,6 +58,7 @@ public static CornersList getCollisionPoints(World world, Coord3 position) {
CornersList collisionCorners = new CornersList();

SubBlockCoord blockPos = position.toSubBlock();

// check each corner of the player's hitbox for being inside a block at the new position
for (var hitboxEntry : playerCornerOffsets.entrySet()) {
Corner collisionPoint = hitboxEntry.getKey();
Expand Down
2 changes: 1 addition & 1 deletion src/com/nixinova/player/Player.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import com.nixinova.world.World;

public class Player {
public static final double PLAYER_HEIGHT = 1.8;
public static final double PLAYER_HEIGHT = 1.6;
public static final double PLAYER_WIDTH = 0.8;

private Coord3 position;
Expand Down

0 comments on commit 22aa029

Please sign in to comment.