1 - Bouncing Ball

Introduction

This section demonstrates the physics behind the Bouncing Ball simulation.

Concept of Motion

This ball moves in a [2-D] two-dimensional space with horizontal and vertical velocity components. The direction of motion is determined by the sign of these components.

  1. Velocity in the X-axis (vx): Determines horizontal movement. Positive values move right, negative values move left.
  2. Velocity in the Y-axis (vy): Determines vertical movement. Positive values move down, negative values move up.
  3. Combined Motion: The overall movement is a combination of both components, resulting in diagonal trajectories when both vx and vy are non-zero. In short, Each frame updates the ball's position using both the velocities.

Understanding Collisions

  • When the ball reaches the left or right boundary, the X velocity reverses.
  • When the ball reaches the top or bottom boundary, the Y velocity reverses.
  • This reversal simulates a bounce effect [Elastic collision], changing the ball's direction while maintaining its speed.
vafter=vbeforev_{after} = -v_{before}
In a perfectly elastic collision, no energy is lost, and the ball retains its speed after bouncing.

Code Example

let x = 100, y = 100;
let vx = 2.5, vy = 2;

function draw(){
  x += vx;
  y += vy;

// Check for collision with walls
  if(x <= 0 || x >= canvasWidth) {
    vx = -vx; // Reverse X velocity
  }
   if(y <= 0 || y >= canvasHeight) {
      vy = -vy; // Reverse Y velocity
   }
}

Parameters Overview

ParameterDescriptionExample Value
Velocity XControls the ball's speed in the horizontal direction.2.5
Velocity YControls the ball's speed in the vertical direction.2.0
Ball SizeDefines the radius or diameter of the ball.48

Tips and Tricks

Did you know
This simulation is a basic form of motion physics used in most 2D games and animation engines.
Common Mistake
Forgetting to reverse the velocity component upon collision will result in the ball getting stuck at the wall.
Try It Yourself!
Experiment by changing velocity values or ball size to see how motion patterns change.
Success!
You've learned the basics of 2D motion and collision handling!