|
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 | circleInfLineFlIntersectionPoints (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 a, lineFl_t b, vecFl_t *intersection) |
| Check if two lines intersect. | |
| vecFl_t | infLineIntersectionPoint (lineFl_t a, lineFl_t b) |
| Find the intersection point between two infinitely long lines. | |
| lineFl_t | getLineBoundingBox (lineFl_t line) |
| Get a bounding box around a line where p1.x < p2.x and p1.y < p2.y. | |
| int16_t | circleLineSegFlIntersection (circleFl_t circle, lineFl_t line, lineFl_t boundingBox, vecFl_t *intersections) |
| Check if a line segment intersects with a circle. This uses circleInfLineFlIntersectionPoints(). | |
| 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 circleInfLineFlIntersectionPoints | ( | 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 |
| intersection | [OUT] The point at which the two lines intersect. May be NULL |
Find the intersection point between two infinitely long lines.
| a | One infinitely long line |
| b | Another infinitely long line |
Get a bounding box around a line where p1.x < p2.x and p1.y < p2.y.
| line | The line to get a bounding box for |
| int16_t circleLineSegFlIntersection | ( | circleFl_t | circle, |
| lineFl_t | line, | ||
| lineFl_t | boundingBox, | ||
| vecFl_t * | intersections ) |
Check if a line segment intersects with a circle. This uses circleInfLineFlIntersectionPoints().
| circle | [IN] The circle to check for intersection |
| line | [IN] The line to check for intersection |
| boundingBox | [IN] The bounding box of the line, found with getLineBoundingBox() |
| intersections | [OUT] Array of two vecFl_t. If it exists, the intersections will be written to this pointer |