Angel 3.2
A 2D Game Prototyping Engine
Public Types | Static Public Member Functions | Static Public Attributes | List of all members
MathUtil Class Reference

A set of static functions that handle typical math needed for games. More...

#include <MathUtil.h>

Public Types

enum  AABBSplittingAxis { AA_X, AA_Y }
 

Static Public Member Functions

template<typename T >
static T Abs (T val)
 
template<typename T >
static T Max (T value1, T value2)
 
template<typename T >
static T Min (T value1, T value2)
 
template<typename T >
static T Distance (T value1, T value2)
 
template<typename T >
static T Lerp (T value1, T value2, float amount)
 
template<typename T >
static T SmoothStep (T value1, T value2, float amount)
 
static int Clamp (int value, int min, int max)
 
static float Clamp (float value, float min, float max)
 
static double Clamp (double value, double min, double max)
 
static float ToDegrees (float radians)
 
static float ToRadians (float degrees)
 
static Vector2 VectorFromAngle (float angle_in_degrees)
 
static float AngleFromVector (const Vector2 &v1)
 
static float AngleFromVectors (const Vector2 &v1, const Vector2 &v2)
 
static int RoundToInt (double x)
 
static int RandomInt (int maximum)
 
static int RandomIntInRange (int min, int max)
 
static int RandomIntWithError (int target, int error)
 
static float RandomFloat (float maximum=1.0f)
 
static float RandomFloatInRange (float min, float max)
 
static float RandomFloatWithError (float target, float error)
 
static bool RandomBool ()
 
static Vector2 RandomVector ()
 
static Vector2 RandomVector (const Vector2 &maxValues)
 
static Vector2 RandomVector (const Vector2 &minValues, const Vector2 &maxValues)
 
static Vector2List RandomPointField (int numPoints, const Vector2 &minValue, const Vector2 &maxValue, float minDistance=0.5f)
 
static bool FuzzyEquals (float value1, float value2, float epsilon=Epsilon)
 
static bool FuzzyEquals (const Vector2 &v1, const Vector2 &v2, float epsilon=Epsilon)
 
static Vector2 ScreenToWorld (int x, int y)
 
static Vector2 ScreenToWorld (const Vec2i &screenCoordinates)
 
static Vector2 WorldToScreen (float x, float y)
 
static Vector2 WorldToScreen (const Vector2 &worldCoordinates)
 
static Vector2 GetWorldDimensions ()
 
static float PixelsToWorldUnits (float pixels)
 
static float WorldUnitsToPixels (float worldUnits)
 
static AABBSplittingAxis GetMajorAxis (const BoundingBox &source)
 
static void SplitBoundingBox (const BoundingBox &source, AABBSplittingAxis axis, BoundingBox &LHS, BoundingBox &RHS)
 
static float DeltaAngle (float A1, float A2)
 
static float VectorDeltaAngle (const Vector2 &v1, const Vector2 &v2)
 

Static Public Attributes

static const float E = 2.718282f
 
static const float Log10E = 0.4342945f
 
static const float Log2E = 1.442695f
 
static const float Pi = 3.141593f
 
static const float PiOver2 = 1.570796f
 
static const float PiOver4 = 0.7853982f
 
static const float TwoPi = 6.283185f
 
static const float MaxFloat = 3.402823E+38f
 
static const float MinFloat = -3.402823E+38f
 
static const float Epsilon = 0.000001f
 

Detailed Description

This class is just a wrapper around a whole bunch of otherwise-loose functions that handle all kinds of math that is typically required in a game.

It also contains a set of constants so you don't have to look them up or declare them yourself.

Definition at line 45 of file MathUtil.h.

Member Function Documentation

template<typename T >
static T MathUtil::Abs ( val)
inlinestatic

A templated absolute value function that can handle any class where a comparison against 0 makes sense (floats and ints, mostly).

Parameters
valThe number
Returns
The absolute value of that number

Definition at line 108 of file MathUtil.h.

template<typename T >
static T MathUtil::Max ( value1,
value2 
)
inlinestatic

A templated max function that returns the greater of two values. Works for any class that implements a > operator.

Parameters
value1The first value
value2The second value
Returns
The greater of the two

Definition at line 122 of file MathUtil.h.

template<typename T >
static T MathUtil::Min ( value1,
value2 
)
inlinestatic

A templated min function that returns the lesser of two values. Works for any class that implements a < operator.

Parameters
value1The first value
value2The second value
Returns
The lesser of the two

Definition at line 136 of file MathUtil.h.

