![]() |
Angel
A 2D Game Prototyping Engine
|
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 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 |
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.
| static T MathUtil::Abs | ( | T | val | ) | [inline, static] |
A templated absolute value function that can handle any class where a comparison against 0 makes sense (floats and ints, mostly).
| val | The number |
Definition at line 108 of file MathUtil.h.
| static T MathUtil::Max | ( | T | value1, |
| T | value2 | ||
| ) | [inline, static] |
A templated max function that returns the greater of two values. Works for any class that implements a > operator.
| value1 | The first value |
| value2 | The second value |
Definition at line 122 of file MathUtil.h.
| static T MathUtil::Min | ( | T | value1, |
| T | value2 | ||
| ) | [inline, static] |
A templated min function that returns the lesser of two values. Works for any class that implements a < operator.
| value1 | The first value |
| value2 | The second value |
Definition at line 136 of file MathUtil.h.
| static T MathUtil::Distance | ( | T | value1, |
| T | value2 | ||
| ) | [inline, static] |
Find the distance between two values. Works for any class that has a subtraction operator and will work with the Abs function.
| value1 | The first value |
| value2 | The second value |
Definition at line 150 of file MathUtil.h.
| static T MathUtil::Lerp | ( | T | value1, |
| T | value2, | ||
| float | amount | ||
| ) | [inline, static] |
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)
| value1 | The starting value |
| value2 | The ending value |
| amount | The amount to lerp (from 0.0 to 1.0) |
Definition at line 168 of file MathUtil.h.
| static T MathUtil::SmoothStep | ( | T | value1, |
| T | value2, | ||
| float | amount | ||
| ) | [inline, static] |
Smoothly step between two values. Works for any classes that Lerp would work for (and is essentially a drop-in replacement).
http://www.fundza.com/rman_shaders/smoothstep/index.html
| value1 | The starting value |
| value2 | The ending value |
| amount | The amount to interpolate (from 0.0 to 1.0) |
Definition at line 185 of file MathUtil.h.
| static int MathUtil::Clamp | ( | int | value, |
| int | min, | ||
| int | max | ||
| ) | [inline, static] |
Clamps an integer to a specified range
| value | The integer in question |
| min | The minimum of the range |
| max | The maximum of the range |
Definition at line 199 of file MathUtil.h.
| static float MathUtil::Clamp | ( | float | value, |
| float | min, | ||
| float | max | ||
| ) | [inline, static] |
Clamps a float to a specified range
| value | The float in question |
| min | The minimum of the range |
| max | The maximum of the range |
Definition at line 212 of file MathUtil.h.
| static double MathUtil::Clamp | ( | double | value, |
| double | min, | ||
| double | max | ||
| ) | [inline, static] |
Clamps a double-precision float to a specified range
| value | The double in question |
| min | The minimum of the range |
| max | The maximum of the range |
Definition at line 225 of file MathUtil.h.
| float MathUtil::ToDegrees | ( | float | radians | ) | [static] |
Convert radians to degrees
| radians | The angle in radians |
Definition at line 51 of file MathUtil.cpp.
| float MathUtil::ToRadians | ( | float | degrees | ) | [static] |
Convert degrees to radians
| degrees | The angle in degrees |
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.
| angle_in_degrees | The angle, in degrees |
Definition at line 61 of file MathUtil.cpp.
| float MathUtil::AngleFromVector | ( | const Vector2 & | v1 | ) | [static] |
Get an angle from a vector indicating direction
| v1 | The vector direction |
Definition at line 66 of file MathUtil.cpp.
Get the angle between two vectors
| v1 | The first vector |
| v2 | The second vector |
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.
| x | The double or float to round |
Definition at line 80 of file MathUtil.cpp.
| int MathUtil::RandomInt | ( | int | maximum | ) | [static] |
Get a random non-negative integer.
| maximum | The maximum value you want to see |
Definition at line 85 of file MathUtil.cpp.
| int MathUtil::RandomIntInRange | ( | int | min, |
| int | max | ||
| ) | [static] |
Get a random integer in a specified range.
| min | The minimum value you want to see |
| max | The maximum value you want to see |
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.
| target | The target integer |
| error | The maximum amount the returned value can differ from the target (in either direction) |
Definition at line 105 of file MathUtil.cpp.
| float MathUtil::RandomFloat | ( | float | maximum = 1.0f | ) | [static] |
Get a random non-negative float.
| maximum | The maximum value you want to see |
Definition at line 110 of file MathUtil.cpp.
| float MathUtil::RandomFloatInRange | ( | float | min, |
| float | max | ||
| ) | [static] |
Get a random float in a specified range.
| min | The minimum value you want to see |
| max | The maximum value you want to see |
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.
| target | The target float |
| error | The maximum amount the returned value can differ from the target (in either direction) |
Definition at line 123 of file MathUtil.cpp.
| bool MathUtil::RandomBool | ( | ) | [static] |
Get a random bool.
Definition at line 128 of file MathUtil.cpp.
| Vector2 MathUtil::RandomVector | ( | ) | [static] |
Get a random unit-length Vector2
Definition at line 133 of file MathUtil.cpp.
Get a random Vector2 with specified maximum values
| maxValues | The highest values for both axes |
Definition at line 138 of file MathUtil.cpp.
Get a random Vector2 within a specified range
| minValues | The lowest values for both axes |
| maxValues | The highest values for both axes |
Definition at line 143 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 )
| value1 | The first value |
| value2 | The second value |
| epsilon | The maximum allowable difference (defaults to MathUtil::Epsilon) |
Definition at line 148 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.
| v1 | The first vector |
| v2 | The second vector |
| epsilon | The maximum allowable difference between them |
Definition at line 159 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.
| x | The pixel X coordinate |
| y | The pixel Y coordinate |
Definition at line 173 of file MathUtil.cpp.
Convert screen (pixel) coordinates to world (GL unit) coordinates. This function is not terribly efficient, so be careful calling it too frequently.
| screenCoordinates | The pixel coordinates |
Definition at line 168 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.
| x | The world X coordinate |
| y | The world Y coordinate |
Definition at line 208 of file MathUtil.cpp.
Convert world (GL unit) coordinates to screen (pixel) coordinates. This function is not terribly efficient, so be careful calling it too frequently.
| worldCoordinates | The world coordinates |
Definition at line 192 of file MathUtil.cpp.
| Vector2 MathUtil::GetWorldDimensions | ( | ) | [static] |
Find out the dimensions of the area currently displayed in the window.
Definition at line 213 of file MathUtil.cpp.
| float MathUtil::PixelsToWorldUnits | ( | float | pixels | ) | [static] |
Take a number of pixels and find out how many GL units they cover
| pixels | The number of pixels |
Definition at line 240 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.
| worldUnits | The number of GL units |
Definition at line 247 of file MathUtil.cpp.
| MathUtil::AABBSplittingAxis MathUtil::GetMajorAxis | ( | const BoundingBox & | source | ) | [static] |
Used internally by the SpatialGraph when it generates.
| source | The bounding box to split |
Definition at line 254 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
| source | The bounding box to split |
| axis | The axis on which to split it |
| LHS | An out parameter that will be set to the left-hand-side bounding box resulting from the split |
| RHS | An out parameter that will be set to the right-hand-side bounding box resulting from the split |
Definition at line 269 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.
| A1 | The first angle (in radians) |
| A2 | The second angle (in radians) |
Definition at line 287 of file MathUtil.cpp.
Calculate the difference in angles between two vectors.
| v1 | The first vector |
| v2 | The second vector |
Definition at line 309 of file MathUtil.cpp.
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.


1.7.5.1