1 / 19

CSE 380 – Advanced Game Programming Sweep & Prune

CSE 380 – Advanced Game Programming Sweep & Prune. 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 Sort the collisions, earliest first

zody
Download Presentation

CSE 380 – Advanced Game Programming Sweep & Prune

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CSE 380 – Advanced Game ProgrammingSweep & Prune

  2. 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 • Sort the collisions, earliest first • Move all affected objects to time of first collision, updating positions • Update velocities of collided objects • Go back to 1 for collided objects only • 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 NO COLLISION COLLISION A C B D

  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 University

  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: • Calculate position range on x-axis • extract (minX, maxX) • Place all (minX, maxX) pairs into array • Sort array by minX • now, it’s easy to see what overlaps in X axis • an O(N) problem, not O(N2). Why? • don’t compare all objects to one another, only ones “near” each other • note, we’ll have sorting costs, of course • For x-axis collided pairs, repeat for Y & Z axes • 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 • 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 • For each sprite, do the same as step 1 but for all Sprite-Sprite collisions • Sort the Collision array by time of collision • 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) • Resolve collision (change vX, vY of sprites involved) …

  16. GamePhysics::update strategy • 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 • Remove all collisions from array involved in resolved collision • 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 Ericson • [2] Physics for Game Programmers by Squirrel Eiserloh • [3] Collision Detection in Interactive 3D Environments by Gino van den Bergen

  18. References • [1] Real Time Collision Detection by Christer Ericson • [2] Physics for Game Programmers by Squirrel Eiserloh • [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.htm • N-Tutorials: Collision Detection & Response • http://www.harveycartel.org/metanet/tutorials/tutorialA.html • Physics in BSP Trees: Collision Detection • http://www.devmaster.net/articles/bsp-trees-physics/ • 3D Game Engine Design by David H Eberly

More Related