18
CRE Programming Club Class #9 Robert Eckstein and Robert Heard

CRE Programming Club Class #9 Robert Eckstein and Robert Heard

Embed Size (px)

Citation preview

Page 1: CRE Programming Club Class #9 Robert Eckstein and Robert Heard

CRE Programming Club Class #9

Robert Eckstein and Robert Heard

Page 2: 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)

Page 3: CRE Programming Club Class #9 Robert Eckstein and Robert Heard

Video

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

Page 4: CRE Programming Club Class #9 Robert Eckstein and Robert Heard

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!

Page 5: CRE Programming Club Class #9 Robert Eckstein and Robert Heard

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.

Page 6: CRE Programming Club Class #9 Robert Eckstein and Robert Heard

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.

Page 7: CRE Programming Club Class #9 Robert Eckstein and Robert Heard
Page 8: CRE Programming Club Class #9 Robert Eckstein and Robert Heard

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!

Page 9: CRE Programming Club Class #9 Robert Eckstein and Robert Heard

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.

Page 10: CRE Programming Club Class #9 Robert Eckstein and Robert Heard

Bounding Boxes

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

Page 11: CRE Programming Club Class #9 Robert Eckstein and Robert Heard

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!

Page 12: CRE Programming Club Class #9 Robert Eckstein and Robert Heard

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.

Page 13: CRE Programming Club Class #9 Robert Eckstein and Robert Heard

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

Page 14: CRE Programming Club Class #9 Robert Eckstein and Robert Heard

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

Page 15: CRE Programming Club Class #9 Robert Eckstein and Robert Heard

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!

Page 16: CRE Programming Club Class #9 Robert Eckstein and Robert Heard

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.

Page 17: CRE Programming Club Class #9 Robert Eckstein and Robert Heard

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…

Page 18: CRE Programming Club Class #9 Robert Eckstein and Robert Heard

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.