Skip to content

Instantly share code, notes, and snippets.

@xem
Last active February 26, 2026 09:38
Show Gist options
  • Select an option

  • Save xem/99930986c5333125a13b0ea50600391f to your computer and use it in GitHub Desktop.

Select an option

Save xem/99930986c5333125a13b0ea50600391f to your computer and use it in GitHub Desktop.
Maths & trigonometry cheat sheet for 2D & 3D games

Conventions

  • o = [xo = 0, yo = 0] is the origin
  • A = [xA, yA] is a point on the 2D plane. Same for B, C, ...
  • lengths are in any unit (ex: pixels)
  • code snippets are in JavaScript

Degrees to radians

angleRad = angleDeg * Math.PI / 180;

Radians to degrees

angleDeg = angleRad * 180 / Math.PI;

Distance between two points (Pythagore)

  • dist = function(A,B){ return Math.sqrt((xB - xA)*(xB - xA) + (yB - yA)*(yB - yA)) } // ES5
  • dist = (A, B) => Math.hypot(xB -x A, yB - yA) // ES6

Line passing through 2 points

  • line equation: y = ax + b
  • a = (yB - yA) / (yB - yA) = tan θ
  • θ = angle between line and x axis
  • b = yA - a * xA (because yA = a * xA + b)

Intersection of 2 secant lines

  • line 1: y = a * x + b
  • line 2: y' = a' * x + b'
  • intersection point P:
    • xP = (a - a')/(b' - b);
      • yP = a * xP + b;
  • Ex with y=5x+1 & y'=2x+8:
    • xP = 7/3;
    • yP = 12.666;

Angle in radians between the x axis at the origin and a point on the plane

angle = Math.atan2(Ax, Ay)

Angle in radians between two points

angle = Math.atan2(By - Ay, Bx - Ax);

Rotate a point (angle in radians)

  • Anew_x = Ax * Math.cos(angle) - Ay * Math.sin(angle)
  • Anew_y = Ax * Math.sin(angle) + Ay * Math.cos(angle)
  • It's the same as applying the following rotation matrix:
vec2 (
    +cos(a), -sin(a)
    +sin(a), +cos(a)
)

Project a point on the trigonometric circle

  • Anew_x = Math.cos(atan2(Ax, Ay))
  • Anew_y = Math.sin(atan2(Ax, Ay))

Intersections between a line and the grid (2D raycasting)

Projection of a plane on a sphere (on a 2D canvas)

@NaxeCode
Copy link

Sweet! Thanks a ton!

@zsumair
Copy link

zsumair commented Jul 18, 2016

Awesome... Thanks

@Siorki
Copy link

Siorki commented Jul 19, 2016

Line thru 2 points should be a = (yB - yA) / (xB - xA) , not (yB - yA) / (yB - yA)

@xem
Copy link
Author

xem commented Aug 13, 2016

thanks @Siorki!

@Julien-laville
Copy link

https://jsperf.com/hypotvstrigonometric/1
best perf using hypot than trigonometric to normalize vector

@terkelg
Copy link

terkelg commented Oct 23, 2017

Nice gist!

@hinell
Copy link

hinell commented Mar 19, 2019

Would be nice if someone adds normal/dot/cross production calculations.

@hinell
Copy link

hinell commented Jun 5, 2019

You must also must add easing functions to make this list complete:
https://gist.github.com/gre/1650294

@xem
Copy link
Author

xem commented Jun 5, 2019

ooh thanks @hinell

@farteryhr
Copy link

better to also have a "rotation along arbitrary axis", length being radians to rotate, no singularity at zero.
even more, somehow in favor of quaternion "slerp" because i think quaternion describes "state" or "instant transformation" but not the dynamic, continuous "rotation".
for reference and free copy: http://farter.cn/math/rotvec3/

@hinell
Copy link

hinell commented May 30, 2022

@farteryhr Quaternions are good, thanks for sharing.

@weijuer
Copy link

weijuer commented Jul 17, 2025

awesome

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment