I try to make a very simple 2d physic functions to solve rigidBody with with sphere particles as colliders,
I have so a rigidBody and particle class :
function _rigidBody () {
this._position = cc.p(0.0, 0.0);
this._angle = 0;
this._linearVelocity = cc.p(0.0, 0.0);
this._angularVelocity = 0.0;
this._force = cc.p(0.0, 0.0);
this._angularMoment = 0;
this._mass = 1.0;
this._particles = [];
}
function _particle () {
this._initialPosition = cc.p(0.0, 0.0);
this._relativePosition = cc.p(0.0, 0.0);
this._worldPosition = cc.p(0.0, 0.0);
this._linearVelocity = cc.p(0.0, 0.0);
this._force = cc.p(0.0, 0.0);
this._radius = 10.0;
}
and so basically, each frame I compute the new position of each particles store in each rigidBody, then I check for particle to particle collision and in case of collision, I compute a force to repulse them (base on radius and how munch there are inside them), then I store this force in the particle,
And I compute the LinearVelocity and AngularVelocity by adding and impulse to my rigidBody for all the force store on all particle it contain.
my function addImpulse (with impulse the force store in particle and position the world pos of my particle) :
this.AddImpulse = function(bodyIdx,impulse,position){
var b = g_Bodies[ bodyIdx ];
if(position == null){position = b._position;}
b._linearVelocity.x += (impulse.x/ b._mass);
b._linearVelocity.y += (impulse.y/ b._mass);
b._angularVelocity += ((position.x - b._position.x) * impulse.y -
(position.y - b._position.y) * impulse.x);
};
My problems is, this function works pretty fine for linearVelocity by for the angular velocity it seam to get really big at the most little collision, for now, I don't use any damping (linear or angular), and I don't have inertia for my angularVelocity,
I guess the miss of inertia is my problem, but I get a little confuse when I look in internet how to compute it,
I look in box2d to see were the inertia is store and it seams to be a variable of rigidBody (as mass).
I find in internet that the Inertia = mass * (r*r)
problem => what is r ...
-if is the distance for my gravity center (so the position of my rigidBody), is obviously equal to 0 all the time (body.position - body.position == 0 )
in box2d I found : this.m_I = massData.I - this.m_mass * (massData.center.x * massData.center.x + massData.center.y * massData.center.y); And massData.center seam to be the center of the shape from the rigidBody, problem in my case my shape is 2 sphere one at (0,10) and one at (0,-10), so the center of that is (0,0) ....
in an other website I found that : for(i < numberOfParticle) I += ( massOfParticle(i) * ( RelativePosOfParticle(i)*RelativePosOfParticle(i) ) In this case is better, because as I square the distance of each particle (instead of searching the center first), my I will be greater than 0. But so I'm not sure is the correct formula to use ...
If someone can explain me in a simple way, how to compute a Inertia ? If I can compute the inertia for my rigidBody directly (and not by particle) ? if I can store the inertia or if it deepens of where is apply the impulse (I read that also in an other website...) ? and/or why can I do wrong in my function of impulse to get a such big angular velocity (maybe is normal and the damping solve this problems is the others physic engine).
And also if someone could answer (just by curiosity), does box2d use a simple version of the inertia because is quicker to compute and the result are good enough ? Or does the calculation of Box2d is correct, and I miss something when I read the code ?
Thanks a lot ^^,
Marius.