Collision Detection in Processing

We can then use nested if statements or loops to check for collisions between all pairs of objects, including our moving object and any stationary obstacles that are positioned on the left side of the screen (which would be stored in variables such as `leftObstacle1X` and `leftObstacle2X`, etc.).

Here’s an example code snippet in JavaScript:

// Declaring variables for the player character and stationary obstacles
let playerX = 0; // starting position of moving object on x-axis (player character)
let obstacle1X = 50; // starting position of first stationary obstacle on left side of screen (x-coordinate)
let obstacle2X = 300; // starting position of second stationary obstacle on right side of screen (x-coordinate)

// Declaring variables for the width of the player character and stationary obstacles
let widthOfPlayerCharacter = 50; // width of the player character in pixels
let widthOfObstacle1 = 100; // width of first stationary obstacle in pixels
let widthOfObstacle2 = 75; // width of second stationary obstacle in pixels

// Function to draw the game elements on the canvas
function draw() {
  background(64); // set background color to light gray (hex code #40)
  
  // Drawing the player character as a circle at its current position
  ellipse(playerX, height/2, 50, 50); // draw a circle with radius of 25 pixels at current position of player character
  
  // Drawing the stationary obstacles as rectangles at their starting positions
  rect(obstacle1X, height/2, widthOfObstacle1, widthOfObstacle1); // draw a rectangle with dimensions of first stationary obstacle at its starting position on the left side of screen
  rect(obstacle2X, height/2, widthOfObstacle2, widthOfObstacle2); // draw a rectangle with dimensions of second stationary obstacle at its starting position on the right side of screen
  
  // Checking for collisions between the player character and stationary obstacles
  if (playerX >= obstacle1X && playerX <= obstacle1X + widthOfObstacle1) { // check for collision between player character and first stationary obstacle on left side of screen
    console.log("Collision detected with first stationary obstacle!"); // log message to the console when collision is detected
  } else if (playerX >= obstacle2X && playerX <= obstacle2X + widthOfObstacle2) { // check for collision between player character and second stationary obstacle on right side of screen in case of reversed order
    console.log("Collision detected with second stationary obstacle!"); // log message to the console when collision is detected
  } else if (playerX >= obstacle1X && playerX <= obstacle1X + widthOfObstacle1) { // check for collision between first stationary obstacle on left side of screen and second stationary obstacle on right side of screen in case of overlapping positions
    console.log("Collision detected between first and second stationary obstacles!"); // log message to the console when collision is detected
  } else if (playerX >= obstacle2X && playerX <= obstacle1X + widthOfObstacle1) { // check for collision between second stationary obstacle on right side of screen and first stationary obstacle on left side of screen in case of overlapping positions
    console.log("Collision detected between second and first stationary obstacles!"); // log message to the console when collision is detected
  } else { // if there are no collisions, update horizontal position of player character based on speed (left or right)
    playerX += 5; // move player character horizontally by 5 pixels per frame
  }
}

// Note: The original script had a few errors, such as missing commas and incorrect variable names. These have been corrected in the above script. Additionally, the script now includes comments to explain the purpose and functionality of each code segment.

In this example, we’re using nested if statements to check for collisions between all pairs of objects. We first check whether the x-coordinate of our player character is greater than or equal to the x-coordinate of our first stationary obstacle on the left side of the screen and less than or equal to its right edge (which would be `leftObstacle1X + widthOfObstacle1`). If this condition is true, we know that there has been a collision between these two objects! We then check for collisions in case of reversed order using an else if statement. We also check for collisions between first and second stationary obstacles on left side of screen and second and first stationary obstacles on right side of screen in case of overlapping positions. If there are no collisions, we update the horizontal position of our player character based on speed (left or right).

SICORPS