Post on 09-Oct-2020
Building a Better Jump
J. Kyle PittmanJ. Kyle Pittman@PirateHeartsCo-founder, Minor Key Games
Hi
● I’m Kyle
● Hi Kyle● Hi Kyle
2007-2013 2013-20XX
Motivation
● Avoid hardcoding, guessing games
● Design jump trajectory on paper● Design jump trajectory on paper
● Derive constants to model jump in code
Motivation
● Has this ever happened to you?
● There’s GOT to be a better way!!
Assumptions
● Model player as a simple projectile
● Game state● Game state
● Position, velocity integrated on a timestep
● Acceleration from gravity
● No air friction / drag
Gravity
● Single external force
● Constant acceleration● Constant accelerationover time
Integration
● Integrate over timeto find velocityto find velocity
0vgt
dtg
+
=∫
Integration
● Integrate over timeagain to find positionagain to find position
00
2
21
0
ptvgt
dtvgt
++
=+∫
Projectile motion
00
2
21)( ptvgttf ++=
● Textbox Physics 101 projectile motion
● Understand how we got there
Parabolas
● Algebraic definition
● cbxaxxf ++=2
)(●
● Substituting
cbxaxxf ++=)(
ga21→
tx → 0vb →
0pc →
00
2
21)( ptvgttf ++=
Properties of parabolas
● Symmetric
Properties of parabolas
● Geometric self-similarity
Properties of parabolas
● Shaped by quadratic coefficient ga21→
Design on paper
Design on paper
Maths
● Derive valuesfor gravity andfor gravity andinitial velocityin terms ofpeak height andduration to peak
Initial velocity
tf
vgttf
=′
+=′0
0)(
)(
Solve for v0:
h
h
h
gtv
vgt
tf
−=
+=
=′
0
00
0)(
Gravity
21
00
2
21
)(
)(
h
ptvgth
htf
ptvgttf
++=
=
++=
0−= gtv h
Known values:
Solve for g:
2
2
21
2
21
00
2
21
2
0)(
h
h
hhh
hh
t
hg
gth
tgtgth
ptvgth
−=
−=
+−+=
++=
00
0
=
−=
p
gtv h
Back to init. vel.
h
h
t
hg
gtv
22
0
−=
−=
Solve for v0:
h
h
h
h
t
hv
tt
hv
t
2
)2
(
0
20
=
−−=
Review
t
hv
20
=
ht0
2
2
ht
hg
−=
Time � space
● Design with x-axis as distance in space
● Introduce lateral (foot) speed● Introduce lateral (foot) speed
● Keep horizontal and vertical velocity components separate
Parameters
Time � space
Time � space
Maths
● Rewrite gravity and initial velocity in terms of foot speed and lateral distanceterms of foot speed and lateral distanceto peak of jump
Maths
hxt =
ht
hv
20
=2
2
ht
hg
−=
x
hh
v
xt =
h
x
x
hvv
20
=2
22
h
x
x
hvg
−=
Review
x
x
hvv
20
=
hx0
2
22
h
x
x
hvg
−=
Breaking it down
● Real world: Projectiles always follow parabolic trajectories.parabolic trajectories.
● Game world: We can break the rules in interesting ways.
● Break our path into a series of parabolic arcs of different shapes.
Breaks
● Maintain continuityin position and velocityin position and velocity
● Trivial in implementation
● Choose a new gravityto shape our jump
Fast falling
Position Velocity / Acceleration
Variable height jumping
Position Velocity / Acceleration
Double jumping
Position Velocity / Acceleration
Integration
● Put our gravity and initial velocity constants to use in practiceconstants to use in practice
● Integrate from a past state to a future state over a time step
Integration
Euler
● Pseudocodepos += vel * ∆tpos += vel * ∆t
vel += acc * ∆t
● Easy
● Unstable
● We can do better
Runge-Kutta (RK4)
● The “top-shelf” integrator.
● No pseudocode here. :V● No pseudocode here. :V
● Gaffer on Games: “Integration Basics”
● Too complex for our needs.
Velocity Verlet
● Pseudocodepos += vel*∆t + ½acc*∆t*∆tpos += vel*∆t + ½acc*∆t*∆t
new_acc = f(pos)
vel += ½(acc+new_acc)*∆t
acc = new_acc
Observations
● Similarity to projectile motion formula
● What if our acceleration were constant?● What if our acceleration were constant?
● We could integrate with 100% accuracy
Assuming constant acceleration
Assuming constant acceleration
● Pseudocodepos += vel*∆t + ½acc*∆t*∆tpos += vel*∆t + ½acc*∆t*∆t
vel += acc*∆t
● Trivially simple change from Euler
● 100% accurate as long as our acceleration is constant
Near-constant acceleration
● What if we don’t change a thing?
● The error we accumulate when our ● The error we accumulate when our acceleration does change (versus Velocity Verlet) will be:
● ∆acc * ∆t * ∆t
● Acceptable?
The takeaway
● Design jump trajectories as a series of parabolic arcsparabolic arcs
● Can author unique game feel
● Trust result to feel grounded in physical truths
Questions?
● In practice: You Have to Win the Game (free game PLAY IT PLAY MY THING)(free game PLAY IT PLAY MY THING)
● The Twitters: @PirateHearts
● http://minorkeygames.com
● http://gunmetalarcadia.com