So, I am trying to apply response to my SAT, Circle - Poly, Poly - Poly collisions. I ported this code on this article to JavaScript:
http://rocketmandevelopment.com/blog/separation-of-axis-theorem-for-collision-detection/
Now, the detection works on all types, but the response 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
JSFiddle (gravity not applied for simulation, move with arrow keys), the first part in JS is the Vectors then the Physics and then the Main.
This is my definition of shapes: (had to add some code for "JSFiddle" link :P)
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 < vs.length; i++) {
area += (vs[j].x + vs[i].x) * (vs[j].y - vs[i].y);
j = i;
}
return (area / 2);
}
All Collision functions give failed response results so there must be some kind of connection.
Back to the angular velocity I know how to rotate polygons, but I am looking to get the new av after the collision:
px = x * cos(a) - y * sin(a);
py = x * sin(a) + y * cos(a);
Also I made a nice physics simulator for lines and beziers it might help: http://murplyx.net/projects/csb/
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.
EDIT: Added functions that calculate the area^2 to set the mass as equal.
270
-` 200` =70
(rep) O_o – Unquote