+ All Categories
Home > Documents > Chapter 15 Graphics and Java 2D™ - Columbia Universityboyaci/graphics/slides/Computer...

Chapter 15 Graphics and Java 2D™ - Columbia Universityboyaci/graphics/slides/Computer...

Date post: 03-Apr-2018
Category:
Upload: dokhuong
View: 222 times
Download: 3 times
Share this document with a friend
12
(C) 2010 Pearson Education, Inc. All rights reserved. Omer Boyaci
Transcript

(C) 2010 Pearson Education, Inc. All rights reserved.

Omer Boyaci

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.

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.

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.

public Rectangle getMyRectangle( ){

return new Rectangle(locx, locy, width, height);

}

private void hasHitBat( ){

Rectangle rect = getMyRectangle( );

if (rect.intersects( bat.getMyRectangle( ) )) { // bat collision?

Rectangle interRect = rect.intersection(bat.getMyRectangle( ));

dy = -dy; // reverse ball's y-step direction

locy -= interRect.height; // move the ball up

}

}

(C) 2010 Pearson Education, Inc. All rights reserved.

public abstract boolean hit(Rectangle rect,Shape s, boolean onStroke)

Checks whether or not the specified Shape intersects the specified Rectangle, which is in device space. If onStroke is false, this method checks whether or not the interior of the specified Shape intersects the specified Rectangle. If onStrokeis 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

onStroke - 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.

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 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.

@Override public void paintComponent(Graphics g) {

//... Downcast to a Graphics2D context.

Graphics2D g2 = (Graphics2D)g;

g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,

RenderingHints.VALUE_ANTIALIAS_ON);

(C) 2010 Pearson Education, Inc. All rights reserved.

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.

Use the getTransform 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 setTransform method.

(C) 2010 Pearson Education, Inc. All rights reserved.

AffineTransform saveXform = g2.getTransform(); This transform will be restored to the Graphics2D after rendering.

After retrieving the current transform, another AffineTransform, toCenterAt, is created that causes shapes to be rendered in the center of the panel. The at AffineTransform is concatenated onto toCenterAt:

AffineTransform toCenterAt = new AffineTransform(); toCenterAt.concatenate(at);

toCenterAt.translate(-(r.width/2), -(r.height/2));

The toCenterAt transform is concatenated onto the Graphics2D transform with the transform method: g2.transform(toCenterAt);

After rendering is completed, the original transform is restored using the setTransform method: g2.setTransform(saveXform);

(C) 2010 Pearson Education, Inc. All rights reserved.


Recommended