Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- struct Vector3;
- typedef class MVector3
- {
- public:
- float x, y, z;
- MVector3();
- MVector3(float x, float y, float z);
- MVector3 Normalized();
- static MVector3 Zero()
- {
- return MVector3(0.0f, 0.0f, 0.0f);
- }
- MVector3 WorldUp()
- {
- return MVector3(0.0f, 0.0f, 1.0f);
- }
- static MVector3 WorldDown()
- {
- return MVector3(0.0f, 0.0f, -1.0f);
- }
- static MVector3 WorldNorth()
- {
- return MVector3(0.0f, 1.0f, 0.0f);
- }
- static MVector3 WorldSouth()
- {
- return MVector3(0.0f, -1.0f, 0.0f);
- }
- static MVector3 WorldEast()
- {
- return MVector3(1.0f, 0.0f, 0.0f);
- }
- static MVector3 WorldWest()
- {
- return MVector3(-1.0f, 0.0f, 0.0f);
- }
- static MVector3 RelativeRight()
- {
- return MVector3(1.0f, 0.0f, 0.0f);
- }
- static MVector3 RelativeLeft()
- {
- return MVector3(-1.0f, 0.0f, 0.0f);
- }
- static MVector3 RelativeFront()
- {
- return MVector3(0.0f, 1.0f, 0.0f);
- }
- static MVector3 RelativeBack()
- {
- return MVector3(0.0f, -1.0f, 0.0f);
- }
- static MVector3 RelativeTop()
- {
- return MVector3(0.0f, 0.0f, 1.0f);
- }
- static MVector3 RelativeBottom()
- {
- return MVector3(0.0f, 0.0f, -1.0f);
- }
- float Length();
- float LengthSquared();
- void Normalize();
- float DistanceTo(MVector3 position);
- float DistanceToSquared(MVector3 position);
- float DistanceTo2D(MVector3 position);
- float DistanceToSquared2D(MVector3 position);
- static float Distance(MVector3 position1, MVector3 position2);
- static float DistanceSquared(MVector3 position1, MVector3 position2);
- static float Distance2D(MVector3 position1, MVector3 position2);
- static float DistanceSquared2D(MVector3 position1, MVector3 position2);
- static float Angle(MVector3 from, MVector3 to);
- static float SignedAngle(MVector3 from, MVector3 to, MVector3 planeNormal);
- float ToHeading();
- MVector3 Around(float distance);
- static MVector3 RandomXY();
- static MVector3 RandomXYZ();
- static MVector3 Add(MVector3 left, MVector3 right);
- static MVector3 Subtract(MVector3 left, MVector3 right);
- static MVector3 Modulate(MVector3 left, MVector3 right);
- static MVector3 Multiply(MVector3 value, float scale);
- static MVector3 Divide(MVector3 value, float scale);
- static MVector3 Negate(MVector3 value);
- static MVector3 Clamp(MVector3 value, MVector3 min, MVector3 max);
- static MVector3 Lerp(MVector3 start, MVector3 end, float factor);
- static float Dot(MVector3 left, MVector3 right);
- static MVector3 Cross(MVector3 left, MVector3 right);
- static MVector3 Project(MVector3 vector, MVector3 onNormal);
- static MVector3 ProjectOnPlane(MVector3 vector, MVector3 planeNormal);
- static MVector3 Reflect(MVector3 vector, MVector3 normal);
- static MVector3 Normalize(MVector3 vector);
- static MVector3 Minimize(MVector3 left, MVector3 right);
- static MVector3 Maximize(MVector3 left, MVector3 right);
- //operator Vector3() const;
- //MVector3& MVector3::operator+=(const MVector3& rhs);
- friend MVector3 operator + (MVector3 left, MVector3 right);
- friend MVector3 operator += (MVector3 left, MVector3 right);
- friend MVector3 operator - (MVector3 left, MVector3 right);
- friend MVector3 operator -= (MVector3 left, MVector3 right);
- friend MVector3 operator - (MVector3 value);
- friend MVector3 operator * (MVector3 value, float scale);
- friend MVector3 operator * (float scale, MVector3 vec);
- friend MVector3 operator / (MVector3 value, float scale);
- friend bool operator == (MVector3 left, MVector3 right);
- friend bool operator != (MVector3 left, MVector3 right);
- virtual char *ToString();
- virtual bool Equals(MVector3 *value);
- virtual bool Equals(MVector3 value);
- static bool Equals(MVector3 &value1, MVector3 &value2);
- //friend const MVector3& operator+(const MVector3& a, const MVector3& b);
- };
- MVector3::MVector3()
- {
- x = y = z = 0;
- }
- MVector3::MVector3(float x, float y, float z)
- {
- this->x = x; this->y = y; this->z = z;
- }
- MVector3 MVector3::Normalized()
- {
- return MVector3::Normalize(MVector3(x, y, z));
- }
- float MVector3::Length()
- {
- return static_cast<float>(sqrt((x * x) + (y* y) + (z* z)));
- }
- float MVector3::LengthSquared()
- {
- return (x * x) + (y * y) + (z * z);
- }
- void MVector3::Normalize()
- {
- float length = Length();
- if (length == 0) return;
- float num = 1 / length;
- x *= num;
- y *= num;
- z *= num;
- }
- float MVector3::DistanceTo(MVector3 position)
- {
- return (position - *this).Length();
- }
- float MVector3::DistanceToSquared(MVector3 position)
- {
- return DistanceSquared(position, *this);
- }
- float MVector3::DistanceTo2D(MVector3 position)
- {
- MVector3 lhs(x, y, 0.0f);
- MVector3 rhs(position.x, position.y, 0.0f);
- return Distance(lhs, rhs);
- }
- float MVector3::DistanceToSquared2D(MVector3 position)
- {
- MVector3 lhs(x, y, 0.0f);
- MVector3 rhs(position.x, position.y, 0.0f);
- return DistanceSquared(lhs, rhs);
- }
- float MVector3::Distance(MVector3 position1, MVector3 position2)
- {
- return (position1 - position2).Length();
- }
- float MVector3::DistanceSquared(MVector3 position1, MVector3 position2)
- {
- return (position1 - position2).LengthSquared();
- }
- float MVector3::Distance2D(MVector3 position1, MVector3 position2)
- {
- MVector3 pos1 = MVector3(position1.x, position1.y, 0);
- MVector3 pos2 = MVector3(position2.x, position2.y, 0);
- return (pos1 - pos2).Length();
- }
- float MVector3::DistanceSquared2D(MVector3 position1, MVector3 position2)
- {
- MVector3 pos1 = MVector3(position1.x, position1.y, 0);
- MVector3 pos2 = MVector3(position2.x, position2.y, 0);
- return (pos1 - pos2).LengthSquared();
- }
- float MVector3::Angle(MVector3 from, MVector3 to)
- {
- double dot = MVector3::Dot(from.Normalized(), to.Normalized());
- return (float)(acosf((dot)) * (180.0 / M_PI));
- }
- float MVector3::SignedAngle(MVector3 from, MVector3 to, MVector3 planeNormal)
- {
- MVector3 perpVector = MVector3::Cross(planeNormal, from);
- double angle = MVector3::Angle(from, to);
- double dot = MVector3::Dot(perpVector, to);
- if (dot < 0)
- {
- angle *= -1;
- }
- return (float)angle;
- }
- float MVector3::ToHeading()
- {
- return (float)((atan2f(x, -y) + M_PI) * (180.0 / M_PI));
- }
- MVector3 MVector3::Around(float distance)
- {
- //return *this + MVector3::RandomXY() * distance;
- }
- MVector3 MVector3::RandomXY()
- {
- MVector3 v;
- double radian = _rand() * 2 * M_PI;
- v.x = (float)(cosf(radian));
- v.y = (float)(sinf(radian));
- v.Normalize();
- return v;
- }
- MVector3 MVector3::RandomXYZ()
- {
- MVector3 v;
- double radian = _rand() * 2.0 * M_PI;
- double cosTheta = (_rand() * 2.0) - 1.0;
- double theta = acosf(cosTheta);
- v.x = (float)(sinf(theta) * cosf(radian));
- v.y = (float)(sinf(theta) * sinf(radian));
- v.z = (float)(cosf(theta));
- v.Normalize();
- return v;
- }
- MVector3 MVector3::Add(MVector3 left, MVector3 right)
- {
- return MVector3(left.x + right.x, left.y + right.y, left.z + right.z);
- }
- MVector3 MVector3::Subtract(MVector3 left, MVector3 right)
- {
- return MVector3(left.x - right.x, left.y - right.y, left.z - right.z);
- }
- MVector3 MVector3::Modulate(MVector3 left, MVector3 right)
- {
- return MVector3(left.x * right.x, left.y * right.y, left.z * right.z);
- }
- MVector3 MVector3::Multiply(MVector3 value, float scale)
- {
- return MVector3(value.x * scale, value.y * scale, value.z * scale);
- }
- MVector3 MVector3::Divide(MVector3 value, float scale)
- {
- return MVector3(value.x / scale, value.y / scale, value.z / scale);
- }
- MVector3 MVector3::Negate(MVector3 value)
- {
- return MVector3(-value.x, -value.y, -value.z);
- }
- MVector3 MVector3::Clamp(MVector3 value, MVector3 min, MVector3 max)
- {
- float xx = value.x;
- xx = (xx > max.x) ? max.x : xx;
- xx = (xx < min.x) ? min.x : xx;
- float yy = value.y;
- yy = (yy > max.y) ? max.y : yy;
- yy = (yy < min.y) ? min.y : yy;
- float zz = value.z;
- zz = (zz > max.z) ? max.z : zz;
- zz = (zz < min.z) ? min.z : zz;
- return MVector3(xx, yy, zz);
- }
- MVector3 MVector3::Lerp(MVector3 start, MVector3 end, float factor)
- {
- MVector3 vector;
- vector.x = start.x + ((end.x - start.x) * factor);
- vector.y = start.y + ((end.y - start.y) * factor);
- vector.z = start.z + ((end.z - start.z) * factor);
- return vector;
- }
- float MVector3::Dot(MVector3 left, MVector3 right)
- {
- return (left.x * right.x + left.y * right.y + left.z * right.z);
- }
- MVector3 MVector3::Cross(MVector3 left, MVector3 right)
- {
- MVector3 result;
- result.x = left.y * right.z - left.z * right.y;
- result.y = left.z * right.x - left.x * right.z;
- result.z = left.x * right.y - left.y * right.x;
- return result;
- }
- MVector3 MVector3::Project(MVector3 vector, MVector3 onNormal)
- {
- return onNormal * Dot(vector, onNormal) / Dot(onNormal, onNormal);
- }
- MVector3 MVector3::ProjectOnPlane(MVector3 vector, MVector3 planeNormal)
- {
- return (vector - Project(vector, planeNormal));
- }
- MVector3 MVector3::Reflect(MVector3 vector, MVector3 normal)
- {
- MVector3 result;
- float dot = ((vector.x * normal.x) + (vector.y * normal.y)) + (vector.x * normal.x);
- result.x = vector.x - ((2.0f * dot) * normal.x);
- result.y = vector.y - ((2.0f * dot) * normal.y);
- result.z = vector.z - ((2.0f * dot) * normal.z);
- return result;
- }
- MVector3 MVector3::Normalize(MVector3 vector)
- {
- vector.Normalize();
- return vector;
- }
- MVector3 MVector3::Minimize(MVector3 left, MVector3 right)
- {
- MVector3 vector;
- vector.x = (left.x < right.x) ? left.x : right.x;
- vector.y = (left.y < right.y) ? left.y : right.y;
- vector.z = (left.z < right.z) ? left.z : right.z;
- return vector;
- }
- MVector3 MVector3::Maximize(MVector3 left, MVector3 right)
- {
- MVector3 vector;
- vector.x = (left.x > right.x) ? left.x : right.x;
- vector.y = (left.y > right.y) ? left.y : right.y;
- vector.z = (left.z > right.z) ? left.z : right.z;
- return vector;
- }
- /*MVector3::operator Vector3() const
- {
- return (x, y, z);
- }
- inline MVector3 operator+(MVector3 lhs, const MVector3& rhs)
- {
- lhs += rhs;
- return lhs;
- }
- MVector3& MVector3::operator+=(const MVector3& rhs)
- {
- x += rhs.x;
- y += rhs.y;
- z += rhs.z;
- return *this;
- }*/
- MVector3 operator + (MVector3 left, MVector3 right)
- {
- return MVector3(left.x + right.x, left.y + right.y, left.z + right.z);
- }
- MVector3 operator += (MVector3 left, MVector3 right)
- {
- return MVector3(left.x += right.x, left.y += right.y, left.z += right.z);
- }
- MVector3 operator - (MVector3 left, MVector3 right)
- {
- return MVector3(left.x - right.x, left.y - right.y, left.z - right.z);
- }
- MVector3 operator -= (MVector3 left, MVector3 right)
- {
- return MVector3(left.x -= right.x, left.y -= right.y, left.z -= right.z);
- }
- MVector3 operator - (MVector3 value)
- {
- return MVector3(-value.x, -value.y, -value.z);
- }
- MVector3 operator * (MVector3 value, float scale)
- {
- return MVector3(value.x * scale, value.y * scale, value.z * scale);
- }
- MVector3 operator * (float scale, MVector3 vec)
- {
- return vec * scale;
- }
- MVector3 operator / (MVector3 value, float scale)
- {
- return MVector3(value.x / scale, value.y / scale, value.z / scale);
- }
- bool operator == (MVector3 left, MVector3 right)
- {
- return MVector3::Equals(left, right);
- }
- bool operator != (MVector3 left, MVector3 right)
- {
- return !MVector3::Equals(left, right);
- }
- char *MVector3::ToString()
- {
- char data[55];
- _snprintf(data, sizeof(data), "X:%.5f Y:%.5f Z:%.5f", x, y, z);
- return data;
- }
- bool MVector3::Equals(MVector3 *value)
- {
- if (value == NULL)
- return false;
- return Equals(value);
- }
- bool MVector3::Equals(MVector3 value)
- {
- return (x == value.x && y == value.y && z == value.z);
- }
- bool MVector3::Equals(MVector3 &value1, MVector3 &value2)
- {
- return (value1.x == value2.x && value1.y == value2.y && value1.z == value2.z);
- }
- #pragma pack(push, 1)
- typedef struct Vector3
- {
- float x, y, z;
- operator MVector3() const
- {
- return MVector3(x, y, z);
- }
- Vector3(float, float, float);
- Vector3(float, int, float, int, float, int);
- Vector3(float, int);
- Vector3(float);
- Vector3();
- } Vector3;
- #pragma pack(pop)
- Vector3::Vector3(float X, float Y, float Z) :
- x(X),
- y(Y),
- z(Z)
- {}
- Vector3::Vector3(float X, int, float Y, int, float Z, int) :
- x(X),
- y(Y),
- z(Z)
- {}
- Vector3::Vector3(float v, int) :
- x(v),
- y(v),
- z(v)
- {}
- Vector3::Vector3(float v) :
- x(v),
- y(v),
- z(v)
- {}
- Vector3::Vector3() :
- x(0.f),
- y(0.f),
- z(0.f)
- {}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement