Collision Detection Box-to-Box.

Slides:



Advertisements
Similar presentations
Introduction to C Programming
Advertisements

Area Under a Curve (Linear). Find the area bounded by the x-axis, y = x and x =1. 1. Divide the x-axis from 0 to 1 into n equal parts. 2. Subdividing.
AE1APS Algorithmic Problem Solving John Drake
Working with images and scenes CS 5010 Program Design Paradigms “Bootcamp” Lesson 2.5 TexPoint fonts used in EMF. Read the TexPoint manual before you delete.
Intersection Testing Chapter 13 Tomas Akenine-Möller Department of Computer Engineering Chalmers University of Technology.
2.3. B OUNDING V OLUMES Bounding volumes of use for collision detection.
Collision Detection and Resolution Zhi Yuan Course: Introduction to Game Development 11/28/
Game Mathematics & Game State The Complexity of Games Expectations of Players Efficiency Game Mathematics o Collision Detection & Response o Object Overlap.
Area.
Fundamentals of Python: From First Programs Through Data Structures
Fundamentals of Python: First Programs
APPLICATIONS OF INTEGRATION 6. A= Area between f and g Summary In general If.
Programming Games Simulated ballistic motion: cannon ball. Classwork: Final day for midterm project and all projects for first part of class. Homework:
1 Lesson Finding Complex Areas. 2 Lesson Finding Complex Areas California Standards: Algebra and Functions 3.1 Use variables in expressions.
Week 13 - Friday.  What did we talk about last time?  Ray/sphere intersection  Ray/box intersection  Slabs method  Line segment/box overlap test.
Alice Pong Recreating Pong in Alice By Elizabeth Liang under the direction of Professor Susan Rodger Duke University June 2010.
(C) 2010 Pearson Education, Inc. All rights reserved. Omer Boyaci.
AD 305 Electronic Visualization I : School of Art and Design : University of Illinois at Chicago : Spring 2007 Intro to Action Script 2 "The games of a.
Addison Wesley is an imprint of © 2010 Pearson Addison-Wesley. All rights reserved. Chapter 7 The Game Loop and Animation Starting Out with Games & Graphics.
CO1301: Games Concepts Dr Nick Mitchell (Room CM 226) Material originally prepared by Gareth Bellaby.
Multitouch and Collision Detection MOBILE SOFTWARE DEVELOPMENT.
11 Working with Images Session Session Overview  Find out more about image manipulation and scaling when drawing using XNA  Start to implement.
11 Adding Tomato Targets Session Session Overview  We now have a game which lets a player bounce a piece of cheese on a bread bat  Now we have.
Controlling Execution Dong Shao, Nanjing Unviersity.
CIS 350 – I Game Programming Instructor: Rolf Lakaemper.
Collision Detection And Response Jae Chun KyungSoo Im Chau Vo Hoang Vu.
CRE Programming Club Class #9 Robert Eckstein and Robert Heard.
Programming games using Visual Basic Detecting a position on top/within a rectangle, near a point, past a line Mouse events.
11 Adding a Bread Bat Session Session Overview  We have created a cheese sprite that bounces around the display  We now need to create a bread.
While loops. Iteration We’ve seen many places where repetition is necessary in a problem. We’ve been using the for loop for that purpose For loops are.
Introduction to Game Programming & Design III Lecture III.
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
6.1 Areas Between Curves In this section we learn about: Using integrals to find areas of regions that lie between the graphs of two functions. APPLICATIONS.
For each statement below, write whether the statement is true or false. A set of ordered pairs describe a function if each x-value is paired with only.
In this chapter, we explore some of the applications of the definite integral by using it to compute areas between curves, volumes of solids, and the work.
Drawing Two Dimensional Shapes
2D Graphics Optimizations
2D Collision Detection For CSE 3902 By: Matt Boggus.
MORE Genre Specific Physics
Graphical Output Basic Images.
Pixels, Colors and Shapes
Mile-long hurdle race Suppose that we want to program Karel to run a one-mile long hurdle race, where vertical wall sections represent hurdles. The hurdles.
4-2 Unit 4 Transformations.
Background Shapes & Collision Resolution (Top-down and Side-scrolling)
Preview Warm Up California Standards Lesson Presentation.
Intro & Point-to-Box, Circle-to-Circle, Point-to-Circle
Chapter 5 Classes.
Activity Directions Get into groups of 4.
Sketch the region enclosed by {image} and {image}
Sketch the region enclosed by {image} and {image}
Conditional Loops.
Creating games with game editors
Karnaugh Maps Topics covered in this presentation: Karnaugh Maps
Scratch: selection / branching/ if / If…else / compound conditionals / error trapping by Mr. Clausen.
Coding Concepts (Sub- Programs)
Perimeter and area of Combined shapes
Topic 1: Problem Solving
Factoring Polynomials
Transformations-Reflections
Collision Detection Platforms.
Collision Detection.
CO Games Concepts Week 12 Collision Detection
Determining When Two Objects Collide in a Turing Program
Question 31.
2. Area Between Curves.
Working with images and scenes
Acceleration 3.1 Changing Velocity
Decisions.
Chapter 7 The Game Loop and Animation
GPAT – Chapter 7 Physics.
Presentation transcript:

