300 likes | 428 Views
Lecture 15: Physics. CS 490/590 Wesley Kerr. Physics. Issues that we must solve Moving objects around Collisions between objects Collision detection : did a collision occur? Collision resolution : what did we do?. Box2D Physics Library.
E N D
Lecture 15: Physics CS 490/590 Wesley Kerr
Physics • Issues that we must solve • Moving objects around • Collisions between objects • Collision detection: did a collision occur? • Collision resolution: what did we do?
Box2D Physics Library • Next week we’ll discuss the important parts for building your own physics implementation. • Two Modules • Dynamics Module • Collision Module • It is important to understand the units: • Mass: kilograms • Length: meters • Time: seconds
World • The 2D rectangular area where everything will take place. • Make the world bigger than the region where bodies will be stored • Additional Parameters • Gravity – vector in the direction of gravity (experienced by all dynamic objects) • Allow sleep – when a dynamic object slows down enough, put it to sleep World
Bodies • Static – will not move • In Box2D static objects have a mass = 0 • Dynamic – will move • In Box2D create dynamic objects by giving them mass • Define a body • Have the world create the body • Define a fixture • Create the shapes as part of the fixtures • Adjust mass as necessary
Body Creation BodyDef boolean activedoes this body start out active boolean awakedoes the body start out awake booleanallowSleepis this body going to be allowed to go to sleep? float anglethe starting angle of this body (radians) float angularDampingafter each update how much do we dampen the angular velocity by? float angularVelocity the angular velocity of the body booleanfixedRotationcan this body rotate? booleanbullet is this a fast moving body? float gravityScale scale the gravity applied to this body float linearDampingafter each update how much do we damping the linear velocity by? Vector2linearVelocity the velocity this body should begin with Vector2 positionthe starting position of this body BodyDef.BodyTypebodyType the body type (static, kinematic, dynamic) • Body b = world.createBody(BodyDef)
Shape creation • Shapes describe collision geometry CircleShape float radius – the radius of the circle PolygonShape List vertices – convex vertices in local coordinates Edge Vertex v1, v2 – start and end points of a line segment • Shapes provide the following capabilities • Test a point for overlap • Ray cast against the shape • Compute the AABB of the shape • Compute the mass properties of the shape
Fixture Creation • Fixtures attach shapes to bodies and provide information about the materials of the shape. FixtureDef float density – the density, usually in kg/m2 Filter filter – contact filtering data float friction – The friction coefficient of the shape [0,1] booleanisSensor– collects contact information but doesn’t generate a collision response float restitution – elasticity. in the range [0,1] Shape shape– holds the type of the shape • body.createFixture(FixtureDef)
Adjust the Mass • Dynamic: • body.setMassData(MassData) • MassData • Vector2 center – the center of mass • float I – the rotational inertia • float mass – the mass
Simulation • World.step(seconds, vIter, pIter) • Integrator • Move the world forward by a fixed amount of time • A good value for seconds is 1/60 • Constraint Solver • Velocity Phase – update impulses to make bodies move correctly • Position Phase – update positions to reduce overlap and joint detachment • Recommended values – velocity (8) and position (3)
Making Stuff Move • You can add force directly to objects • Body.applyForce(Vector2 force, Vector2 point) • You can add impulses directly to objects • Body.applyLinearImpulse(Vector2 impulse, Vector2 point) • You can also add torque and rotational velocity • Body.applyAngularImpulse(float impulse) • Body.applyTorque(float torque)
Collision Detection ContactListener beginContact(Contact) endContact(Contact) preSolve(Contact contact, Manifold manifold) preSolve(Contact contact, ContactImpulse impulse) Contact Fixture getFixtureA() Fixture getFixtureB() WorldManifoldgetWorldManifold() WorldManifold normal contactPoints
Joints • Constrains the motion of bodies • ragdolls, pulleys, etc.
Distance Joint • Constrain to bodies to stay within the same distance of each other. • Example Usage: • Picking up another object in a 2D top down world.
Revolute Joint • Two bodies share a common anchor point • 1 degree of freedom • The relative rotation of the two bodies • Example Usage: • Door
Prismatic Joint • Relative translation of two bodies along a specified axis. • 1 degree of freedom • Example Usage: • Elevator
Pulley Joint • Creates an idealized pulley • As one body goes up, the other goes down • The length of the rope is constant • Example Usage: • Pulley
Gear Joint • Most sophisticated joint • Combines 2 revolute or prismatic joints • Requires that you have two bodies connected to the ground body • Example Usage: • Elevator
Collision Detection • First try… • You have n objects in your world. • Compare each pair of objects to see if there is a collision. • Complexity O(n2) • What do we do to speed this up? • Spatial Partitioning Techniques
Uniform Grid • Divide the world into regular sized grids • Good • Accessing any cell and all of the objects inside is fast • Finding neighbor cells is also fast • Issues • Finding the right grid size is non-trivial
Uniform Grid • Heuristics for selecting the correct grid size • Select a grid size that is large enough to hold the largest object at any rotation • Twice as large as the average-sized object • What if you have a very large number of cells? • Spatial Hashing • Projects a 2D space onto a 1D hashtable • http://www.cs.ucf.edu/~jmesit/publications/scsc%202005.pdf • http://conkerjo.wordpress.com/2009/06/13/spatial-hashing-implementation-for-fast-2d-collisions/ • http://www.beosil.com/download/CollisionDetectionHashing_VMV03.pdf • Other things to keep in mind • Clear the grid after each update cycle or move objects? • If all of the objects are dynamic, it might be cheaper to rebuild
Hierarchical Grids • Construct a set of m grids with varying cell sizes
Hierarchical Grids • Lowest level should be large enough to encompass the smallest objects • Each additional level doubles the size
Sort and Sweep aka Sweep and Prune • We can project the beginning and ending of an AABB onto one of the global axes • Algorithm • Construct a sortedlist from the beginnings and endings of the AABBs for the GameObjects • Iterate (sweep) through the sorted list • Add the GameObject to a list when its beginning is encountered • Remove the GameObject from the list when its ending is encountered • Any new interval added to the list overlapsthe other active GameObjects on the list
Trees • Used by Box2D and Chipmunk Physics • Quadtrees, BSP Trees, k-d trees, etc. • Some Articles • http://bit.ly/VCgMDg • Some Books • Real-Time Collision Detection • Christer Ericson