← Collision detection for beginners

[Chapter 1 · Part I · 2D]

Analytic geometry

How a coordinate system turns points and shapes into numbers, which is all a program can work with.

What analytic geometry is

Analytic geometry, also called coordinate geometry or Cartesian geometry, is the branch of mathematics that studies geometric figures using a coordinate system and algebra. It represents points by ordered pairs of numbers, and describes lines, curves and shapes with equations and inequalities.

That is exactly how a game sees the world. A point is two numbers. A circle is a centre and a radius. A rectangle is a range of values on each axis. The rest of this book is about turning those numbers into collision tests.

Coordinates of a point on a line

A number axis is a line xx with a chosen unit and a chosen origin OO. The origin divides the line into two opposite half-lines, the negative and the positive half-axis. Every point on the line then has exactly one real number, its coordinate.

−5−4−3−2−1012345negative half-axispositive half-axisOx

The Cartesian coordinate system

The Cartesian coordinate system in a plane is two perpendicular number axes, the xx-axis and the yy-axis, that intersect at the origin OO. Every point in the plane is represented by exactly one ordered pair of real numbers (x,y)(x, y): how far it is along the xx-axis, and how far along the yy-axis. In the figure below, the point AA has the coordinates (4,3)(4, 3).

Shapes are numbers too. The circle is its centre S(5,2)S(-5, 2) and its radius, 22. The rectangle is every point whose xx lies between 33 and 88 and whose yy lies between 4-4 and 1-1.

xyO43A(4, 3)rS(−5, 2), r = 2x ∈ [3, 8], y ∈ [−4, −1]

Screen coordinates

Screens flip the yy-axis. On a canvas, (0,0)(0, 0) is the top-left corner and yy grows downwards. None of the formulas in this book change because of that, since they only ever use differences and squares; it only matters when you think about which way is up. The interactive demos in the following chapters use screen coordinates.

Comments

No comments yet. Questions and corrections are welcome.

Plain text, line breaks kept. Your IP address is stored only as a one-way hash, to limit spam.