cre programming club class #9 robert eckstein and robert heard

Post on 29-Jan-2016

217 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

CRE Programming Club Class #9

Robert Eckstein and Robert Heard

Let’s Look at some Homework

JGT020-6 (Andy Friedrich)

BDJ016-0 (Ashley Hartenstein)

BXX981 (Arnav Chopra)

Video

https://www.youtube.com/watch?v=dU1xS07N-FA

Collision Detection

Two objects are said to “collide” if they come in contact with each other.

Collision detection is an essential aspect of nearly all video games.

Algorithms help to detect the collision. For the most part, you can just find someone else’s algorithm and copy it!

Collision Detection

Import LLW706

Have fun with this “game” for a little bit.

Enter an angle between 0 and 360 (0 is up, angles increase clockwise) and a distance in pixels. See if you can get the turtle to collide with the CRE Logo.

Source Code Hunting

Look through the source code for LLW706.

See if you can find where the code that checks for the collision detection is…

Look for a place where the X and Y position of the turtle is compared to the right, left, top, and bottom position of the graphic.

If Turtle.x > x and Turtle.x < x + 93

and Turtle.y > y and Turtle.y < y + 96 Then

‘ There is a collision between logo/turtle!

EndIf

Line 52!

Bounding BoxesWhen working with collision detection, it helps to think of shapes or graphics as being surrounded by a “shrinkwrapped rectangle.” This is called a bounding box. Here is a blue bounding box around an early Mario.

Bounding Boxes

Bounding Boxes can also be very complex, and are used in more modern 3D games as well!

Why Use a Bounding Box?

Because it’s quick and easy to compute when things run into each other…. just figure out if the rectangles are touching!

Works With Circles TooTo figure out if something is colliding with a circle, just

see if the distance between some point and the center of the circle is less than the radius.

Two circles? Just see if the distance between the centers is less than the two radius values added together.

You’ll need to be familiar with the Pythagoreon Therom.

Are BBs Perfect?No! But for simple games, it’s generally good

enough.

Sub DoBoxesIntersect

box1Left = box1X

box1Top = box1Y

box1Bottom = box1Y + box1Height

box1Right = box1X + box1Width

box2Left = box2X

box2Top = box2Y

box2Bottom = box2Y + box2Height

box2Right = box2X + box2Width

Here’s the Rest of It…

If (box1Bottom < box2Top) Or (box1Top > box2Bottom) Or (box1Left > box2Right) Or (box1Right < box2Left) Then

Shapes.HideShape(intersect)

Else

Shapes.ShowShape(intersect)

EndIf

EndSub

Import QSP227

Run this program. Use the arrow keys to move one of the rectangles around.

Note that when it collides with the other rectangle, a message appears at the bottom.

USE THIS ALGORITHM IN YOUR OWN PROGRAMS!

Why Does This Work?

Talk To Your Teachers! See if you can figure out why that algorithm works. (HINT: Look at the intersecting rectangle made by the two squares on slide 12.)

It’s much more efficient than checking to see if any of the four corners of one rectangle are inside the boundaries of another rectangle.

Homework

Continue working on your state machine.

This time, when in the PLAY_GAME state, don’t kick it into the third state unless there is a collision of some sort. Add multiple shapes. Be creative! Have fun!

Email me with questions…

Next Time...

No class next week due to STAAR tests.

We’ll get back together the following week and really start cranking out some game code.

top related