
the gamedesigninitiative at cornell university Lecture 18 Box2D Physics 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 Physics Overview gamedesigninitiative 2 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 Physics Overview gamedesigninitiative 3 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 Collisions gamedesigninitiative 4 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 Collisions gamedesigninitiative 5 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 Collisions gamedesigninitiative 6 at cornell university Forces vs. Impulses Forces Impulses Instantaneous push Push with duration To be applied over time To be applied in one frame Gradually accelerates Quickly accelerates Momentum if sustained Immediate momentum Impulse = Force x Time Impulse the Collisions gamedesigninitiative 7 at cornell university Forces vs. Impulses Forces Impulses Instantaneous push Push with duration To be applied over time To be applied in one frame Gradually accelerates Quickly accelerates Momentum if sustained Immediate momentum Impulse = Force x 1 Sec Impulse in Box2D the Collisions gamedesigninitiative 8 at cornell university Four Ways to Move a Dynamic Body Forces applyForce (linear) applyTorque (angular) Force Torque Impulses applyLinearImpulse applyAngularImpulse Velocity setLinearVelocity setAngularVelocity Translation setTransform the Collisions gamedesigninitiative 9 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 setLinearVelocity Excellent response to user input setAngularVelocity Very easy to control Translation Completely ignores physics! setTransform Very easy to control the Collisions gamedesigninitiative 10 at cornell university Example: Box2D Demo the Collisions gamedesigninitiative 11 at cornell university Example: Box2D Demo Controls: WASD for linear force Left-right arrows to rotate 9 or 0 to change controls the Collisions gamedesigninitiative 12 at cornell university Four Ways to Move a Dynamic Body Forces applyForce (linear) applyTorque (angular) Impulses Must Cap Velocity applyLinearImpulse applyAngularImpulse Velocity setLinearVelocity setAngularVelocity Translation setTransform the Collisions gamedesigninitiative 13 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.setLinearVelocity(maximum velocity); } else { body.applyForce(force) body.applyTorque(torque) } // Use physics engine to update positions world.step(dt,vel_iterations,pos_iterations); } the Collisions gamedesigninitiative 14 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.setLinearVelocity(maximum velocity); } else { body.applyForce(force) body.applyTorque(torque) } // Use physics engine to update positions world.step(dt,vel_iterations,pos_iterations); } Multiple times to improve accuracy the Collisions gamedesigninitiative 15 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.setLinearVelocity(maximum velocity); } else { Only before body.applyForce(force) first iteration! body.applyTorque(torque) } // Use physics engine to update positions world.step(dt,vel_iterations,pos_iterations); } Multiple times to improve accuracy the Collisions gamedesigninitiative 16 at cornell university Collision Objects in Box 2D Shape Fixture Stores the object geometry Attaches a shape to a body Boxes, circles 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 Collisions gamedesigninitiative 17 at cornell university Making a Box2D Physics Object // Create a body definition // (this can be reused) bodydef = new BodyDef(); bodydef.type = type; bodydef.position.set(position); bodydef.angle = angle; // Allocate the body body1 = world.createBody(bodydef); // Another? bodydef.position.set(position2); body2 = world.createBody(bodydef); the Collisions gamedesigninitiative 18 at cornell university Making a Box2D Physics Object // Create a body definition // (this can be reused) bodydef = new BodyDef(); bodydef.type = type; bodydef.position.set(position); Normal Allocation bodydef.angle = angle; // Allocate the body body1 = world.createBody(bodydef); // Another? Optimized Allocation bodydef.position.set(position2); body2 = world.createBody(bodydef); the Collisions gamedesigninitiative 19 at cornell university Making a Box2D Physics Object // Create two triangles as shapes shape1 = new PolygonShape().; shape2 = new PolygonShape(); shape1.set(verts1); shape2.set(verts2); // Create a fixture definition fixdef = new FixtureDef(); fixdef.density = density; // Attach the two shapes to body fixdef.shape = shape1; fixture1 = body1.createFixture(fixdef); fixdef.shape = shape2; fixture2 = body1.createFixture(fixdef); the Collisions gamedesigninitiative 20 at cornell university Making a Box2D Physics Object // Create two triangles as shapes shape1 = new PolygonShape().; Other shapes possible shape2 = new PolygonShape(); shape1.set(verts1); shape2.set(verts2); // Create a fixture definition Also set friction and fixdef = new FixtureDef(); restitution parameters fixdef.density = density; // Attach the two shapes to body Reason for separating fixdef.shape = shape1; fixture1 = body1.createFixture(fixdef); Fixture & Body classes fixdef.shape = shape2; fixture2 = body1.createFixture(fixdef); the Collisions gamedesigninitiative 21 at cornell university Making a Box2D Physics Object // Create a body definition // Create two triangles as shapes // (this can be reused) shape1 = new PolygonShape().; bodydef = new BodyDef(); shape2 = new PolygonShape(); bodydef.type = type; shape1.set(verts1); shape2.set(verts2); bodydef.position.set(position); // Create a fixture definition bodydef.angle = angle; fixdef = new FixtureDef(); fixdef.density = density; // Allocate the body body1 = world.createBody(bodydef); // Attach the two shapes to body fixdef.shape = shape1; // Another? fixture1 = body1.createFixture(fixdef); bodydef.position.set(position2); fixdef.shape = shape2; body2 = world.createBody(bodydef); fixture2 = body1.createFixture(fixdef); the Collisions gamedesigninitiative 22 at cornell university Observations on Fixture 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 Collisions gamedesigninitiative 23 at cornell university Example: Box2D Demo the Collisions gamedesigninitiative 24 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 Collisions gamedesigninitiative 25 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
Details
-
File Typepdf
-
Upload Time-
-
Content LanguagesEnglish
-
Upload UserAnonymous/Not logged-in
-
File Pages42 Page
-
File Size-