Does anyone see anything wrong with the following code? There's something wrong with my normals, they don't bounce properly.
EDIT
The highlighted lines are wrong. (Because the DotProduct function was wrong)
I would suspect the problem to be here :
if (I1M > 0.0) { /// Magnitude of N is always 1, they're normalizedcosA1 = DotProduct(I1 , N1)/I1M;} if (I2M > 0.0) {/// Object has momentum /// Magnitude of N is always 1, they're normalizedcosA2 = DotProduct(I2 , N2)/I2M;} /// Momentum of circle one in the normal directionVec2 I1N = N1*cosA1*I1M;/// Momentum of circle two in the normal directionVec2 I2N = N2*cosA2*I2M;
I’m a little thrown by your instance variables starting with capitals. You should consider all class names with capitals and instances with lowercase.
Too drunk to actually think math, but you might consider breaking up parts of your function into smaller parts and testing on them individually. There’s a lot of potential points of failure here and if you can narrow it down with confidence then you can eliminate the guessing and checking.
I’m a little thrown by your instance variables starting with capitals. You should consider all class names with capitals and instances with lowercase.
Next time I'll consult a style guide.
They're mathematical variables, and constants. Usually I DO use lowercase for variables, but in this case, I felt it made things clearer, not worse.
The real clincher is, does the projection of A unto B really equal ^B*|A|(A.B)?
Ah hah ha hha haahahah hhaaaaaaaa I fixed it.
First, my dot product was wrong. I was doing some bizarre hybrid of a cross product and a dot product.
Second, you don't need to multiply by the |A|.
The projection of A unto B really is as simple as ^B*(A.^B).
Here's a little demo program to play with :
Download win32 binary here (CVC.7z).
{"name":"611744","src":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/8\/9\/89998192365f296251917fb8ccf783fe.png","w":802,"h":633,"tn":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/8\/9\/89998192365f296251917fb8ccf783fe"}
There's a little problem of balls escaping the pen every once in a while, and I have to figure that out next.
If anyone wants to check out the source code, it's on GitHub here :
So, for the benefit of others (obviously I know
) which line was incorrect in your original code?
So the problem was DotProduct. It returned x1y2 + x2y1, which is nonsense. It should be x1x2 + y1*y2.
The updated code is here :
https://github.com/EdgarReynaldo/Interceptor
https://github.com/EdgarReynaldo/Interceptor/blob/master/Circle.cpp
The collision detection is super easy. Here is the code :
And the new code for bouncing is here :
The code for ScalarProjection is here :
Vec2 ScalarProjection(Vec2 A , Vec2 B) { B.Normalize(); return B*DotProduct(A,B); }
And the code for DotProduct is here :
inline double DotProduct(const Vec2& v1 , const Vec2& v2) { return v1.x*v2.x + v1.y*v2.y; }
What's really interesting though, is the collision table. Check it out :
https://github.com/EdgarReynaldo/Interceptor/blob/master/CollTable.cpp
https://github.com/EdgarReynaldo/Interceptor/blob/master/CollTable.hpp
It is a vector of pairs of circles, representing each possible combination of N circles. It is exactly (N*(N-1))/2 in size. If you're doing a 1000 circle collision resolution, it will make 499,500 pairs. But the beauty is you only have to recalculate it if the velocity of one of the circles changes. This eliminates 99% of the wasted calculations of doing a frame by frame overlap check.
EDIT
{"name":"611747","src":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/6\/3\/635673b9af24f3c2c81797b18d2333ee.png","w":1027,"h":800,"tn":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/6\/3\/635673b9af24f3c2c81797b18d2333ee"}
You'll notice that balls 13 and 14 are missing. They escaped. 
{"name":"611748","src":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/c\/9\/c9d7ea71f2e32e14391e295f5548be94.png","w":1027,"h":800,"tn":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/c\/9\/c9d7ea71f2e32e14391e295f5548be94"}
The fuller it is the more often they escape.
It's really quite stable, except for the escaping bit.
Aha! I fixed it. No more escape for you my pretties. 
{"name":"611749","src":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/a\/a\/aab5277509ad27b99a5096f0cd412ec9.png","w":1026,"h":801,"tn":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/a\/a\/aab5277509ad27b99a5096f0cd412ec9"}
The problem was in CollTable.cpp on line 114 :