# Contact Solver problem

## Recommended Posts

Hi guys,

I am having some issues with a contact solver and I can't quite figure out what I am doing wrong in my physics simulation.

First, I will show you what works


/////////////////////
struct ContactInfo{
Vec2 normal;
float e;
float pen;

};

/////////////////////

class Contact{
public:
Contact(Rigid *b1, Rigid *b2, ContactInfo ci);

void solveImpulse();
void solvePenetration();
float calcSepVel();
private:
Rigid *b1;//holds two bodies
Rigid *b2;
ContactInfo ci;
};

/////////////////////

std::vector<Rigid*> bodies;

//missing lines
for(auto out=bodies.begin();out!=bodies.end();out++){
for(auto in=out+1;in!=bodies.end();in++){
ContactInfo ci;
if(someCollision()){
Contact contact((*out),(*in),ci);
contact.solveImpulse();
contact.solvePenetration();
}
}

}


Using the above algo, everything works fine

However, I need to solve each frame int the following manner

		std::vector<Rigid*> bodies;
std::vector<Contact> contacts;
//missing lines

contacts.clear();
for(auto out=bodies.begin();out!=bodies.end();out++){
for(auto in=out+1;in!=bodies.end();in++){

ContactInfo ci;
if(someCollision())){

contacts.push_back(Contact((*out),(*in),ci));
}
}

}

for(auto it=contacts.begin();it!=contacts.end();it++){
it->solveImpulse();
}
for(auto it=contacts.begin();it!=contacts.end();it++){
it->solvePenetration();
}


Basically, I will be adding other collisions, such as cables, and I need to solve all contacts in one loop after all collisions from whatever physical source are added to the collision vector.

The second way is adding the contacts to the vector correctly, but it's not solving them accuretly. I have shapes falling through each other and only the first contact appears to be solved (temporarly).

If any one has any ideas what I am doing wrong please let me know.

Thanks,

Mike

##### Share on other sites
Randy Gaul    2762

Your loops look fine, so you must have bugs in code not shown here.

##### Share on other sites
Irlan    4067

for(auto it=contacts.begin();it!=contacts.end();it++){
it->solveImpulse();
}
for(auto it=contacts.begin();it!=contacts.end();it++){
it->solvePenetration();
}

That look suspecious. Try to solve all penetrations first and later apply the impulses to the rigid bodies.

You should clear the contacts batch after solving it not before (altough doesn't change the strategy but it's whats makes sense).

You need to tell us how you're resolving the contacts. Since you said that the first example was correct, this shouldn't be a problem.

Also, try to post physics topics in Math and Physics section on the next time (you'll be receiving a quick feedback there!)

Edited by Irlan