Introduction to Collision Detection

Slides:



Advertisements
Similar presentations
Christian Lauterbach COMP 770, 2/16/2009. Overview  Acceleration structures  Spatial hierarchies  Object hierarchies  Interactive Ray Tracing techniques.
Advertisements

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.
Chapter 4.2 Collision Detection and Resolution. 2 Collision Detection Complicated for two reasons 1. Geometry is typically very complex, potentially requiring.
Collision Detection CSCE /60 What is Collision Detection?  Given two geometric objects, determine if they overlap.  Typically, at least one of.
Collision Detection and Resolution Zhi Yuan Course: Introduction to Game Development 11/28/
Computer graphics & visualization Collisions. computer graphics & visualization Simulation and Animation – SS07 Jens Krüger – Computer Graphics and Visualization.
2.1. C OLLISION D ETECTION Overview. Collision detection is used within many types of application, e.g. from robotics, through engineering simulations,
CSE 380 – Computer Game Programming Collision Detection & Response Erin Catto’s Box2D.
Week 14 - Monday.  What did we talk about last time?  Bounding volume/bounding volume intersections.
Space Partitioning for Broad Sweep Collision Detection Part 2 - Quadtrees Game Design Experience Professor Jim Whitehead February 13, 2009 Creative Commons.
Computer Science – Game DesignUC Santa Cruz Common Bounding Volumes Most introductory game programming texts call AABBs simply “bounding boxes” Circle/SphereAxis-Aligned.
Computer Science – Game DesignUC Santa Cruz Today Homework Bounding Boxes Quadtrees Per-pixel collision Drawing text (if we have time)
Computer Science – Game DesignUC Santa Cruz CMPS 20: Game Design Experience Gforge SVN Collision Detection and Resolution.
Chapter 4.2 Collision Detection and Resolution. 2 Collision Detection Complicated for two reasons 1. Geometry is typically very complex, potentially requiring.
1 Geometry A line in 3D space is represented by  S is a point on the line, and V is the direction along which the line runs  Any point P on the line.
Space Partitioning for Broad Sweep Collision Detection Part 1 - Grids Game Design Experience Professor Jim Whitehead February 11, 2009 Creative Commons.
Introduction to Collision Detection Lecture based on Real Time Collision Detection, Christer Ericson, Morgan Kauffman, 2005 Game Design Experience Professor.
1 Advanced Scene Management System. 2 A tree-based or graph-based representation is good for 3D data management A tree-based or graph-based representation.
Physics Simple – Pong Medium – Rigid bodies F = ma Circles, spheres, rectangles for collisions Complex – Fluids, clothings, explosions, hair.
10/11/2001CS 638, Fall 2001 Today Kd-trees BSP Trees.
Introduction to 3D Graphics John E. Laird. Basic Issues u Given a internal model of a 3D world, with textures and light sources how do you project it.
CS 4730 Collision Detection CS 4730 – Computer Game Design.
CSE 380 – Computer Game Programming AI & Collision Strategy Erin Catto’s Box2D.
2D Collision Detection For CSE 3902 By: Matt Boggus.
AI & 2D Development COSC 315 Fall 2014 Bridget M. Blodgett.
CSE 380 – Computer Game Programming Box2D Box2D TestBed.
Learning Game Maker Studio:
CSE 381 – Advanced Game Programming Quickhull and GJK.
Week 13 - Friday.  What did we talk about last time?  Ray/sphere intersection  Ray/box intersection  Slabs method  Line segment/box overlap test.
Collision/Acceleration University of British Columbia CPSC 314 Computer Graphics Jan-Apr 2013 Tamara Munzner.
12/4/2001CS 638, Fall 2001 Today Managing large numbers of objects Some special cases.
PRESENTED BY – GAURANGI TILAK SHASHANK AGARWAL Collision Detection.
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.
CIS 350 – I Game Programming Instructor: Rolf Lakaemper.
Game Maker Terminology
1 Perception and VR MONT 104S, Fall 2008 Lecture 21 More Graphics for VR.
CSE 381 – Advanced Game Programming Collision Detection.
ONLINE CONFERENCE DESIGN.BUILD.DELIVE R with WINDOWS PHONE THURSDAY 24 MARCH 2011.
Computer Game Design and Development
CSE 380 – Advanced Game Programming Sweep & Prune.
11/20/03CS679 - Fall Copyright Univ. of Wisconsin Last Time Collision Detection Overview Player-Environment handling.
1 Sage Demo 4 Collisions SAGE Lecture Notes Ian Parberry University of North Texas.
Computer Game Design and Development Collision Detection Havok Destruction.
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.
1 Advanced Scene Management. 2 This is a game-type-oriented issue Bounding Volume Hierarchies (BVHs) Binary space partition trees (BSP Trees) “Quake”
Introduction to Game Programming & Design III Lecture III.
Game Maker Tutorials Introduction Clickball IntroductionClickball Where is it? Shooting Where is it?Shooting.
Hierarchical Data Structure in Game Programming Yanci Zhang Game Programming Practice.
2D Graphics Optimizations
2D Collision Detection For CSE 3902 By: Matt Boggus.
Background Shapes & Collision Resolution (Top-down and Side-scrolling)
Intro & Point-to-Box, Circle-to-Circle, Point-to-Circle
Collision Detection Spring 2004.
Lecture 3 Announcements.
3D Object Representations
2.1. Collision Detection Overview.
Parts of these slides are based on
Projectiles in Unreal Engine 4
Chapter 4.2 Collision Detection and Resolution
Craig Schroeder October 26, 2004
Quadtrees 1.
Data Structures Review Session
What you will learn today
CS679 - Fall Copyright Univ. of Wisconsin
Computer Animation Algorithms and Techniques
Collision Detection.
CO Games Concepts Week 12 Collision Detection
Game Programming Algorithms and Techniques
David Johnson Cs6360 – Virtual Reality
Presentation transcript:

