Game Development Reference
In-Depth Information
var hitting:Boolean = wall.hitTestObject(_ballDO);
if (hitting)
var ballRect:Rectangle = _ball.getRect
var wallRect:Rectangle = wall.getRect
var intersection:Rectangle = ballRect.
if (!intersection.width && !intersection.
height) continue;
var rectCenter:Point = new Point
(intersection.x + intersection.width/2,
intersection.y + intersection.height/2);
var angle:Number = Math.atan2(rectCenter.
y - _ball.y, rectCenter.x - _ball.x);
angle = Math.round(angle/(Math.PI/2));
angle *= Math.PI/2;
var angleSin:Number = Math.sin(angle);
var angleCos:Number = Math.cos(angle);
var offsetX:Number = angleCos * intersection.
var offsetY:Number = angleSin * intersection.
_ball.x -= offsetX;
_ball.y -= offsetY;
The checkCollisions method is the one in which the heaviest
ments is tested against the ball to see if they overlap. First, every
comparison starts with the rather blunt but fast hitTestObject check.
If there is a collision, a more informative test is performed. This is
done by getting the bounding box rectangles of both objects and
by determining their rectangle of overlap (or intersection). The
angle of the collision is then determined by measuring the angle
between the middle of the ball and middle of the collision rectan-
gle. This is illustrated in Fig. 15.7 . The gray area represents the col-
lision rectangle. The dashed line connecting the center point of the
Search Nedrilad ::

Custom Search