Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Auto Save

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                
              
            
!

CSS

              
                html {
  overflow: hidden;
}

body {
  margin: 0px;
  background: black;
}
              
            
!

JS

              
                // Thanks to http://archive.ncsa.illinois.edu/Classes/MATH198/townsend/math.html
// for an introduction the maths behind this simulation

//////////////////////////////////////////////////////////////////////
// Framework

var nextEvent
var automata

function determine() {
  nextEvent = undefined
  
  automata.forEach(a => {
    let event = a.determine()
    if (event && (!nextEvent || event.dt < nextEvent.dt)) {
      nextEvent = event
    }
  })
}

function calcAutomata() {
  let t = spf
  while (!nextEvent || nextEvent.dt <= t) {
    automata.forEach(a => {
      a.advance(nextEvent.dt)
    })
    t -= nextEvent.dt
    nextEvent.notify()
    determine()
  }
  automata.forEach(a => {
    a.advance(t)
  })
  nextEvent.dt -= t
}

function drawAutomata() {
  automata.forEach(a => {
    point(a.pos.x, a.pos.y)
  })
}

//////////////////////////////////////////////////////////////////////
// Simulation

const epsilon = 1E-13
const radius = 30
const fps = 60
const spf = 1 / fps

class CollisionVEdge {
  constructor(dt, ball) {
    this.dt = dt
    this.ball = ball
  }
  
  notify() {
    this.ball.handleCollisionVEdge()
  }
}

class CollisionHEdge {
  constructor(dt, ball) {
    this.dt = dt
    this.ball = ball
  }
  
  notify() {
    this.ball.handleCollisionHEdge()
  }
}

class CollisionBalls {
  constructor(dt, ball1, ball2) {
    this.dt = dt
    this.ball1 = ball1
    this.ball2 = ball2
  }
  
  notify() {
    let copy = this.ball1.copy()
    this.ball1.handleCollisionBall(this.ball2)
    this.ball2.handleCollisionBall(copy)
  }
}

class Ball {
  constructor(pos, vel) {
    this.pos = pos
    this.vel = vel
  }
  
  copy() {
    return new Ball(this.pos.copy(), this.vel.copy()) 
  }
  
  determine() {
    let nextCollision
    
    [
      [this.pos.x, this.vel.x, CollisionVEdge],
      [width - this.pos.x, -this.vel.x, CollisionVEdge],
      [this.pos.y, this.vel.y, CollisionHEdge],
      [height - this.pos.y, -this.vel.y, CollisionHEdge]
    ].forEach(([d, v, C]) => {
      if (v < 0) {
        // Object is heading towards this edge
        if (d <= radius) {
          // Object has already hit this edge
          nextCollision = new C(0, this)
        } else {
          let dt = (radius - d) / v
          if (dt < epsilon) dt = 0
          if (!nextCollision || dt < nextCollision.dt) {
            nextCollision = new C(dt, this)
          }
        }
      }
    })
    
    let foundThis = false
    automata.forEach(b2 => {
      if (!foundThis) {
        if (b2 === this) {
          foundThis = true
        }
        return
      }
 
      let rp = this.pos.copy().sub(b2.pos)
      let rv = this.vel.copy().sub(b2.vel)
      let a = rv.dot(rv)
      let b = 2 * rp.dot(rv)
      let c = rp.dot(rp)
      if (b < 0) {
        // Objects are heading towards each other
        if (c <= 4 * radius * radius) {
          // Objects have already collided
          nextCollision = new CollisionBalls(0, this, b2)
        } else {
          let dt = (-b - Math.sqrt(b * b - 4 * a * (c - 4 * radius * radius))) / (2 * a)
          if (dt < epsilon) dt = 0
          if (!nextCollision || dt < nextCollision.dt) {
            nextCollision = new CollisionBalls(dt, this, b2)
          }
        }
      }
    })
                  
    return nextCollision
  }
  
  advance(dt) {
    let dp = this.vel.copy().mult(dt)
    this.pos.add(dp)
  }
  
  handleCollisionVEdge() {
    this.vel.x = -this.vel.x
  }
  
  handleCollisionHEdge() {
    this.vel.y = -this.vel.y
  }
  
  handleCollisionBall(ball2) {
    let n = this.pos.copy().sub(ball2.pos).normalize()
    let v2 = ball2.vel
    let v2n = n.copy().mult(v2.dot(n))
    let nn = n.copy().mult(-1)
    let v1n = nn.mult(this.vel.dot(nn))
    this.vel.sub(v1n).add(v2n)
  }
}

function initNBalls(n) {
  automata = [];
  for (let i = 0; i < n; i++) {
    automata.push(new Ball(
      createVector(width * 0.5, height * 0.5),
      createVector((Math.random() - 0.5) * 1200, (Math.random() - 0.5) * 1200)
    ))
  }
}

function init3Balls() {
  automata = [];
  automata.push(new Ball(
    createVector(width * 0.5 - 300, height * 0.5),
    createVector(300, 0)
  ))
  automata.push(new Ball(
    createVector(width * 0.5 + 300, height * 0.5),
    createVector(-300, 0)
  ))
  automata.push(new Ball(
    createVector(width * 0.5, height * 0.5),
    createVector(0, 0)
  ))
}

function init4Balls() {
  automata = [];
  automata.push(new Ball(
    createVector(width * 0.5 - 300, height * 0.5),
    createVector(300, 0)
  ))
  automata.push(new Ball(
    createVector(width * 0.5 + 300, height * 0.5),
    createVector(-300, 0)
  ))
  automata.push(new Ball(
    createVector(width * 0.5, height * 0.5 - 300),
    createVector(0, 300)
  ))
  automata.push(new Ball(
    createVector(width * 0.5, height * 0.5 + 300),
    createVector(0, -300)
  ))
}

function initNewton(n) {
  automata = [];
  for (let i = 0; i < 6; i++) {
    automata.push(new Ball(
      createVector((i - 2.5) * 2 * radius + width * 0.5, height * 0.5),
      createVector(0, 0)
    ))
  }
  for (let i = 0; i < n; i++) {
    automata[i].pos.x -= 100
    automata[i].vel.x = 1200
  }
}

function setup() {
  createCanvas(windowWidth, windowHeight)
  background(0)
	stroke(255,50,0)
	strokeWeight(2 * radius)
	noFill()
	frameRate(fps)
  
  initNBalls(10)
  //init3Balls()
  //init4Balls()
  //initNewton(1)
  //automata[2].pos.y += 1E-13
  
  determine()
  //noLoop()
}

function draw() {
	calcAutomata()
  background(0)
  drawAutomata()
}

              
            
!
999px

Console