Answer the question
In order to leave comments, you need to log in
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.
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;
}
Answer the question
In order to leave comments, you need to log in
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 questionAsk a Question
731 491 924 answers to any question