Forum > Games

Simple ball bounce

(1/6) > >>

lazboy10:
Hi!
So my problem is that i have been trying to get a ball to hit another ball and then bounce in the correct angle.
i have working code for the actual collision part, but for the life of me i just cant get the other ball to bounce correctly in the right angle.

My math skills are terrible so im hoping someone could help me with some working code... what i need is the bounce code only, i don't need any velocity or optimized code. just the basic bounce part.

here is my collision code that works:

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---function TestCollision: boolean;var  CentreAX, CentreAY, CentreBX, CentreBY, vDx, vDy, fDist: Single;begin  {red ball}  CentreAX := (Balls[1].Position.X + 24) / 2;  CentreAY := (Balls[1].Position.Y + 24) / 2;  {white ball}  CentreBX := (Balls[0].Position.X + 24) / 2;  CentreBY := (Balls[0].Position.Y + 24) / 2;   vDx := CentreBX - CentreAX;  vDy := CentreBY - CentreAY;   fDist := Sqrt((vDx * vDx) + (vDy * vDy));   if fDist<12 then    Result := True  else    Result := False;end;

It's simple conservation of momentum. In case of equal masses velocities are simply swapped and mirrored against plane, that is perpendicular to line between two ball centers. Collision point also lies in a middle of that line. Hit detection - is simple distance measurement between centers. If distance <= 2 radius, then it's hit.

Looks like simple googling gives proper result.

BobDog:

You can look at this.
(simple console graphics only)
Tested win 10, 32 and 64 bit fp.

speter:
In case anyone is interested, here is a GUI version of BobDog's code.

cheers
S.