Introduction to Collision Detection 1

The problem can be defined as if, when and where two objects intersect Overview The problem can be defined as if, when and where two objects intersect

Collision Detection Collision Detection Determining if two objects intersect (true or false) Example: did bullet hit opponent Collision Resolution (Collision Determination) Determining when two objects came into contact At what point during their motion did they intersect? Example: Two balls needing to bounce off each other Determining where two objects intersect Which points on the objects touch during a collision? Example: Pong – where the ball hits the paddle is important Complexity of answering these questions increases in order given IF < WHEN < WHERE

Keys to Collision Detection Key constraint: only 16.667ms per clock tick Limited time to perform collision detection Object shapes in 2D games can be quite complex Naïve: check two objects pixel-by-pixel for collisions Many objects can potentially collide Naïve: check every object pair for collisions Drawback of naïve approach Takes too long Doesn’t scale to large numbers of objects Approaches for scaling Reduce # of collision pairs Reduce cost of each collision check

Naïve Collision Detection: n x n checking Assume n objects in a game All n objects can potentially intersect with each other Conceptually easiest approach Check each object against every other object for intersections Leads to (n-1) + (n-2) + … + 1 = n(n-1)/2 = O(n2) checks Done poorly, can be exactly n2 checks Example: 420 objects leads to 87,990 checks per clock tick

Broad vs. Narrow Sweep With many small objects in a large playfield Each object only has the potential to collide with nearby objects Broad Sweep Perform a quick pass over n objects to determine which pairs have potential to intersect p Narrow Sweep Perform p x p check of object pairs that have potential to intersect Dramatically reduces # of checks

Broad Sweep Approaches Grid Divide playfield into a grid of squares Place each object in its square Only need to check contents of square against itself and neighboring squares Space partitioning tree Subdivide space as needed into rectangles Use tree data structure to point to objects in space Only need to check tree leaves Quadtree, Binary Space Partition (BSP) tree

Reducing Cost of Checking Two Objects for Collision General approach is to substitute a bounding volume for a more complex object Desirable properties of bounding volumes Inexpensive intersection tests Tight fitting Inexpensive to compute Easy to rotate and transform Low memory use

Axis-Aligned Bounding Boxes (AABBs) struct Point { int x; int y; } Three common representations Min-Max Min-Widths Center-Radius min // min.x<=x<=max.x // min.y<=y<=max.y struct AABB { Point min; Point max; } // min.x<=x<=min.x+dx // min.y<=y<=min.y+dy int dx; // x width int dy; // y width // | c.x-x | <= rx | c.y-y | <= ry Point c; int rx; // x radius int ry; // y radius max min dx dy ry rx c

AABB Intersection (min-max) a.max.x<b.min.x a.min.x=b.min.x a.min.x>b.max.x Two AABBs intersect only if they overlap on both axes a.max.y<b.min.y bool IntersectAABB (AABB a, AABB b) { if (a.max.x < b.min.x || a.min.x > b.max.x) return false; if (a.max.y < b.min.y || a.min.y > b.max.y) return false; return true; } a.min.y=b.min.y a.min.y>b.max.y

AABB Intersection (min-width) -(a.min.x-b.min.x)>a.dx (a.min.x-b.min.x)>b.dx a.min.x=b.min.x Two AABBs intersect only if they overlap on both axes -(a.min.y-b.min.y)>a.dy bool IntersectAABB(AABB a, AABB b) { int t; t=a.min.x-b.min.x; if (t > b.dx || -t > a.dx) return false; t=a.min.y-b.min.y; if (t > b.dy || -t > a.dy) return false; return true; } // Note: requires more operations than // min-max case (2 more subtractions, 2 more negations) a.min.y=b.min.y (a.min.y-b.min.y)>b.dy

