<commit_before>window.physics = ->
  window.stats = Utils.drawStats()
  window.ctx = Sketch.create
    element: document.getElementById "physics"
    # retina: true
  window.grid = new Grid 100, "#056"
  controls = new KeyboardControls

  window.rect = new Rectangle 100, 100, [0, 200], 0.3, "#F00"
  rect.inverseMass = 1/10
  floor = new Rectangle 800, 10, [0, -5], 0, "#888"
  paused = false

  _.extend ctx,
    update: ->
      rect.reset()
      rect.resetDebug()
      contacts = rect.contactPoints(floor)

      unless paused
        rect.integrate ctx.dt / 1000, controls

      if contacts.length > 0
        unless paused
          for c, i in contacts
            console.log "contact #{i}", c.normal, c.depth
        paused = true
    draw: ->
      grid.draw ctx
      rect.draw ctx
      floor.draw ctx
      rect.drawDebug ctx
      stats.update()
    clear: ->
      ctx.clearRect -ctx.width/2, -ctx.height/2, ctx.width, ctx.height
    keyup: (e) ->
      if e.keyCode is 32 # space
        paused = false
      controls.keyup e
    keydown: controls.keydown

  # | a c e |    |  1  0  0 |
  # | b d f | => |  0 -1  0 |
  # | 0 0 1 |    |  0  0  1 |
  ctx.setTransform 1, 0, 0, -1, 0, 0 # flip y axis so it goes up (and z goes out)
  ctx.translate ctx.width/2, -ctx.height/2 # center the origin

class Edge
  constructor: (@deepest, @from, @to) ->
    @vec = Vec.sub @to, @from
  dot: (other) ->
    Vec.dotProduct @vec, other
  normalize: ->
    @vec = Vec.normalize @vec
  invert: ->
    @vec = Vec.invert @vec

class Polygon
  position: [0, 0]
  rotation: [1, 0] # Rotation.fromAngle(0)

  velocity: [0, 0]
  angularVelocity: 0
  inverseMass: 0 # required value
  inverseMoment: 0 # calculated value

  damping: 0.999 # minimal

  vertices: -> []

  color: "#CCC"
  debug: {}

  reset: -> # reset caches, forces, etc.

  integrate: (dt, controls) ->
    return if @inverseMass <= 0 or dt <= 0

    @position = Vec.add @position, Vec.scale @velocity, dt
    acceleration = [0, -15]
    @velocity = Vec.add @velocity, Vec.scale acceleration, dt
    @velocity = Vec.scale @velocity, Math.pow(@damping, dt)



  draw: (ctx) -><commit_msg>Better gravity. Need a scale factor (pixels != meters)<commit_after>window.physics = ->
  window.stats = Utils.drawStats()
  window.ctx = Sketch.create
    element: document.getElementById "physics"
    # retina: true
  window.grid = new Grid 100, "#056"
  controls = new KeyboardControls

  window.rect = new Rectangle 100, 100, [0, 200], 0.3, "#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
      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
    draw: ->
      grid.draw ctx
      rect.draw ctx
      floor.draw ctx
      rect.drawDebug ctx
      stats.update()
    clear: ->
      ctx.clearRect -ctx.width/2, -ctx.height/2, ctx.width, ctx.height
    keyup: (e) ->
      if e.keyCode is 32 # space
        paused = false
      controls.keyup e
    keydown: controls.keydown

  # | a c e |    |  1  0  0 |
  # | b d f | => |  0 -1  0 |
  # | 0 0 1 |    |  0  0  1 |
  ctx.setTransform 1, 0, 0, -1, 0, 0 # flip y axis so it goes up (and z goes out)
  ctx.translate ctx.width/2, -ctx.height/2 # center the origin

class Edge
  constructor: (@deepest, @from, @to) ->
    @vec = Vec.sub @to, @from
  dot: (other) ->
    Vec.dotProduct @vec, other
  normalize: ->
    @vec = Vec.normalize @vec
  invert: ->
    @vec = Vec.invert @vec

class Polygon
  position: [0, 0]
  rotation: [1, 0] # Rotation.fromAngle(0)

  velocity: [0, 0]
  angularVelocity: 0
  inverseMass: 0 # required value
  inverseMoment: 0 # calculated value

  acceleration: [0, -100] # gravity
  damping: 0.999 # minimal

  vertices: -> []

  color: "#CCC"
  debug: {}

  reset: -> # reset caches, forces, etc.

  integrate: (dt, controls) ->
    return if @inverseMass <= 0 or dt <= 0

    @position = Vec.add @position, Vec.scale @velocity, dt


    @velocity = Vec.add @velocity, Vec.scale @acceleration, dt
    @velocity = Vec.scale @velocity, Math.pow(@damping, dt)

  draw: (ctx) ->
{'fraction_non_alphanumeric': 0.13429752066115702, 'fraction_numerical': 0.03787878787878788, 'mean_word_length': 2.293272864701436, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 33, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '619589', 'n_tokens_mistral': 1646, 'n_tokens_neox': 1495, 'n_words': 468}