JAVA elastic collision of moving and motionless circles

I am trying to write a java mobile application (J2ME) and I have a problem: in my project there are moving circles called snapshots and not moving circles called orbs. When a shot falls into the sphere, it should bounce off the classical physical laws. However, I could not find any algorithm of this kind.

The motion of the frame is described by the speed on the x and y axis (pixels / refresh). all the information about the circles is known: their location, radius and speed (along the x and y axis) of the frame.

Note: the ball does not start to move after the collision, it remains in its place. Collision is an elastic collision between them, while the ball remains static

here is a collision resolution method in the Shot class:

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

    //variables vx and vy are the shot 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 width, width is the shot width

    double angle=0;  //here should be some sort of calculation of the shot 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;
}

+1
2

, , angular . m1, m2 , p1 = (p1x, p1y), p2 = (p2x, p2y) , u1, u2 - v1, v2 - . ,

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,

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

, (u1-v1) , , (u2-v2) (p1-p2), , . a,

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

,

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


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


.. v1 u1 , (p1-p2) . , norm(p1-p2)=r1+r2

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

.


oc{x,y} , sc{x,y} {vx,vy} .

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

  • dot = dc.x*vx+dcy*vy dot = dot/dist2

  • vx = vx - 2*dot*dc.x, vy = vy - 2*dot*dc.y

, , dc.y==0, oc.y==sc.y dot=vx/dc.x, vx=-vx, vy=vy .

+1

, , , . , . , , , , .

, , . , , :

dx = -dx; //Reverse direction
dy = -dy;
double speed = Math.sqrt(dx*dx + dy*dy);
double currentAngle = Math.atan2(dy, dx);

//The angle between the ball center and the orbs center
double reflectionAngle = Math.atan2(oc.y - sc.y, oc.x - sc.x);
//The outcome of this "static" collision is just a angular reflection with preserved speed
double newAngle = 2*reflectionAngle - currentAngle;

dx = speed * Math.cos(newAngle); //Setting new velocity
dy = speed * Math.sin(newAngle);

, , , . , :

  • ( )
  • , , /. , .
0

Source: https://habr.com/ru/post/1627358/


All Articles