Computer Graphics through Game Programming Collision DetectionAA Transformations












- Slides: 12

Computer Graphics through Game Programming Collision Detection/AA Transformations Omer Boyaci (C) 2010 Pearson Education, Inc. All rights reserved.

Collision Processing A sprite must monitor the game environment, for example, reacting to collisions with different sprites or stopping when it encounters an obstacle. Collision processing can be split into two basic categories: § collision detection § collision response, with the range of responses being application specific. (C) 2010 Pearson Education, Inc. All rights reserved.

Collision Processing Many varieties of collision detection exist: § a sprite may be represented by a single bounding box, § a reduced size bounding box, § or several bounding areas. (C) 2010 Pearson Education, Inc. All rights reserved.

Collision Processing A single bounding box is simple to manipulate but prone to inaccuracy. The reduced bounding box is better, but choosing a suitable reduction factor is difficult. The greatest accuracy can be achieved with several boxes for each sprite at the expense of additional calculations. (C) 2010 Pearson Education, Inc. All rights reserved.

Collision Processing public Rectangle get. My. Rectangle( ){ return new Rectangle(locx, locy, width, height); } private void has. Hit. Bat( ){ Rectangle rect = get. My. Rectangle( ); if (rect. intersects( bat. get. My. Rectangle( ) )) { // bat collision? Rectangle inter. Rect = rect. intersection(bat. get. My. Rectangle( )); dy = -dy; // reverse ball's y-step direction locy -= inter. Rect. height; // move the ball up } } (C) 2010 Pearson Education, Inc. All rights reserved.

Collision Processing public abstract boolean hit(Rectangle rect, Shape s, boolean on. Stroke) Checks whether or not the specified Shape intersects the specified Rectangle, which is in device space. If on. Stroke is false, this method checks whether or not the interior of the specified Shape intersects the specified Rectangle. If on. Stroke is true, this method checks whether or not the Stroke of the specified Shape outline intersects the specified Rectangle. The rendering attributes taken into account include the Clip, Transform, and Stroke attributes. Parameters: rect - the area in device space to check for a hit s - the Shape to check for a hit on. Stroke - flag used to choose between testing the stroked or the filled shape. If the flag is true, the Stroke oultine is tested. If the flag is false, the filled Shape is tested. (C) 2010 Pearson Education, Inc. All rights reserved.

Collision Processing public boolean intersects(double x, double y, double w, double h) Tests whether the interior of this Area object intersects the interior of the specified rectangular area. Specified by: intersects in interface Shape Parameters: x, y - the coordinates of the upper left corner of the specified rectangular area w - the width of the specified rectangular area h - the height of teh specified rectangular area Returns: true if the interior intersects the specified rectangular area; false otherwise; (C) 2010 Pearson Education, Inc. All rights reserved.

Collision Processing Collision detection is a matter of seeing if the bounding boxes for the ball and the bat intersect. If they overlap, the ball is made to bounce by having its y-step reversed. The ball’s y-axis location is moved up slightly so it no longer intersects the bat. This rules out the (slim) possibility that the collision test of the ball and bat during the next update will find them still overlapping. This would occur if the rebound velocity were too small to separate the objects within one update. The collision algorithm could be improved. For instance, some consideration could be given to the relative positions and speeds of the ball and bat to determine the direction and speed of the rebound. This would complicate the coding but improve the ball’s visual appeal. (C) 2010 Pearson Education, Inc. All rights reserved.

Anti-Aliasing @Override public void paint. Component(Graphics g) { //. . . Downcast to a Graphics 2 D context. Graphics 2 D g 2 = (Graphics 2 D)g; g 2. set. Rendering. Hint(Rendering. Hints. KEY_ANTIALIASING, Rendering. Hints. VALUE_ANTIALIAS_ON) ; (C) 2010 Pearson Education, Inc. All rights reserved.

Transformations Rotate § to specify an angle of rotation in radians Scale § to specify a scaling factor in the x and y directions Shear § to specify a shearing factor in the x and y directions Translate § to specify a translation offset in the x and y directions (C) 2010 Pearson Education, Inc. All rights reserved.

Transformations Use the get. Transform method to get the current transform. Use transform, translate, scale, shear, or rotate to concatenate a transform. Perform the rendering. Restore the original transform using the set. Transform method. (C) 2010 Pearson Education, Inc. All rights reserved.

Transformations Affine. Transform save. Xform = g 2. get. Transform(); § This transform will be restored to the Graphics 2 D after rendering. After retrieving the current transform, another Affine. Transform, to. Center. At, is created that causes shapes to be rendered in the center of the panel. The at Affine. Transform is concatenated onto to. Center. At: Affine. Transform to. Center. At = new Affine. Transform(); to. Center. At. concatenate(at); to. Center. At. translate(-(r. width/2), -(r. height/2)); The to. Center. At transform is concatenated onto the Graphics 2 D transform with the transform method: g 2. transform(to. Center. At); After rendering is completed, the original transform is restored using the set. Transform method: g 2. set. Transform(save. Xform); (C) 2010 Pearson Education, Inc. All rights reserved.