   window.grid = new Grid 100, "#056"
   controls = new KeyboardControls
 
-  window.rect = new Rectangle 100, 100, [0, 200], 0.3, "#F00"
+  window.rect = new Rectangle 100, 100, [0, 80], 0.1, "#F00"
   rect.inverseMass = 1/10
   floor = new Rectangle 800, 10, [0, -5], 0, "#888"
-  paused = false
 
   _.extend ctx,
     update: ->
       rect.reset()
       rect.resetDebug()
 
-      unless paused
-        rect.integrate ctx.dt / 1000, controls
+      rect.integrate ctx.dt / 1000, controls
       contacts = rect.contactPoints(floor)
 
       if contacts.length > 0
-        unless paused
-          for c, i in contacts
-            console.log "contact #{i}", c.normal, c.depth
-        paused = true
+        for c, i in contacts
+          console.log "contact #{i}", c.normal, c.depth, c.from.velocity
+
+        for n in [1..contacts.length*2]
+          worst = null
+          for contact in contacts
+            if not worst or contact.separatingVelocity() < worst.separatingVelocity()
+              worst = contact
+          break if worst.separatingVelocity() >= 0
+          worst.resolve()
+
     draw: ->
       grid.draw ctx
       rect.draw ctx

    @cachedVertices ?= (Vec.transform offset, @position, @orientation for offset in @offsets)

class Contact
  constructor: (@position, @normal, @depth) ->

window.Rotation =
  fromAngle: (angle) -> [Math.cos(angle), Math.sin(angle)]

    clear: ->
      ctx.clearRect -ctx.width/2, -ctx.height/2, ctx.width, ctx.height
    keyup: (e) ->
      controls.keyup e
    keydown: controls.keydown

