Off-Road Racing Physics, Indie Style Jim Buck Twitchy Thumbs Entertainment, Inc....

32
Off-Road Racing Physics, Indie Style Jim Buck Twitchy Thumbs Entertainment, Inc. [email protected] August 19, 2009

Transcript of Off-Road Racing Physics, Indie Style Jim Buck Twitchy Thumbs Entertainment, Inc....

Page 1: Off-Road Racing Physics, Indie Style Jim Buck Twitchy Thumbs Entertainment, Inc. jbuck@twitchythumbs.com August 19, 2009.

Off-Road Racing Physics,

Indie Style

Jim BuckTwitchy Thumbs Entertainment, Inc.

[email protected] 19, 2009

Page 2: Off-Road Racing Physics, Indie Style Jim Buck Twitchy Thumbs Entertainment, Inc. jbuck@twitchythumbs.com August 19, 2009.

Who is this dude??

• 1996 – 2000 : 989 Studios, programmer

• 2000 – 2003 : DepthQ, founder

• 2003 – 2005 : SCEA, programmer

• 2005 – present : Twitchy Thumbs, founder

Page 3: Off-Road Racing Physics, Indie Style Jim Buck Twitchy Thumbs Entertainment, Inc. jbuck@twitchythumbs.com August 19, 2009.

Why?

Yesterday’s next-gen technology is today’s:

• achievability by indie developers

• handheld technology

• next-gen effects during spare CPU time

“Doing a lot with a little is something we can't forget how to do.”

– Mike Acton, Insomniac Studios

Page 4: Off-Road Racing Physics, Indie Style Jim Buck Twitchy Thumbs Entertainment, Inc. jbuck@twitchythumbs.com August 19, 2009.

What is Rally Cross?

Page 5: Off-Road Racing Physics, Indie Style Jim Buck Twitchy Thumbs Entertainment, Inc. jbuck@twitchythumbs.com August 19, 2009.

What is Rally Cross?

• PlayStation off-road racing

• Sony-developed/published

• February ‘97 in N. America

• 200,000 sold in N. America

• Japanese/PAL releases

• Considered “cult” classic

• One of first for PS1-on-PSP

Page 6: Off-Road Racing Physics, Indie Style Jim Buck Twitchy Thumbs Entertainment, Inc. jbuck@twitchythumbs.com August 19, 2009.

What is Rally Cross?

Demo

Page 7: Off-Road Racing Physics, Indie Style Jim Buck Twitchy Thumbs Entertainment, Inc. jbuck@twitchythumbs.com August 19, 2009.

Rally Cross physics history

• Chris Hecker physics articles too late (October 1996 - June 1997)

• SGI 4Dgifts newton demo

• Robert Sheehan’s bouncer simulation

Page 8: Off-Road Racing Physics, Indie Style Jim Buck Twitchy Thumbs Entertainment, Inc. jbuck@twitchythumbs.com August 19, 2009.

Physics Primer

• F = m*a

• v += a*t = (F/m)*t

• p += v*t

All math is 1.19.12 fixed-point!

Page 9: Off-Road Racing Physics, Indie Style Jim Buck Twitchy Thumbs Entertainment, Inc. jbuck@twitchythumbs.com August 19, 2009.

What is Rally Cross physics?

Springs!(well, actually, damped springs)

Page 10: Off-Road Racing Physics, Indie Style Jim Buck Twitchy Thumbs Entertainment, Inc. jbuck@twitchythumbs.com August 19, 2009.

Damped spring physics

t = 0

t = 1

x

F = – k*x – c*v1

v0

v1

Spring at rest

Hooke’s law

Page 11: Off-Road Racing Physics, Indie Style Jim Buck Twitchy Thumbs Entertainment, Inc. jbuck@twitchythumbs.com August 19, 2009.

Rally Cross physics components

• Vehicle box

• Box collision with ground

• Box collision with track boundaries

• Box collision with another box (vehicle)

• Vehicle-specific physics

Page 12: Off-Road Racing Physics, Indie Style Jim Buck Twitchy Thumbs Entertainment, Inc. jbuck@twitchythumbs.com August 19, 2009.

Vehicle Box

• 8 points, or point masses, representing box corners

• Each point accumulates forces (F) and gets time-stepped at bottom of loop

• 28 damped springs from each point to every other point

Page 13: Off-Road Racing Physics, Indie Style Jim Buck Twitchy Thumbs Entertainment, Inc. jbuck@twitchythumbs.com August 19, 2009.

Vehicle Box

Page 14: Off-Road Racing Physics, Indie Style Jim Buck Twitchy Thumbs Entertainment, Inc. jbuck@twitchythumbs.com August 19, 2009.

Vehicle Box

For each spring:

• x = length(p1 – p0) - at_rest_length

• v = dot(v1 – v0, unit(p1 – p0))

• F = (– k*x – c*v) * unit(p1 – p0)

• F1 += F, F0 += –F

p0

p1

v1

v0

Page 15: Off-Road Racing Physics, Indie Style Jim Buck Twitchy Thumbs Entertainment, Inc. jbuck@twitchythumbs.com August 19, 2009.

Vehicle Box

• “Shock” springs are special

• Separated into Y and ortho-Y vectors

• “Softer” in Y than in ortho-Y

• Ortho-Y treated same as other springs

Page 16: Off-Road Racing Physics, Indie Style Jim Buck Twitchy Thumbs Entertainment, Inc. jbuck@twitchythumbs.com August 19, 2009.

Box Collision with Ground

• Ground is a height-mapped grid

• Gravity constantly affects point masses:

F = m*G

