So I have a system with colliding balls that generally works, except for when they collide with similar directions, less than 90 degrees apart.
This is because the ball above tries to collide against the yellow line which is supposedly the collision plane, but it sends it off the wrong direction, and it "follows" the other ball. The general algorithm for the collision is:
dot = direction.surface;
parallel = surface * dot;
perpendicular = direction - parallel;
direction = perpendicular - parallel;
Which negates the component of the direction parallel to the surface normal, which is perpendicular to the collision plane, and the part perpendicular to the surface normal is unchanged.
Does anyone know a fix for this? Have I done something wrong?
Edit: So now I added:
average = (ball1.velocity + ball2.velocity) / 2;
ball1.velocity -= average;
ball2.velocity -= average;
Before doing the calculations above, and after that:
ball1.velocity += average;
ball2.velocity += average;
To get in the right reference frame, according to @Beta's answer. The problem now is that the speeds of the balls aren't maintained, since they both have the same speeds and masses, yet after the collisions they're different. I do not think this is supposed to happen, or is it?