Michael Chuah's page for MAS.864 The Nature of Mathematical Modelling

mcx "at" mit "dot" edu

Home

Introduction - Bouncing Balls

Bouncing Ball using MATLAB

BallMatlab
close all; clear all; clc;

graymap = [1:1000;1:1000;1:1000]'/1000;
graymap = flipud(graymap);
figure; set(gcf,'DefaultAxesColorOrder',graymap);

pos = 8;
v = 0;
g = -9.81;
cor = 0.8;
dt = 0.01;
i = 1;
x = [1:1000]/100;
while(1)
    pos = pos+v*dt;
    v = v+g*dt;
    if pos<=0
        v = -v*cor;
%         pos = 0;
    end
    plot(x(i),pos,'o','Color',graymap(i,:));
    xlim([-1,11]); ylim([-1,10]);
    drawnow; hold on;
    pause(dt/2);
    i=i+1;
    if i > 1000
        break;
    end
end

Bouncing Ball using Python

BallPython
# Adapted from Visual Python bounce.py example
from visual import *

dt = 0.01
g = 9.81
y = 10
COR = 0.9 # Coefficient of restitution
##bounce = False

scene = display(title = 'Ball Python', x=0, y=0, width=500, height=500, center=(0,5,1))
floor = box(length=10, height=0.5, width=10, color = color.cyan, material = materials.marble)
ball = sphere(pos=(0,y,0), radius = 1, color = color.orange, material = materials.chrome)
ball.velocity = vector(0,0,0)

while 1:
    rate(100)
    ball.pos = ball.pos + ball.velocity*dt
##    if ball.velocity > 0:
##        bounce = True
    if ball.y < 1:
        ball.velocity.y = -ball.velocity.y*COR
        ball.y = 1
##        if bounce :
##            ball.velocity.y = -ball.velocity.y*COR
##            bounce = False
##        else:
##            ball.color = color.red
    else:
        ball.velocity.y = ball.velocity.y - g*dt

Bouncing Ball using Processing

BallProcessing
PVector pos;
PVector v;
PVector g;
int r = 50;
int h = 500;
float COR = 0.8;

void setup() {
  size(600, 600, P3D);
  smooth();
  pos = new PVector(0, 100, 0);
  v = new PVector(0, 0, 0);
  g = new PVector(0, 9.81, 0);
}

void draw() {
  background(0);
  lights();
  
  pos.add(v);
  v.add(g);
  
  if(pos.y > h){
    if(v.y < 50){
      v.y = 0; // prevent permabounce
    }
    v.y = -v.y*COR;
    // pos.y = h;
  }
  
   noStroke();
   translate(300,pos.y,0);
   sphere(r);
//  stroke(255);
//  strokeWeight(1);
//  fill(127);
//  ellipse(300, pos.y, r,r);
}