Collision detection. Piotr Fulma«ski. 19 pa¹dziernika Wydziaª Matematyki i Informatyki, Uniwersytet Šódzki, Polska

Similar documents
Collision detection. Piotr Fulma«ski. 1 grudnia

Using Bounding Volume Hierarchies Efficient Collision Detection for Several Hundreds of Objects

Lesson 05. Mid Phase. Collision Detection

Physics Tutorial 4: Collision Detection

Geometric Computations for Simulation

2D rendering takes a photo of the 2D scene with a virtual camera that selects an axis aligned rectangle from the scene. The photograph is placed into

Math for Gameplay / AI. John O Brien Senior Gameplay Programmer Insomniac Games, Inc

Rigid Body Simulation. Jeremy Ulrich Advised by David Mount Fall 2013

Simulation (last lecture and this one)

Collisions/Reflection

CPSC / Sonny Chan - University of Calgary. Collision Detection II

Collision Detection II. These slides are mainly from Ming Lin s course notes at UNC Chapel Hill

This blog addresses the question: how do we determine the intersection of two circles in the Cartesian plane?

Collision and Proximity Queries

Lecture 13 Geometry and Computational Geometry

Collision Detection. These slides are mainly from Ming Lin s course notes at UNC Chapel Hill

Glossary of dictionary terms in the AP geometry units

Spatial Data Structures

Spatial Data Structures

Collision Detection with Bounding Volume Hierarchies

Collision Detection between Dynamic Rigid Objects and Static Displacement Mapped Surfaces in Computer Games

Computer Graphics (CS 543) Lecture 13b Ray Tracing (Part 1) Prof Emmanuel Agu. Computer Science Dept. Worcester Polytechnic Institute (WPI)

Collision handling: detection and response

Intersection of an Oriented Box and a Cone

Tomas Akenine-Möller Department of Computer Engineering Chalmers University of Technology

Computational Geometry

Virtual Environments

Lecture 25 of 41. Spatial Sorting: Binary Space Partitioning Quadtrees & Octrees

Course Number: Course Title: Geometry

Basics of Computational Geometry

Dynamic Collision Detection

Geometry Definitions and Theorems. Chapter 9. Definitions and Important Terms & Facts

Lecture 11: Ray tracing (cont.)

Some Open Problems in Graph Theory and Computational Geometry

Overview. Collision Detection. A Simple Collision Detection Algorithm. Collision Detection in a Dynamic Environment. Query Types.

Curriki Geometry Glossary

Geometry proxies (in 2D): a Convex Polygon

Collision Detection. Jane Li Assistant Professor Mechanical Engineering & Robotics Engineering

A Different Approach for Continuous Physics. Vincent ROBERT Physics Programmer at Ubisoft

UNM - PNM STATEWIDE MATHEMATICS CONTEST XLI. February 7, 2009 Second Round Three Hours

Interactive Math Glossary Terms and Definitions

COMPUTER GRAPHICS - MIDTERM 1

Ray Tracing Acceleration Data Structures

Section 12.1 Translations and Rotations

The radius for a regular polygon is the same as the radius of the circumscribed circle.

Aston Hall s A-Z of mathematical terms

It s hard to believe that it s already The last year moved at an incredible

The Geometry of Carpentry and Joinery

layers in a raster model

Prime Time (Factors and Multiples)

Ray-tracing Acceleration. Acceleration Data Structures for Ray Tracing. Shadows. Shadows & Light Sources. Antialiasing Supersampling.

Continuous Collision Detection and Physics

Accelerating Geometric Queries. Computer Graphics CMU /15-662, Fall 2016

Spatial Data Structures and Speed-Up Techniques. Tomas Akenine-Möller Department of Computer Engineering Chalmers University of Technology

Grade 9 Math Terminology

Spatial Data Structures

CURVES OF CONSTANT WIDTH AND THEIR SHADOWS. Have you ever wondered why a manhole cover is in the shape of a circle? This

We have set up our axioms to deal with the geometry of space but have not yet developed these ideas much. Let s redress that imbalance.

Data Organization and Processing

Topics. Ray Tracing II. Intersecting transformed objects. Transforming objects

Developmental Math An Open Program Unit 7 Geometry First Edition

Dynamic Bounding Volume Hierarchies. Erin Catto, Blizzard Entertainment

Collision Detection of Triangle Meshes using GPU

Spatial Data Structures

Topic 6 Representation and Description

Lecture 3: Art Gallery Problems and Polygon Triangulation

Polygon decomposition. Motivation: Art gallery problem

Properties of a Circle Diagram Source:

Math 113 Exam 1 Practice

