Download presentation
Presentation is loading. Please wait.
1
CSE 380 – Advanced Game Programming Sweep & Prune
2
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]
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
4
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
5
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)))?
6
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
7
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)
8
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
9
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
10
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
11
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; };
12
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
13
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
14
Oh, and Collision Events What events might these be? Some Invoked on bot to bot basis –Damage Some invoked globally –Game over
15
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) …
16
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
17
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
18
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
19
References Advanced Collision Detection Techniques –http://www.gamasutra.com/features/20000330/bobic_01.htmhttp://www.gamasutra.com/features/20000330/bobic_01.htm N-Tutorials: Collision Detection & Response –http://www.harveycartel.org/metanet/tutorials/tutorialA.htmlhttp://www.harveycartel.org/metanet/tutorials/tutorialA.html Physics in BSP Trees: Collision Detection –http://www.devmaster.net/articles/bsp-trees-physics/http://www.devmaster.net/articles/bsp-trees-physics/ 3D Game Engine Design by David H Eberly
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.