AABB Intersection (center - radius) b.c.x-a.c.x > a.rx+b.ry a.c.x-b.c.x > a.rx+b.rx a.c.x=b.c.x Two AABBs intersect only if they overlap on both axes b.c.y-a.c.y > a.ry+b.ry bool IntersectAABB(AABB a, AABB b) { if (Abs(a.c.x – b.c.x) > (a.r.dx + b.r.dx)) return false; if (Abs(a.c.y – b.c.y) > (a.r.dy + b.r.dy)) ) return false; return true; } // Note: Abs() typically single instruction on modern processors a.c.y=b.c.y a.c.y-b.c.y > a.ry+b.ry

Uniform Grids Fast mechanism for reducing the problem Steps Divide the world into a grid of equal sized cells Associate each object with the cells it overlaps Only objects sharing a cell are compared Serves 2 purposes Reduces the problem for sprite-sprite collision detection Provides solution for easy sprite-world collision detection

Calculating Grid Position What cells does our sprite currently occupy? How would you calculate that? For min/max tile rows/columns: sprite.X / tile.Width sprite.Y / tile.Height (sprite.X + sprite.Width) / tileWidth (sprite.Y + sprite.Height) / tileHeight For this character, cells (0, 0), (0, 1), (1, 0) & (1, 1) Only test against: Other objects in those cells Collidable tiles in those cells (treat like objects) Don’t let sprites occupy “collidable cells”

XNA and Rectangle methods Given two rectangles rectangleA and rectangleB Rectangle.Intersects (Rectangle) Returns true if the rectangles are intersecting, otherwise false Example: bool overlaps = rectangleA.Intersects (rectangleB); Rectangle.Intersect (Rectangle, Rectangle) // static method Returns the area where the two rectangles overlap, an empty rectangle if there is no overlap Rectangle overlap = Rectangle.Intersect (rectangleA, rectangleB);

Object Tests Premise Think of all collision tests as between pairs of collidable objects In our games this means: Sprite object to game tile object Sprite object to sprite object In a single game loop, for each sprite, we may have to check against: Many other sprites Many other tiles

Why does this matter…

Why does this matter…

Why does this matter…

Priori vs. Posteri Approaches to collision detection & response Priori: check if something is going to collide before moving it, then make the necessary correction Posteri: check if something has collided and make the necessary corrections

Discrete vs. Continuous Collision Detection Discrete collision detection Perform collision detection at sampled instances of time (like end of frame) Can lead to tunneling problems “contact data is obtained by computing the penetration depth of a collision” Continuous collision detection Perform collision detection in continuous space-time

Time in between frames We will make calculations and update velocities and positions at various times When? In response to: Input at start of frame AI at start of frame Collision/physics when they happen Likely to be mid-frame

Be careful of Tunneling An object moves through another object because collision is never detected

One way to avoid Tunneling Swept shapes Potential problem: False Positives Good really only for early rejection tests

Continuous Collision Detection For each moving object, compute what grid cells it occupies and will cross & occupy if its current velocity is added Find the first contact time between each object pair that may occupy the same grid cells Sort the collisions, earliest first Move all objects to time of first collision, updating positions Update velocities of collided objects in pair Go back to 1 for collided objects only If no more collisions, update all objects to end of frame

Times as % You might want to think of your times as % of the frame Then correct positions according to this % For example, if I know my object is supposed to move 10 units this frame (we’ve locked the frame rate), if a collision happens ½ way through, move all affected objects ½ distance of their velocity and recompute collisions data for collided data Re-sort collisions and find next contact time

% Example A at (1, 5), velocity of (4, -3) B stationary at (4, 4) When will they collide? When A goes one unit to the right How long will that take? If it takes 1 frame to go 4 units, it will take .25 frames to go 1

What about 2 moving objects Solution, give the velocity of one to the other for your calculations make one of them stationary then do the calculation as with grid tiles make sure you move both objects make sure you update the velocities of both objects

Mind your Collision Boundaries The collision boundary of a friendly object is bigger than the object. The collision boundary of a harmful object is smaller than the object. Friendly objects include power-ups Harmful objects include enemies The bigger collision makes it easier to interact with, or collect the friendly object. The smaller collision makes it easier to avoid the harmful object.

Mind your Collision Boundaries The green rectangles are the collision boundaries of friendly objects, and the red rectangles are harmful objects. The yellow rectangle is the player.

?

References Catto, Erin. Collision Detection & Response