Operation Null Mind 0.4.1
A 2D roguelike top down shooter game made with SDL2
 
Loading...
Searching...
No Matches
vec2.h File Reference

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.
 

Detailed Description

Provides 2D vector functionality for positions, directions, and transformations.

Being a top down 2D game, the Vec2 struct can be used to represent:

  • Positions
  • Velocities
  • Directions
  • Sizes
  • Offsets
  • And so on

The functions in this file can be used to perform common vector operations such as addition, subtraction, normalization, and rotation.

Usage Examples

Usage Examples1. Movement calculations:

Vec2 position = {10, 20};
Vec2 velocity = {5, 0};
// Move the position by the velocity
position = Vec2_Add(position, velocity);
A 2D vector structure.
Definition vec2.h:81
Vec2 Vec2_Add(Vec2 a, Vec2 b)
[Utility] Adds two vectors.
Definition vec2.c:62

2. Move towards a target:

Vec2 position = {10, 20};
Vec2 target = {100, 50};
float speed = 5;
// Calculate the direction to the target (Read more about Vec2_Normalize)
Vec2 direction = Vec2_Normalize(Vec2_Subtract(target, position));
// Move towards the target
position = Vec2_Add(position, Vec2_Multiply(direction, speed));
Vec2 Vec2_Normalize(Vec2 v)
[Utility] Returns the normalized vector of the input vector.
Definition vec2.c:49
Vec2 Vec2_Multiply(Vec2 v, float scalar)
[Utility] Multiplies a vector by a scalar.
Definition vec2.c:110
Vec2 Vec2_Subtract(Vec2 a, Vec2 b)
[Utility] Subtracts vector b from vector a.
Definition vec2.c:86

3. Rendering a rectangle:

Vec2 position = {100, 200};
Vec2 size = {50, 50};
SDL_Rect renderRect = Vec2_ToRect(position, size);
SDL_SetRenderDrawColor(app.resources.renderer, 255, 0, 0, 255);
SDL_RenderFillRect(app.resources.renderer, &renderRect);
AppData app
A global variable containing the app's resources, state, and configurations.
AppResources resources
The app's resources.
Definition app.h:108
SDL_Renderer * renderer
The SDL renderer.
Definition app.h:68
SDL_Rect Vec2_ToRect(Vec2 position, Vec2 size)
[Utility] Converts a position and size into an SDL_Rect.
Definition vec2.c:208

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:

// ...
SDL_Rect = Vec2_ToCenteredRect(position, size);
// ...
SDL_Rect Vec2_ToCenteredRect(Vec2 position, Vec2 size)
[Utility] Converts a position and size into a centered SDL_Rect.
Definition vec2.c:225

Here's a visualization of this:

Author
Mango
Date
2025-02-28

Typedef Documentation

◆ Vec2

typedef struct Vec2 Vec2

Function Documentation

◆ Vec2_Abs()

Vec2 Vec2_Abs ( Vec2  v)

Get the absolute value of a vector.

Parameters
vThe vector
Returns
The absolute value vector

Get the absolute value of a vector.

Parameters
vThe vector to compute absolute values for.
Returns
A vector where each component is the absolute value of v.

◆ Vec2_Add()

Vec2 Vec2_Add ( Vec2  a,
Vec2  b 
)

Add two vectors.

Parameters
aFirst vector
bSecond vector
Returns
The resulting vector

Add two vectors.

Parameters
aThe first vector.
bThe second vector.
Returns
The resulting vector from a + b.

◆ Vec2_AngleBetween()

float Vec2_AngleBetween ( Vec2  a,
Vec2  b 
)

Calculates the angle between two vectors in degrees.

Parameters
aThe first vector.
bThe second vector.
Returns
The angle in degrees between the two vectors. This means that Vec2_RotateDegrees(a, angle) will be equal to b.

◆ Vec2_AreEqual()

bool Vec2_AreEqual ( Vec2  a,
Vec2  b 
)

◆ Vec2_Clamp()

Vec2 Vec2_Clamp ( Vec2  v,
Vec2  min,
Vec2  max 
)

Clamp a vector between two vectors.

Parameters
vThe vector to clamp
minThe minimum vector
maxThe maximum vector
Returns
The clamped vector

Clamp a vector between two vectors.

Ensures the vector's components stay within the provided bounds.

Parameters
vThe vector to clamp.
minThe minimum values.
maxThe maximum values.
Returns
The clamped vector.

◆ Vec2_Decrement()

Vec2 Vec2_Decrement ( Vec2 *  a,
Vec2  b 
)

Decrement vector a by vector b.

