Building a Better Jump - Math for Game Programmers...J. Kyle Pittman @PirateHearts Co-founder, Minor...

43
Building a Better Jump J. Kyle Pittman J. Kyle Pittman @PirateHearts Co-founder, Minor Key Games

Transcript of Building a Better Jump - Math for Game Programmers...J. Kyle Pittman @PirateHearts Co-founder, Minor...

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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