template<typename T >
static T MathUtil::Distance ( value1,
value2 
)
inlinestatic

Find the distance between two values. Works for any class that has a subtraction operator and will work with the Abs function.

Parameters
value1The first value
value2The second value
Returns
The distance between them

Definition at line 150 of file MathUtil.h.

template<typename T >
static T MathUtil::Lerp ( value1,
value2,
float  amount 
)
inlinestatic

Linearly interpolates between two values. Works for any classes that define addition, subtraction, and multiplication (by a float) operators.

http://en.wikipedia.org/wiki/Lerp_(computing)

Parameters
value1The starting value
value2The ending value
amountThe amount to lerp (from 0.0 to 1.0)
Returns
The interpolated value

Definition at line 168 of file MathUtil.h.

template<typename T >
static T MathUtil::SmoothStep ( value1,
value2,
float  amount 
)
inlinestatic

Smoothly step between two values. Works for any classes that Lerp would work for (and is essentially a drop-in replacement). Often looks visually better than a simple linear interpolation as it gives ease-in and ease-out.

http://www.fundza.com/rman_shaders/smoothstep/index.html

Parameters
value1The starting value
value2The ending value
amountThe amount to interpolate (from 0.0 to 1.0)
Returns
The interpolated value

Definition at line 187 of file MathUtil.h.

static int MathUtil::Clamp ( int  value,
int  min,
int  max 
)
inlinestatic

Clamps an integer to a specified range

Parameters
valueThe integer in question
minThe minimum of the range
maxThe maximum of the range
Returns
The clamped value

Definition at line 201 of file MathUtil.h.

static float MathUtil::Clamp ( float  value,
float  min,
float  max 
)
inlinestatic

Clamps a float to a specified range

Parameters
valueThe float in question
minThe minimum of the range
maxThe maximum of the range
Returns
The clamped value

Definition at line 214 of file MathUtil.h.

static double MathUtil::Clamp ( double  value,
double  min,
double  max 
)
inlinestatic

Clamps a double-precision float to a specified range

Parameters
valueThe double in question
minThe minimum of the range
maxThe maximum of the range
Returns
The clamped value

Definition at line 227 of file MathUtil.h.

float MathUtil::ToDegrees ( float  radians)
static

Convert radians to degrees

Parameters
radiansThe angle in radians
Returns
The angle in degrees

Definition at line 51 of file MathUtil.cpp.

float MathUtil::ToRadians ( float  degrees)
static

Convert degrees to radians

Parameters
degreesThe angle in degrees
Returns
The angle in radians

Definition at line 56 of file MathUtil.cpp.

Vector2 MathUtil::VectorFromAngle ( float  angle_in_degrees)
static

Get a unit-length vector which indicate a direction along the given angle relative to the screen.

Parameters
angle_in_degreesThe angle, in degrees
Returns
A vector moving along that angle

Definition at line 61 of file MathUtil.cpp.

float MathUtil::AngleFromVector ( const Vector2 v1)
static

Get an angle from a vector indicating direction

Parameters
v1The vector direction
Returns
The vector's angle

Definition at line 66 of file MathUtil.cpp.

float MathUtil::AngleFromVectors ( const Vector2 v1,
const Vector2 v2 
)
static

Get the angle between two vectors

Parameters
v1The first vector
v2The second vector
Returns
The angle between them in radians

Definition at line 74 of file MathUtil.cpp.

int MathUtil::RoundToInt ( double  x)
static

Takes a double or float and removes everything after the decimal point, making it into an integer.

Parameters
xThe double or float to round
Returns
The rounded integer

Definition at line 80 of file MathUtil.cpp.

int MathUtil::RandomInt ( int  maximum)
static

Get a random non-negative integer.

Parameters
maximumThe maximum value you want to see
Returns
A random number between 0 (inclusive) and maximum (exclusive)

Definition at line 85 of file MathUtil.cpp.

int MathUtil::RandomIntInRange ( int  min,
int  max 
)
static

Get a random integer in a specified range.

Parameters
minThe minimum value you want to see
maxThe maximum value you want to see
Returns
A random number between min (inclusive) and max (exclusive)

Definition at line 100 of file MathUtil.cpp.

int MathUtil::RandomIntWithError ( int  target,
int  error 
)
static

Get a random integer within a specified range of another one.

Parameters
targetThe target integer
errorThe maximum amount the returned value can differ from the target (in either direction)
Returns
A random int from (target - error) to (target + error)

Definition at line 105 of file MathUtil.cpp.

