I am a newbie at Java, but decided to make a application that has a bunch of balls bouncing around. Please keep in mind that I know almost nothing about normals, which I have seen mentioned a lot in this kind of thread. I have also only take Algebra 1, and know a little bit of trig (sin, cosin and tangent). Anyways...
I have collision detection covered, using
if (java.awt.geom.Point2D.distance(X1, Y1, X2, Y2) < ball1.radius + ball2.radius) {return true;} else {return false;}
and if (ball.x + ball.radius > getWidth) {COLLISION}
and so on for all four walls
What I need to do is handle these collisions in a semi realistic manner beyond switching the velocities and directions.
Everything is drawn in a JPanel
Thanks for your help in advance
You're going to have to understand what a normal is in order to calculate the reflection vector on collison. Fortunately, it's not very hard to understand. For the math heads out there, I'm going to be a little imprecise, so please don't beat me up over it. :)
A normal is simply a unit vector (unit: having magnitude = 1) that's orthogonal (at a 90-degree angle) to a surface. You can visualize a normal as an arrow sticking straight out of a surface. Since your walls aren't slanted, figuring out the normals for them is easy (assuming the top-left corner of your screen is 0,0 here):
What we need to do is take the velocity of your ball and "reflect" it along the normal vector for the wall that you hit. The formula for reflection is the following:
Where V1 is your incident vector (your old velocity in this case), N is the normal for the wall that we hit, and V2 is your reflected vector (your new velocity). "N.dot(V1)" is the "dot product" of N and V1, which is just (N.xV1.x + N.yV1.y).
A picture from Wikipedia of what we're doing (P is the incident vector, Q is the reflected vector):
Here's the whole thing in psuedocode:
Let me know if anything's unclear. :) Also, while this is kind of overkill for the walls, you will need to compute the normal when you do ball-ball collisions, so it's not a total waste...
You might like the article 2-Dimensional Elastic Collisions without Trigonometry, which discusses elastic collisions using vectors. This kinetic model implements the approach in Java.