b2GravityController.prototype.Step = function (step) {
var i = null;
var body1 = null;
var p1 = null;
var mass1 = 0;
var j = null;
var body2 = null;
var p2 = null;
var dx = 0;
var dy = 0;
var r2 = 0;
var f = null;
if (this.invSqr) {
for (i = this.m_bodyList;
i; i = i.nextBody) {
body1 = i.body;
p1 = body1.GetWorldCenter();
mass1 = body1.GetMass();
for (j = this.m_bodyList;
j != i; j = j.nextBody) {
body2 = j.body;
p2 = body2.GetWorldCenter();
dx = p2.x - p1.x;
dy = p2.y - p1.y;
r2 = dx * dx + dy * dy;
if (r2 < Number.MIN_VALUE) continue;
f = new b2Vec2(dx, dy);
f.Multiply(this.G / r2 / Math.sqrt(r2) * mass1 * body2.GetMass());
if (body1.IsAwake()) body1.ApplyForce(f, p1);
f.Multiply((-1));
if (body2.IsAwake()) body2.ApplyForce(f, p2);
}
}
}
else {
for (i = this.m_bodyList;
i; i = i.nextBody) {
body1 = i.body;
p1 = body1.GetWorldCenter();
mass1 = body1.GetMass();
for (j = this.m_bodyList;
j != i; j = j.nextBody) {
body2 = j.body;
p2 = body2.GetWorldCenter();
dx = p2.x - p1.x;
dy = p2.y - p1.y;
r2 = dx * dx + dy * dy;
if (r2 < Number.MIN_VALUE) continue;
f = new b2Vec2(dx, dy);
f.Multiply(this.G / r2 * mass1 * body2.GetMass());
if (body1.IsAwake()) body1.ApplyForce(f, p1);
f.Multiply((-1));
if (body2.IsAwake()) body2.ApplyForce(f, p2);
}
}
}
}
The code is used to simulating the universal gravitation in the box2dweb. Why there are two different ways to calculate the universal gravitation? The if statement is used for what? And i have seen this on the internet invSqr: Boolean; /// If true, gravity is proportional to r^-2, otherwise r^-1
, but i dont understant what this invsqr means.
You can see the main difference is this line
Its to avoid a costly
sqrt
and subsequent normalisation in all the physics calculations. Without understanding the library in question, I can't explain to you the ramifications of doing this.