Java中的完美圆到完美圆和完美圆到直线碰撞处理

发布于 2024-09-08 15:11:22 字数 441 浏览 5 评论 0原文

我是 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, 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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

表情可笑 2024-09-15 15:11:22

为了计算碰撞上的反射矢量,您必须了解法线是什么。幸运的是,这并不难理解。对于那些数学头脑的人来说,我会有点不精确,所以请不要为此责备我。 :)

法线只是一个与表面正交(呈 90 度角)的单位向量(单位:大小 = 1)。您可以将法线想象为直接从表面伸出的箭头。由于您的墙壁没有倾斜,因此计算出它们的法线很容易(假设屏幕的左上角为 0,0):

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

我们需要做的是获取球的速度并“反射”它沿着你撞到的墙壁的法线向量。反射的公式如下:

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

其中 V1 是您的入射矢量(在本例中是您的旧速度),N 是我们撞击的墙壁的法线,V2 是您的反射矢量(您的新速度)。 “N.dot(V1)”是N和V1的“点积”,即(NxV1.x + NyV1.y)。

来自维基百科的我们正在做的事情的图片(P 是入射向量,Q 是反射向量):

alt text

这是伪代码中的全部内容:

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

如果有任何不清楚的地方,请告诉我。 :) 另外,虽然这对墙壁来说有点过分了,但当你进行球与球碰撞时,你需要计算法线,所以这并不是完全浪费......

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

alt text

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

望喜 2024-09-15 15:11:22

您可能会喜欢这篇文章无三角学的二维弹性碰撞,其中讨论使用矢量的弹性碰撞。此动力学模型在 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文