# Impulse on multiple object

This topic is 2179 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I am new here, hi to everyone

I made my physic engine and I finally reach the point where object resting on top of each other produce some kind of anomally which I kinda know what the cause is.

I try a few method from this series

http://gamedev.tutsplus.com/tutorials/implementation/create-custom-2d-physics-engine-aabb-circle-impulse-resolution/

http://www.gamedev.net/page/resources/_/technical/game-programming/swept-aabb-collision-detection-and-response-r3084

But I kinda one of some little part of each tutorial and I even have my own method in some small part as well.

So I cannot really say which tutorial I follow..

I took ResolveCollision function from the first tutorial

void ResolveCollision( Object A, Object B )
{
// Calculate relative velocity
Vec2 rv = B.velocity - A.velocity

// Calculate relative velocity in terms of the normal direction
float velAlongNormal = DotProduct( rv, normal )

// Do not resolve if velocities are separating
if(velAlongNormal > 0)
return;

// Calculate restitution
float e = min( A.restitution, B.restitution)

// Calculate impulse scalar
float j = -(1 + e) * velAlongNormal
j /= 1 / A.mass + 1 / B.mass

// Apply impulse
Vec2 impulse = j * normal
A.velocity -= 1 / A.mass * impulse
B.velocity += 1 / B.mass * impulse
}


I use this algorithm to solve impulse problem between 2 object

But the problem is when 1 object collide to another object and then that other object collide with another object and so on...

if it's just one object with another it's not so hard

but if one object collide with 2 or more object simultaneously there occur a problem I cannot solve,

I don't know, I haven't learn physic to that level

Can someone help me ?

##### Share on other sites

maybe this should do the trick the interaction between all objects:

void __fastcall TGLgalaxy::movegals() {
try {
//lastcenter = center;
//								 center.x = 0.0;
//								 center.y = 0.0;
//								 center.z = 0.0;
int i, j;
float ep_t_mass;

for(i = 0; i < n; i++) {
for(j = 0; j< n; j++) {

if(j == i)
continue;
//jezeli cos do siebie dobija to: :)
//	co  ma mniejsza mase = acc czyli przyspieszenia :) accumulation :)
x1 = s[i].pos.x - s[j].pos.x;
x2 = s[i].pos.y - s[j].pos.y;
x3 = s[i].pos.z - s[j].pos.z;

long double f = x1*x1+x2*x2+x3*x3;
if (f>0.0)
f = sqrtl(f); else f = 1.0;

d = f;

if(d > 10)      //-0.001
ep_t_mass = -0.001*G*(s[j].mass/(d*d));
else
ep_t_mass = 0;
//						ep_t_mass = +(G*s[j].mass/(d*d));// / 2.00f;

s[i].acc.x = ep_t_mass * x1;
s[i].acc.y = ep_t_mass * x2;
s[i].acc.z = ep_t_mass * x3;

s[i].vel.x += s[i].acc.x;
s[i].vel.y += s[i].acc.y;
s[i].vel.z += s[i].acc.z;

s[i].pos.x += s[i].vel.x;
s[i].pos.y += s[i].vel.y;
s[i].pos.z += s[i].vel.z;

}

//		center = vector_multiple(center,1.0f/float(n));
}

} catch (...) {

}

}


you see there that there are two loops each goes through all objects (except that second cant interact with itself)

##### Share on other sites

I don't quite understand this code

This part is

-0.001*G*(s[j].mass/(d*d))


I don't know what this equation is

I shall re explain some part that might be ambigious

I've built many parts of my physic engine and I have find a way to know which object collide with whichs on which side..

The problem is only when one object collide with more than 2 objects or

one object collide with another and that other object collide with yet another object...

The problem is in the equation I think

The problem only occur when that case I mention above

I can always make a game where it doesn't happen

but I want this problem ( Bug ) fixed

I am bit frustrated, I making building this physic engine while cutting corners everywhere and yet it's not up to the point where I want it to be

I still have many things on hand like A.I. , Animation, The viewport and other GL Stuff, Responsive background music, sound Fx, Database and much more ...

I am running a bit late on the schedule

I think that building this physic engine take something like a month, but it's been 5 weeks,,,

sorry for making you listen to my frustration...

Edited by rmxhaha

##### Share on other sites

i dont know if it help but if you have code for collision between more than 2 objects you should do instancing, find the first object that colliudes \with another, collide with it, then apply collision between another object to collided model, but i think you didnt read whole article and ou use just only one piece of code.

##### Share on other sites

I am new here, hi to everyone

I made my physic engine and I finally reach the point where object resting on top of each other produce some kind of anomally which I kinda know what the cause is.

I try a few method from this series

http://gamedev.tutsplus.com/tutorials/implementation/create-custom-2d-physics-engine-aabb-circle-impulse-resolution/

http://www.gamedev.net/page/resources/_/technical/game-programming/swept-aabb-collision-detection-and-response-r3084

But I kinda one of some little part of each tutorial and I even have my own method in some small part as well.

So I cannot really say which tutorial I follow..

I took ResolveCollision function from the first tutorial

void ResolveCollision( Object A, Object B )
{
// Calculate relative velocity
Vec2 rv = B.velocity - A.velocity

// Calculate relative velocity in terms of the normal direction
float velAlongNormal = DotProduct( rv, normal )

// Do not resolve if velocities are separating
if(velAlongNormal > 0)
return;

// Calculate restitution
float e = min( A.restitution, B.restitution)

// Calculate impulse scalar
float j = -(1 + e) * velAlongNormal
j /= 1 / A.mass + 1 / B.mass

// Apply impulse
Vec2 impulse = j * normal
A.velocity -= 1 / A.mass * impulse
B.velocity += 1 / B.mass * impulse
}


I use this algorithm to solve impulse problem between 2 object

But the problem is when 1 object collide to another object and then that other object collide with another object and so on...

if it's just one object with another it's not so hard

but if one object collide with 2 or more object simultaneously there occur a problem I cannot solve,

I don't know, I haven't learn physic to that level

Can someone help me ?

You almost got it.  Just 2 things missing.

1)  An array/list of contact point information (contact normal, penetration depth, the objects involved, etc.)

2)  Solve each contact, several times.

Assuming "contactData" is the array of contacts, pseudocode would be:

for ( int iteration = 0; iteration < maxIterations; ++iteration )

{

for ( int contactPoint = 0; contactPoint < numberOfContactPoints; ++contactPoint )

{

Object& a = contactData[ contactPoint ].ObjectA;

Object& b = contactData[ contactPoint ].ObjectB;

ResolveCollision( a, b );

}

}

##### Share on other sites

I don't quite understand this code

This part is

-0.001*G*(s[j].mass/(d*d))


I don't know what this equation is

From over here, it appears to be integrating force ( f=ma = kg*m/s^2)

• 18
• 18
• 11
• 21
• 16