Java中的完美圆到完美圆和完美圆到直线碰撞处理 [英] Perfect circle to perfect circle and Perfect circle to straight line collision HANDLING in Java

查看:28
本文介绍了Java中的完美圆到完美圆和完美圆到直线碰撞处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 Java 的新手,但决定制作一个应用程序,里面有一堆弹跳的球.请记住,我对法线几乎一无所知,我在此类线程中看到了很多提及.我也只学过代数 1,并且知道一点三角函数(sin、cosin 和 tangent).总之……

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

我已经涵盖了碰撞检测,使用
if (java.awt.geom.Point2D.distance(X1, Y1, X2, Y2)

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}

对所有四面墙以此类推

我需要做的是以一种半真实的方式处理这些碰撞,而不仅仅是切换速度和方向.

What I need to do is handle these collisions in a semi realistic manner beyond switching the velocities and directions.

一切都在一个JPanel中绘制

Everything is drawn in a JPanel

提前感谢您的帮助

推荐答案

为了计算碰撞上的反射向量,您必须了解法线是什么.幸运的是,这并不难理解.对于那里的数学题,我会有点不精确,所以请不要因为它而殴打我.:)

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

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

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

其中 V1 是您的入射向量(在这种情况下是您的旧速度),N 是我们撞到的墙壁的法线,V2 是您的反射向量(您的新速度).N.dot(V1)"是N和V1的点积",就是(N.xV1.x + N.yV1.y).

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

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

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

这篇关于Java中的完美圆到完美圆和完美圆到直线碰撞处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