Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "CRE Programming Club Class #9 Robert Eckstein and Robert Heard."— Presentation transcript:

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

2 Let’s Look at some Homework JGT020-6 (Andy Friedrich) BDJ016-0 (Ashley Hartenstein) BXX981 (Arnav Chopra)

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

4 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!

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

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

7

8 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!

9 Bounding Boxes When 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.

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

11 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!

12 Works With Circles Too To 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.

13 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

14 Here’s the Rest of It… If (box1Bottom box2Bottom) Or (box1Left > box2Right) Or (box1Right < box2Left) Then Shapes.HideShape(intersect) Else Shapes.ShowShape(intersect) EndIf EndSub

15 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!

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

17 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…

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


Download ppt "CRE Programming Club Class #9 Robert Eckstein and Robert Heard."

Similar presentations


Ads by Google