Java中的完美圆到完美圆和完美圆到直线碰撞处理
我是 Java 新手,但决定制作一个有一堆弹跳球的应用程序。请记住,我对法线几乎一无所知,我在此类帖子中看到了很多提及的内容。我也只学过代数 1,并且了解一点三角函数(正弦、余弦和正切)。无论如何......
我已经涵盖了碰撞检测,使用if (java.awt.geom.Point2D.distance(X1, Y1, X2, Y2) < ball1.radius + ball2.radius) {返回 true;} else {返回 false;}
和 if (ball.x + ball.radius > getWidth) {COLLISION}
等等,对于所有四堵墙
我需要做的是以半现实的方式处理这些碰撞,而不仅仅是切换速度和方向。
一切都在 JPanel 中绘制,
提前感谢您的帮助
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, usingif (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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
为了计算碰撞上的反射矢量,您必须了解法线是什么。幸运的是,这并不难理解。对于那些数学头脑的人来说,我会有点不精确,所以请不要为此责备我。 :)
法线只是一个与表面正交(呈 90 度角)的单位向量(单位:大小 = 1)。您可以将法线想象为直接从表面伸出的箭头。由于您的墙壁没有倾斜,因此计算出它们的法线很容易(假设屏幕的左上角为 0,0):
我们需要做的是获取球的速度并“反射”它沿着你撞到的墙壁的法线向量。反射的公式如下:
其中 V1 是您的入射矢量(在本例中是您的旧速度),N 是我们撞击的墙壁的法线,V2 是您的反射矢量(您的新速度)。 “N.dot(V1)”是N和V1的“点积”,即(NxV1.x + NyV1.y)。
来自维基百科的我们正在做的事情的图片(P 是入射向量,Q 是反射向量):
这是伪代码中的全部内容:
如果有任何不清楚的地方,请告诉我。 :) 另外,虽然这对墙壁来说有点过分了,但当你进行球与球碰撞时,你需要计算法线,所以这并不是完全浪费......
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...
您可能会喜欢这篇文章无三角学的二维弹性碰撞,其中讨论使用矢量的弹性碰撞。此动力学模型在 Java 中实现了该方法。
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.