Perfect circle to perfect circle and Perfect circle to straight line collision HANDLING in Java

帅比萌擦擦* 提交于 2019-12-10 19:54:18

问题


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


回答1:


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):

top: (0,1)
bottom: (0,-1)
left: (1,0)
right: (-1,0)

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:

V2 = V1 - 2*N*(N.dot(V1))

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:

float nx = 0, ny = 1; // replace these with the normal of the wall you hit
float ix = ball.vx, iy = ball.vy; // incident vector
float dotprod = (nx*ix + ny*iy); // the dot product i mentioned earlier

ball.vx = ix - 2*nx*(dotprod);
ball.vy = iy - 2*ny*(dotprod);

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...




回答2:


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.



来源:https://stackoverflow.com/questions/3221472/perfect-circle-to-perfect-circle-and-perfect-circle-to-straight-line-collision-h

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!