Mail Archives: djgpp/1997/02/06/14:05:24
Shawn Hargreaves wrote:
>
> One of the simplest and fastest is to model each object as a circle.
> Given the centre points of two objects (x1,y1 and x2,y1), and their
> radius (radiii? what's the plural?) r1 and r2, they have collided if
> sqrt((x1-x2)*(x1-x2) + (y1-y2)*(y1-y2)) < sqrt(r1*r1 + r2*r2).
>
Shouldn't it be :
sqrt((x1-x2)*(x1-x2) + (y1-y2)*(y1-y2)) < r1 + r2
(distance between the two points is inferior to the sum of the radii of
the two spheres?)
With this formula, you should still avoid the square root:
(x1-x2)*(x1-x2) + (y1-y2)*(y1-y2) < (r1+r2)*(r1+r2)
which can be further simplified in
((r1+r2)-(x1-x2))*((r1+r2)+(x1-x2))>(y1-y2)*(y1-y2)
this trades a multiply for an addition, which can be a little faster on
some machines, especially if you use fixed point.
Francois
- Raw text -