CSE 380 – Computer Game Programming Collision Detection & Response Erin Catto’s Box2D.

Slides:



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

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.
Ray Tracing CMSC 635. Basic idea How many intersections?  Pixels  ~10 3 to ~10 7  Rays per Pixel  1 to ~10  Primitives  ~10 to ~10 7  Every ray.
2.1. C OLLISION D ETECTION Overview. Collision detection is used within many types of application, e.g. from robotics, through engineering simulations,
CAP4730: Computational Structures in Computer Graphics Visible Surface Determination.
CSE 381 – Advanced Game Programming Scene Management
Week 14 - Monday.  What did we talk about last time?  Bounding volume/bounding volume intersections.
Collision Detection CSE 191A: Seminar on Video Game Programming Lecture 3: Collision Detection UCSD, Spring, 2003 Instructor: Steve Rotenberg.
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.
Adapted from: CULLIDE: Interactive Collision Detection Between Complex Models in Large Environments using Graphics Hardware Naga K. Govindaraju, Stephane.
Space Partitioning for Broad Sweep Collision Detection Part 1 - Grids Game Design Experience Professor Jim Whitehead February 11, 2009 Creative Commons.
Oct 26, Fall 2006IAT 4101 Collision Detection. Oct 26, Fall 2006IAT 4102 Collision Detection  Essential for many games –Shooting –Kicking, punching,
OBBTree: A Hierarchical Structure for Rapid Interference Detection Gottschalk, M. C. Lin and D. ManochaM. C. LinD. Manocha Department of Computer Science,
Haptic Cloth Rendering 6th Dutch-Belgian Haptics Meeting TUDelft, 21 st June 2006 Lode Vanacken Expertise centre for Digital Media (EDM) Hasselt University.
Bounding Volume Hierarchies and Spatial Partitioning Kenneth E. Hoff III COMP-236 lecture Spring 2000.
Oct 3, Fall 2005Game Design1 Collision Detection.
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.
Collision Detection CSE169: Computer Animation Instructor: Steve Rotenberg UCSD, Winter 2004.
Collision Detection David Johnson Cs6360 – Virtual Reality.
Computer graphics & visualization Collision Detection – Narrow Phase.
CS 4730 Collision Detection CS 4730 – Computer Game Design.
12/4/2001CS 638, Fall 2001 Today Using separating planes/axes for collision testing Collision detection packages.
Computer Graphics 2 Lecture x: Acceleration Techniques for Ray-Tracing Benjamin Mora 1 University of Wales Swansea Dr. Benjamin Mora.
INTERACTION TECHNIQUES Collision Detection and Other Interactions Collision Detection and Other Interactions.
CSE 380 – Computer Game Programming AI & Collision Strategy Erin Catto’s Box2D.
10/09/2001CS 638, Fall 2001 Today Spatial Data Structures –Why care? –Octrees/Quadtrees –Kd-trees.
CSE 380 – Computer Game Programming Box2D Box2D TestBed.
CSE 381 – Advanced Game Programming Quickhull and GJK.
Contact Manifolds Erin Catto Blizzard Entertainment.
Collision handling: detection and response
3D Graphics for Game Programming Chapter XII Physics-based Simulation.
Computer Animation Rick Parent Computer Animation Algorithms and Techniques Collisions & Contact.
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.
Using BSP for CD Ref: BSP in deBerg et al ’ s book (url)url.
Collaborative Visual Computing Lab Department of Computer Science University of Cape Town Graphics Topics in VR By Shaun Nirenstein.
1 KIPA Game Engine Seminars Jonathan Blow Ajou University December 6, 2002 Day 10.
CIS 350 – I Game Programming Instructor: Rolf Lakaemper.
Binary Space Partitioning Trees Ray Casting Depth Buffering
CSE 381 – Advanced Game Programming Collision Detection.
Comparison of Collision Detection Algorithms Tony Young M.Math Candidate July 19th, 2004.
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.
Presented by Paul Phipps
3D Object Representations 2011, Fall. Introduction What is CG?  Imaging : Representing 2D images  Modeling : Representing 3D objects  Rendering : Constructing.
1 Advanced Scene Management. 2 This is a game-type-oriented issue Bounding Volume Hierarchies (BVHs) Binary space partition trees (BSP Trees) “Quake”
3D Object Representations 2009, Fall. Introduction What is CG?  Imaging : Representing 2D images  Modeling : Representing 3D objects  Rendering : Constructing.
Hierarchical Data Structure in Game Programming Yanci Zhang Game Programming Practice.
Bounding Volume Hierarchies and Spatial Partitioning
Introduction to Collision Detection
Collision Detection Spring 2004.
Bounding Volume Hierarchies and Spatial Partitioning
3D Object Representations
2.1. Collision Detection Overview.
Parts of these slides are based on
Chapter 4.2 Collision Detection and Resolution
Collision handling: detection and response
Motion in Real and Virtual Worlds
Computer Animation Algorithms and Techniques
Collision Detection.
David Johnson Cs6360 – Virtual Reality
GPAT – Chapter 7 Physics.
Presentation transcript:

CSE 380 – Computer Game Programming Collision Detection & Response Erin Catto’s Box2D

