Polar coordinates represent a position using an angle and a distance from a point. This is another way to represent a position than Cartesian coordinates, which use distances from a point along axes.
Among other applications, polar coordinates are useful to represent:
- The force of throwing a projectile at a given angle. For example, you can use this in a game that lets the player orient and control the strength at which they throw a grenade.
- A random position in a circular area. You can easily calculate a random angle and distance to generate a random position within a circle.
Given an angle and a distance, you can take a unit vector pointing to the right, rotate it by the angle, and multiply it by the distance to convert polar coordinates to Cartesian ones.
Here's a GDScript code example that uses polar coordinates to generate a random position within a circle:
var random_angle := randf_range(0.0, 2.0 * PI)
var random_direction := Vector2(1.0, 0.0).rotated(random_angle)
var random_distance := randf_range(60.0, 120.0)
position = random_direction * random_distance
This code generates a random angle between 0 and 2π, a random direction by rotating a unit vector by the random angle, and a random distance between 60 and 120. By multiplying the direction by the distance, we get a random position within a circle.See Also
Related terms in the Glossary