Refactoring to use pixel-perfect Collision check

This commit is contained in:
Urban Modig
2025-08-15 20:45:29 +02:00
parent fe2eb8cb48
commit 7bcc46122c
4 changed files with 15 additions and 13 deletions

View File

@ -16,6 +16,7 @@ public class CollisionChecker {
} }
public Point getValidDestination(Direction direction, Point position, int agent_width, int agent_height) { public Point getValidDestination(Direction direction, Point position, int agent_width, int agent_height) {
List<Point> list = switch (direction) { List<Point> list = switch (direction) {
case RIGHT -> List.of( case RIGHT -> List.of(
new Point(position.x + agent_width, position.y), // TOPRIGHT new Point(position.x + agent_width, position.y), // TOPRIGHT
@ -32,6 +33,8 @@ public class CollisionChecker {
default -> Collections.EMPTY_LIST; default -> Collections.EMPTY_LIST;
}; };
System.out.println( direction + " bounderies for " + position + " are " + list);
List<Point> list2 = list.stream() List<Point> list2 = list.stream()
.map(p -> normalizePosition(direction, p, agent_width, agent_height)) .map(p -> normalizePosition(direction, p, agent_width, agent_height))
.toList(); .toList();
@ -51,9 +54,6 @@ public class CollisionChecker {
// tunnel // tunnel
if (x < GameMap.OFFSET_X) x = width - agent_width - GameMap.OFFSET_X; // right if (x < GameMap.OFFSET_X) x = width - agent_width - GameMap.OFFSET_X; // right
if (x >= (width - GameMap.OFFSET_X)) x = GameMap.OFFSET_X; // left if (x >= (width - GameMap.OFFSET_X)) x = GameMap.OFFSET_X; // left
//
// if (y < 0) y = height - 1; // optional vertical wrap
// else if (y >= height) y = 0;
return new Point(x, y); return new Point(x, y);
} }

View File

@ -79,12 +79,11 @@ public class PacMan {
case DOWN -> new Point(position.x, position.y + speed); case DOWN -> new Point(position.x, position.y + speed);
default -> throw new IllegalStateException("Unexpected value: " + direction); default -> throw new IllegalStateException("Unexpected value: " + direction);
}; };
System.out.println("At: " + position+ ",trying to move " + direction.name() + " to " + newPosition);
Point destination = collisionChecker.getValidDestination(direction, newPosition, COLLISION_BOX_SIZE, COLLISION_BOX_SIZE); Point destination = collisionChecker.getValidDestination(direction, newPosition, COLLISION_BOX_SIZE, COLLISION_BOX_SIZE);
if(destination != null) { if(destination != null) {
position = destination; position = destination;
System.out.println("Position: + " + position);
} }
} }

View File

@ -6,7 +6,7 @@ import javax.swing.*;
public class Game implements Runnable { public class Game implements Runnable {
public final static int FPS_SET = 120; public final static int FPS_SET = 120;
public final static int UPS_SET = 200; public final static int UPS_SET = 120;
private final static double timePerFrame = 1000000000.0 / FPS_SET; private final static double timePerFrame = 1000000000.0 / FPS_SET;
private final static double timePerUpdate = 1000000000.0 / UPS_SET; private final static double timePerUpdate = 1000000000.0 / UPS_SET;
@ -69,7 +69,7 @@ public class Game implements Runnable {
if (System.currentTimeMillis() - lastCheck >= 1000) { if (System.currentTimeMillis() - lastCheck >= 1000) {
lastCheck = System.currentTimeMillis(); lastCheck = System.currentTimeMillis();
System.out.println("FPS: " + frames + " | UPS: " + updates); //System.out.println("FPS: " + frames + " | UPS: " + updates);
frames = 0; frames = 0;
updates = 0; updates = 0;
} }

View File

@ -135,17 +135,20 @@ public class GameMap {
} }
} }
public boolean isPassable(List<Point> list) { public boolean isPassable(List<Point> list){
return list.stream().allMatch(p -> isPassable(p.x, p.y)); return list.stream().allMatch(p -> isPassable(p.x, p.y));
} }
public boolean isPassable(int x, int y) { public boolean isPassable(int x, int y) {
int row = (y - OFFSET_Y) / MAP_TILESIZE; int row = (y - OFFSET_Y) / MAP_TILESIZE;
int col = (x - OFFSET_X) / MAP_TILESIZE; int col = (x - OFFSET_X) / MAP_TILESIZE;
System.out.println("(x,y)" + x+","+y + " -> col,row: " + col + "," +row); int tileY = (y - OFFSET_Y) % MAP_TILESIZE;
boolean passable = mapData[row][col].isPassable(); int tileX = (x - OFFSET_X) % MAP_TILESIZE;
System.out.println(row + "," + col + "is" +(passable?"":" not") + " passable"); System.out.print("Point[x="+x+",y="+y+"] is row="+ row + ", col=" + col + " with reminder x=" +tileX+",y=" +tileY);
return passable;
boolean[][] mask = mapData[row][col].getCollisionMask();
boolean b = mask == null || !mask[tileY][tileX];
System.out.println(b?" - passable":" - not passable");
return b;
} }
public int getWidth() { public int getWidth() {