Swadge 2024 2.0.0
APIs to develop games for the Magfest Swadge
Loading...
Searching...
No Matches
geometryFl.h File Reference

Detailed Description

Design Philosophy

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/

Usage

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.

Example

circleFl_t circ = {
.pos.x = 2.5f,
.pos.y = 5.5f,
.radius = 8.5f,
};
rectangleFl_t rect = {
.pos.x = -2.5f,
.pos.y = -4.5f,
.width = 6.5f,
.height = 9.5f,
};
if (circleRectFlIntersection(circ, rect, NULL))
{
printf("Shapes intersect\n");
}
else
{
printf("Shapes do not intersect\n");
}
bool circleRectFlIntersection(circleFl_t circle, rectangleFl_t rect, vecFl_t *collisionVec)
Check if a circle and a rectangle intersect.
Definition geometryFl.c:123
vecFl_t pos
The position of the center of the circle.
Definition geometryFl.h:55
Signed integer representation of a circle.
Definition geometryFl.h:54
Signed integer representation of a rectangle.
Definition geometryFl.h:63
float x
The signed integer X component of the vector.
Definition vectorFl2d.h:38

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.
 

Data Structure Documentation

◆ circleFl_t

struct circleFl_t
Data Fields
vecFl_t pos The position of the center of the circle.
float radius The radius of the circle.

◆ rectangleFl_t

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.

◆ lineFl_t

struct lineFl_t
Data Fields
vecFl_t p1 One end point of the line segment.
vecFl_t p2 The other end point of the line segment.

Macro Definition Documentation

◆ EPSILON

#define EPSILON   0.0001f

A small value to account for floating point rounding errors

Function Documentation

◆ circleCircleFlIntersection()

bool circleCircleFlIntersection ( circleFl_t circle1,
circleFl_t circle2,
vecFl_t * collisionPoint,
vecFl_t * collisionVec )

Check if two circles intersect.

Parameters
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.
Returns
true if the circles intersect or touch, false if they do not

◆ circlePointFlIntersection()

bool circlePointFlIntersection ( circleFl_t circle,
vecFl_t point,
vecFl_t * collisionVec )

Check if a point and a circle intersect.

Parameters
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.
Returns
true if the point intersects or touches the circle, false if it doesn't

◆ circleRectFlIntersection()

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

Parameters
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.
Returns
true if the shapes intersect or touch, false if they do not

◆ circleLineFlIntersection()

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

Parameters
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.
Returns
true if the circle intersects the line, false if it doesn't

◆ circleLineFlIntersectionPoints()

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

Parameters
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
Returns
The number of intersections, between 0 and 2. intersection_1 is filled before intersection_2.

◆ rectRectFlIntersection()

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

Parameters
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.
Returns
true if the rectangles intersect or touch, false if they do not

◆ rectLineFlIntersection()

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

Parameters
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.
Returns
true if the line intersects the rectangle, false if it doesn't

◆ lineLineFlIntersection()

bool lineLineFlIntersection ( lineFl_t a,
lineFl_t b )

Check if two lines intersect.

Adapted from https://www.jeffreythompson.org/collision-detection/line-line.php

Parameters
a[IN] One line to check for intersection
b[IN] Another line to check for intersection
Returns
true if the lines intersect, false if they do not

◆ infLineIntersectionPoint()

vecFl_t infLineIntersectionPoint ( lineFl_t a,
lineFl_t b )

Find the intersection point between two infinitely long lines.

Parameters
aOne infinitely long line
bAnother infinitely long line
Returns
The intersection, or (FLT_MAX, FLT_MAX) for parallel lines