l Without collision detection (CD), it is practically impossible to construct e.g., games, movie production tools (e.g., Avatar)

CMSC 425: Lecture 10 Geometric Data Structures for Games: Index Structures Tuesday, Feb 26, 2013

Grade 6 Math Circles October 16 & Non-Euclidean Geometry and the Globe

Topics. Ray Tracing II. Transforming objects. Intersecting transformed objects

Acceleration Data Structures

Ray Tracing Basics I. Computer Graphics as Virtual Photography. camera (captures light) real scene. photo. Photographic print. Photography: processing

Announcements. Written Assignment2 is out, due March 8 Graded Programming Assignment2 next Tuesday

Ray Tracing. Cornell CS4620/5620 Fall 2012 Lecture Kavita Bala 1 (with previous instructors James/Marschner)

Overview. Collision detection. Collision detection. Brute force collision detection. Brute force collision detection. Motivation

Bridges To Computing

9. Visible-Surface Detection Methods

6 Randomized rounding of semidefinite programs

1 Proximity via Graph Spanners

Grids Geometry Computational Geometry

References. Additional lecture notes for 2/18/02.

From Vertices To Fragments-1

Scene Management. Video Game Technologies 11498: MSc in Computer Science and Engineering 11156: MSc in Game Design and Development

Basic and Intermediate Math Vocabulary Spring 2017 Semester

1.OA.1. 1.OA.2 MP: Make sense of problems and persevere in solving them MP: Model with mathematics. Common Core Institute

Chapter 8. Voronoi Diagrams. 8.1 Post Oce Problem

Polygon Triangulation

Principles of Computer Game Design and Implementation. Revision Lecture

Mathematics Curriculum

Chapter 11 Arc Extraction and Segmentation

Mathematical Approaches for Collision Detection in Fundamental Game Objects

3.7. Vertex and tangent

Ray shooting from convex ranges

Objects DO overlap. Objects DO NOT overlap. No calculations needed.

Advanced Ray Tracing

Today. Acceleration Data Structures for Ray Tracing. Cool results from Assignment 2. Last Week: Questions? Schedule

Physically-Based Laser Simulation

Transcription:

Collision detection Piotr Fulma«ski Wydziaª Matematyki i Informatyki, Uniwersytet Šódzki, Polska 19 pa¹dziernika 2015

Table of contents

Collision in games Algorithms to detect collision in games depend on the type of shapes that can collide (e.g. rectangle to rectangle, rectangle to circle, circle to circle). Generally we have a simple generic shape known as a "hitbox" that covers somehow the entity so even though collision may not be pixel perfect, it will look good enough and be performant across multiple entities.

Collision in games Dierent hitbox: bounding sphere

Collision in games Dierent hitbox: axis aligned bounding boxes (AABB)

Collision in games Dierent hitbox: oriented bounding boxes (OBB)

Collision in games Dierent hitbox: capsule

Collision in games Dierent hitbox: convex polygon

Collision in games Dierent hitbox: concave polygon

Collision in games Dierent hitbox: concave polygon A simple polygon that is not convex is called concave or non-convex (sometimes also reentrant). It is always possible to partition a concave polygon into a set of convex polygons 1. 1 A polynomial-time algorithm for nding a decomposition into as few convex polygons is described in: Chazelle, Bernard; Dobkin, David P., Optimal convex decompositions, in Toussaint, G.T., Computational Geometry, Elsevier 1985, pp. 63133.

Collision in games Dierent hitbox: concave polygon

Collision in games Multiple levels of collision It's also worth noting that it's not uncommon for games to have multiple levels of collision geometry per object. This way, a simple collision check (such as with spheres) can be performed rst to check whether there's any possibility whatsoever that two objects collided. If the simple collision check says there might be a collision, we can then perform calculations with more complex collision geometry.

Collision in games Sphere versus sphere intersection Two spheres intersect if the distance between their center points is less than the sum of their radii. However, computing a distance requires the use of a square root, so to avoid the square root, it is common instead to check the distance squared against the sum of the radii squared.

Collision in games Sphere versus sphere intersection The algorithm for this is only a couple of lines of code. It's also extremely ecient, which is what makes using spheres a very popular basic collision detection option. function SphereIntersection(BoundingSphere a, BoundingSphere b) // Construct a vector between centers, and get length squared Vector3 centervector = b. center - a. center // Recall that the length squared of v is the same as v dot v float distsquared = DotProduct(centerVector, centervector) // Is distsquared < sum of radii squared? if distsquared < ((a.radius+b.radius)*(a.radius+b.radius)) return true else return false end end

Collision in games AABB versus AABB intersection As with spheres, AABB intersection is not very expensive, even for 3D games.