float MathUtil::RandomFloat ( float  maximum = 1.0f)
static

Get a random non-negative float.

Parameters
maximumThe maximum value you want to see
Returns
A random number between 0.0 (inclusive) and maximum (exclusive)

Definition at line 110 of file MathUtil.cpp.

float MathUtil::RandomFloatInRange ( float  min,
float  max 
)
static

Get a random float in a specified range.

Parameters
minThe minimum value you want to see
maxThe maximum value you want to see
Returns
A random number between min (inclusive) and max (exclusive)

Definition at line 118 of file MathUtil.cpp.

float MathUtil::RandomFloatWithError ( float  target,
float  error 
)
static

Get a random float within a specified range of another one.

Parameters
targetThe target float
errorThe maximum amount the returned value can differ from the target (in either direction)
Returns
A random float from (target - error) to (target + error)

Definition at line 123 of file MathUtil.cpp.

bool MathUtil::RandomBool ( )
static

Get a random bool.

Returns
Either true or false, randomly. :-)

Definition at line 128 of file MathUtil.cpp.

Vector2 MathUtil::RandomVector ( )
static

Get a random unit-length Vector2

Returns
A unit-length Vector2 pointing in a random direction

Definition at line 133 of file MathUtil.cpp.

Vector2 MathUtil::RandomVector ( const Vector2 maxValues)
static

Get a random Vector2 with specified maximum values

Parameters
maxValuesThe highest values for both axes
Returns
A random vector ranging from (0, 0) to (maxValues.X, maxValuesY)

Definition at line 138 of file MathUtil.cpp.

Vector2 MathUtil::RandomVector ( const Vector2 minValues,
const Vector2 maxValues 
)
static

Get a random Vector2 within a specified range

Parameters
minValuesThe lowest values for both axes
maxValuesThe highest values for both axes
Returns
A random vector ranging from (minValues.X, minValues.Y) to (maxValues.X, maxValues.y)

Definition at line 143 of file MathUtil.cpp.

Vector2List MathUtil::RandomPointField ( int  numPoints,
const Vector2 minValue,
const Vector2 maxValue,
float  minDistance = 0.5f 
)
static