m

G

Page 17: Off-Road Racing Physics, Indie Style Jim Buck Twitchy Thumbs Entertainment, Inc. jbuck@twitchythumbs.com August 19, 2009.

vn

Box Collision with Ground

• For each point mass, if under height-map:– xn = (height_map(p.x, p.z) – p.y) *

normal_map(p.x, p.z).y

– vn = dot(v, normal_map(p.x, p.z)

– F = (– k*xn – c*vn) * normal_map(p.x, p.z)

• Formula is for a damped spring!

p

xxn

v

n

height_map

Page 18: Off-Road Racing Physics, Indie Style Jim Buck Twitchy Thumbs Entertainment, Inc. jbuck@twitchythumbs.com August 19, 2009.

Sidebar: Friction

• Static: Fmax = fs*N

• Kinetic: F = fk*N

v = 0

m*G

NF fs*N

v

m*G

N fk*N

Page 19: Off-Road Racing Physics, Indie Style Jim Buck Twitchy Thumbs Entertainment, Inc. jbuck@twitchythumbs.com August 19, 2009.

Box Collision with Ground

Friction

• Viscous: F = –fv*vo

• Static/kinetic– If vo == 0

• If Fo <= fs*xn, Fo = 0

• Else Fo –= fk*xn

– Else if length(vo + (Fo/m)*t) <= (fk*xn)*t, vo = 0

– Else F = –unit(vo) * fk*xn

Page 20: Off-Road Racing Physics, Indie Style Jim Buck Twitchy Thumbs Entertainment, Inc. jbuck@twitchythumbs.com August 19, 2009.

Box Collision with Boundaries

• 2d (infinite height) line segments surrounding the track

• Physics is same as for hitting the ground– Damped spring pushing back along normal– Friction applied ortho to the normal

vn

pxn

v

n

Out of boundsIn bounds

Page 21: Off-Road Racing Physics, Indie Style Jim Buck Twitchy Thumbs Entertainment, Inc. jbuck@twitchythumbs.com August 19, 2009.

Box Collision with Another Box

• Sphere test for each car pair

• If succeeds, ray-through-box test

x

F = (– k*x – c*vn) * normal

(Formula look familiar?)

Page 22: Off-Road Racing Physics, Indie Style Jim Buck Twitchy Thumbs Entertainment, Inc. jbuck@twitchythumbs.com August 19, 2009.

Box Collision with Another Box

• If ray-through-box fails, test each car’s points against other’s box

x

Again, F = (– k*x – c*vn) * normal

n

Page 23: Off-Road Racing Physics, Indie Style Jim Buck Twitchy Thumbs Entertainment, Inc. jbuck@twitchythumbs.com August 19, 2009.

Vehicle-specific Physics

• For steering, F = –kv*vx (viscous friction!)

• For acceleration in Z, that would require a separate presentation

X X ZZ

Page 24: Off-Road Racing Physics, Indie Style Jim Buck Twitchy Thumbs Entertainment, Inc. jbuck@twitchythumbs.com August 19, 2009.

But briefly…

engine axle

gear ratio

wheel axle

fs*N or fk*N

Page 25: Off-Road Racing Physics, Indie Style Jim Buck Twitchy Thumbs Entertainment, Inc. jbuck@twitchythumbs.com August 19, 2009.

Physics Rendering

• Points time-stepped: v += (F/m)*t, p += v*t

• Display matrix:

• Car position = top_center – box_height

• Wheel position = based on shock length

X

ZTop face

Page 26: Off-Road Racing Physics, Indie Style Jim Buck Twitchy Thumbs Entertainment, Inc. jbuck@twitchythumbs.com August 19, 2009.

Physics Demo

Page 27: Off-Road Racing Physics, Indie Style Jim Buck Twitchy Thumbs Entertainment, Inc. jbuck@twitchythumbs.com August 19, 2009.

Problems

• Box freaking out

• Stuck on track boundaries

Page 28: Off-Road Racing Physics, Indie Style Jim Buck Twitchy Thumbs Entertainment, Inc. jbuck@twitchythumbs.com August 19, 2009.

• Check “crossbars” dot product is in safe range

• Check upper points are “above” lower points

Box Freaking Out

Lower points

Upper points

+

= bad!

Page 29: Off-Road Racing Physics, Indie Style Jim Buck Twitchy Thumbs Entertainment, Inc. jbuck@twitchythumbs.com August 19, 2009.

Stuck on Track Boundaries

• Stuck if 2 adjacent points such that: dot(n0b, n1b) < 0

• Find boundary intersected by center of box to center of the 2 points

• Push out with spring equation

Page 30: Off-Road Racing Physics, Indie Style Jim Buck Twitchy Thumbs Entertainment, Inc. jbuck@twitchythumbs.com August 19, 2009.

Optimizations

• Constants mostly power-of-2

• Cached calculations (ex. 1/mass * time)

• Local variables to avoid aliasing

• 64-bit MIPS multiplications

• Assembly language for key math functions and height-map lookup

Page 31: Off-Road Racing Physics, Indie Style Jim Buck Twitchy Thumbs Entertainment, Inc. jbuck@twitchythumbs.com August 19, 2009.

Evolution of PhysicsCART World Series• correct wheel friction

Twisted Metal 3• 3d poly collision with no

penetration (no height-map)

Twisted Metal 4• car-to-car collision with 3d

boolean intersection

Page 32: Off-Road Racing Physics, Indie Style Jim Buck Twitchy Thumbs Entertainment, Inc. jbuck@twitchythumbs.com August 19, 2009.

Q&A

Contact: [email protected]