Swadge 2024 2.0.0
APIs to develop games for the Magfest Swadge
|
These utility functions are can be used for geometric math with floating point numbers.
Much of this file was adapted from https://www.jeffreythompson.org/collision-detection/
No initialization or deinitialization is required. Each function will not modify the given shapes. See the functions below for what is provided and how to use them.
Go to the source code of this file.
Data Structures | |
struct | circleFl_t |
Signed integer representation of a circle. More... | |
struct | rectangleFl_t |
Signed integer representation of a rectangle. More... | |
struct | lineFl_t |
Signed integer representation of a line segment. More... | |
Macros | |
#define | EPSILON 0.0001f |
Functions | |
bool | circleCircleFlIntersection (circleFl_t circle1, circleFl_t circle2, vecFl_t *collisionPoint, vecFl_t *collisionVec) |
Check if two circles intersect. | |
bool | circlePointFlIntersection (circleFl_t circle, vecFl_t point, vecFl_t *collisionVec) |
Check if a point and a circle intersect. | |
bool | circleRectFlIntersection (circleFl_t circle, rectangleFl_t rect, vecFl_t *collisionVec) |
Check if a circle and a rectangle intersect. | |
bool | circleLineFlIntersection (circleFl_t circle, lineFl_t line, bool checkEnds, vecFl_t *cpOnLine, vecFl_t *collisionVec) |
Check if a circle and a line intersect. | |
int16_t | circleLineFlIntersectionPoints (circleFl_t circle, lineFl_t line, vecFl_t *intersection_1, vecFl_t *intersection_2) |
Find the points of intersection between a circle's circumference and a line. There may be zero (no intersection), one (tangent), or two points of intersection. | |
bool | rectRectFlIntersection (rectangleFl_t rect1, rectangleFl_t rect2, vecFl_t *collisionVec) |
Check if two rectangles intersect. | |
bool | rectLineFlIntersection (rectangleFl_t rect, lineFl_t line, vecFl_t *collisionVec) |
Check if a line intersects with a rectangle. | |
bool | lineLineFlIntersection (lineFl_t line1, lineFl_t line2) |
Check if two lines intersect. | |
vecFl_t | infLineIntersectionPoint (lineFl_t a, lineFl_t b) |
Find the intersection point between two infinitely long lines. | |
struct circleFl_t |
Data Fields | ||
---|---|---|
vecFl_t | pos | The position of the center of the circle. |
float | radius | The radius of the circle. |
struct rectangleFl_t |
Data Fields | ||
---|---|---|
vecFl_t | pos | The position the top left corner of the rectangle. |
float | width | The width of the rectangle. |
float | height | The height of the rectangle. |
struct lineFl_t |
#define EPSILON 0.0001f |
A small value to account for floating point rounding errors
bool circleCircleFlIntersection | ( | circleFl_t | circle1, |
circleFl_t | circle2, | ||
vecFl_t * | collisionPoint, | ||
vecFl_t * | collisionVec ) |
Check if two circles intersect.
circle1 | [IN] One circle to check for intersection, assumed to be moving |
circle2 | [IN] The other circle to check for intersection, assumed to be fixed |
collisionPoint | [OUT] The point at which the two circles collide, on the circumference of circle2. May be NULL |
collisionVec | [OUT] A vector pointing from the second circle to the first in the direction of the collision. May be NULL. |
bool circlePointFlIntersection | ( | circleFl_t | circle, |
vecFl_t | point, | ||
vecFl_t * | collisionVec ) |
Check if a point and a circle intersect.
circle | [IN] A circle to check for intersection |
point | [IN] A point to check for intersection |
collisionVec | [OUT] A vector pointing from the point to the circle in the direction of the collision. May be NULL. |
bool circleRectFlIntersection | ( | circleFl_t | circle, |
rectangleFl_t | rect, | ||
vecFl_t * | collisionVec ) |
Check if a circle and a rectangle intersect.
Adapted from https://www.jeffreythompson.org/collision-detection/circle-rect.php
circle | [IN] The circle to check for intersection |
rect | [IN] The rectangle to check for intersection |
collisionVec | [OUT] A vector pointing from the rectangle to the circle in the direction of the collision. May be NULL. |
bool circleLineFlIntersection | ( | circleFl_t | circle, |
lineFl_t | line, | ||
bool | checkEnds, | ||
vecFl_t * | cpOnLine, | ||
vecFl_t * | collisionVec ) |
Check if a circle and a line intersect.
Adapted from https://www.jeffreythompson.org/collision-detection/line-circle.php
circle | [IN] A circle to test intersection |
line | [IN] A line to test intersection |
checkEnds | [IN] True to check the points at the end of the line, false to ignore them |
cpOnLine | [OUT] The closest point on the line to the circle |
collisionVec | [OUT] A vector pointing from the line to the circle in the direction of the collision. This may be NULL. |
int16_t circleLineFlIntersectionPoints | ( | circleFl_t | circle, |
lineFl_t | line, | ||
vecFl_t * | intersection_1, | ||
vecFl_t * | intersection_2 ) |
Find the points of intersection between a circle's circumference and a line. There may be zero (no intersection), one (tangent), or two points of intersection.
See https://cp-algorithms.com/geometry/circle-line-intersection.html
circle | [IN] The circle to check for intersection |
line | [IN] The line to check for intersection |
intersection_1 | [OUT] If it exists, the first intersection will be written to this pointer |
intersection_2 | [OUT] If it exists, the second intersection will be written to this pointer |
intersection_1
is filled before intersection_2
. bool rectRectFlIntersection | ( | rectangleFl_t | rect1, |
rectangleFl_t | rect2, | ||
vecFl_t * | collisionVec ) |
Check if two rectangles intersect.
Adapted from https://www.jeffreythompson.org/collision-detection/rect-rect.php
rect1 | [IN] One rectangle to check for intersection |
rect2 | [IN] The other rectangle to check for intersection |
collisionVec | [OUT] A vector pointing from the second rectangle to the first in the direction of the collision. May be NULL. |
bool rectLineFlIntersection | ( | rectangleFl_t | rect, |
lineFl_t | line, | ||
vecFl_t * | collisionVec ) |
Check if a line intersects with a rectangle.
Adapted from https://www.jeffreythompson.org/collision-detection/line-rect.php
rect | [IN] A rectangle to check for intersection |
line | [IN] A line to check for intersection |
collisionVec | [OUT] A vector pointing from the rectangle to the line in the direction of the collision. May be NULL. |
Check if two lines intersect.
Adapted from https://www.jeffreythompson.org/collision-detection/line-line.php
a | [IN] One line to check for intersection |
b | [IN] Another line to check for intersection |