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