We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpPrograms › Fixing a Collision
Page Index Toggle Pages: 1
Fixing a Collision (Read 309 times)
Fixing a Collision
Jan 21st, 2009, 9:38pm
 
Hey all, wetting my feet by programming gravitation/circle collisions. This works alright but circles will often get stuck inside of each other, especially when there are a lot of them acting upon each other or when they're coming to a standstill.

Code:

void checkCollisions (ArrayList bodies) {
for (int i = 0; i < bodies.size(); i++) {
Body b = (Body) bodies.get(i);

if (b == this) continue;

PVector radius = PVector.sub(pos, b.pos);
if ( radius.mag() < (dia + b.dia)/2 ) {
PVector sNorm = radius.get();
sNorm.normalize();

sNorm.mult(2 * vel.dot(sNorm));
vel.sub(sNorm);
vel.mult(COR);
}
}
}


Where dia is the radius (float), vel is the velocity (PVector), and COR is the coefficient of restitution (float). This is called every frame, after acceleration and velocity have been calculated. Pretty standard, but obviously not robust. How can I improve it?
Page Index Toggle Pages: 1