3 - Ball Acceleration

Introduction

This simulation demonstrates the physics behind the Ball Acceleration towards the target - in this scenario, the mouse pointer.

Concept of Acceleration

Acceleration is the rate of change of velocity of an object. Unlike the bouncing ball, which moves at a constant speed or velocity, this ball gradually increases its speed towards the mouse position. The closer it gets, the smaller the acceleration becomes.

  1. Velocity determines how fast the ball is moving per frame.
  2. Acceleration changes the velocity over time, allowing smooth movement towards the target.
  3. DIrection is determined by the vector from the ball to the mouse position.

Physics Behind It

  • The ball calculates the direction vector between its current position and the mouse position.
  • This direction is normalized (made unit length).
  • Acceleration is applied along that direction to update velocity
v=awherea=(mouse×arate)\vec{v} = \vec{a} \quad\text{where}\quad \vec{a} = (\text{mouse} - \times a_{rate})
The ball stops accelerating when it reaches the defined maximum speed (Max speed)

Code Example

let x = 100, y = 100;
let vx = 0, vy = 0;
let ax = 0, ay = 0;
const a_rate = 0.1; // Acceleration rate
const maxSpeed = 5; // Maximum speed

function draw(mouseX, mouseY){
   // Calculate direction to mouse
   let dirX = mouseX - x;
   let dirY = mouseY - y;
   let length = Math.sqrt(dirX * dirX + dirY * dirY);

   // Normalize direction and apply acceleration
   ax = (dirX / length) * a_rate;
   ay = (dirY / length) * a_rate;
  vx += ax;
  vy += ay;

   // Limit speed to maxSpeed (limiting velocity to max speed)
   let speed = Math.sqrt(vx * vx + vy * vy);
   if(speed > maxSpeed) {
      vx = (vx / speed) * maxSpeed;
      vy = (vy / speed) * maxSpeed;
   }

   // Update position
   x += vx;
   y += vy;
}

Parameters Overview

ParameterDescriptionExample Value
Ball SizeDetermines the visual size of moving ball.48
Max SpeedSets the highest velocity the ball can reach.5
Acceleration RateControls how quickly the ball accelerates towards the mouse.0.1

Tips and Tricks

Fun Fact
This concept of acceleration towards a target is widely used in game development for character movement and AI behavior like homing missiles or character following certain behavior in games.
Attention!
If acceleration rate is too high, the ball may overshoot the target and oscillate around it instead of smoothly approaching.
Try This!
Experiment with different acceleration rates and max speeds to see how they affect the ball's movement dynamics.
You Did It!
You now understand how acceleration and direction combine to create realistic motion physics.