Skip to content

Commit

Permalink
Fix tall Hitboxes glitching on slopes; fix interactions with slope "d…
Browse files Browse the repository at this point in the history
…iamonds"
  • Loading branch information
Danjb1 committed Mar 10, 2020
1 parent 060429f commit 005dc31
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 13 deletions.
6 changes: 3 additions & 3 deletions docs/TODO.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,16 @@
## Bugs

- Slopes:
- Hitbox behaves strangely when colliding with 2 slope tiles in the same column
- Corners of a slope "diamond"
- Very tight sloped tunnels (especially when jumping!)
- Hitbox behaves strangely in very tight sloped tunnels
- Hitbox clips through solid blocks if "wedged" between a slope and a solid block
- Hitbox can clip through the "back" of a slope (not yet supported)
- Hitbox can fall through a slope if there is no solid tile immediately below it
- Hitboxes with width > height can clip through a wall at the top of a slope

## Tech Debt

- Rename package to something less generic

- Don't log to System.out

- Move more code from demo project to engine?
Expand Down
34 changes: 24 additions & 10 deletions src/engine/game/physics/Hitbox.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,15 @@ public boolean isOnLeftEdge() {
return x == 0;
}

/**
* Determines if this Node is on the right edge of a Hitbox.
*
* @return
*/
public boolean isOnRightEdge() {
return x == Hitbox.this.width - Physics.SMALLEST_DISTANCE;
}

/**
* Determines if this Node is on the top edge of a Hitbox.
*
Expand All @@ -59,6 +68,15 @@ public boolean isOnTopEdge() {
return y == 0;
}

/**
* Determines if this Node is on the top edge of a Hitbox.
*
* @return
*/
public boolean isOnBottomEdge() {
return y == Hitbox.this.height - Physics.SMALLEST_DISTANCE;
}

// Auto-generated
@Override
public int hashCode() {
Expand All @@ -69,21 +87,17 @@ public int hashCode() {
return result;
}

// Auto-generated
@Override
public boolean equals(Object obj) {
if (this == obj)
if (this == obj) {
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
}
if (obj == null || getClass() != obj.getClass()) {
return false;
}
CollisionNode other = (CollisionNode) obj;
if (Float.floatToIntBits(x) != Float.floatToIntBits(other.x))
return false;
if (Float.floatToIntBits(y) != Float.floatToIntBits(other.y))
return false;
return true;
return x == other.x
&& y == other.y;
}

}
Expand Down
5 changes: 5 additions & 0 deletions src/engine/game/tiles/LeftCeilingSlope.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ public LeftCeilingSlope(int id) {
super(id);
}

@Override
protected boolean isNodeValidForSlope(CollisionNode node) {
return node.isOnTopEdge();
}

@Override
protected boolean isCollisionValid_Y(
CollisionResult result,
Expand Down
5 changes: 5 additions & 0 deletions src/engine/game/tiles/LeftSlope.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ public LeftSlope(int id) {
super(id);
}

@Override
protected boolean isNodeValidForSlope(CollisionNode node) {
return node.isOnBottomEdge();
}

@Override
protected boolean isCollisionValid_Y(
CollisionResult result,
Expand Down
5 changes: 5 additions & 0 deletions src/engine/game/tiles/RightCeilingSlope.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ public RightCeilingSlope(int id) {
super(id);
}

@Override
protected boolean isNodeValidForSlope(CollisionNode node) {
return node.isOnTopEdge();
}

@Override
protected boolean isCollisionValid_Y(
CollisionResult result,
Expand Down
5 changes: 5 additions & 0 deletions src/engine/game/tiles/RightSlope.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ public RightSlope(int id) {
super(id);
}

@Override
protected boolean isNodeValidForSlope(CollisionNode node) {
return node.isOnBottomEdge();
}

@Override
protected boolean isCollisionValid_Y(
CollisionResult result,
Expand Down
17 changes: 17 additions & 0 deletions src/engine/game/tiles/Slope.java
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,11 @@ public boolean hasSpecialCollisionProperties() {
public void postProcessing(
CollisionResult result, PostProcessCollision slopeCollision) {

// Ignore CollisionNodes that are not relevant to this Slope
if (!isNodeValidForSlope(slopeCollision.node)) {
return;
}

// Prevent slopes from interfering with each other;
// the slope containing the slope node takes precedence over others
boolean hasPriority = doesSlopeHavePriority(result, slopeCollision);
Expand Down Expand Up @@ -127,6 +132,18 @@ public void postProcessing(
}
}

/**
* Determines if a CollisionNode can generate a collision for this slope.
*
* <p>The collision generated is always relative to the node involved in
* the collision, so considering nodes on the wrong side of the Hitbox (or
* in the middle of a tall Hitbox) can result in strange behaviour.
*
* @param node
* @return
*/
protected abstract boolean isNodeValidForSlope(CollisionNode node);

/**
* Determines if a slope collision should take priority over collisions
* generated by other slopes.
Expand Down

0 comments on commit 005dc31

Please sign in to comment.