Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Math for Game Developers

LD Smith
March 10, 2019

Math for Game Developers

This month Levi D. Smith will cover math for game developers. He will explain how algebra, geometry, trigonometry, and calculus apply to game development with examples.
Video presentation - https://www.youtube.com/watch?v=Z13AOvfRSNg

LD Smith

March 10, 2019
Tweet

More Decks by LD Smith

Other Decks in Education

Transcript

  1. Overview • 16 years of math condensed into one presentation

    • Algebra, Geometry, Trigonometry, Calculus, Physics • Skip to the shortcuts • What I’ve used in games • Real game examples • Unity and GameMaker demos • What I’ve never used
  2. Division and Modulo • Modulo – Returns the remainder when

    dividing two whole numbers • Useful for finding column number • Example: Fill 2D array with 1D array of random numbers • Find rotation angle of object after spinning multiple times (modulo 360) • Typically “mod” or “%” • Division • Dividing integers typically returns a whole number, depending on language • Useful for finding row • Dividing floats usually returns number with fraction • Can use floor or cast to integer to round down • Typically “/” • GameMaker – “/” with decimal, “div” for whole number • Obviously, dividing by 0 is a bad thing to do
  3. Coordinate Conversion • 2D array • array[row][column] • y =

    row * cell height • Unity = y increases as screen goes upward • GameMaker = y increases as screen goes downward • Subtract the total height to move from bottom up • x = column * cell width • 3D • Z up to Y up • Example, Blender to Unity • Multiply by -90 degrees or 270 degrees on X axis
  4. Pythagorean Theorem • Pythagoras of Samos 570 BC – 495

    BC • a2 + b2 = c2 • Useful for finding distance between two 2D points • Make right triangle and find hypotenuse • Right triangle has one 90 degree angle (perpendicular) • Sum of angles in triangle 180 degrees • Newbie gamedev mistake in overhead movement • Left arrow: subtract speed from X • Right arrow: add speed to X • Up arrow: subtract speed from Y • Down arrow: add speed to Y • Player will move sqrt(2) * speed or 1.4142 times (41%) faster when moving in both X and Y at the same time
  5. Pythagorean Theorem • Just need to know the distance between

    two objects • Also useful for quick collision detection when sizes of objects aren’t important • Small objects with no collision boxes (bullets) • Sqrt( (x1 -x2 )2 + (y1 -y2 )2 ) • Don’t have to worry about absolute values since squares are always positive • Square root is same as x1/2 or x0.5
  6. Exponents • Important to learn powers of 2 • 1,

    2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, …, 65536 • 2 to power of 5 is 2 x 2 x 2 x 2 x 2 = 32 • Anything to power of 0 is 1 8-bit 216 128 ASCII characters 20 21 22 23 24 25 26 27 28
  7. Base conversion • Base 2 = binary • 42 =

    1 * 25 + 0 * 24 + 1 * 23 + 0 * 22 + 1 * 21 + 0 * 20 • = 32 + 0 + 8 + 0 + 2 + 0 • = 101010 binary • Base 16 = hexadecimal (16 “digits”) • A = 10, B = 11, C = 12, D = 13, E = 14, F = 15 • 42 = 2 * 161 + 10 * 160 • = 2A hexadecimal ( 0x2A ) • Common hex values: 0xFF = 255, 0x80 = 128
  8. Why learn base conversion? • Binary • Bit shifts, roll

    over, overflow • Variable size limits • chmod • Hexadecimal • HTML color codes • Assembly
  9. Degrees and Radians • π = pi = 3.14159… •

    No point to memorizing pi to a hundred places other than to sound nerdy • Radians to degrees • π / 2 radians = 90 degrees • π radians = 180 degrees • 3 / 2π radians = 270 degrees • 2π radians = 360 degrees • Divide from source and multiply by destination • 42 degrees = 42 * (3.14 / 180) = 0.73 radians • Whether to use degrees or radians depends on the language
  10. Sine / Cosine • sine starts at 0, cosine starts

    at 1 • On unit circle, sine gives the y coordinate of angle, cosine gives the x coordinate of angle • Goes from 0 to 360 degrees (0 to 2 π radians) and then repeats • Amplitude, period, frequency • SOH CAH TOA • sine = opposite / hypotenuse • cosine = adjacent / hypotenuse • tangent = opposite / hypotenuse
  11. Circles • Defined by center point, Radius • Spiral is

    a circle with constantly increasing / decreasing radius • Circumfrence = 2 π r • Area = π r2
  12. Normalize a Vector • Can get the vector (x,y direction)

    of object moving towards its destination • Example: Jet flying from current location to destination • Normalize to get the unit vector (length of one) • Can use unit vector and multiply by the speed to get the new position • Normalize x = 2, y = 5 (right two units, up five units) • slope is 5 / 2 = 2.5 (rise over run, useful for y = mx+b form) • A = 2, O = 5, H = sqrt(22 + 52) = 5.3852 • angle is O/H = sin(x), x = arcsin(O/H) = arcsin(5/5.3852) = arcsin(0.9285) = 68 degrees • Normalized vector is: • x = 2 / 5.3852 = 0.3714 • y = 5 / 5.3852 = 0.9285 • Verify with Pythagorean! Sqrt(0.92852 + 0.37142)= 1.0000
  13. Normalize a Vector, example • Move our jet towards destination

    at 0.4 units per second • Unity Update() • xMove = 0.3714 * 0.4 * Time.deltaTime • yMove = 0.9285 * 0.4 * Time.deltaTime • transform.Translate(xMove, yMove, 1); • GameMaker Step • x = x + 0.3714 * 0.4 / room_speed • y = y + 0.9285 * 0.4 / room_speed • Will take distance / speed = 5.3852 / 0.4 = 13.463 seconds to get there
  14. Cross Products and Determinants • Cross Product to find the

    normal to a polygon • Normal != Normalize • Normalize: convert a vector to unit length • Normal: vector perpendicular to a polygon (useful for shading, culling) • Determinant • Angle between two vectors • Find the cross product
  15. Derivatives • Find the slope of a curve at a

    certain point • Notations • Leibniz: d/dx • Lagrange (prime): f’(x) • Quick way to take derivative • Coefficient = exponent times coefficient • Exponent = exponent minus 1 • Example • f(x) = 4x3 + 8x2 + 2x + 5 • f’(x) = 12x2 + 16x + 2 • Integration opposite of derivative • Find the area of a curve
  16. Kinematics, Derivatives • Position • distance (how far away are

    two points) • Units are meters (or miles, feet, yards, kilometers, etc) • Most graphics/ 3D game software defaults to 1 unit = 1 meter (a person is about 1.72 meters tall or 5’8”) • Velocity (how fast you’re going) • distance / time • Units are meters / seconds (or miles per hour) • First derivative with respect to time • Speed is the absolute value of velocity (speed is never negative) • Acceleration (how fast you’re speeding up or slowing down) • distance / time2 (meters / seconds2) • Second derivative with respect to time • Also have to account for friction
  17. Physics Units • Base units • Length (meter), mass (kilogram),

    time (second), and others • Mass is constant, weight depends on gravitational force of planet • 200 lbs on Earth = 200 / 9.81 m/s2 * 1.622 m/s2 = 33.07 lbs on moon • force = mass * acceleration (Newton’s second law) • newton = kilogram * meters / seconds2 • work = force * distance • joule = (newton * meter), kilowatt hour • power = work / time • watt = joule / second = kg * m2 / s2 • Very important when making a physics based game like pinball!
  18. (Typically) Useless Stuff for GameDev • Surface Area (a liquid

    game like Mario Sunshine?) • Circumference (Katamari Damacy?) • Logarithm • Quadratic formula • Twos compliment (subtraction for designing a processor?) • Imaginary numbers (square root of -1) • Other trigonometry functions: secant, cosecant, cotangent • Octal