Collision Detection Calculations What data are we looking for? 1.Do these two objects potentially collide? 2.Do these two objects collide? 3.When did these two objects collide? 4.Where did these two objects collide? where on geometry of objects, points of impact These 4 questions get progressively: –more computationally expensive to implement –more complex to implement (more math)

Phases of Collision Detection Spatial Partitioning Algorithms –problem reduction –only perform additional phases on pairs of object on same “islands” Broad Phase – early rejection tests –Do the coarse Bounding Volumes of two objects collide? Narrow Phase –Do the tight bounding volumes of two objects collide? –What are the contact points on object geometries? Done down to the last triangle in 3D games

Common Bounding Volumes Note that the space craft has been rotated Ref: [1], Figure 4.2 This semester, we like AABBs –axis-aligned bounding boxes

Bounding Volumes The base geometry used for all collision tests –instead of the shape’s geometry, which is too expensive Properties of desirable BVs: –inexpensive intersection tests –tight fitting –inexpensive to compute –easy to rotate and transform –use little memory Ref: [1]

Assumptions for this semester All sprites & game objects: –have rectangular Bounding Volumes (AABBs) –don’t rotate, or –if they do rotate, we don’t care about them colliding with stuff Thus: –no polytope collision detection equations –no rotation equations –this makes life much easier

How about a big, complicated object? We can have 2 AABBs –Don’t test them against each other

Spatial Partitioning First, reduce the problem only perform additional phases on pairs of object on same “islands” Common Solutions: –Octree Algorithms (quadtrees for 2D) –Uniform Grid Algorithms –also: Portals (ala Quake), BSP trees (Binary Space Partitions), Spatial Hashing, etc.

Octrees Used to divide a 3D world into “islands” –2D divided via Quadtrees Why? –to make rendering more efficient –to make collision detection more efficient What would be the data for these nodes? –region coordinates for cell though a smart implementation might eliminate this too –AABBs

Octree Source:

Octree Drawbacks Objects cross islands –octree has to be constantly updated –not so bad Objects straddle islands –collision detection may involve objects from multiple islands –can be a headache

Uniform Grids Fast mechanism for reducing the problem –used for 2D & 3D games 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/tileWidth sprite.Y/tileHeight (sprite.X+sprite.Width)/tileWidth (sprite.Y+sprite.Height)/tileHeight –For this guy, 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”

Side-scrollers simulate gravity –not necessarily accelerated (constant velocity) –move all affected sprites down by dY This way, characters can fall We must detect when sprites are colliding with a floor/platform Easy solution: make a tile with a walkable surface at the very top of the image Collision system will handle response Grid Cell Collision & Walkable Surfaces

Grid Cell Collision Advantages/Disadvantages Advantages –easy to implement –very fast (computationally inexpensive) Disadvantages –constrains look of game to grid-like world Action Game alternative: –use this algorithm for some board collision detection –use other algorithms for other collisions i.e. inclines

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

Broad Phase Do the coarse AABBs of two objects collide? Common solution: –separating axis algorithms –including temporal coherence More sophisticated solution: –Sweep & Prune –an extension of separating axis, more efficient for many elements

Narrow Phase What are the contact points on object geometries? –for 3D might be convex hulls Two Steps 1.determine potentially colliding primitives (ex: triangles) of a pair of objects AABB tree algorithms 2.determine contact between primitives GJK algorithms Ref[3] CSE 381 will cover these things next fall

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 –More on this in a moment

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 –collisions/physics when they happen likely to be mid-frame

Axis Separation Tests How do we know if two AABBs are colliding? –if we can squeeze a plane in between them –i.e. if their projections overlap on all axes A B C D NO COLLISIONCOLLISION

Be careful of Tunneling What’s that? An object moves through another object because collision is never detected

A note about timing If we are running at 30 fps, we are only rendering 1/30 th of the physical states of objects –Squirrel Eiserloh calls this Flipbook syndrome [2] More things happen that we don’t see than we do We likely won’t see the ball in contact with the ground

One way to avoid tunneling Swept shapes Potential problem: false positives –Good really only for early rejection tests

Better way to avoid tunneling Calculate first contact times Resolve contacts in order of occurrence

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

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

So how do we calculate the when? We can do it axis by axis –What’s the first time where there is overlap on all 3 axes? For each axis during a frame, what is the time of first and last contact? –t xf, t xl, t yf, t yl When is the time of collision? –first moment tx and ty overlap

What collision system should you use? AABB for each collidable object Velocity vector for each dynamic object Uniform Grids for problem reduction Continuous Collision Detection –using method of separating axis algorithm –momentum equations for collision responses if necessary –pweep and prune for efficiency

Physics & Timing Christer Ericson & Erin Catto’s recommendations: –physics frame rate should be higher resolution than rendering –minimum of 60 frames/second for physics calculations –makes for more precise, and so realistic interactions How do we manage this? –tie frame rate to 60 fps, but don’t render every frame –don’t worry about this for now Note: these operations might commonly be done in different threads anyway (have their own frame rates)

References [1] Real Time Collision Detection by Christer EricsonReal Time Collision Detection [2] Physics for Game Programmers by Squirrel EiserlohPhysics for Game Programmers [3] Collision Detection in Interactive 3D Environments by Gino van den Bergen