Parameters
aPointer to the first vector
bSecond vector
Returns
The decremented vector

Decrement vector a by vector b.

Parameters
aPointer to the vector to decrement.
bThe vector by which a will be decremented.
Returns
The updated vector a after subtraction.

◆ Vec2_Distance()

float Vec2_Distance ( Vec2  a,
Vec2  b 
)

Calculate the distance between two vectors.

Parameters
aFirst vector
bSecond vector
Returns
The distance as a float

Calculate the distance between two vectors.

Magnitude is the length of a vector.

Parameters
vThe vector to get the magnitude of.
Returns
The magnitude (length) of vector v.

◆ Vec2_Divide()

Vec2 Vec2_Divide ( Vec2  v,
float  scalar 
)

Divide a vector by a scalar.

Parameters
vThe vector
scalarThe scalar value
Returns
The resulting vector

Divide a vector by a scalar.

Parameters
vThe vector to divide.
scalarThe scalar divisor.
Returns
The resulting vector v / scalar.

◆ Vec2_Dot()

float Vec2_Dot ( Vec2  a,
Vec2  b 
)

Calculate the dot product of two vectors.

Parameters
aFirst vector
bSecond vector
Returns
The dot product as a float

Calculate the dot product of two vectors.

Parameters
aThe first vector.
bThe second vector.
Returns
The dot product (a.x * b.x + a.y * b.y).

◆ Vec2_FromPoint()

Vec2 Vec2_FromPoint ( SDL_Point  point)

Convert an SDL_Point to a Vec2.

Parameters
pointThe SDL_Point
Returns
The resulting Vec2

Convert an SDL_Point to a Vec2.

Parameters
pointThe SDL_Point to convert.
Returns
The corresponding Vec2.

◆ Vec2_FromRect()

void Vec2_FromRect ( SDL_Rect  rect,
Vec2 *  position,
Vec2 *  size 
)

Extract position and size from an SDL_Rect.

Parameters
rectThe SDL_Rect
positionPointer to the position vector
sizePointer to the size vector

Extract position and size from an SDL_Rect.

Parameters
rectThe SDL_Rect to decompose.
positionPointer to store the position.
sizePointer to store the size.

◆ Vec2_Increment()

Vec2 Vec2_Increment ( Vec2 *  a,
Vec2  b 
)

Increment vector a by vector b.

Parameters
aPointer to the first vector
bSecond vector
Returns
The incremented vector

Increment vector a by vector b.

Parameters
aPointer to the vector to increment.
bThe vector to add.
Returns
The updated vector a (a + b).

◆ Vec2_Lerp()

Vec2 Vec2_Lerp ( Vec2  a,
Vec2  b,
float  t 
)

Linearly interpolate between two vectors.

Parameters
aFirst vector
bSecond vector
tInterpolation factor (0 to 1)
Returns
The interpolated vector

Linearly interpolate between two vectors.

Parameters
aThe start vector.
bThe destination vector.
tThe interpolation factor (0.0 to 1.0).
Returns
The interpolated vector between a and b.

◆ Vec2_Magnitude()

float Vec2_Magnitude ( Vec2  v)

Calculate the magnitude (length) of a vector.

Parameters
vThe input vector
Returns
The magnitude as a float

Calculate the magnitude (length) of a vector.

Magnitude is the length of a vector.

Parameters
vThe vector to get the magnitude of.
Returns
The magnitude (length) of vector v.

◆ Vec2_Max()

Vec2 Vec2_Max ( Vec2  a,
Vec2  b 
)

Get the maximum of two vectors.

Parameters
aFirst vector
bSecond vector
Returns
The maximum vector

Get the maximum of two vectors.

Parameters
aThe first vector.
bThe second vector.
Returns
A vector of max components.

◆ Vec2_Min()

Vec2 Vec2_Min ( Vec2  a,
Vec2  b 
)

Get the minimum of two vectors.

Parameters
aFirst vector
bSecond vector
Returns
The minimum vector

Get the minimum of two vectors.

Parameters
aThe first vector.
bThe second vector.
Returns
A vector of min components.

◆ Vec2_Multiply()

Vec2 Vec2_Multiply ( Vec2  v,
float  scalar 
)

Multiply a vector by a scalar.

Parameters
vThe vector
scalarThe scalar value
Returns
The resulting vector

Multiply a vector by a scalar.

Parameters
vThe vector to multiply.
scalarThe scalar multiplier.
Returns
The resulting vector v * scalar.

◆ Vec2_Normalize()

Vec2 Vec2_Normalize ( Vec2  v)