Collision in games AABB versus AABB intersection In 2D case when checking for intersection between two AABBs, rather than trying to test the cases where the two AABBs do intersect, it's easier to test the four cases where two AABBs denitely cannot intersect.

Collision in games AABB versus AABB intersection function AABBIntersection(AABB2D a, AABB2D b) bool test = (a.max.x < b.min.x) (b.max.x < a.min.x) (a.max.y < b.min.y) (b.max.y < a.min.y) return!test end

Separating Axis Theorem (SAT) Introduction The Separating Axis Theorem, SAT for short, is a method to determine if two convex shapes are intersecting. The algorithm can also be used to nd the minimum penetration vector. SAT is a fast generic algorithm that can remove the need to have collision detection code for each shape type pair thereby reducing code and maintenance.

Separating Axis Theorem Only for convex SAT can only handle convex shapes, but this is not a problem because as we have see non-convex shapes can be represented by a combination of convex shapes (called a convex decomposition). We can then test each convex shape to determine collision for the whole shape.

Separating Axis Theorem (SAT) An idea of projection The concept that SAT uses is projection. Imagine that you have a light source whose rays are all parallel. If you shine that light at an object it will create a shadow on a surface. A shadow is a two dimensional projection of a three dimensional object. The projection of a two dimensional object is a one dimensional "shadow".

Separating Axis Theorem (SAT) Theorem Theorem (Separating Axis Theorem (SAT)) If two disjoint convex sets in n-dimensional Euclidean space sets are closed and at least one of them is compact, then there is a hyperplane in between them and even two parallel hyperplanes in between them separated by a gap. We can refolmulate this a little bit mathematical statement into more readable version Theorem (Separating Axis Theorem (SAT)) If two convex objects are not collide, there exists an axis for which the projection of the objects will not overlap.

Separating Axis Theorem (SAT) Theorem

Separating Axis Theorem (SAT) Analogy for SAT Imagine taking a torch and shining it on the two shapes we are testing from dierent angles.

Separating Axis Theorem (SAT) Analogy for SAT If we work our way around the shapes and never nd a gap in the shadows then the objects must be touching. If we nd a gap, then they are clearly not touching.

Separating Axis Theorem (SAT) Obtaining the separating axes From a programming point of view it would be to intensive to check every possible angle. Luckily, due to the nature of the polygons, there is only a few key angles we need to check. The number of angles we need to check are the same as the number of sides of the polygons. This means that the maximum number of angles to check is the sum of the number of sides the two shapes we are testing have.

Separating Axis Theorem (SAT) Obtaining the separating axes From a programming point of view it would be to intensive to check every possible angle. Luckily, due to the nature of the polygons, there is only a few key angles we need to check. The number of angles we need to check are the same as the number of sides of the polygons. This means that the maximum number of angles to check is the sum of the number of sides the two shapes we are testing have.

Separating Axis Theorem (SAT) Obtaining the separating axes

Separating Axis Theorem (SAT) Obtaining the separating axes

Separating Axis Theorem (SAT) Obtaining the separating axes

Separating Axis Theorem (SAT) Obtaining the separating axes From the programmer point of view, the axes we must test are the normals of each shape's edges.

Separating Axis Theorem (SAT) Normals The normals of the edges can be easily obtained by ipping the coordinates and negating one.

Separating Axis Theorem (SAT) Determine that the shapes are not intersecting SAT may test many axes for overlap, however, the rst axis where the projections are not overlapping, the algorithm can immediately exit determining that the shapes are not intersecting. Because of this early exit, SAT is ideal for applications that have many objects but few collisions.

Separating Axis Theorem (SAT) Determine that the shapes are intersecting If, for all axes, the shape's projections overlap, then we can conclude that the shapes are intersecting. Note that to determine intersection, all axes must be tested for overlap.

Separating Axis Theorem (SAT) Determine that the shapes are intersecting

Separating Axis Theorem (SAT) Projecting a shape onto an axis When we know how to obtain the separating axes another question is how to project a shape onto an axis. Fortunately to project a polygon onto an axis is relatively simple. It is enought to loop over all the vertices performing the dot product with the axis and storing the minimum and maximum.

Separating Axis Theorem (SAT) Projecting a shape onto an axis

