How can I calculate the bounce angle between two circles ... ??
I use the following code to check and resolve collisions.
-- CHECK COLLISIONS BALLS AND RESOLVE ...
function CheckCollisionResolve(a, b)
local dx = a.x - b.x;
local dy = a.y - b.y;
local d = math.sqrt(dx*dx + dy*dy);
local minDist = (a.radius+b.radius);
local midpointx = (a.x + b.x) / 2;
local midpointy = (a.y + b.y) / 2;
if d <= minDist then
-- RESOLVE POSITION BALLS ...
a.x = midpointx + a.radius * (a.x - b.x) / d;
a.y = midpointy + a.radius * (a.y - b.y) / d;
b.x = midpointx + b.radius * (b.x - a.x) / d;
b.y = midpointy + b.radius * (b.y - a.y) / d;
return true;
end
return false;
end
everything works fine, but if there is no reaction from the angle there is no very satisfactory or realistic effect when trying to resolve the collisions.
This is the code that moves the circles and checks the collisions.
-- UPDATE BALLS ...
function update_balls(dt)
local lenBalls = #balls;
for i = 1, lenBalls do
local ball = balls[i];
-- CALCULATE NEW POSITION ...
local theta = ball.angle * (math.pi/180.0);
ball.x = ball.x + ((ball.speed * dt) * math.cos(theta));
ball.y = ball.y + ((ball.speed * dt) * math.sin(theta));
-- CHECK BALL COLLISION TO SCREEN HORIZONTAL.
if ball.x-ball.r < 0 then
ball.x = ball.r;
ball.angle = reflectHorizontalAngle(ball.angle);
elseif ball.x+ball.r > sw then
ball.x = sw-ball.r;
ball.angle = reflectHorizontalAngle(ball.angle);
end
-- CHECK BALL COLLISION TO SCREEN VERTICAL
if ball.y-ball.r < 0 then
ball.y = ball.r;
ball.angle = reflectVerticalAngle(ball.angle);
elseif ball.y+ball.r > sh then
ball.y = sh-ball.r;
ball.angle = reflectVerticalAngle(ball.angle);
end
end
-- CHECK COLLISIONS BALLS RESOLVE ...
for i = 1, lenBalls do
for j = 1, lenBalls do
if i ~= j then
local col = CheckCollisionResolve(balls[i], balls[j]);
end
end
end
end
This is the code I use to calculate the bounce angles with the walls (horizontal and vertical).
-- REFLECT ANGLES VERTICAL AND HORIZONTAL ...
local reflectVerticalAngle = function(angle) return (360 - angle) < 0 and ((360 - angle) + 360) or (360 - angle); end;
local reflectHorizontalAngle = function(angle) return (180 - angle) < 0 and ((180 - angle) + 360) or (180 - angle); end;
the speed of the circles is constant without variation, since I am working with angles.
I hope you can help me.