title: JavaScript - Separating Axis Theorem - Collision working, but not Response?
body: <p>So, I am trying to apply response to my SAT, Circle - Poly, Poly - Poly collisions. I ported this code on this article to JavaScript:</p>
  
  <p><a href="http://rocketmandevelopment.com/blog/separation-of-axis-theorem-for-collision-detection/" rel="nofollow">http://rocketmandevelopment.com/blog/separation-of-axis-theorem-for-collision-detection/</a></p>
  
  <p>Now, the <em>detection</em> works on all types, but the <em>response</em> fails and goes with an insane speed and in wrong angle, it's not dependent by the mass of the objects (area^2 instead of mass) and angular velocity isn't applied</p>
  
  <p><a href="http://jsfiddle.net/7Lx6h/" rel="nofollow">JSFiddle</a> (gravity not applied for simulation, move with arrow keys), the first part in JS is the Vectors then the Physics and then the Main.</p>
  
  <p>This is my definition of shapes: (had to add some code for "JSFiddle" link :P)</p>
  
  <pre><code>var Circle = function(body, c, r, cor, cof) {
      this.body = body // Static or dynamic
      this.c = c; // Center
      this.r = r; // Radius
      this.m = getCMass(r); // Mass = Area
      this.v = new Vector(); // Velocity
      this.cor = cor; // Coefficient of restitution
      this.cof = cof; // Coefficient of friction
      this.a = 0; // Angle
      this.av = 0; // Angular velocity
      this.type = "Circle";
  }
  
  var Polygon = function(body, c, vs, cor, cof) {
      this.body = body // Static or dynamic
      this.c = c; // Center
      this.vs = vs; // Vertices
      this.m = getPMass(vs); // Mass = Area
      this.v = new Vector(); // Velocity
      this.cor = cor; // Coefficient of restitution
      this.cof = cof; // Coefficient of friction
      this.a = 0; // Angle
      this.av = 0; // Angular velocity
      this.type = "Polygon";
  }
  
  function getCMass(r) { // More like, getCArea
      return (r * r * Math.PI);
  }
  
  function getPMass(vs) {
      var area = 0;
      var j = vs.length - 1;
  
      for (var i = 0; i &lt; vs.length; i++) {
          area += (vs[j].x + vs[i].x) * (vs[j].y - vs[i].y); 
          j = i;
      }
  
      return (area / 2);
  }
  </code></pre>
  
  <p>All Collision functions give failed <em>response</em> results so there must be some kind of connection.</p>
  
  <p>Back to the angular velocity I know how to rotate polygons, but I am looking to get the new av after the collision:</p>
  
  <pre><code>px = x * cos(a) - y * sin(a); 
  py = x * sin(a) + y * cos(a);
  </code></pre>
  
  <p>Also I made a nice physics simulator for lines and beziers it might help: <a href="http://murplyx.net/projects/csb/" rel="nofollow">http://murplyx.net/projects/csb/</a></p>
  
  <p>So the most important part in this for me is how am I going to fix so that the velocity goes correctly by speed and angle? (I am using Vectors not Trigonometry -.-) Then I can think about mass and angular velocities. Thanks. I stay out of the Box2D box.</p>
  
  <p>EDIT: Added functions that calculate the area^2 to set the mass as equal.</p>