Collision Detection Box-to-Box

If collision, return true for that body part. Introduction Box to box collision tests are among the most used in 2D games. The simple reason for this, is that all images in 2D games are actually rectangles. Meaning, every image has an invisible rectangle tightly bound to the width and height of the image. We call this rectangle the bounding box Sometimes we will break body parts up into their own bounding boxes to determine which part of the body was actually hit However that means a collision test would need to be done on each body part 60 times/second, to make this more efficient we do what is called 2 stage collision detection. Stage 1: Bounding Box Test Stage 2: Sub Collision Tests If collision  Go to Stage 2 If collision, return true for that body part. And stop further tests

Box-to-Box (AABB Test) Also called the Axis Aligned Bounding Box (AABB) Test, meaning non rotated rectangles of any size. The problem with detecting whether two arbitrarily sized boxes collide, is that there are so many possibilities… As you can see, there are a lot of scenarios to cover. Too many for an efficient test that is needed to be done for every image against every other image in the game, 60 times/second We need a better way. The solution, we flip the script. Instead of testing for all the possible collisions, we will test for only the impossible collisions

Test the Impossible… There are only 4 scenarios in which it is completely impossible for two boxes to collide. Box A’s right wall is to the left of Box B’s left wall (too far left) Box A’s left wall is to the right of Box B’s right wall (too far right) Box A’s bottom wall is above Box B’s top wall (too far above) Box A’s top wall is below Box B’s bottom wall (too far below) If at least one of these tests is true it is impossible for a collision to occur, return false. (compound OR statement) However, if ALL four tests fail, the only logical conclusion is the opposite of an impossible collision, a definite collision, return true. Box B A A A A

Box-to-Box Step 1: Define the shape A box is an (x,y) coordinate (top left corner) and a width and height Step 2: Understand the dimensions of your shapes We know the coordinate of the top left corner of the box, we can calculate the other four corners using this information as well as the box’s dimensions. To take this one step further, we can define the four walls with respect to these values to make our logic simpler to read and understand Left Wall: x Right Wall: x + w Top Wall: y Bottom Wall: y + h w (x,y) (x + w,y) h (x,y + h) (x + w,y + h)

Box-to-Box: Test The test is a simple four part compound if statement testing each impossible collision scenario If any impossible test is true, there is no collision, return false. If all four tests are false, there must be a collision, return true. We can use the original dimensions of the shapes or to make things more readable, you could define variables for the four walls private boolean BoxBoxTest(Rectangle box1, Rectangle box2) { if ((box1.x + box1.width) < box2.x) || //(b1.right < b2.left) box1.x > (box2.x + box2.width) || //(b1.left > b1.right) (box1.y + box1.height) < box2.y) || //(b1.bottom < b2.top) box1.y > (box2.y + box2.height)) || //(b1.top > b2.bottom) { //impossible collision return false; } else { //definite collision return true; } }