Get a set of random points in a Poisson disc distribution. (If you're not familiar with that term, just know that it's more likely to give a somewhat even distribution of random points, a more "natural" looking distribution, along the lines of what you're imagining when you think of "random."

Parameters
numPointsThe desired number of points
minValueA point representing the bottom-left coordinate of the field
maxValueA point representing the top-right coordinate of the field
minDistanceThe smallest distance that will be allowed in the field, effectively determining the spacing. Note that this value will iteratively decrease if the field ends up too tightly packed.
Returns
The list of generated points

Definition at line 159 of file MathUtil.cpp.

bool MathUtil::FuzzyEquals ( float  value1,
float  value2,
float  epsilon = Epsilon 
)
static

Compare two floating point values for "equality," with a permissible amount of error. Oftentimes you only care if floats are "close enough for government work," and this function lets you make that determination.

(Because of rounding errors inherent in floating point arithmetic, direct comparison of floats is often inadvisable. http://en.wikipedia.org/wiki/Floating_point#Accuracy_problems )

Parameters
value1The first value
value2The second value
epsilonThe maximum allowable difference (defaults to MathUtil::Epsilon)
Returns
Whether the two values are within epsilon of each other

Definition at line 193 of file MathUtil.cpp.

bool MathUtil::FuzzyEquals ( const Vector2 v1,
const Vector2 v2,
float  epsilon = Epsilon 
)
static

A Vector2 comparison function using FuzzyEquals on the components.

Parameters
v1The first vector
v2The second vector
epsilonThe maximum allowable difference between them
Returns
Whether the two vectors have components within epsilon of each other

Definition at line 204 of file MathUtil.cpp.

Vector2 MathUtil::ScreenToWorld ( int  x,
int  y 
)
static

Convert screen (pixel) coordinates to world (GL unit) coordinates. This function is not terribly efficient, so be careful calling it too frequently.

Parameters
xThe pixel X coordinate
yThe pixel Y coordinate
Returns
The world space coordinates

Definition at line 218 of file MathUtil.cpp.

Vector2 MathUtil::ScreenToWorld ( const Vec2i screenCoordinates)
static

Convert screen (pixel) coordinates to world (GL unit) coordinates. This function is not terribly efficient, so be careful calling it too frequently.

Parameters
screenCoordinatesThe pixel coordinates
Returns
The world space coordinates

Definition at line 213 of file MathUtil.cpp.

Vector2 MathUtil::WorldToScreen ( float  x,
float  y 
)
static

Convert world (GL unit) coordinates to screen (pixel) coordinates. This function is not terribly efficient, so be careful calling it too frequently.

Parameters
xThe world X coordinate
yThe world Y coordinate
Returns
The screen space coordinates

Definition at line 253 of file MathUtil.cpp.

Vector2 MathUtil::WorldToScreen ( const Vector2 worldCoordinates)
static

Convert world (GL unit) coordinates to screen (pixel) coordinates. This function is not terribly efficient, so be careful calling it too frequently.

Parameters
worldCoordinatesThe world coordinates
Returns
The screen space coordinates

Definition at line 237 of file MathUtil.cpp.

Vector2 MathUtil::GetWorldDimensions ( )
static

Find out the dimensions of the area currently displayed in the window.

Returns
The dimensions of the viewport in GL units

Definition at line 258 of file MathUtil.cpp.

float MathUtil::PixelsToWorldUnits ( float  pixels)
static

Take a number of pixels and find out how many GL units they cover

Parameters
pixelsThe number of pixels
Returns
The number of GL units

Definition at line 280 of file MathUtil.cpp.

float MathUtil::WorldUnitsToPixels ( float  worldUnits)
static

Take a number of GL units and find out how many pixels stretch across that distance.

Parameters
worldUnitsThe number of GL units
Returns
The number of pixels

Definition at line 287 of file MathUtil.cpp.

MathUtil::AABBSplittingAxis MathUtil::GetMajorAxis ( const BoundingBox source)
static

Used internally by the SpatialGraph when it generates.

Parameters
sourceThe bounding box to split
Returns
The appropriate axis on which to split it

Definition at line 294 of file MathUtil.cpp.

void MathUtil::SplitBoundingBox ( const BoundingBox source,
AABBSplittingAxis  axis,
BoundingBox LHS,
BoundingBox RHS 
)
static

Used internally by the SpatialGraph when it generates

Parameters
sourceThe bounding box to split
axisThe axis on which to split it
LHSAn out parameter that will be set to the left-hand-side bounding box resulting from the split
RHSAn out parameter that will be set to the right-hand-side bounding box resulting from the split

Definition at line 309 of file MathUtil.cpp.

float MathUtil::DeltaAngle ( float  A1,
float  A2 
)
static

Calculates the difference in two angles (in radians) and remaps it to a range from -Pi to Pi.

Parameters
A1The first angle (in radians)
A2The second angle (in radians)
Returns
The difference, mapped appropriately

Definition at line 327 of file MathUtil.cpp.

float MathUtil::VectorDeltaAngle ( const Vector2 v1,
const Vector2 v2 
)
static

Calculate the difference in angles between two vectors.

Parameters
v1The first vector
v2The second vector
Returns
The difference in their angles

Definition at line 349 of file MathUtil.cpp.

Member Data Documentation

const float MathUtil::E = 2.718282f
static

Euler's number: http://en.wikipedia.org/wiki/E_(mathematical_constant)

Definition at line 51 of file MathUtil.h.

const float MathUtil::Log10E = 0.4342945f
static

Base-10 logarithm of Euler's number

Definition at line 56 of file MathUtil.h.

const float MathUtil::Log2E = 1.442695f
static

Base-2 logarithm of Euler's number

Definition at line 61 of file MathUtil.h.

const float MathUtil::Pi = 3.141593f
static

Pi: http://en.wikipedia.org/wiki/Pi

Definition at line 66 of file MathUtil.h.

const float MathUtil::PiOver2 = 1.570796f
static

Pi divided by 2

Definition at line 71 of file MathUtil.h.

const float MathUtil::PiOver4 = 0.7853982f
static

Pi divided by 4

Definition at line 76 of file MathUtil.h.

const float MathUtil::TwoPi = 6.283185f
static

2 times Pi (Tau)

Definition at line 81 of file MathUtil.h.

const float MathUtil::MaxFloat = 3.402823E+38f
static

The maximum value that can be represented by a 32-bit floating point number

Definition at line 87 of file MathUtil.h.

const float MathUtil::MinFloat = -3.402823E+38f
static

The minimum value that can be represented by a 32-bit floating point number

Definition at line 93 of file MathUtil.h.

const float MathUtil::Epsilon = 0.000001f
static

A very tiny number, useful for floating point comparisons (10e-6)

Definition at line 98 of file MathUtil.h.


The documentation for this class was generated from the following files: