JAVA动圈和非动圈的弹性碰撞 [英] JAVA elastic collision of moving and non moving circles

查看:82
本文介绍了JAVA动圈和非动圈的弹性碰撞的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图编写一个Java移动应用程序(J2ME),但遇到了一个问题:在我的项目中,有一个称为镜头的动圈,而一个名为orbs的动圈.当镜头击中球体时,应该按照经典的物理定律反弹.但是我找不到这种算法.

I'm trying to write a java mobile application (J2ME) and I got stuck with a problem: in my project there are moving circles called shots, and non moving circles called orbs. When a shot hits an orb, it should bounce off by classical physical laws. However I couldn't find any algorithm of this sort.

镜头的移动由x和y轴上的速度(像素/更新)来描述.所有有关圆的信息都是已知的:它们的位置,半径和射击的速度(在x和y轴上).

The movement of a shot is described by velocity on axis x and y (pixels/update). all the information about the circles is known: their location, radius and the speed (on axis x and y) of the shot.

注意:球在碰撞后不会开始移动,而是停留在原处.碰撞是两者之间的弹性碰撞,而球体保持静态

Note: the orb does not start moving after the collision, it stays at its place. The collision is an elastic collision between the two while the orb remains static

这是Shot类中的碰撞解决方法:

here is the collision solution method in class Shot:

public void collision(Orb o)
{
    //the orb's center point
    Point oc=new Point(o.getTopLeft().x+o.getWidth()/2,o.getTopLeft().y+o.getWidth()/2);
    //the shot's center point
    Point sc=new Point(topLeft.x+width/2,topLeft.y+width/2);

    //variables vx and vy are the shot's velocity on axis x and y
    if(oc.x==sc.x)
    {
        vy=-vy;
        return ;
    }

    if(oc.y==sc.y)
    {
        vx=-vx;
        return ;
    }

    // o.getWidth() returns the orb's width, width is the shot's width

    double angle=0;  //here should be some sort of calculation of the shot's angle
    setAngle(angle);
}

public void setAngle(double angle)
{
    double v=Math.sqrt(vx*vx+vy*vy);
    vx=Math.cos(Math.toRadians(angle))*v;
    vy=-Math.sin(Math.toRadians(angle))*v;
}

提前感谢所有帮手

推荐答案

在碰撞点,动量,角动量和能量得以保留.设置m1,m2的磁盘质量,p1 =(p1x,p1y),p2 =(p2x,p2y)排序时磁盘中心的位置,u1,u2之前的速度和v1,v2后面的速度碰撞.然后,保护法要求

At the point of collision, momentum, angular momentum and energy are preserved. Set m1, m2 the masses of the disks, p1=(p1x,p1y), p2=(p2x,p2y) the positions of the centers of the disks at collition time, u1, u2 the velocities before and v1,v2 the velocities after collision. Then the conservation laws demand that

0 = m1*(u1-v1)+m2*(u2-v2)
0 = m1*cross(p1,u1-v1)+m2*cross(p2,u2-v2)
0 = m1*dot(u1-v1,u1+v1)+m2*dot(u2-v2,u2+v2)

使用第一个方程式消除u2-v2

Eliminate u2-v2 using the first equation

0 = m1*cross(p1-p2,u1-v1)
0 = m1*dot(u1-v1,u1+v1-u2-v2)

第一个告诉我们(u1-v1)和(u2-v2)是(p1-p2)的倍数,脉冲交换沿法线或径向方向进行,没有切向相互作用.脉冲和能量的守恒现在导致相互作用常数a,所以

The first tells us that (u1-v1) and thus (u2-v2) is a multiple of (p1-p2), the impulse exchange is in the normal or radial direction, no tangential interaction. Conservation of impulse and energy now leads to a interaction constant a so that

u1-v1 = m2*a*(p1-p2)
u2-v2 = m1*a*(p2-p1)
0 = dot(m2*a*(p1-p2), 2*u1-m2*a*(p1-p2)-2*u2+m1*a*(p2-p1))

产生非零交互项a

2 * dot(p1-p2, u1-u2) = (m1+m2) * dot(p1-p2,p1-p2) * a

现在可以使用分数来解决

which can now be solved using the fraction

b = dot(p1-p2, u1-u2) / dot(p1-p2, p1-p2)

a = 2/(m1+m2) * b

v1 = u1 - 2 * m2/(m1+m2) * b * (p1-p2)
v2 = u2 - 2 * m1/(m1+m2) * b * (p2-p1)

要使第二个磁盘静止,请将u2 = 0设置,并将其质量m2设置为非常大或无限大,然后第二个公式说v2 = u2 = 0,第一个公式为

To get the second disk stationary, set u2=0 and its mass m2 to be very large or infinite, then the second formula says v2=u2=0 and the first

v1 = u1-2-*点(p1-p2,u1)/点(p1-p2,p1-p2)*(p1-p2)


也就是说,v1是u1在以(p1-p2)为法线的平面上的反射.请注意,碰撞点的特征是norm(p1-p2)=r1+r2

dot(p1-p2, p1-p2) = (r1+r2)^2

这样分母就可以从碰撞检测中得知.

so that the denominator is already known from collision detection.

根据您的代码,oc{x,y}包含固定磁盘或球的中心,sc{x,y}包含中心,而{vx,vy}包含移动磁盘的速度.

Per your code, oc{x,y} contains the center of the fixed disk or orb, sc{x,y} the center and {vx,vy} the velocity of the moving disk.

  1. 计算dc={sc.x-oc.x, sc.y-oc.y}dist2=dc.x*dc.x+dc.y*dc.y

1.a检查sqrt(dist2)是否足够接近sc.radius+oc.radius.常见问题说比较正方形更为有效.如果dist2太小,请微调相交点的位置.

1.a Check that sqrt(dist2) is sufficiently close to sc.radius+oc.radius. Common lore says that comparing the squares is more efficient. Fine-tune the location of the intersection point if dist2 is too small.

计算dot = dc.x*vx+dcy*vydot = dot/dist2

更新vx = vx - 2*dot*dc.xvy = vy - 2*dot*dc.y

这些公式中包含特殊情况,例如,对于dc.y==0,即oc.y==sc.y会得到dot=vx/dc.x,从而得出vx=-vxvy=vy.

The special cases are contained inside these formulas, e.g., for dc.y==0, that is, oc.y==sc.y one gets dot=vx/dc.x, so that vx=-vx, vy=vy results.

这篇关于JAVA动圈和非动圈的弹性碰撞的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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