Platformer Prototype
A Bare-Bones Platformer
Functions
Helpers.cpp File Reference

Code for helper functions. More...

#include "Helpers.h"

Functions

const Vector2 AngleToVector (const float theta)
 Convert angle to vector. More...
 
const Vector2 VectorNormalCC (const Vector2 &v)
 Counterclockwise normal. More...
 
void NormalizeAngle (float &theta)
 Normalize angle to \(\pm\pi\). More...
 
bool GetCollisionInfo (const float a, const float b, const float r, float &d)
 
bool GetCollisionInfo (BoundingBox b, BoundingSphere s, Vector2 &norm, float &d)
 Get collision information. More...
 

Function Documentation

◆ AngleToVector()

const Vector2 AngleToVector ( const float  theta)

Compute a unit vector at an angle measured in radians counterclockwise from the positive X-axis. If \(\vec{v} = [v_x, v_y]\) is a unit vector with tail at the Origin and \(\theta\) is the angle between \(\vec{v}\) and the positive X-axis, then \(\sin \theta = v_y\) and \(\cos \theta = v_x\). Hence, \(\vec{v} = [\cos \theta, \sin \theta]\), as shown below. Therefore, this function returns Vector2(cosf(theta), sinf(theta)).

AngleToVector.png
Parameters
thetaAngle in radians.
Returns
Unit vector at angle theta counterclockwise from positive X.

◆ GetCollisionInfo() [1/2]

bool GetCollisionInfo ( const float  a,
const float  b,
const float  r,
float &  d 
)

Get collision information for a circle intersecting a line segment given that neither end point of the line is inside the circle. The result will not be valid if either end point is inside the circle. All computation is done in one dimension only, and therefore can be used for both the x-axis and the y-axis if desired.

Parameters
aOne end of the line segment.
bThe other end of the line segment.
rRadius of the circle.
d[out] Overlap distance.
Returns
true if the circle overlaps the line.

◆ GetCollisionInfo() [2/2]

bool GetCollisionInfo ( BoundingBox  b,
BoundingSphere  s,
Vector2 &  norm,
float &  d 
)

Get collision information for a circle intersecting an AABB.

Parameters
bAn AABB.
sA bounding sphere.
norm[out] The collision normal.
d[out] The overlap distance.
Returns
true if the AABB overlaps the bounding sphere.

◆ NormalizeAngle()

void NormalizeAngle ( float &  theta)

Normalize an angle in radians to \(\pm\pi\). If the angle is very large or very small, then this function will be very slow. However, it will be very fast if the angle is not too far out of range.

Parameters
theta[in, out] An angle in radians.

◆ VectorNormalCC()

const Vector2 VectorNormalCC ( const Vector2 &  v)

Compute the counterclockwise unit perpendicular to a vector. If \(\vec{v} = [v_x, v_y]\), then both dot products \(\vec{v} \cdot [-v_y, v_x]\) and \(\vec{v} \cdot [v_y, -v_x]\) are equal to zero, and therefore both \([-v_y, v_x]\) and \([v_y, -v_x]\) are perpendicular to \(\vec{v}\). The former (drawn in green below) points counterclockwise from \(\vec{v}\) and the latter (drawn in purple below) points clockwise from \(\vec{v}\). Therefore, this function computes Vector2(-v.y, v.x) and normalizes it before returning it.

Perp.png
Parameters
vA vector, not necessarily normalized.
Returns
The counterclockwise unit perpendicular to v.