A
A
alkolove12018-12-10 17:14:05
Java
alkolove1, 2018-12-10 17:14:05

How to make absolutely elastic collision of circles?

How to make ball's absolutely elastic collision?
It kind of ricochets off that round badge like a rubber band.
giphy.gif
Box2d didn’t screw it up, because there are too many variables that I don’t need are processed, it will be unoptimized in meat situations.
function of this very ricochet:

boolean collision(float Radius2, float X2, float Y2) //R1 - мяч, R2- объект, XY1- мяч, XY2- объект dx dy - скорость мяча
    {
        Circle ball = new Circle();
        ball.radius = Radius / 2;
        ball.x = rect.x + ball.radius;
        ball.y = rect.y + ball.radius;

        Circle obj = new Circle();
        obj.radius = Radius2 / 2;
        obj.x = X2 + obj.radius;
        obj.y = Y2 + obj.radius;


        if (ball.overlaps(obj)) {
            Vector2 otskok = new Vector2();
            otskok.x = ball.x - obj.x;
            otskok.y = ball.y - obj.y;

            Vector2 currentMov = new Vector2();
            currentMov.x = rect.dx;
            currentMov.y = rect.dy;
            Vector2 newDir; 
            newDir = currentMov.add(otskok);
            rect.dx = newDir.x;
            rect.dy = newDir.y;
            return true;
        } else return false;
    }

Where rect.x rect.y rect.dx rect.dy are the position and increments of the ball's coordinates. Then I will remake it from a rectangle to a circle, in this function the parameters are still assigned to the circle, so for now this is not important.
THEORETICALLY, I myself know that when a collision is detected, you need to immediately push the ball out in one frame by the sum of the radii of both circles. I can calculate this. But here is the offset angle.
And in general, it seems to me that I have a hacky code, and I can somehow make it more compact, especially using Libgdx, where there are vector classes. I'm just a noob at this. Help. I beg you...

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
alkolove1, 2018-12-11
@alkolove1

Slightly altered the code
Collision became absolutely elastic. But the ball at some angles changes position rather abruptly.
Still, the crutch variant with the intersection cycle is not the most successful.

boolean collision(float Radius2, float X2, float Y2) 
    {
        Circle ball = new Circle();
        ball.radius = Radius / 2;
        ball.x = rect.x + ball.radius;
        ball.y = rect.y + ball.radius;

        Circle obj = new Circle();
        obj.radius = Radius2 / 2;
        obj.x = X2 + obj.radius;
        obj.y = Y2 + obj.radius;

        if (ball.overlaps(obj)) {

            while (ball.overlaps(obj)) {
                double xDelta = (ball.x + ball.radius / 2 + rect.dx) - (obj.x + obj.radius / 2);
                double YDelta = (ball.y + ball.radius / 2 + rect.dy) - (obj.y + obj.radius / 2);
                rect.dx = (float) xDelta;
                rect.dy = (float) YDelta;
                ball.x+=rect.dx*Gdx.graphics.getDeltaTime();
                ball.y+=rect.dy*Gdx.graphics.getDeltaTime();
            }
            rect.x=ball.x-ball.radius;
            rect.y=ball.y-ball.radius;
            return true;
        } else return false;

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question