diff --git a/Vector3.cpp b/Vector3.cpp
index 5ba27ac..c08c1c0 100644
--- a/Vector3.cpp
+++ b/Vector3.cpp
@@ -38,6 +38,13 @@ const Vector3 Vector3::down = Vector3(0, -1, 0);
const Vector3 Vector3::forward = Vector3(0, 0, 1);
const Vector3 Vector3::back = Vector3(0, 0, -1);
+// inline float Vector3::Forward() { return z; }
+// inline float Vector3::Up() { return y; }
+// inline float Vector3::Right() { return x; }
+Vector3 Vector3::FromHorizontal(const Vector2 &v) {
+ return Vector3(v.x, 0, v.y);
+}
+
float Vector3::Magnitude(const Vector3 &a) {
return sqrtf(a.x * a.x + a.y * a.y + a.z * a.z);
}
@@ -48,7 +55,7 @@ float Vector3::SqrMagnitude(const Vector3 &a) {
}
float Vector3::sqrMagnitude() const { return (x * x + y * y + z * z); }
-Vector3 Vector3::Normalize(Vector3 v) {
+Vector3 Vector3::Normalize(const Vector3 &v) {
float num = Vector3::Magnitude(v);
Vector3 result = Vector3::zero;
if (num > epsilon) {
@@ -83,7 +90,7 @@ Vector3 Vector3::operator*(float f) const {
return Vector3(this->x * f, this->y * f, this->z * f);
}
-Vector3 Vector3::operator/(const float &d) {
+Vector3 Vector3::operator/(float d) const {
return Vector3(this->x / d, this->y / d, this->z / d);
}
@@ -104,7 +111,7 @@ Vector3 Vector3::Cross(const Vector3 &v1, const Vector3 &v2) {
v1.x * v2.y - v1.y * v2.x);
}
-Vector3 Vector3::Project(Vector3 vector, Vector3 onNormal) {
+Vector3 Vector3::Project(const Vector3 &vector, const Vector3 &onNormal) {
float sqrMagnitude = Dot(onNormal, onNormal);
if (sqrMagnitude < epsilon)
return Vector3::zero;
@@ -115,12 +122,13 @@ Vector3 Vector3::Project(Vector3 vector, Vector3 onNormal) {
}
}
-Vector3 Vector3::ProjectOnPlane(Vector3 vector, Vector3 planeNormal) {
+Vector3 Vector3::ProjectOnPlane(const Vector3 &vector,
+ const Vector3 &planeNormal) {
Vector3 r = vector - Project(vector, planeNormal);
return r;
}
-Vector2 Vector3::ProjectHorizontalPlane(Vector3 vector) {
+Vector2 Vector3::ProjectHorizontalPlane(const Vector3 &vector) {
Vector2 r = Vector2(vector.x, vector.z);
return r;
}
@@ -131,7 +139,7 @@ float clamp(float x, float lower, float upper) {
return upperClamp;
}
-float Vector3::Angle(Vector3 from, Vector3 to) {
+float Vector3::Angle(const Vector3 &from, const Vector3 &to) {
float denominator = sqrtf(from.sqrMagnitude() * to.sqrMagnitude());
if (denominator < epsilon)
return 0;
@@ -146,7 +154,8 @@ float Vector3::Angle(Vector3 from, Vector3 to) {
return r;
}
-float Vector3::SignedAngle(Vector3 from, Vector3 to, Vector3 axis) {
+float Vector3::SignedAngle(const Vector3 &from, const Vector3 &to,
+ const Vector3 &axis) {
// angle in [0,180]
float angle = Vector3::Angle(from, to);
@@ -160,7 +169,7 @@ float Vector3::SignedAngle(Vector3 from, Vector3 to, Vector3 axis) {
return signed_angle;
}
-Vector3 Vector3::Lerp(Vector3 from, Vector3 to, float f) {
+Vector3 Vector3::Lerp(const Vector3 &from, const Vector3 &to, float f) {
Vector3 v = from + (to - from) * f;
return v;
}
\ No newline at end of file
diff --git a/Vector3.h b/Vector3.h
index 8d50cd0..50fc1f3 100644
--- a/Vector3.h
+++ b/Vector3.h
@@ -63,30 +63,36 @@ public:
///
const static Vector3 one;
///
- /// A vector with values (1, 0, 0)
+ /// A normalized vector pointing in the right direction
///
const static Vector3 right;
///
- /// A vector3 with values (-1, 0, 0)
+ /// A normalized vector pointing in the left direction
///
const static Vector3 left;
///
- /// A vector with values (0, 1, 0)
+ /// A normalized vector pointing in the upward direction
///
const static Vector3 up;
///
- /// A vector with values (0, -1, 0)
+ /// A normalized vector pointing in the downward direcion
///
const static Vector3 down;
///
- /// A vector with values (0, 0, 1)
+ /// A normalized vector pointing in the forward direction
///
const static Vector3 forward;
///
- /// A vector with values (0, 0, -1)
+ /// A normalized vector pointing in the backward direction
///
const static Vector3 back;
+ // Experimental Access functions which are intended to replace the use of XYZ
+ inline float Forward() { return z; };
+ inline float Up() { return y; };
+ inline float Right() { return x; };
+ static Vector3 FromHorizontal(const Vector2 &vector);
+
///
/// The length of a vector
///
@@ -119,7 +125,7 @@ public:
///
/// The vector to convert
/// The vector with length 1
- static Vector3 Normalize(Vector3 vector);
+ static Vector3 Normalize(const Vector3 &vector);
///
/// Convert the vector to a length of a
///
@@ -161,14 +167,14 @@ public:
/// The scaling factor
/// The scaled vector
/// Each component of the vector will be multipled with the same factor.
- Vector3 operator*(float factor) const;
+ Vector3 operator*(const float factor) const;
///
/// Scale a vector uniformy down
///
/// The scaling factor
/// The scaled vector
/// Each componet of the vector will be divided by the same factor.
- Vector3 operator/(const float &factor);
+ Vector3 operator/(const float factor) const;
///
/// The dot product of two vectors
@@ -209,7 +215,7 @@ public:
/// The vector to project
/// The normal vector to project on
/// The projected vector
- static Vector3 Project(Vector3 vector, Vector3 onNormal);
+ static Vector3 Project(const Vector3 &vector, const Vector3 &onNormal);
///
/// Projects a vector onto a plane defined by a normal orthogonal to the
/// plane.
@@ -217,7 +223,8 @@ public:
/// The vector to project
/// The normal of the plane to project on
///
- static Vector3 ProjectOnPlane(Vector3 vector, Vector3 planeNormal);
+ static Vector3 ProjectOnPlane(const Vector3 &vector,
+ const Vector3 &planeNormal);
///
/// Projects a vector onto the horizontal plane.
@@ -225,7 +232,7 @@ public:
/// The vector to project
/// A 2D carthesian vector with the coordinates in the horizontal
/// plane.
- static Vector2 ProjectHorizontalPlane(Vector3 vector);
+ static Vector2 ProjectHorizontalPlane(const Vector3 &vector);
///
/// Calculate the angle between two vectors
@@ -236,7 +243,7 @@ public:
/// This reterns an unsigned angle which is the shortest distance
/// between the two vectors. Use Vector3::SignedAngle if a
/// signed angle is needed.
- static float Angle(Vector3 vector1, Vector3 vector2);
+ static float Angle(const Vector3 &vector1, const Vector3 &vector2);
///
/// Calculate the angle between two vectors rotation around an axis.
@@ -245,7 +252,8 @@ public:
/// The ending vector
/// The axis to rotate around
/// The signed angle
- static float SignedAngle(Vector3 from, Vector3 to, Vector3 axis);
+ static float SignedAngle(const Vector3 &from, const Vector3 &to,
+ const Vector3 &axis);
///
/// Lerp between two vectors
@@ -257,7 +265,7 @@ public:
/// 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 Vector3 Lerp(Vector3 from, Vector3 to, float f);
+ static Vector3 Lerp(const Vector3 &from, const Vector3 &to, float f);
};
#endif
\ No newline at end of file