In a 2D display, 3 squares, each with a given speed, mass, and size (size defined based on mass), will collide. The goal of this project is to explore the physics (ignoring friction) behind the collision of 3 squares, and also observe how the squares move this time along two axes.
Project: Collision Between 3 Squares
Project Objective:
Visual Demonstration
Algorithmic Structure
// Request basic info
Request mass and speed of square1
Request mass and speed of square2
Request mass and speed of square3
// Place the squares
Place square1 at a (x, y) position
Place square2 at another (x, y) position
Place square3 at another (x, y) position
// Main loop
While the program is running
If the user wants to quit, exit
// Move the squares
Move square1 with its (x, y) velocities
Move square2 with its (x, y) velocities
Move square3 with its (x, y) velocities
// Handle wall collisions
If square1 hits a wall (top, bottom, left, right), invert its speed
If square2 hits a wall (top, bottom, left, right), invert its speed
If square3 hits a wall (top, bottom, left, right), invert its speed
// Handle collisions between squares
If two squares touch
Change their (x, y) velocities
Count a collision
End If
// Display
Draw the squares and their velocities