von dp am 23.August 98 um 20:38:36:
From: "Terry R. Schussler"
Subject: "Intersection of circles" handler
A number of recent posts were made about preventing jitter when executing a
"bouncing balls" routine in Lingo. The jitter usually occurs because the
collision detection code relies on either testing the intersection of the
sprite rects for two objects (comparing rectangles) or by using the Lingo
"sprite...intersects" function.
Following is some simple code I wrote to help address this problem by
allowing accurate collision detection of two "perfect" circles. The
circles can be of any radius. Enjoy.
P.S. If anyone modifies this code the handles ellipses, I would appreciate
seeing it!
-- compare the area of the circles and determine if they intersect
-- if they intersect return TRUE, else return FALSE
-- note: assumes that the circles are "perfect" circles and not ellipses of
irregular dimensions
-- note: operates using integer math for faster performance
on circleIntersect sprite1, sprite2
-- sprite1 and sprite2 are channel numbers containing circle sprites
-- first get the radii of the circles
set circle1Radius = the width of sprite sprite1 / 2
set circle2Radius = the width of sprite sprite2 / 2
-- next determine the required distance between the two centerpoints for
non-intersection
set minDistanceForNonIntersect = circle1Radius + circle2Radius
-- now determine the actual distance between the centerpoints of the two
sprites
set actualDistance = distance(the loc of sprite sprite1 +
point(circle1Radius, circle1Radius), the loc of sprite sprite2 +
point(circle2Radius, circle2Radius))
-- now compare the two
if actualDistance > minDistanceForNonIntersect then
return FALSE
else
return TRUE
end if
end circleIntersect
-- compute distance between two points
on distance point1, point2
set deltaH = the locH of point1 - the locH of point2
set deltaV = the locV of point1 - the locV of point2
return sqrt(deltaH * deltaH + deltaV * deltaV)
end distance
Regards,
Terry
D. Plänitz