Separating Axis Theorem (SAT) Projecting a shape onto an axis As we have seen, SAT is a simple but repetitive method. Here we have one iteration of it, step by step: 1 Take one side from one of the polygons we are testing and nd the normal (perpendicular) vector from it. This will be the `axis'. 2 Loop through every point on the rst polygon and project it onto the axis. Keep track of the highest and lowest values found for this polygon. 3 Do the same for the second polygon. 4 Check the values you found and see if they overlap. If there is no gap, then polygons might be touching and we have to keep checking until we have gone through every side of both polygons. If we get through them all without nding a gap then they collide.

Separating Axis Theorem (SAT) Circle case Testing a circle against a polygon in SAT is a little bit strange but it can be done. The main thing to note is that a circle does not have any sides so there is no obvious axis that we can test against. Other words, there are innitely many axis we should take into account. There is one 'not so obvious' axis we do need to test however. This is the axis that runs from the centre of the circle to the closest vertex on the polygon.

Separating Axis Theorem (SAT) Circle case

Separating Axis Theorem (SAT) Circle case After that it is just a matter of going through the usual routine of looping through every axis on the other polygon and checking for overlaps. To project a circle onto the axis, we simply project the centre point of the circle and then add and subtract the radius.

Separating Axis Theorem (SAT) Finding the Minimum Translation Vector (MTV) In addition to returning true or false depending on whether the two shapes are intersecting SAT can return a Minimum Translation Vector (MTV). The MTV is the minimum magnitude vector used to push the shapes out of the collision. That axis which has the smallest overlap and that overlap is the MTV, the axis being the vector portion, and the overlap being the magnitude portion. To determine if the shapes are intersecting we must loop over all the axes from both shapes, so at the same time we can keep track of the minimum overlap and axis to be able to return a MTV when the shapes intersect.

Collision in games Bullet-through-paper problem To this point, we have covered instantaneous collision detection algorithms. This means the algorithm checks to see if the two objects collide on the current frame. Although this can work in many cases, there are some instances where it won't.

Collision in games Bullet-through-paper problem If a bullet is red at a piece of paper, it's unlikely there will be a precise frame where the bullet and the paper intersect with each other. That's because the bullet is travelling so fast and the paper is so thin. This problem is ttingly known as the bullet-through-paper problem.

Collision in games Swept sphere intersection In order to solve this problem, we can use either form of continuous collision detection (CCD) or simpler swept sphere intersection. In swept sphere intersection, there are two moving spheres. The inputs are the positions of both spheres during the last frame (t = 0) and the current frame (t = 1). Given these values, we can determine whether or not the two spheres collided at any point between the two frames. So unlike the instantaneous sphere-sphere intersection, it won't miss out on any intersections that occur between frames.

Collision in games Swept sphere intersection

Collision in games Swept sphere intersection We might notice that a swept sphere looks a lot like a capsule. That's because a swept sphere is a capsule. A swept sphere has a start point, end point, and a radius, which is exactly like a capsule. So the algorithm discussed for this case can also be used for "capsule versus capsule" intersection. Algorithm: use SAT, use dedicated algorithm like one described in: Sanjay Madhav, Game Programming Algorithms and Techniques, Addison-Wesley, 2014, pp 141-145.

Collision performance Broad and narrow phase Although some of algorithms for collision detection are simple enough to calculate, it can be a waste of time to test every entity with every other entity. Usually games will split collision into two phases, broad and narrow. Broad Phase Broad phase should give us a list of entities that possibly could be colliding. This can be implemented with a spacial data structure that will give us a rough idea of where the entity exists and what exist around it. Some examples of spacial data structures are Quad Trees, R-Trees but we can also try to use less sophisticated but not less eective like spacial hashmap. Narrow Phase When you have a small list of entities to check you will want to use a narrow phase algorithm (like the ones listed above) to provide a certain answer as to whether there is a collision or not.

Hash table A hash table (hash map) is a data structure used to implement an associative array, a structure that can map keys to values. A hash table uses a hash function to compute an index into an array of buckets or slots, from which the desired value can be found. The idea of hashing is to distribute the entries (key/value pairs) across an array of buckets. Given a key, the algorithm computes an index that suggests where the entry can be found.

Spatial Hashing A spatial hash is a 2 or 3 dimensional extension of the hash table. Typically the keys to a hash table would be strings, but in a spatial hash we use 2 or 3 dimensional points as the keys. And here is where the twist comes in: for a normal hash table, a good hash function distributes keys as evenly as possible across the available buckets, in an eort to keep lookup time short. The result of this is that keys which are very close (lexicographically speaking) to each other, are likely to end up in distant buckets. But in a spatial hash we are dealing with locations in space, and locality is very important to us (especially for collision detection), so our hash function will not change the distribution of the inputs.

Spatial Hashing There are a few problems with inserting objects other than simple points into the spatial hash: the object may overlap several cells/buckets if it is near the edge of a bucket, an object may actually be larger than a single bucket. To solve this problem we add the object to all relevant buckets.