Added Vector2::Rotate, Moved Rad2Deg/Deg2Rad to Angle class
This commit is contained in:
parent
307a94c567
commit
c1a926582e
@ -7,6 +7,9 @@
|
||||
|
||||
class Angle {
|
||||
public:
|
||||
const static float Rad2Deg;
|
||||
const static float Deg2Rad;
|
||||
|
||||
static float Normalize(float angle);
|
||||
static float Clamp(float angle, float min, float max);
|
||||
static float Difference(float a, float b);
|
||||
|
@ -91,11 +91,10 @@ public:
|
||||
/// <summary>
|
||||
/// The squared length of a vector
|
||||
/// </summary>
|
||||
/// <param name="vector">The vector for which you need the squared length</param>
|
||||
/// <returns>The squatred length</returns>
|
||||
/// The squared length is computationally simpler than the real length.
|
||||
/// Think of Pythagoras A^2 + B^2 = C^2.
|
||||
/// This leaves out the calculation of the squared root of C.
|
||||
/// <param name="vector">The vector for which you need the squared
|
||||
/// length</param> <returns>The squatred length</returns> The squared length
|
||||
/// is computationally simpler than the real length. Think of Pythagoras A^2 +
|
||||
/// B^2 = C^2. This leaves out the calculation of the squared root of C.
|
||||
static float SqrMagnitude(const Vector2& vector);
|
||||
/// <summary>
|
||||
/// The squared length of this vector
|
||||
@ -191,7 +190,7 @@ public:
|
||||
/// </summary>
|
||||
/// <param name="vector1">The first vector</param>
|
||||
/// <param name="vector2">The second vector</param>
|
||||
/// <returns></returns>
|
||||
/// <returns>The angle</returns>
|
||||
/// This reterns an unsigned angle which is the shortest distance
|
||||
/// between the two vectors. Use Vector3::SignedAngle if a
|
||||
/// signed angle is needed.
|
||||
@ -206,6 +205,14 @@ public:
|
||||
/// <returns>The signed angle</returns>
|
||||
static float SignedAngle(Vector2 from, Vector2 to);
|
||||
|
||||
/// <summary>
|
||||
/// Rotate the vector
|
||||
/// </summary>
|
||||
/// <param name="v">The vector to rotate</param>
|
||||
/// <param name="angle">Angle in radias to rotate</param>
|
||||
/// <returns>The rotated vector</returns>
|
||||
static Vector2 Rotate(Vector2 v, float angle);
|
||||
|
||||
/// <summary>
|
||||
/// Lerp between two vectors
|
||||
/// </summary>
|
||||
@ -213,8 +220,9 @@ public:
|
||||
/// <param name="to">The to vector</param>
|
||||
/// <param name="f">The interpolation distance (0..1)</param>
|
||||
/// <returns>The lerped vector</returns>
|
||||
/// The factor f is unclamped. Value 0 matches the *from* vector, Value 1 matches the *to* vector
|
||||
/// Value -1 is *from* vector minus the difference between *from* and *to* etc.
|
||||
/// The factor f is unclamped. Value 0 matches the *from* vector, Value 1
|
||||
/// matches the *to* vector Value -1 is *from* vector minus the difference
|
||||
/// between *from* and *to* etc.
|
||||
static Vector2 Lerp(Vector2 from, Vector2 to, float f);
|
||||
|
||||
static float ToFactor(Vector2 a, Vector2 b);
|
||||
|
@ -2,16 +2,21 @@
|
||||
// License, v. 2.0.If a copy of the MPL was not distributed with this
|
||||
// file, You can obtain one at https ://mozilla.org/MPL/2.0/.
|
||||
|
||||
#include <math.h>
|
||||
#include "Angle.h"
|
||||
#include <math.h>
|
||||
#include "FloatSingle.h"
|
||||
|
||||
const float Angle::Rad2Deg = 57.29578F;
|
||||
const float Angle::Deg2Rad = 0.0174532924F;
|
||||
|
||||
float Angle::Normalize(float angle) {
|
||||
if (!isfinite(angle))
|
||||
return angle;
|
||||
|
||||
while (angle <= -180) angle += 360;
|
||||
while (angle > 180) angle -= 360;
|
||||
while (angle <= -180)
|
||||
angle += 360;
|
||||
while (angle > 180)
|
||||
angle -= 360;
|
||||
return angle;
|
||||
}
|
||||
|
||||
@ -30,5 +35,5 @@ float Angle::MoveTowards(float fromAngle, float toAngle, float maxAngle) {
|
||||
float d = toAngle - fromAngle;
|
||||
float sign = signbit(d) ? -1 : 1;
|
||||
d = sign * Float::Clamp(fabs(d), 0, maxAngle);
|
||||
return d;
|
||||
return fromAngle + d;
|
||||
}
|
||||
|
@ -2,12 +2,10 @@
|
||||
// License, v. 2.0.If a copy of the MPL was not distributed with this
|
||||
// file, You can obtain one at https ://mozilla.org/MPL/2.0/.
|
||||
|
||||
#include <math.h>
|
||||
#include "Vector2.h"
|
||||
|
||||
const float Deg2Rad = 0.0174532924F;
|
||||
const float Rad2Deg = 57.29578F;
|
||||
const float epsilon = 1E-05f;
|
||||
#include <math.h>
|
||||
#include "Angle.h"
|
||||
#include "FloatSingle.h"
|
||||
|
||||
Vector2::Vector2() {
|
||||
x = 0;
|
||||
@ -24,8 +22,7 @@ Vector2::Vector2(Vec2 v) {
|
||||
y = v.y;
|
||||
}
|
||||
|
||||
Vector2::~Vector2() {
|
||||
}
|
||||
Vector2::~Vector2() {}
|
||||
|
||||
const Vector2 Vector2::zero = Vector2(0, 0);
|
||||
const Vector2 Vector2::right = Vector2(1, 0);
|
||||
@ -52,7 +49,7 @@ float Vector2::sqrMagnitude() const {
|
||||
Vector2 Vector2::Normalize(Vector2 v) {
|
||||
float num = Vector2::Magnitude(v);
|
||||
Vector2 result = Vector2::zero;
|
||||
if (num > epsilon) {
|
||||
if (num > Float::epsilon) {
|
||||
result = v / num;
|
||||
}
|
||||
return result;
|
||||
@ -60,7 +57,7 @@ Vector2 Vector2::Normalize(Vector2 v) {
|
||||
Vector2 Vector2::normalized() const {
|
||||
float num = this->magnitude();
|
||||
Vector2 result = Vector2::zero;
|
||||
if (num > epsilon) {
|
||||
if (num > Float::epsilon) {
|
||||
result = ((Vector2) * this) / num;
|
||||
}
|
||||
return result;
|
||||
@ -117,7 +114,18 @@ float Vector2::SignedAngle(Vector2 from, Vector2 to) {
|
||||
|
||||
float angleFrom = atan2(from.y, from.x);
|
||||
float angleTo = atan2(to.y, to.x);
|
||||
return (angleTo - angleFrom) * Rad2Deg;
|
||||
return (angleTo - angleFrom) * Angle::Rad2Deg;
|
||||
}
|
||||
|
||||
Vector2 Rotate(Vector2 v, float angle) {
|
||||
float sin = (float)sinf(angle * Angle::Deg2Rad);
|
||||
float cos = (float)cosf(angle * Angle::Deg2Rad);
|
||||
|
||||
float tx = v.x;
|
||||
float ty = v.y;
|
||||
v.x = (cos * tx) - (sin * ty);
|
||||
v.y = (sin * tx) + (cos * ty);
|
||||
return v;
|
||||
}
|
||||
|
||||
Vector2 Vector2::Lerp(Vector2 from, Vector2 to, float f) {
|
||||
@ -128,3 +136,4 @@ Vector2 Vector2::Lerp(Vector2 from, Vector2 to, float f) {
|
||||
float Vector2::ToFactor(Vector2 a, Vector2 b) {
|
||||
return (1 - Vector2::Dot(a, b)) / 2;
|
||||
}
|
||||
|
||||
|
@ -420,7 +420,10 @@ TEST(Vector2, SignedAngle) {
|
||||
TEST(Vector2, DISABLED_Lerp) {
|
||||
}
|
||||
|
||||
TEST(Vector2, DIABLED_ToFactor) {
|
||||
TEST(Vector2, DISABLED_ToFactor) {
|
||||
}
|
||||
|
||||
TEST(Vector2, DISABLED_Rotate) {
|
||||
}
|
||||
|
||||
#endif
|
Loading…
x
Reference in New Issue
Block a user