正圆完美圆形,并正圆在Java直线冲突处理 [英] Perfect circle to perfect circle and Perfect circle to straight line collision HANDLING in Java

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

问题描述

我是一个新手在Java中,但决定做出有一堆球蹦跳着的应用程序。请记住,我几乎一无所知法线,这是我在这样的线程见过提到了很多。我也只取代数1,知道触发一点点(罪,cosin和切线)。反正...

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

我有碰撞检测覆盖,使用
如果(java.awt.geom.Point2D.distance(X1,Y1,X2,Y2)< ball1.radius + ball2.radius){返回true;}其他{返回false; }

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

如果(ball.x + ball.radius&GT;的getWidth){碰撞}

等了四面墙

我需要做的就是处理这些冲突在一个半现实的方式超越切换速度和方向。

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

感谢您的帮助提前

推荐答案

你将要明白一个正常的是为了计算科里森反射向量是什么。幸运的是,它不是很难理解。对于数学头在那里,我要成为一个小的IM precise,所以请不要打我了吧。 :)

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

一个正常的仅仅是一个单位向量(单位:有大小= 1),这是正交(成90度角),以一个面。你可以想像一个普通的箭笔直伸出表面的。因为你的墙没有倾斜,摸清了法线对他们来说是很容易(假设你的屏幕的左上角是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,这仅仅是(NX * 1.x版+尹恩惠* V1.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.x*V1.x + N.y*V1.y).

这是我们正在做的事情维基百科的图像(P为入射矢量,Q是反射向量):

A picture from Wikipedia of what we're doing (P is the incident vector, Q is the reflected vector):

替代文字

下面是在伪code整个事情:

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天全站免登陆