Collision in 2D

This is an example of doing collision in 2D.

There are a bunch of balls that collide against each other and the edges of the screen.

To use it you would have to convert it to 3D. You will probably want to do most collision in the x and z axes and then do collisions with the floor (a constant y value?)

To collide against a box, just reverse the code for colliding against walls.

The ball class

class Ball
  // position
  PVector pos;
  // velocity 
  PVector vel;
  // size (radius)
  float sz;
 // constructor
  Ball(PVector p, PVector v, float s)
    pos = p;
    vel = v;
    sz = s;
 // collision with the sides of the screen
 // h is the height w is the width
 void collideWalls(float h, float w)
   // test the x-position
   // if it is less than 0 or greater than w it is off screen
   // and we need to do something
   // add and subtract sz to make sure you are 
   // colliding the edges rather than the middle 
   // of the ball
   if((pos.x - sz) < 0 || (pos.x + sz) > w)
     // if you have a collision you reverse the
     // velocity in the x direction
     vel.x = -vel.x;
   // do the same for y
   if((pos.y - sz) < 0 || (pos.y + sz) > h)
     vel.y = -vel.y;
 // collide against other balls
 void collideBalls(Ball balls[])
    for (int i = 0; i < balls.length; i++)
        // check if the distance between the centres is less 
        // than the sum of the sizes of the balls
        // if so you have a collision 
        if(dist(pos.x, pos.y, balls[i].pos.x, balls[i].pos.y) < sz+ balls[i].sz)
          // accelerate the ball away from the collision
          // that means along the line from the other ball 
          // to the centre of this ball
          PVector acceleration = PVector.sub(pos, balls[i].pos);
          // get it proportional to the velocity
          // first normalise it
          // the dot product gets the velocity in the direction of 
          // the acceleration
          float speed =;
          // set the acceleration to minus twice that speed
          // (to cancel out the existing velocity and 
          // then add a negative speed)
          // add it to the velocity
 void updatePos()
 void draw()
   ellipseMode (CENTER);
    ellipse (pos.x, pos.y, sz, sz);

The application

// an array of objects
Ball balls[];
void setup()
   size(800, 800);
   balls = new Ball[20]; 
   // the size of the balls
   float sz = 10.0f;
   // create a bunch of balls with random positions and velocities
   for (int i = 0; i < balls.length; i++)
      PVector pos = new PVector(random(sz, width-sz), random(sz, height-sz));
      PVector vel = new PVector(random(-2, 2), random(-2, 2));
      balls[i] = new Ball(pos, vel, sz);
void draw()
   // cycle through all the balls doing the operations
   for (int i = 0; i < balls.length; i++)
       // colision with the edges of the screen
       balls[i].collideWalls(height, width);
       // collision with other balls
       // move the balls
       // draw them
Create PDF    Send article as PDF to