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

View File

@ -79,12 +79,11 @@ public class PacMan {
case DOWN -> new Point(position.x, position.y + speed);
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);
if(destination != null) {
position = destination;
System.out.println("Position: + " + position);
}
}

View File

@ -6,7 +6,7 @@ import javax.swing.*;
public class Game implements Runnable {
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 timePerUpdate = 1000000000.0 / UPS_SET;
@ -69,7 +69,7 @@ public class Game implements Runnable {
if (System.currentTimeMillis() - lastCheck >= 1000) {
lastCheck = System.currentTimeMillis();
System.out.println("FPS: " + frames + " | UPS: " + updates);
//System.out.println("FPS: " + frames + " | UPS: " + updates);
frames = 0;
updates = 0;
}

View File

@ -138,14 +138,17 @@ public class GameMap {
public boolean isPassable(List<Point> list){
return list.stream().allMatch(p -> isPassable(p.x, p.y));
}
public boolean isPassable(int x, int y) {
int row = (y - OFFSET_Y) / MAP_TILESIZE;
int col = (x - OFFSET_X) / MAP_TILESIZE;
System.out.println("(x,y)" + x+","+y + " -> col,row: " + col + "," +row);
boolean passable = mapData[row][col].isPassable();
System.out.println(row + "," + col + "is" +(passable?"":" not") + " passable");
return passable;
int tileY = (y - OFFSET_Y) % MAP_TILESIZE;
int tileX = (x - OFFSET_X) % MAP_TILESIZE;
System.out.print("Point[x="+x+",y="+y+"] is row="+ row + ", col=" + col + " with reminder x=" +tileX+",y=" +tileY);
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() {