CSE 380 – Advanced Game Programming Sweep & Prune.

Slides:



Advertisements
Similar presentations
Introduction to Macromedia Director 8.5 – Lingo
Advertisements

Advanced Character Physics
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/
Searching on Multi-Dimensional Data
Here is where my object is Here is where my object is going to be Here is where I want my object to be.
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.
David Luebke 1 5/20/2015 CS 332: Algorithms Quicksort.
ElastoLab A Physics Playground for Kids David Buck Simberon Inc.
9/5/06CS 6463: AT Computational Geometry1 CS 6463: AT Computational Geometry Fall 2006 Plane Sweep Algorithms and Segment Intersection Carola Wenk.
CSE 872 Dr. Charles B. Owen Advanced Computer Graphics1 Basic 3D collision detection We want to know if objects have touched Objects are considered to.
Lecture 8 – Collective Pattern Collectives Pattern Parallel Computing CIS 410/510 Department of Computer and Information Science.
Hashing Techniques.
Chapter 6: Vertices to Fragments Part 2 E. Angel and D. Shreiner: Interactive Computer Graphics 6E © Addison-Wesley Mohan Sridharan Based on Slides.
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.
Haptic Cloth Rendering 6th Dutch-Belgian Haptics Meeting TUDelft, 21 st June 2006 Lode Vanacken Expertise centre for Digital Media (EDM) Hasselt University.
Programming Fundamentals (750113) Ch1. Problem Solving
1 ES 314 Advanced Programming Lec 2 Sept 3 Goals: Complete the discussion of problem Review of C++ Object-oriented design Arrays and pointers.
Collision Detection Michael Fuller. Overlap testing Most Common Technique Most Error Prone Test if two bodies overlap.
Computer graphics & visualization Collision Detection – Narrow Phase.
Physics for Game Developers Jim Van Verth Christer Ericson Squirrel Eiserloh Gino van den Bergen Erin Catto Marq Singer.
Advanced Algorithm Design and Analysis (Lecture 9) SW5 fall 2004 Simonas Šaltenis E1-215b
CSE 380 – Computer Game Programming AI & Collision Strategy Erin Catto’s Box2D.
Fast and Simple Physics using Sequential Impulses
Lecture VII Rigid Body Dynamics CS274: Computer Animation and Simulation.
CSE 380 – Computer Game Programming Sprite Animation
CSE 380 – Computer Game Programming Box2D Box2D TestBed.
CSE 381 – Advanced Game Programming Quickhull and GJK.
Computer Animation Rick Parent Computer Animation Algorithms and Techniques Collisions & Contact.
3.4. C ONTACT G ENERATION Generating contacts between rigid bodies.
CO1301: Games Concepts Dr Nick Mitchell (Room CM 226) Material originally prepared by Gareth Bellaby.
12/4/2001CS 638, Fall 2001 Today Managing large numbers of objects Some special cases.
PRESENTED BY – GAURANGI TILAK SHASHANK AGARWAL Collision Detection.
David Luebke 1 6/3/2016 CS 332: Algorithms Heapsort Priority Queues Quicksort.
CIS 350 – I Game Programming Instructor: Rolf Lakaemper.
1Computer Graphics Implementation II Lecture 16 John Shearer Culture Lab – space 2
Implementation II Ed Angel Professor of Computer Science, Electrical and Computer Engineering, and Media Arts University of New Mexico.
CS 325 Introduction to Computer Graphics 03 / 22 / 2010 Instructor: Michael Eckmann.
Implementation II.
CSE 381 – Advanced Game Programming Collision Detection.
Chipmunk Physics Remember that we talked about this a bit when we did collision handlers for the space ship integration task (SpritesActionsPhysicsSound).
CS 2133: Data Structures Quicksort. Review: Heaps l A heap is a “complete” binary tree, usually represented as an array:
COSC 2007 Data Structures II Chapter 13 Advanced Implementation of Tables IV.
Collision Detection And Response Jae Chun KyungSoo Im Chau Vo Hoang Vu.
Computer Game Design and Development
CS274 Spring 01 Lecture 7 Copyright © Mark Meyer Lecture VII Rigid Body Dynamics CS274: Computer Animation and Simulation.
Game Programming 13 Physics in Games (cont.) 2010 년 2 학기 디지털콘텐츠전공.
Introduction to Game Programming & Design III Lecture III.
Concepts for Programming a Rigid Body Physics Engine Part 2 Presented by Scott Hawkins.
UNC Chapel Hill David A. O’Brien Automatic Simplification of Particle System Dynamics David O’Brien Susan Fisher Ming C. Lin Department of Computer Science.
11 Making Decisions in a Program Session 2.3. Session Overview  Introduce the idea of an algorithm  Show how a program can make logical decisions based.
2D Simulation of Rigid Bodies
Introduction to Collision Detection
Background Shapes & Collision Resolution (Top-down and Side-scrolling)
By: Ziji Song Advisor: Prof. J. Spinelli
Error Correcting Code.
Distance Computation “Efficient Distance Computation Between Non-Convex Objects” Sean Quinlan Stanford, 1994 Presentation by Julie Letchner.
Parts of these slides are based on
Chapter 4.2 Collision Detection and Resolution
CSE 381 – Advanced Game Programming Terrain
Implementation II Ed Angel Professor Emeritus of Computer Science
Programming Fundamentals (750113) Ch1. Problem Solving
Computer Animation Algorithms and Techniques
Collision Detection.
CO Games Concepts Week 12 Collision Detection
Implementation II Ed Angel Professor Emeritus of Computer Science
Presentation transcript:

CSE 380 – Advanced Game Programming Sweep & Prune

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 3.Sort the collisions, earliest first 4.Move all affected objects to time of first collision, updating positions 5.Update velocities of collided objects 6.Go back to 1 for collided objects only 7.If no more collisions, update all objects to end of frame Ref[3]

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

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

First time of contact between A & B For object A, in x-axis (xA, yA, vxA-vxB, vyA-vyB) Time = distance/velocity Since velocity = distance/time if ((xB-(Bwidth/2)) > (xA+(Awidth/2))) { tx_first_contact = (xB-(Bwidth/2) – (xA + (Awidth/2)))/(vxA-vxB); tx_last_contact = ((xB + (Bwidth/2)) – (xA-(Awidth/2)))/vxA-vxB); if (tx_first_contact > 1) no collision this frame } What if ((xB-(Bwidth/2)) < (xA+(Awidth/2)))?

Time Calculation Example Awidth = 2, Bwidth = 2 A at (1, 1), velocity of (4, 3) B at (4, 2), velocity of (-1, 0) When will they start colliding on x-axis? if ((xB-(Bwidth/2)) > (xA+(Awidth/2))) { tx_first_contact = (xB-(Bwidth/2) – (xA + (Awidth/2)))/(vxA-vxB); tx_last_contact = ((xB + (Bwidth/2)) – (xA-(Awidth/2)))/vxA-vxB); if (first_contact > 1) no collision } (0, 0, 0) tx_first_contact = 1/5 t tx_last_contact = 1t

Sweep & Prune Also called Sort & Sweep A broad phase algorithm This is an efficient way of managing data for method of separating axis (MSA) tests Baraff, D. (1992), Dynamic Simulation of Non- Penetrating Rigid Bodies, (Ph. D thesis), Computer Science Department, Cornell UniversityDynamic Simulation of Non- Penetrating Rigid Bodies, (Ph. D thesis)

Improvements on MSA It greatly reduces the number of object-object comparisons. How? –it only compares objects near each other on one of the axes (x/y or x/y/z) How? –sorting

Sweep & Prune approach Think of each axis separately, for each object considered: 1.Calculate position range on x-axis –extract (minX, maxX) 2.Place all (minX, maxX) pairs into array 3.Sort array by minX –now, it’s easy to see what overlaps in X axis –an O(N) problem, not O(N 2 ). Why? don’t compare all objects to one another, only ones “near” each other –note, we’ll have sorting costs, of course 4.For x-axis collided pairs, repeat for Y & Z axes 5.Pairs that overlapped on all 3 tests collided

Combining our Algorithms Sweep and Prune will tell us if a collision might happen Continuous collision detection will tell us when it happens –it might also tell us S & P gave us a false positive

What about physics? Every time we detect a collision, we can store data about that event in one of these objects: class Collision { public: CollidableObject *co1; CollidableObject *co2; float timeOfCollision; float startTimeOfXCollision; float startTimeOfYCollision; float endTimeOfXCollision; float endTimeOfYCollision; };

Collision Construction Warning Each frame you’ll test for collisions You may find multiple collisions each frame You’ll want to store info about each collision in a Collision object. Why? –so you can sort them –use them to update the CollidableObjects DON’T construct new Collision objects each frame –recycle them instead

Recycling Collision objects When your game starts, construct an array of Collision objects. How many? –100 should do, make it 1000 to be over-safe –an array stack is easiest When a collision is detected: –take a collision object from array to use When collision resolved: –put it back

Oh, and Collision Events What events might these be? Some Invoked on bot to bot basis –Damage Some invoked globally –Game over

GamePhysics::update strategy 1.For each sprite: find all collisions with tiles, make a Collision object for each and compute time of collision. Add each Collision object to a collisions array 2.For each sprite, do the same as step 1 but for all Sprite-Sprite collisions 3.Sort the Collision array by time of collision 4.If collisions array is not empty, move all sprites to time of first collision (change their x, y according to vX, vY and % of frame) 5.Resolve collision (change vX, vY of sprites involved) …

GamePhysics::update strategy 6.Execute collision response code –Ex: sprite type 1 collides with sprite type 2, so reduce HP of sprite type 1 –this step may be combined with step 5. i.e., sometimes you might not want to change velocity of a sprite after collision 7.Remove all collisions from array involved in resolved collision 8.Perform steps 1 & 2 only for sprites involved in collisions Continue to do these steps until you reach the end of the frame NOTE: after each collision resolution, make sure the objects are not colliding. Why? –floating point error

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

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

References Advanced Collision Detection Techniques – N-Tutorials: Collision Detection & Response – Physics in BSP Trees: Collision Detection – 3D Game Engine Design by David H Eberly