Collision Detection

Collision detection is implemented in the game.ts file. This file provides methods for determining if a collision exists. These methods are employed for various situations, such as detecting collisions between Pac-Man and ghosts, Pac-Man and pellets, or ghosts and walls.

The collision detection logic utilizes the concept of “bounding boxes”, which are rectangular areas that encompass each game object. These bounding boxes allow for efficient collision checks by comparing the positions and dimensions of the boxes.

Methods for Collision Detection:

  • checkCollision(a: { x: number; y: number; width: number; height: number }, b: { x: number; y: number; width: number; height: number }): This function takes two objects (a and b) as input, each represented by a bounding box. It returns true if the bounding boxes of the two objects overlap, indicating a collision. Otherwise, it returns false.

    Source Code:

    // game.ts
              checkCollision(a: { x: number; y: number; width: number; height: number }, b: { x: number; y: number; width: number; height: number }): boolean {
                return (
                  a.x < b.x + b.width &&
                  a.x + a.width > b.x &&
                  a.y < b.y + b.height &&
                  a.y + a.height > b.y
                );
              }
              
  • checkPelletCollision(pacMan: { x: number; y: number; width: number; height: number }): This function determines if Pac-Man has collided with a pellet. It iterates through all pellets and checks if their bounding boxes overlap with Pac-Man’s bounding box.

    Source Code:

    // game.ts
              checkPelletCollision(pacMan: { x: number; y: number; width: number; height: number }): boolean {
                let didCollide = false;
                this.pellets.forEach((pellet) => {
                  if (this.checkCollision(pacMan, pellet)) {
                    this.pellets.splice(this.pellets.indexOf(pellet), 1);
                    didCollide = true;
                  }
                });
                return didCollide;
              }
              
  • checkGhostCollision(pacMan: { x: number; y: number; width: number; height: number }): This function determines if Pac-Man has collided with any ghost. It iterates through each ghost and checks for overlapping bounding boxes. If a collision occurs, it calls the ghostCollision function, which handles the consequences of the collision.

    Source Code:

    // game.ts
              checkGhostCollision(pacMan: { x: number; y: number; width: number; height: number }): void {
                this.ghosts.forEach((ghost) => {
                  if (this.checkCollision(pacMan, ghost)) {
                    this.ghostCollision();
                  }
                });
              }
              
  • checkWallCollision(direction: string, x: number, y: number, width: number, height: number): This function determines if an object (such as Pac-Man or a ghost) collides with a wall. It takes the direction of movement and the object’s dimensions as input and checks for potential collisions with wall segments in the specified direction.

    Source Code:

    // game.ts
              checkWallCollision(direction: string, x: number, y: number, width: number, height: number): boolean {
                const newX = direction === "left" ? x - width : direction === "right" ? x + width : x;
                const newY = direction === "up" ? y - height : direction === "down" ? y + height : y;
              
                const wallSections = this.wallSections.filter((section) => {
                  return (
                    (section.x === newX && section.y === newY && section.width === width && section.height === height) ||
                    (section.x === x && section.y === y && section.width === width && section.height === height)
                  );
                });
              
                return wallSections.length > 0;
              }
              

Example Usage:

The following code snippet demonstrates how collision detection is utilized in the game loop to determine if Pac-Man has collided with a pellet:

// game.ts
          update() {
            // ... other game logic
          
            // Check for pellet collision
            if (this.checkPelletCollision(this.pacMan)) {
              // Handle pellet collision logic (e.g., increase score)
            }
          
            // ... other game logic
          }
          

The code iterates through the pellets array, checking for collisions between each pellet and Pac-Man. If a collision is detected, the pellet is removed from the array, and the game logic updates accordingly.