Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #ifndef HVH_WC_MATH_VMATHINCLUDES_H
- #define HVH_WC_MATH_VMATHINCLUDES_H
- #include <stdint.h>
- #include <limits.h>
- #include <math.h>
- #include <float.h>
- constexpr float PI = 3.14159265359f;
- constexpr float QUARTER_PI = PI / 4;
- constexpr float HALF_PI = PI / 2;
- constexpr float PI2 = PI * 2;
- constexpr float PI4 = PI * 4;
- constexpr float TAU = 6.28318530718f;
- constexpr float QUARTER_TAU = TAU / 4;
- constexpr float HALF_TAU = TAU / 2;
- constexpr float TAU2 = TAU * 2;
- constexpr float TAU4 = TAU * 4;
- constexpr float to_radians(float degrees)
- {
- return degrees * (TAU / 360.0f);
- }
- constexpr float to_degrees(float radians)
- {
- return radians * (360.0f / TAU);
- }
- inline float springf(float current, float& velocity, float target, float delta_time, float tightness)
- {
- float current_to_target = target - current;
- float spring_force = current_to_target * tightness;
- float damping_force = -velocity * 2 * sqrtf(tightness);
- float force = spring_force + damping_force;
- velocity += force * delta_time;
- float displacement = velocity * delta_time;
- return current + displacement;
- }
- inline float lerp(float from, float to, float amount)
- {
- return (from * (1.0f - amount)) + (to * amount);
- }
- inline float clampf(float value, float minimum, float maximum)
- {
- return fminf(maximum, fmaxf(minimum, value));
- }
- inline bool nearly_equals(float a, float b, float epsilon = 0.000001f)
- {
- // http://floating-point-gui.de/errors/comparison/
- float absA = fabsf(a);
- float absB = fabsf(b);
- float diff = fabsf(a - b);
- if (a == b) // shortcut, handles infinities
- return true;
- else if (a == 0 || b == 0 || diff < FLT_EPSILON)
- return diff < epsilon;
- else
- return (diff / fminf(absA + absB, FLT_MAX)) < epsilon;
- }
- #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement