Slides 18
Slides 18
gamedesigninitiative
at cornell university
Lecture 18
box2d Physics
Physics in Games
the
2 Physics Overview gamedesigninitiative
at cornell university
Physics in Games
the
3 Physics Overview gamedesigninitiative
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
4 Collisions gamedesigninitiative
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
5 Collisions gamedesigninitiative
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
Forces Impulses
Impulse
Impulse = Force x Time
the
7 Collisions gamedesigninitiative
at cornell university
Forces vs. Impulses
Forces Impulses
Impulse
Impulse = Force x 1 Sec
in Box2D
the
8 Collisions gamedesigninitiative
at cornell university
Force and Acceleration
What do we need to compute motion?
Dp = vDt = v0Dt + ½a(Dt)2 = v0Dt + ½(F/m)(Dt)2
So depends on Force, current velocity and mass
Where does that mass come from?
Class Body has a getter, but no setter!
It comes from the Fixture class
Fixture gives volume to body
Will revisit this later with collisions
the
9 Collisions gamedesigninitiative
at cornell university
Force and Acceleration
What do we need to compute motion?
Dp = vDt = v0Dt + ½a(Dt)2 = v0Dt + ½(F/m)(Dt)2
So depends on Force, current velocity and mass
Where does that mass come from?
Class Body has a getter, but no setter!
It comes from the Fixture class
Fixture gives volume to body
Will revisit this later with collisions
the
10 Collisions gamedesigninitiative
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
11 Collisions gamedesigninitiative
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
12 Collisions gamedesigninitiative
at cornell university
Example: box2d Demo
the
13 Collisions gamedesigninitiative
at cornell university
Example: box2d Demo
Controls:
WASD for linear force
Left-right arrows to rotate
9 or 0 to change controls
the
14 Collisions gamedesigninitiative
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
15 Collisions gamedesigninitiative
at cornell university
Basic Structure of a Update Loop
Shape Fixture
// Another?
bodydef.position.set(position2);
body2 = world.createBody(bodydef);
the
20 Collisions gamedesigninitiative
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;
the
21 Collisions gamedesigninitiative
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);
the
22 Collisions gamedesigninitiative
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);
the
23 Collisions gamedesigninitiative
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
24 Collisions gamedesigninitiative
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
25 Collisions gamedesigninitiative
at cornell university
A Word on Units
Size is not in pixels
1 box2d unit = 1 meter
1.5 60
Also 1 density = 1 kg/m2
b2d units pixels
Drawing scale in Lab 4
This is rescalable
Could say 1 unit = 10 m
But must be consistent
60
box2d likes units near 1 b2d units
Best if objects same size
Adjust scale so 1 default
the
26 Collisions gamedesigninitiative
at cornell university
Example: Box2D Demo
the
27 Collisions gamedesigninitiative
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
28 Collisions gamedesigninitiative
at cornell university
How Do We Find the Shape?
Do not try to learn boundary
Image recognition is hard
Hull will have many sides
the
29 Collisions gamedesigninitiative
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
30 Collisions gamedesigninitiative
at cornell university
Custom Collisions: ContactListeners
Special listener attached to world object
Reacts to any two fixtures that collide
Allow you to override collision behavior
Or you can augment collision behavior
Two primary methods in interface
beginContact: When objects first collide
endContact: When objects no longer collide
Example: Color changing in box2d demo
the
31 Collisions gamedesigninitiative
at cornell university
Collision is About Fixtures!
the
38 Collisions gamedesigninitiative
at cornell university
The RayCastCallback Interface
float reportRayFixture(Fixture fixture, // Fixture found
Vector2 point, // Collision point
Vector2 nom, // Collision normal
float fraction // Fraction of ray
)
Revolute Weld
Prismatic Pulley
Bridge in Lab 4
the
47 Collisions gamedesigninitiative
at cornell university
Making a Rope: The Better Way