We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hi, I'm not sure if this is the best spot to ask this, but I thought it might be worth a shot to see if the glitches in my code could be fixed.
I've been making a verlet physics program, but, instead of having actual shapes programmed, I built all the shapes out of constraints and have only point - constraint collisions. Right now I have a way to detect collisions:
void shapeShapeCollision(Atom a, Link b) {//!!!!!!!!!!!!!!!
if(b.bonk){
PVector futL = PVector.sub(b.b.pos.copy(),b.a.pos.copy());
PVector pasL = PVector.sub(b.b.ppos.copy(),b.a.ppos.copy());
PVector ppoint = PVector.sub(a.ppos.copy(),b.a.ppos.copy());
PVector point = PVector.sub(a.pos.copy(),b.a.pos.copy());
//this is translating,
// rotating and rescaling where the constraint was to where it will
//be and checks if the translated old point and the new point
//crossed the where the constraint is.
float mx = futL.x*pasL.x+futL.y*pasL.y;
float my = futL.y*pasL.x-futL.x*pasL.y;
float q = pasL.x*pasL.x+pasL.y*pasL.y;
PVector newppoint = new PVector(ppoint.x*mx-ppoint.y*my,ppoint.x*my+ppoint.y*mx);
newppoint.div(q);
if(acrossLine(newppoint,point,new PVector(0,0),futL)) {
if(acrossLine(new PVector(0,0),futL,newppoint,point)) {
b.hit(true);
collide(a,b);
}
}
}
}
void collide(Atom a, Link b) {
PVector aToPos = a.pos.copy();//this is from the constraint's left side to the point
aToPos.sub(b.a.pos);
PVector aTob = b.b.pos.copy();this is from the line's left to its right side
aTob.sub(b.a.pos);
float z = aTob.mag();
PVector substitute = aTob.copy();
PVector notNorm = PVector.mult(substitute.normalize(), (aTob.dot(aToPos))/aTob.mag());
float x = notNorm.mag();
PVector norm = PVector.sub(aToPos, notNorm);
float y = norm.mag();
float t = (z*z)/(2*(z*z-x*z+x*x))+.2;
a.move(PVector.mult(norm, -t));
b.moveA(PVector.mult(norm, (1-x/z)*t));
b.moveB(PVector.mult(norm, (x/z)*t));
}
These work well with a few shapes. Unfortunately, for more than three shapes composed of points and lines if one shape collides with another it could push that one into a third without detection, and, since I'm only checking collisions with constraints, the point becomes stuck in the square.
Additionally, every once in a while if two shapes collide too hard a 'black hole' forms, sucking everything into it. Finally, is it possible to make the squares less bouncy? dropping one onto the other really squishes the one on the bottom.
Thanks for your help!
If you need to see the sketch I can post everything later.
Answers
Hmm. Maybe if I clarify what my guess for the problem is. Is it possible, using verlet physics, to guarantee that every collision has been solved for and no objects are intersecting each frame?
don't expect us to be able to help you debug a program that we cannot run.
This is everything. I'm not sure if the forum will format it correctly, but, if you copy paste the above into processing, it should run.
If you know of a better way to give you it please tell me.
Edit: it won't format correctly, but, if I leave it as plain text, when you paste it into Processing everything will reformat correctly.
https://forum.Processing.org/two/discussion/15473/readme-how-to-format-code-and-text
Thanks, it's formatted now.
Well, since I still don't have any suggestions, I deleted some of the less consequential parts: