building a better jump - math for game programmers...j. kyle pittman @piratehearts co-founder, minor...
TRANSCRIPT
![Page 1: Building a Better Jump - Math for Game Programmers...J. Kyle Pittman @PirateHearts Co-founder, Minor Key Games. Hi I’m Kyle Hi Kyle 2007-2013 2013-20XX. Motivation](https://reader033.vdocuments.us/reader033/viewer/2022051814/603aae7d6026aa7a0745845e/html5/thumbnails/1.jpg)
Building a Better Jump
J. Kyle PittmanJ. Kyle Pittman@PirateHeartsCo-founder, Minor Key Games
![Page 2: Building a Better Jump - Math for Game Programmers...J. Kyle Pittman @PirateHearts Co-founder, Minor Key Games. Hi I’m Kyle Hi Kyle 2007-2013 2013-20XX. Motivation](https://reader033.vdocuments.us/reader033/viewer/2022051814/603aae7d6026aa7a0745845e/html5/thumbnails/2.jpg)
Hi
● I’m Kyle
● Hi Kyle● Hi Kyle
2007-2013 2013-20XX
![Page 3: Building a Better Jump - Math for Game Programmers...J. Kyle Pittman @PirateHearts Co-founder, Minor Key Games. Hi I’m Kyle Hi Kyle 2007-2013 2013-20XX. Motivation](https://reader033.vdocuments.us/reader033/viewer/2022051814/603aae7d6026aa7a0745845e/html5/thumbnails/3.jpg)
Motivation
● Avoid hardcoding, guessing games
● Design jump trajectory on paper● Design jump trajectory on paper
● Derive constants to model jump in code
![Page 4: Building a Better Jump - Math for Game Programmers...J. Kyle Pittman @PirateHearts Co-founder, Minor Key Games. Hi I’m Kyle Hi Kyle 2007-2013 2013-20XX. Motivation](https://reader033.vdocuments.us/reader033/viewer/2022051814/603aae7d6026aa7a0745845e/html5/thumbnails/4.jpg)
Motivation
● Has this ever happened to you?
● There’s GOT to be a better way!!
![Page 5: Building a Better Jump - Math for Game Programmers...J. Kyle Pittman @PirateHearts Co-founder, Minor Key Games. Hi I’m Kyle Hi Kyle 2007-2013 2013-20XX. Motivation](https://reader033.vdocuments.us/reader033/viewer/2022051814/603aae7d6026aa7a0745845e/html5/thumbnails/5.jpg)
Assumptions
● Model player as a simple projectile
● Game state● Game state
● Position, velocity integrated on a timestep
● Acceleration from gravity
● No air friction / drag
![Page 6: Building a Better Jump - Math for Game Programmers...J. Kyle Pittman @PirateHearts Co-founder, Minor Key Games. Hi I’m Kyle Hi Kyle 2007-2013 2013-20XX. Motivation](https://reader033.vdocuments.us/reader033/viewer/2022051814/603aae7d6026aa7a0745845e/html5/thumbnails/6.jpg)
Gravity
● Single external force
● Constant acceleration● Constant accelerationover time
![Page 7: Building a Better Jump - Math for Game Programmers...J. Kyle Pittman @PirateHearts Co-founder, Minor Key Games. Hi I’m Kyle Hi Kyle 2007-2013 2013-20XX. Motivation](https://reader033.vdocuments.us/reader033/viewer/2022051814/603aae7d6026aa7a0745845e/html5/thumbnails/7.jpg)
Integration
● Integrate over timeto find velocityto find velocity
0vgt
dtg
+
=∫
![Page 8: Building a Better Jump - Math for Game Programmers...J. Kyle Pittman @PirateHearts Co-founder, Minor Key Games. Hi I’m Kyle Hi Kyle 2007-2013 2013-20XX. Motivation](https://reader033.vdocuments.us/reader033/viewer/2022051814/603aae7d6026aa7a0745845e/html5/thumbnails/8.jpg)
Integration
● Integrate over timeagain to find positionagain to find position
00
2
21
0
ptvgt
dtvgt
++
=+∫
![Page 9: Building a Better Jump - Math for Game Programmers...J. Kyle Pittman @PirateHearts Co-founder, Minor Key Games. Hi I’m Kyle Hi Kyle 2007-2013 2013-20XX. Motivation](https://reader033.vdocuments.us/reader033/viewer/2022051814/603aae7d6026aa7a0745845e/html5/thumbnails/9.jpg)
Projectile motion
00
2
21)( ptvgttf ++=
● Textbox Physics 101 projectile motion
● Understand how we got there
![Page 10: Building a Better Jump - Math for Game Programmers...J. Kyle Pittman @PirateHearts Co-founder, Minor Key Games. Hi I’m Kyle Hi Kyle 2007-2013 2013-20XX. Motivation](https://reader033.vdocuments.us/reader033/viewer/2022051814/603aae7d6026aa7a0745845e/html5/thumbnails/10.jpg)
Parabolas
● Algebraic definition
● cbxaxxf ++=2
)(●
● Substituting
cbxaxxf ++=)(
ga21→
tx → 0vb →
0pc →
00
2
21)( ptvgttf ++=
![Page 11: Building a Better Jump - Math for Game Programmers...J. Kyle Pittman @PirateHearts Co-founder, Minor Key Games. Hi I’m Kyle Hi Kyle 2007-2013 2013-20XX. Motivation](https://reader033.vdocuments.us/reader033/viewer/2022051814/603aae7d6026aa7a0745845e/html5/thumbnails/11.jpg)
Properties of parabolas
● Symmetric
![Page 12: Building a Better Jump - Math for Game Programmers...J. Kyle Pittman @PirateHearts Co-founder, Minor Key Games. Hi I’m Kyle Hi Kyle 2007-2013 2013-20XX. Motivation](https://reader033.vdocuments.us/reader033/viewer/2022051814/603aae7d6026aa7a0745845e/html5/thumbnails/12.jpg)
Properties of parabolas
● Geometric self-similarity
![Page 13: Building a Better Jump - Math for Game Programmers...J. Kyle Pittman @PirateHearts Co-founder, Minor Key Games. Hi I’m Kyle Hi Kyle 2007-2013 2013-20XX. Motivation](https://reader033.vdocuments.us/reader033/viewer/2022051814/603aae7d6026aa7a0745845e/html5/thumbnails/13.jpg)
Properties of parabolas
● Shaped by quadratic coefficient ga21→
![Page 14: Building a Better Jump - Math for Game Programmers...J. Kyle Pittman @PirateHearts Co-founder, Minor Key Games. Hi I’m Kyle Hi Kyle 2007-2013 2013-20XX. Motivation](https://reader033.vdocuments.us/reader033/viewer/2022051814/603aae7d6026aa7a0745845e/html5/thumbnails/14.jpg)
Design on paper
![Page 15: Building a Better Jump - Math for Game Programmers...J. Kyle Pittman @PirateHearts Co-founder, Minor Key Games. Hi I’m Kyle Hi Kyle 2007-2013 2013-20XX. Motivation](https://reader033.vdocuments.us/reader033/viewer/2022051814/603aae7d6026aa7a0745845e/html5/thumbnails/15.jpg)
Design on paper
![Page 16: Building a Better Jump - Math for Game Programmers...J. Kyle Pittman @PirateHearts Co-founder, Minor Key Games. Hi I’m Kyle Hi Kyle 2007-2013 2013-20XX. Motivation](https://reader033.vdocuments.us/reader033/viewer/2022051814/603aae7d6026aa7a0745845e/html5/thumbnails/16.jpg)
Maths
● Derive valuesfor gravity andfor gravity andinitial velocityin terms ofpeak height andduration to peak
![Page 17: Building a Better Jump - Math for Game Programmers...J. Kyle Pittman @PirateHearts Co-founder, Minor Key Games. Hi I’m Kyle Hi Kyle 2007-2013 2013-20XX. Motivation](https://reader033.vdocuments.us/reader033/viewer/2022051814/603aae7d6026aa7a0745845e/html5/thumbnails/17.jpg)
Initial velocity
tf
vgttf
=′
+=′0
0)(
)(
Solve for v0:
h
h
h
gtv
vgt
tf
−=
+=
=′
0
00
0)(
![Page 18: Building a Better Jump - Math for Game Programmers...J. Kyle Pittman @PirateHearts Co-founder, Minor Key Games. Hi I’m Kyle Hi Kyle 2007-2013 2013-20XX. Motivation](https://reader033.vdocuments.us/reader033/viewer/2022051814/603aae7d6026aa7a0745845e/html5/thumbnails/18.jpg)
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
![Page 19: Building a Better Jump - Math for Game Programmers...J. Kyle Pittman @PirateHearts Co-founder, Minor Key Games. Hi I’m Kyle Hi Kyle 2007-2013 2013-20XX. Motivation](https://reader033.vdocuments.us/reader033/viewer/2022051814/603aae7d6026aa7a0745845e/html5/thumbnails/19.jpg)
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
=
−−=
![Page 20: Building a Better Jump - Math for Game Programmers...J. Kyle Pittman @PirateHearts Co-founder, Minor Key Games. Hi I’m Kyle Hi Kyle 2007-2013 2013-20XX. Motivation](https://reader033.vdocuments.us/reader033/viewer/2022051814/603aae7d6026aa7a0745845e/html5/thumbnails/20.jpg)
Review
t
hv
20
=
ht0
2
2
ht
hg
−=
![Page 21: Building a Better Jump - Math for Game Programmers...J. Kyle Pittman @PirateHearts Co-founder, Minor Key Games. Hi I’m Kyle Hi Kyle 2007-2013 2013-20XX. Motivation](https://reader033.vdocuments.us/reader033/viewer/2022051814/603aae7d6026aa7a0745845e/html5/thumbnails/21.jpg)
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
![Page 22: Building a Better Jump - Math for Game Programmers...J. Kyle Pittman @PirateHearts Co-founder, Minor Key Games. Hi I’m Kyle Hi Kyle 2007-2013 2013-20XX. Motivation](https://reader033.vdocuments.us/reader033/viewer/2022051814/603aae7d6026aa7a0745845e/html5/thumbnails/22.jpg)
Parameters
![Page 23: Building a Better Jump - Math for Game Programmers...J. Kyle Pittman @PirateHearts Co-founder, Minor Key Games. Hi I’m Kyle Hi Kyle 2007-2013 2013-20XX. Motivation](https://reader033.vdocuments.us/reader033/viewer/2022051814/603aae7d6026aa7a0745845e/html5/thumbnails/23.jpg)
Time � space
![Page 24: Building a Better Jump - Math for Game Programmers...J. Kyle Pittman @PirateHearts Co-founder, Minor Key Games. Hi I’m Kyle Hi Kyle 2007-2013 2013-20XX. Motivation](https://reader033.vdocuments.us/reader033/viewer/2022051814/603aae7d6026aa7a0745845e/html5/thumbnails/24.jpg)
Time � space
![Page 25: Building a Better Jump - Math for Game Programmers...J. Kyle Pittman @PirateHearts Co-founder, Minor Key Games. Hi I’m Kyle Hi Kyle 2007-2013 2013-20XX. Motivation](https://reader033.vdocuments.us/reader033/viewer/2022051814/603aae7d6026aa7a0745845e/html5/thumbnails/25.jpg)
Maths
● Rewrite gravity and initial velocity in terms of foot speed and lateral distanceterms of foot speed and lateral distanceto peak of jump
![Page 26: Building a Better Jump - Math for Game Programmers...J. Kyle Pittman @PirateHearts Co-founder, Minor Key Games. Hi I’m Kyle Hi Kyle 2007-2013 2013-20XX. Motivation](https://reader033.vdocuments.us/reader033/viewer/2022051814/603aae7d6026aa7a0745845e/html5/thumbnails/26.jpg)
Maths
hxt =
ht
hv
20
=2
2
ht
hg
−=
x
hh
v
xt =
h
x
x
hvv
20
=2
22
h
x
x
hvg
−=
![Page 27: Building a Better Jump - Math for Game Programmers...J. Kyle Pittman @PirateHearts Co-founder, Minor Key Games. Hi I’m Kyle Hi Kyle 2007-2013 2013-20XX. Motivation](https://reader033.vdocuments.us/reader033/viewer/2022051814/603aae7d6026aa7a0745845e/html5/thumbnails/27.jpg)
Review
x
x
hvv
20
=
hx0
2
22
h
x
x
hvg
−=
![Page 28: Building a Better Jump - Math for Game Programmers...J. Kyle Pittman @PirateHearts Co-founder, Minor Key Games. Hi I’m Kyle Hi Kyle 2007-2013 2013-20XX. Motivation](https://reader033.vdocuments.us/reader033/viewer/2022051814/603aae7d6026aa7a0745845e/html5/thumbnails/28.jpg)
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.
![Page 29: Building a Better Jump - Math for Game Programmers...J. Kyle Pittman @PirateHearts Co-founder, Minor Key Games. Hi I’m Kyle Hi Kyle 2007-2013 2013-20XX. Motivation](https://reader033.vdocuments.us/reader033/viewer/2022051814/603aae7d6026aa7a0745845e/html5/thumbnails/29.jpg)
Breaks
● Maintain continuityin position and velocityin position and velocity
● Trivial in implementation
● Choose a new gravityto shape our jump
![Page 30: Building a Better Jump - Math for Game Programmers...J. Kyle Pittman @PirateHearts Co-founder, Minor Key Games. Hi I’m Kyle Hi Kyle 2007-2013 2013-20XX. Motivation](https://reader033.vdocuments.us/reader033/viewer/2022051814/603aae7d6026aa7a0745845e/html5/thumbnails/30.jpg)
Fast falling
Position Velocity / Acceleration
![Page 31: Building a Better Jump - Math for Game Programmers...J. Kyle Pittman @PirateHearts Co-founder, Minor Key Games. Hi I’m Kyle Hi Kyle 2007-2013 2013-20XX. Motivation](https://reader033.vdocuments.us/reader033/viewer/2022051814/603aae7d6026aa7a0745845e/html5/thumbnails/31.jpg)
Variable height jumping
Position Velocity / Acceleration
![Page 32: Building a Better Jump - Math for Game Programmers...J. Kyle Pittman @PirateHearts Co-founder, Minor Key Games. Hi I’m Kyle Hi Kyle 2007-2013 2013-20XX. Motivation](https://reader033.vdocuments.us/reader033/viewer/2022051814/603aae7d6026aa7a0745845e/html5/thumbnails/32.jpg)
Double jumping
Position Velocity / Acceleration
![Page 33: Building a Better Jump - Math for Game Programmers...J. Kyle Pittman @PirateHearts Co-founder, Minor Key Games. Hi I’m Kyle Hi Kyle 2007-2013 2013-20XX. Motivation](https://reader033.vdocuments.us/reader033/viewer/2022051814/603aae7d6026aa7a0745845e/html5/thumbnails/33.jpg)
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
![Page 34: Building a Better Jump - Math for Game Programmers...J. Kyle Pittman @PirateHearts Co-founder, Minor Key Games. Hi I’m Kyle Hi Kyle 2007-2013 2013-20XX. Motivation](https://reader033.vdocuments.us/reader033/viewer/2022051814/603aae7d6026aa7a0745845e/html5/thumbnails/34.jpg)
Integration
![Page 35: Building a Better Jump - Math for Game Programmers...J. Kyle Pittman @PirateHearts Co-founder, Minor Key Games. Hi I’m Kyle Hi Kyle 2007-2013 2013-20XX. Motivation](https://reader033.vdocuments.us/reader033/viewer/2022051814/603aae7d6026aa7a0745845e/html5/thumbnails/35.jpg)
Euler
● Pseudocodepos += vel * ∆tpos += vel * ∆t
vel += acc * ∆t
● Easy
● Unstable
● We can do better
![Page 36: Building a Better Jump - Math for Game Programmers...J. Kyle Pittman @PirateHearts Co-founder, Minor Key Games. Hi I’m Kyle Hi Kyle 2007-2013 2013-20XX. Motivation](https://reader033.vdocuments.us/reader033/viewer/2022051814/603aae7d6026aa7a0745845e/html5/thumbnails/36.jpg)
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.
![Page 37: Building a Better Jump - Math for Game Programmers...J. Kyle Pittman @PirateHearts Co-founder, Minor Key Games. Hi I’m Kyle Hi Kyle 2007-2013 2013-20XX. Motivation](https://reader033.vdocuments.us/reader033/viewer/2022051814/603aae7d6026aa7a0745845e/html5/thumbnails/37.jpg)
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
![Page 38: Building a Better Jump - Math for Game Programmers...J. Kyle Pittman @PirateHearts Co-founder, Minor Key Games. Hi I’m Kyle Hi Kyle 2007-2013 2013-20XX. Motivation](https://reader033.vdocuments.us/reader033/viewer/2022051814/603aae7d6026aa7a0745845e/html5/thumbnails/38.jpg)
Observations
● Similarity to projectile motion formula
● What if our acceleration were constant?● What if our acceleration were constant?
● We could integrate with 100% accuracy
![Page 39: Building a Better Jump - Math for Game Programmers...J. Kyle Pittman @PirateHearts Co-founder, Minor Key Games. Hi I’m Kyle Hi Kyle 2007-2013 2013-20XX. Motivation](https://reader033.vdocuments.us/reader033/viewer/2022051814/603aae7d6026aa7a0745845e/html5/thumbnails/39.jpg)
Assuming constant acceleration
![Page 40: Building a Better Jump - Math for Game Programmers...J. Kyle Pittman @PirateHearts Co-founder, Minor Key Games. Hi I’m Kyle Hi Kyle 2007-2013 2013-20XX. Motivation](https://reader033.vdocuments.us/reader033/viewer/2022051814/603aae7d6026aa7a0745845e/html5/thumbnails/40.jpg)
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
![Page 41: Building a Better Jump - Math for Game Programmers...J. Kyle Pittman @PirateHearts Co-founder, Minor Key Games. Hi I’m Kyle Hi Kyle 2007-2013 2013-20XX. Motivation](https://reader033.vdocuments.us/reader033/viewer/2022051814/603aae7d6026aa7a0745845e/html5/thumbnails/41.jpg)
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?
![Page 42: Building a Better Jump - Math for Game Programmers...J. Kyle Pittman @PirateHearts Co-founder, Minor Key Games. Hi I’m Kyle Hi Kyle 2007-2013 2013-20XX. Motivation](https://reader033.vdocuments.us/reader033/viewer/2022051814/603aae7d6026aa7a0745845e/html5/thumbnails/42.jpg)
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
![Page 43: Building a Better Jump - Math for Game Programmers...J. Kyle Pittman @PirateHearts Co-founder, Minor Key Games. Hi I’m Kyle Hi Kyle 2007-2013 2013-20XX. Motivation](https://reader033.vdocuments.us/reader033/viewer/2022051814/603aae7d6026aa7a0745845e/html5/thumbnails/43.jpg)
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