Physics Engines Physics in Games

Physics Engines Physics in Games

the gamedesigninitiative at cornell university Lecture 19 Physics Engines Physics in Games Moving objects about the screen Kinematics: Motion ignoring external forces (Only consider position, velocity, acceleration) Dynamics: The effect of forces on the screen Collisions between objects Collision Detection: Did a collision occur? Collision Resolution: What do we do? the gamedesigninitiative 2 Physics Overview at cornell university Physics in Games Moving objects about the screen Kinematics: Motion ignoring external forces (Only considerClass position, Body velocity, acceleration) Dynamics: The effect of forces on the screen Collisions between objects Collision Detection: Did a collision occur? Fixture Collision ResolutionClass : What do we do? the gamedesigninitiative 3 Physics Overview at cornell university Body in Box2D Represents a single point Center of the object’s mass Object must move as unit Properties in class Body Position Linear Velocity Body Angular Velocity Body Type There are 3 body types Static: Does not move Kinematic: Moves w/o force Dynamic: Obeys forces the gamedesigninitiative 4 Collisions at cornell university Body in Box2D Represents a single point Center of the object’s mass Linear Object must move as unit Velocity Properties in class Body Angular Velocity Position Linear Velocity Angular Velocity Body Type Position There are 3 body types Static: Does not move Kinematic: Moves w/o force Dynamic: Obeys forces the gamedesigninitiative 5 Collisions at cornell university Body in Box2D Represents a single point Kinematic is rarely useful Center of the object’s mass Limited collision detection Object must move as unit Only collides w/ dynamics Properties in class Body Does not bounce or react Position Application: Bullets Linear Velocity Light, fast-moving objects Angular Velocity Body Type Should not bounce There are 3 body types Static: Does not move Looks like Kinematic: Moves w/o force last lecture Dynamic: Obeys forces the gamedesigninitiative 6 Collisions at cornell university Forces vs. Impulses Forces Impulses Instantaneous push Push with duration Like a slow push Like a car crash Gradually accelerates Quickly accelerates Momentum if sustained Immediate momentum Impulse = Force x Time Impulse the gamedesigninitiative 7 Collisions at cornell university Forces vs. Impulses Forces Impulses Instantaneous push Push with duration Like a slow push Like a car crash Gradually accelerates Quickly accelerates Momentum if sustained Immediate momentum Impulse = Force x 1 Sec Impulse in Box2D/Farseer the gamedesigninitiative 8 Collisions at cornell university Four Ways to Move a Dynamic Body Forces ApplyForce (linear) ApplyTorque (angular) Force Torque Impulses ApplyLinearImpulse ApplyAngularImpulse Velocity LinearVelocity AngularVelocity Translation SetTransform the gamedesigninitiative 9 Collisions at cornell university Four Ways to Move a Dynamic Body Forces Great for joints, complex shapes ApplyForce (linear) Laggy response to user input ApplyTorque (angular) A bit hard to control Impulses Great for joints, complex shapes ApplyLinearImpulse Good response to user input ApplyAngularImpulse Extremely hard to control Velocity Bad for joints, complex shapes LinearVelocity Excellent response to user input AngularVelocity Very easy to control Translation Completely ignores physics! SetTransform Very easy to control the gamedesigninitiative 10 Collisions at cornell university Example: Box2D Demo the gamedesigninitiative 11 Collisions at cornell university Example: Box2D Demo Controls: WASD for linear force Left-right arrows to rotate 9 or 0 to change controls the gamedesigninitiative 12 Collisions at cornell university Four Ways to Move a Dynamic Body Forces ApplyForce (linear) ApplyTorque (angular) Impulses Must Cap Velocity ApplyLinearImpulse ApplyAngularImpulse Velocity LinearVelocity AngularVelocity Translation SetTransform the gamedesigninitiative 13 Collisions at cornell university Basic Structure of a Update Loop public void Update(float dt) { // Apply movement to relevant bodies if (body above or equal to max velocity) { body.LinearVelocity = maximum velocity } else { body.ApplyForce(force) body.ApplyTorque(torque) } // Use physics engine to update positions world.step(dt); } the gamedesigninitiative 14 Collisions at cornell university Basic Structure of a Update Loop public void Update(float dt) { // Apply movement to relevant bodies if (body above or equal to max velocity) { body.LinearVelocity = maximum velocity } else { body.ApplyForce(force) body.ApplyTorque(torque) } // Use physics engine to update positions world.step(dt,num_iterations); } Multiple times to improve accuracy the gamedesigninitiative 15 Collisions at cornell university Basic Structure of a Update Loop public void Update(float dt) { // Apply movement to relevant bodies if (body above or equal to max velocity) { body.LinearVelocity = maximum velocity } else { Only before body.ApplyForce(force) first iteration! body.ApplyTorque(torque) } // Use physics engine to update positions world.step(dt,num_iterations); } Multiple times to improve accuracy the gamedesigninitiative 16 Collisions at cornell university ForceControllers Special object in Lab 4 Added to World object World Implements Update() Applies forces/velocity Must for multiple iterations ForceController ForceController World uses it as a callback Executed every iteration Good idea in general ForceController Cleanly separates controls Might need iterations later the gamedesigninitiative 17 Collisions at cornell university Collision Objects in Box 2D Shape Fixture Stores the object geometry Attaches a shape to a body Boxes, ellipses or polygons Fixture has only one body Must be convex! Bodies have many fixtures Has own coordinate space Cannot change the shape Associated body is origin Must destroy old fixture Unaffected if body moved Must make a new fixture Cannot be resized later Has other properties Also stores object density Friction: stickiness Mass is area x density Restitution: bounciness the gamedesigninitiative 18 Collisions at cornell university Making a Box2D Physics Object // Make body and place it body = BodyFactory.CreateBody (world); body.BodyType = type; body.Position = position; // Create a shape PolygonShape shape = MyFunction(dimensions, density) // Attach with fixture and set properties fixture = body.CreateFixture(shape); fixture.Friction = friction; fixture.Restitution = restitution; the gamedesigninitiative 19 Collisions at cornell university Making a Box2D Physics Object // Make body and place it body = BodyFactory.CreateBody (world); body.BodyType = type; body.Position = position; Motivation for multiple classes // Create a shape PolygonShape shape = MyFunction(dimensions, density) // Attach with fixture and set properties fixture = body.CreateFixture(shape); fixture.Friction = friction; fixture.Restitution = restitution; the gamedesigninitiative 20 Collisions at cornell university Observations on the Parameters Density can be anything non-zero The higher the density the higher the mass Heavier objects are harder to move Friction should be within 0 to 1 Can be larger, but effects are unpredictable Affects everything, even manual velocity control Restitution should be within 0 to 1 A value of 0 means no bounciness at all Unpredictable with manual velocity control the gamedesigninitiative 21 Collisions at cornell university Example: Box2D Demo the gamedesigninitiative 22 Collisions at cornell university Example: Box2D Demo Controls: 1 or 2 to change density 3 or 4 to change friction 5 or 6 to change restitution 7 or 8 to change shape the gamedesigninitiative 23 Collisions at cornell university How Do We Find the Shape? Do not try to learn boundary Image recognition is hard Hull will have many sides Have artists draw the shape Cover shape with triangles But can ignore interiors Keep # sides small! Store shape in another file Do not ruin the art! Need coordinates as data the gamedesigninitiative 24 Collisions at cornell university Data-Driven Design character.jpg character.shape 120,2" 130,4" 125,50" 150,65" 160,100" 150,110" 125,80" 140,200" 130,200" 120,110" …" ! the gamedesigninitiative 25 Collisions at cornell university Customized Collisions: ContactHandlers Special methods attached to world object Return any two fixtures that collide Allow you to override collision behavior Or you can augment collision behavior World manages two lists of handlers BeginContact List: When objects first collide EndContact List: When objects no longer collide Example: Color changing in Box2D demo the gamedesigninitiative 26 Collisions at cornell university Issues with Collisions: Tunneling Collisions in midstep can lead to tunneling Objects that “pass through” each other Not colliding at start or end of simulation But they collided somewhere in between This is an example of a false negative This is a serious problem; cannot ignore Players getting places they shouldn’t Players missing an event trigger boundary the gamedesigninitiative 27 Collisions at cornell university Tunneling the gamedesigninitiative 28 Collisions at cornell university Tunneling: Observations Small objects tunnel more easily Fast-moving objects tunnel more easily the gamedesigninitiative 29 Collisions at cornell university Tunneling: Observations Small objects tunnel more easily Fast-moving objects tunnel more easily the gamedesigninitiative

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    40 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us