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
• 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
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
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
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
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
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
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
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
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
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
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
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
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!
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