Provides 2D vector functionality for positions, directions, and transformations. More...
#include <SDL.h>#include <stdbool.h>Go to the source code of this file.
Classes | |
| struct | Vec2 |
| A 2D vector structure. More... | |
Typedefs | |
| typedef struct Vec2 | Vec2 |
Functions | |
| float | Vec2_Magnitude (Vec2 v) |
| Calculate the magnitude (length) of a vector. | |
| float | Vec2_Distance (Vec2 a, Vec2 b) |
| Calculate the distance between two vectors. | |
| Vec2 | Vec2_Normalize (Vec2 v) |
| Normalize a vector (make it unit length) | |
| Vec2 | Vec2_Add (Vec2 a, Vec2 b) |
| Add two vectors. | |
| Vec2 | Vec2_Increment (Vec2 *a, Vec2 b) |
| Increment vector a by vector b. | |
| Vec2 | Vec2_Subtract (Vec2 a, Vec2 b) |
| Subtract vector b from vector a. | |
| Vec2 | Vec2_Decrement (Vec2 *a, Vec2 b) |
| Decrement vector a by vector b. | |
| Vec2 | Vec2_Multiply (Vec2 v, float scalar) |
| Multiply a vector by a scalar. | |
| Vec2 | Vec2_Divide (Vec2 v, float scalar) |
| Divide a vector by a scalar. | |
| float | Vec2_Dot (Vec2 a, Vec2 b) |
| Calculate the dot product of two vectors. | |
| Vec2 | Vec2_Lerp (Vec2 a, Vec2 b, float t) |
| Linearly interpolate between two vectors. | |
| Vec2 | Vec2_RotateDegrees (Vec2 v, float angle) |
| Rotate a vector by an angle in degrees. | |
| Vec2 | Vec2_RotateRadians (Vec2 v, float angle) |
| Rotate a vector by an angle in radians. | |
| Vec2 | Vec2_RotateAroundRadians (Vec2 point, Vec2 center, float angle) |
| Rotate a point around a center by an angle in radians. | |
| Vec2 | Vec2_RotateAroundDegrees (Vec2 point, Vec2 center, float angle) |
| Rotate a point around a center by an angle in degrees. | |
| SDL_Rect | Vec2_ToRect (Vec2 position, Vec2 size) |
| Convert a position and size to an SDL_Rect. | |
| SDL_Rect | Vec2_ToCenteredRect (Vec2 position, Vec2 size) |
| Convert a position and size to a centered SDL_Rect. | |
| Vec2 | Vec2_ToCenteredPosition (Vec2 position, Vec2 size) |
| Convert a position and size to a centered position. | |
| void | Vec2_FromRect (SDL_Rect rect, Vec2 *position, Vec2 *size) |
| Extract position and size from an SDL_Rect. | |
| Vec2 | Vec2_Clamp (Vec2 v, Vec2 min, Vec2 max) |
| Clamp a vector between two vectors. | |
| Vec2 | Vec2_Max (Vec2 a, Vec2 b) |
| Get the maximum of two vectors. | |
| Vec2 | Vec2_Min (Vec2 a, Vec2 b) |
| Get the minimum of two vectors. | |
| Vec2 | Vec2_Abs (Vec2 v) |
| Get the absolute value of a vector. | |
| Vec2 | Vec2_FromPoint (SDL_Point point) |
| Convert an SDL_Point to a Vec2. | |
| SDL_Point | Vec2_ToPoint (Vec2 v) |
| Convert a Vec2 to an SDL_Point. | |
| bool | Vec2_AreEqual (Vec2 a, Vec2 b) |
| Vec2 | Vec2_Square (float side) |
| SDL_Rect | Vec2_ToSquareRect (Vec2 position, float side) |
| SDL_Rect | Vec2_ToCenteredSquareRect (Vec2 position, float side) |
| float | Vec2_AngleBetween (Vec2 a, Vec2 b) |
| Calculates the angle between two vectors in degrees. | |
Provides 2D vector functionality for positions, directions, and transformations.
Being a top down 2D game, the Vec2 struct can be used to represent:
The functions in this file can be used to perform common vector operations such as addition, subtraction, normalization, and rotation.
In this case, the position (100, 200) represent where the top-left corner of the rectangle will be rendered.
If you want to render the rectangle centered around the position, you can use Vec2_ToCenteredRect:
Here's a visualization of this:
Get the absolute value of a vector.
| v | The vector |
Get the absolute value of a vector.
| v | The vector to compute absolute values for. |
Add two vectors.
| a | First vector |
| b | Second vector |
Add two vectors.
| a | The first vector. |
| b | The second vector. |
Calculates the angle between two vectors in degrees.
| a | The first vector. |
| b | The second vector. |
Clamp a vector between two vectors.
| v | The vector to clamp |
| min | The minimum vector |
| max | The maximum vector |
Clamp a vector between two vectors.
Ensures the vector's components stay within the provided bounds.
| v | The vector to clamp. |
| min | The minimum values. |
| max | The maximum values. |
Decrement vector a by vector b.
| a | Pointer to the first vector |
| b | Second vector |
Decrement vector a by vector b.
| a | Pointer to the vector to decrement. |
| b | The vector by which a will be decremented. |
Calculate the distance between two vectors.
| a | First vector |
| b | Second vector |
Calculate the distance between two vectors.
Magnitude is the length of a vector.
| v | The vector to get the magnitude of. |
Divide a vector by a scalar.
| v | The vector |
| scalar | The scalar value |
Divide a vector by a scalar.
| v | The vector to divide. |
| scalar | The scalar divisor. |
Calculate the dot product of two vectors.
| a | First vector |
| b | Second vector |
Calculate the dot product of two vectors.
| a | The first vector. |
| b | The second vector. |
| Vec2 Vec2_FromPoint | ( | SDL_Point | point | ) |
Extract position and size from an SDL_Rect.
| rect | The SDL_Rect |
| position | Pointer to the position vector |
| size | Pointer to the size vector |
Extract position and size from an SDL_Rect.
| rect | The SDL_Rect to decompose. |
| position | Pointer to store the position. |
| size | Pointer to store the size. |
Increment vector a by vector b.
| a | Pointer to the first vector |
| b | Second vector |
Increment vector a by vector b.
| a | Pointer to the vector to increment. |
| b | The vector to add. |
Linearly interpolate between two vectors.
| a | First vector |
| b | Second vector |
| t | Interpolation factor (0 to 1) |
Linearly interpolate between two vectors.
| a | The start vector. |
| b | The destination vector. |
| t | The interpolation factor (0.0 to 1.0). |
| float Vec2_Magnitude | ( | Vec2 | v | ) |
Calculate the magnitude (length) of a vector.
| v | The input vector |
Calculate the magnitude (length) of a vector.
Magnitude is the length of a vector.
| v | The vector to get the magnitude of. |
Get the maximum of two vectors.
| a | First vector |
| b | Second vector |
Get the maximum of two vectors.
| a | The first vector. |
| b | The second vector. |
Get the minimum of two vectors.
| a | First vector |
| b | Second vector |
Get the minimum of two vectors.
| a | The first vector. |
| b | The second vector. |
Multiply a vector by a scalar.
| v | The vector |
| scalar | The scalar value |
Multiply a vector by a scalar.
| v | The vector to multiply. |
| scalar | The scalar multiplier. |
Normalize a vector (make it unit length)
| v | The vector to normalize |
Normalize a vector (make it unit length)
A normalized vector has a magnitude of 1 and the same direction as v.
| v | The vector to normalize. |
Rotate a point around a center by an angle in degrees.
| point | The point to rotate |
| center | The center of rotation |
| angle | The angle in degrees |
Rotate a point around a center by an angle in degrees.
| point | The point to rotate. |
| center | The pivot for rotation. |
| angle | The rotation angle in degrees. |
Rotate a point around a center by an angle in radians.
| point | The point to rotate |
| center | The center of rotation |
| angle | The angle in radians |
Rotate a point around a center by an angle in radians.
Translates the point to origin-relative, rotates it, then translates back.
| point | The point to rotate. |
| center | The pivot for rotation. |
| angle | The rotation angle in radians. |
Rotate a vector by an angle in degrees.
| v | The vector |
| angle | The angle in degrees |
Rotate a vector by an angle in degrees.
| v | The vector to rotate. |
| angle | The angle in degrees. |
Rotate a vector by an angle in radians.
| v | The vector |
| angle | The angle in radians |
Rotate a vector by an angle in radians.
| v | The vector to rotate. |
| angle | The angle to rotate by in radians. |
| Vec2 Vec2_Square | ( | float | side | ) |
Subtract vector b from vector a.
| a | First vector |
| b | Second vector |
Subtract vector b from vector a.
| a | The vector to subtract from. |
| b | The vector to subtract. |
Convert a position and size to a centered position.
| position | The position vector |
| size | The size vector |
Convert a position and size to a centered position.
Useful for positioning sprites centered on a coordinate.
| position | The top-left position. |
| size | The size of the object. |
Convert a position and size to a centered SDL_Rect.
| position | The position vector |
| size | The size vector |
Convert a position and size to a centered SDL_Rect.
Centers the rect at the specified position.
| position | The center position. |
| size | The width and height. |
| SDL_Rect Vec2_ToCenteredSquareRect | ( | Vec2 | position, |
| float | side | ||
| ) |
| SDL_Point Vec2_ToPoint | ( | Vec2 | v | ) |
Convert a position and size to an SDL_Rect.
| position | The position vector |
| size | The size vector |
Convert a position and size to an SDL_Rect.
| position | The top-left position. |
| size | The width and height. |
| SDL_Rect Vec2_ToSquareRect | ( | Vec2 | position, |
| float | side | ||
| ) |