Skip to content

Commit

Permalink
Reinstate previous slope fixes; use a Set for PostProcessingCollisions
Browse files Browse the repository at this point in the history
  • Loading branch information
Danjb1 committed Jan 8, 2020
1 parent 008404c commit ee43fb8
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 7 deletions.
10 changes: 8 additions & 2 deletions src/engine/game/physics/CollisionResult.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package engine.game.physics;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import engine.game.physics.Hitbox.CollisionNode;

Expand Down Expand Up @@ -33,8 +35,7 @@ public class CollisionResult {
/**
* All PostProcessCollision that have occurred.
*/
private List<PostProcessCollision> postProcessCollisions
= new ArrayList<>();
private Set<PostProcessCollision> postProcessCollisions = new HashSet<>();

/**
* Nearest x-Collision detected by this CollisionResult.
Expand Down Expand Up @@ -193,6 +194,11 @@ public void addPostProcessCollision(PostProcessCollision collision) {
/**
* Renders a Collision invalid.
*
* <p>It is important to note that if an x-collision is invalidated during
* or after the post-processing stage of collision detection, the results
* may not be entirely as expected, due to the fact that y-collisions will
* have already been calculated based on x-collisions.
*
* @param collision
*/
public void invalidateCollision(Collision collision) {
Expand Down
60 changes: 55 additions & 5 deletions src/engine/game/physics/Physics.java
Original file line number Diff line number Diff line change
Expand Up @@ -208,9 +208,10 @@ public static CollisionResult getCollisionResult(
* Move in each axis independently and resolve collisions along the
* way.
*
* If the Hitbox intersects a PostProcessingTile at the destination,
* a PostProcessingCollision will be registered. After the movement
* is finished, all PostProcessingCollisions will be resolved.
* If the Hitbox intersects a PostProcessingTile at any point during
* the movement, a PostProcessingCollision will be registered. After
* the movement is finished, all PostProcessingCollisions will be
* resolved.
*/

// Move in the x-axis
Expand All @@ -227,8 +228,57 @@ public static CollisionResult getCollisionResult(
detectCollisionsY(result, logic, hitbox.getBottomNodes());
}

// Check for PostProcessingCollisions
if (dx != 0 || dy != 0) {
/*
* POST-PROCESSING COLLISION DETECTION.
*
* STAGE 1:
* Check for PostProcessingCollisions at the initial Hitbox
* position.
*
* It may be that the Hitbox is already intersecting a
* PostProcessingTile, which affects which collisions are permitted.
*
* EXAMPLE:
* - Hitbox is on a right slope, moving right.
* - After the x-movement is applied, the Hitbox is no longer
* intersecting the slope but is intersecting the floor tile at
* the top of the slope.
* - An x-collision is registered, but no PostProcessingCollision
* is registered, so the x-collision never gets invalidated.
*/
detectPostProcessCollisions(
result, logic, hitbox.getAllNodes(), 0, 0);


/*
* STAGE 2:
* Check for PostProcessingCollisions after the x-movement is
* applied.
*
* It is possible that the x-movement could move the Hitbox into a
* PostProcessingTile, but the y-movement could move the Hitbox out
* of it, so we have to check for collisions before the y-movement
* is applied.
*
* EXAMPLE:
* - Hitbox is in the empty tile "between" 2 right slopes (above
* one, and left of the other), moving right.
* - After the x-movement is applied, the Hitbox intersects the
* slope immediately to the right.
* - Were we to apply the y-movement, the bottom node of the
* Hitbox would fall into the solid block BELOW the slope,
* therefore it would never be inside the slope.
*/
if (dx != 0) {
detectPostProcessCollisions(
result, logic, hitbox.getAllNodes(), dx, 0);
}

/*
* STAGE 3:
* Check for PostProcessingCollisions at the final Hitbox position.
*/
if (dy != 0) {
detectPostProcessCollisions(
result, logic, hitbox.getAllNodes(), dx, dy);
}
Expand Down
33 changes: 33 additions & 0 deletions src/engine/game/physics/PostProcessCollision.java
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,37 @@ public float getTileBottom() {
return Tile.getBottom(tileY * Tile.HEIGHT);
}

// Auto-generated
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((node == null) ? 0 : node.hashCode());
result = prime * result + tileX;
result = prime * result + tileY;
return result;
}

// Auto-generated
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
PostProcessCollision other = (PostProcessCollision) obj;
if (node == null) {
if (other.node != null)
return false;
} else if (!node.equals(other.node))
return false;
if (tileX != other.tileX)
return false;
if (tileY != other.tileY)
return false;
return true;
}

}

0 comments on commit ee43fb8

Please sign in to comment.