Normalize a vector (make it unit length)

Parameters
vThe vector to normalize
Returns
The normalized vector

Normalize a vector (make it unit length)

A normalized vector has a magnitude of 1 and the same direction as v.

Parameters
vThe vector to normalize.
Returns
The normalized vector of v.

◆ Vec2_RotateAroundDegrees()

Vec2 Vec2_RotateAroundDegrees ( Vec2  point,
Vec2  center,
float  angle 
)

Rotate a point around a center by an angle in degrees.

Parameters
pointThe point to rotate
centerThe center of rotation
angleThe angle in degrees
Returns
The rotated point

Rotate a point around a center by an angle in degrees.

Parameters
pointThe point to rotate.
centerThe pivot for rotation.
angleThe rotation angle in degrees.
Returns
The rotated point.

◆ Vec2_RotateAroundRadians()

Vec2 Vec2_RotateAroundRadians ( Vec2  point,
Vec2  center,
float  angle 
)

Rotate a point around a center by an angle in radians.

Parameters
pointThe point to rotate
centerThe center of rotation
angleThe angle in radians
Returns
The rotated point

Rotate a point around a center by an angle in radians.

Translates the point to origin-relative, rotates it, then translates back.

Parameters
pointThe point to rotate.
centerThe pivot for rotation.
angleThe rotation angle in radians.
Returns
The rotated point.

◆ Vec2_RotateDegrees()

Vec2 Vec2_RotateDegrees ( Vec2  v,
float  angle 
)

Rotate a vector by an angle in degrees.

Parameters
vThe vector
angleThe angle in degrees
Returns
The rotated vector

Rotate a vector by an angle in degrees.

Parameters
vThe vector to rotate.
angleThe angle in degrees.
Returns
The rotated vector.

◆ Vec2_RotateRadians()

Vec2 Vec2_RotateRadians ( Vec2  v,
float  angle 
)

Rotate a vector by an angle in radians.

Parameters
vThe vector
angleThe angle in radians
Returns
The rotated vector

Rotate a vector by an angle in radians.

Parameters
vThe vector to rotate.
angleThe angle to rotate by in radians.
Returns
The rotated vector.

◆ Vec2_Square()

Vec2 Vec2_Square ( float  side)

◆ Vec2_Subtract()

Vec2 Vec2_Subtract ( Vec2  a,
Vec2  b 
)

Subtract vector b from vector a.

Parameters
aFirst vector
bSecond vector
Returns
The resulting vector

Subtract vector b from vector a.

Parameters
aThe vector to subtract from.
bThe vector to subtract.
Returns
The result of a - b.

◆ Vec2_ToCenteredPosition()

Vec2 Vec2_ToCenteredPosition ( Vec2  position,
Vec2  size 
)

Convert a position and size to a centered position.

Parameters
positionThe position vector
sizeThe size vector
Returns
The resulting centered position vector

Convert a position and size to a centered position.

Useful for positioning sprites centered on a coordinate.

Parameters
positionThe top-left position.
sizeThe size of the object.
Returns
The centered position.

◆ Vec2_ToCenteredRect()

SDL_Rect Vec2_ToCenteredRect ( Vec2  position,
Vec2  size 
)

Convert a position and size to a centered SDL_Rect.

Parameters
positionThe position vector
sizeThe size vector
Returns
The resulting centered SDL_Rect

Convert a position and size to a centered SDL_Rect.

Centers the rect at the specified position.

Parameters
positionThe center position.
sizeThe width and height.
Returns
A centered SDL_Rect.

◆ Vec2_ToCenteredSquareRect()

SDL_Rect Vec2_ToCenteredSquareRect ( Vec2  position,
float  side 
)

◆ Vec2_ToPoint()

SDL_Point Vec2_ToPoint ( Vec2  v)

Convert a Vec2 to an SDL_Point.

Parameters
vThe vector
Returns
The resulting SDL_Point

Convert a Vec2 to an SDL_Point.

Parameters
vThe Vec2 to convert.
Returns
The corresponding SDL_Point.

◆ Vec2_ToRect()

SDL_Rect Vec2_ToRect ( Vec2  position,
Vec2  size 
)

Convert a position and size to an SDL_Rect.

Parameters
positionThe position vector
sizeThe size vector
Returns
The resulting SDL_Rect

Convert a position and size to an SDL_Rect.

Parameters
positionThe top-left position.
sizeThe width and height.
Returns
The SDL_Rect corresponding to position and size.

◆ Vec2_ToSquareRect()

SDL_Rect Vec2_ToSquareRect ( Vec2  position,
float  side 
)