Detecting Collision

Sketching with Math and Quasi Physics

Circles

Two circles, $$A$$ and $$B$$ collide if the distance between the center points is equal or less than the two radii added together.

$$distance(A_{center},B_{center}) \leq A_{radius}+B_{radius}\Rightarrow$$

$$distance(A_{center},B_{center})-A_{radius}-B_{radius}\leq 0$$

Line and Circle

The distance from a point $$P$$ to a line that passes through points $$A$$ and $$B$$ is

$$distance=\left\|\overrightarrow{AP}\right\|\left|\sin(\theta)\right|=\left|(\overrightarrow{AP}_x\cdot \overrightarrow{AB}_y-\overrightarrow{AB}_x\cdot \overrightarrow{AP}_y)\right|/\left\|\overrightarrow{AB}\right\|$$ where $$\theta$$ is the angle between $$\overrightarrow{AP}$$ and $$\overrightarrow{AB}$$

Rectangles

Two rectangles, $$A$$ and $$B$$ that are axis aligned collide if

$$(A_{left}\leq B_{right})\;\wedge\;(A_{right}\geq B_{left})\;\wedge\;(A_{top}\leq B_{bottom})\wedge(A_{bottom}\geq B_{top})$$

This is to project the shapes to x and y-axis and checking overlap on each axis. Note that screen coordinate system in which y increase from top to bottom is assumed.

Triangles

Similarly, collision between two convex polygons can be detected by projecting the shapes to the vectors perpendicular to each side of one of the polygons.