Advertisement
TheRouletteBoi

Vector3 Class (Partly broken)

Oct 15th, 2017
460
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 12.04 KB | None | 0 0
  1. struct Vector3;
  2. typedef class MVector3
  3. {
  4. public:
  5.     float x, y, z;
  6.  
  7.     MVector3();
  8.     MVector3(float x, float y, float z);
  9.  
  10.     MVector3 Normalized();
  11.  
  12.     static MVector3 Zero()
  13.     {
  14.         return MVector3(0.0f, 0.0f, 0.0f);
  15.     }
  16.     MVector3 WorldUp()
  17.     {
  18.         return MVector3(0.0f, 0.0f, 1.0f);
  19.     }
  20.     static MVector3 WorldDown()
  21.     {
  22.         return MVector3(0.0f, 0.0f, -1.0f);
  23.     }
  24.     static MVector3 WorldNorth()
  25.     {
  26.         return MVector3(0.0f, 1.0f, 0.0f);
  27.     }
  28.     static MVector3 WorldSouth()
  29.     {
  30.         return MVector3(0.0f, -1.0f, 0.0f);
  31.     }
  32.     static MVector3 WorldEast()
  33.     {
  34.         return MVector3(1.0f, 0.0f, 0.0f);
  35.     }
  36.     static MVector3 WorldWest()
  37.     {
  38.         return MVector3(-1.0f, 0.0f, 0.0f);
  39.     }
  40.     static MVector3 RelativeRight()
  41.     {
  42.         return MVector3(1.0f, 0.0f, 0.0f);
  43.     }
  44.     static MVector3 RelativeLeft()
  45.     {
  46.         return MVector3(-1.0f, 0.0f, 0.0f);
  47.     }
  48.     static MVector3 RelativeFront()
  49.     {
  50.         return MVector3(0.0f, 1.0f, 0.0f);
  51.     }
  52.     static MVector3 RelativeBack()
  53.     {
  54.         return MVector3(0.0f, -1.0f, 0.0f);
  55.     }
  56.     static MVector3 RelativeTop()
  57.     {
  58.         return MVector3(0.0f, 0.0f, 1.0f);
  59.     }
  60.     static MVector3 RelativeBottom()
  61.     {
  62.         return MVector3(0.0f, 0.0f, -1.0f);
  63.     }
  64.  
  65.  
  66.     float Length();
  67.     float LengthSquared();
  68.     void Normalize();
  69.  
  70.     float DistanceTo(MVector3 position);
  71.     float DistanceToSquared(MVector3 position);
  72.     float DistanceTo2D(MVector3 position);
  73.     float DistanceToSquared2D(MVector3 position);
  74.     static float Distance(MVector3 position1, MVector3 position2);
  75.     static float DistanceSquared(MVector3 position1, MVector3 position2);
  76.     static float Distance2D(MVector3 position1, MVector3 position2);
  77.     static float DistanceSquared2D(MVector3 position1, MVector3 position2);
  78.     static float Angle(MVector3 from, MVector3 to);
  79.     static float SignedAngle(MVector3 from, MVector3 to, MVector3 planeNormal);
  80.     float ToHeading();
  81.     MVector3 Around(float distance);
  82.     static MVector3 RandomXY();
  83.     static MVector3 RandomXYZ();
  84.     static MVector3 Add(MVector3 left, MVector3 right);
  85.     static MVector3 Subtract(MVector3 left, MVector3 right);
  86.     static MVector3 Modulate(MVector3 left, MVector3 right);
  87.     static MVector3 Multiply(MVector3 value, float scale);
  88.     static MVector3 Divide(MVector3 value, float scale);
  89.     static MVector3 Negate(MVector3 value);
  90.     static MVector3 Clamp(MVector3 value, MVector3 min, MVector3 max);
  91.     static MVector3 Lerp(MVector3 start, MVector3 end, float factor);
  92.     static float Dot(MVector3 left, MVector3 right);
  93.     static MVector3 Cross(MVector3 left, MVector3 right);
  94.     static MVector3 Project(MVector3 vector, MVector3 onNormal);
  95.     static MVector3 ProjectOnPlane(MVector3 vector, MVector3 planeNormal);
  96.     static MVector3 Reflect(MVector3 vector, MVector3 normal);
  97.     static MVector3 Normalize(MVector3 vector);
  98.     static MVector3 Minimize(MVector3 left, MVector3 right);
  99.     static MVector3 Maximize(MVector3 left, MVector3 right);
  100.  
  101.     //operator Vector3() const;
  102.     //MVector3&     MVector3::operator+=(const MVector3& rhs);
  103.  
  104.  
  105.     friend MVector3 operator + (MVector3 left, MVector3 right);
  106.     friend MVector3 operator += (MVector3 left, MVector3 right);
  107.     friend MVector3 operator - (MVector3 left, MVector3 right);
  108.     friend MVector3 operator -= (MVector3 left, MVector3 right);
  109.     friend MVector3 operator - (MVector3 value);
  110.     friend MVector3 operator * (MVector3 value, float scale);
  111.     friend MVector3 operator * (float scale, MVector3 vec);
  112.     friend MVector3 operator / (MVector3 value, float scale);
  113.     friend bool operator == (MVector3 left, MVector3 right);
  114.     friend bool operator != (MVector3 left, MVector3 right);
  115.  
  116.     virtual char *ToString();
  117.     virtual bool Equals(MVector3 *value);
  118.     virtual bool Equals(MVector3 value);
  119.     static bool Equals(MVector3 &value1, MVector3 &value2);
  120.  
  121.     //friend const MVector3& operator+(const MVector3& a, const MVector3& b);
  122. };
  123.  
  124. MVector3::MVector3()
  125. {
  126.     x = y = z = 0;
  127. }
  128. MVector3::MVector3(float x, float y, float z)
  129. {
  130.     this->x = x; this->y = y; this->z = z;
  131. }
  132. MVector3 MVector3::Normalized()
  133. {
  134.     return MVector3::Normalize(MVector3(x, y, z));
  135. }
  136. float MVector3::Length()
  137. {
  138.     return static_cast<float>(sqrt((x * x) + (y* y) + (z* z)));
  139. }
  140. float MVector3::LengthSquared()
  141. {
  142.     return (x * x) + (y * y) + (z * z);
  143. }
  144. void MVector3::Normalize()
  145. {
  146.     float length = Length();
  147.     if (length == 0) return;
  148.     float num = 1 / length;
  149.     x *= num;
  150.     y *= num;
  151.     z *= num;
  152. }
  153. float MVector3::DistanceTo(MVector3 position)
  154. {
  155.     return (position - *this).Length();
  156. }
  157. float MVector3::DistanceToSquared(MVector3 position)
  158. {
  159.     return DistanceSquared(position, *this);
  160. }
  161. float MVector3::DistanceTo2D(MVector3 position)
  162. {
  163.     MVector3 lhs(x, y, 0.0f);
  164.     MVector3 rhs(position.x, position.y, 0.0f);
  165.  
  166.     return Distance(lhs, rhs);
  167. }
  168. float MVector3::DistanceToSquared2D(MVector3 position)
  169. {
  170.     MVector3 lhs(x, y, 0.0f);
  171.     MVector3 rhs(position.x, position.y, 0.0f);
  172.  
  173.     return DistanceSquared(lhs, rhs);
  174. }
  175. float MVector3::Distance(MVector3 position1, MVector3 position2)
  176. {
  177.     return (position1 - position2).Length();
  178. }
  179. float MVector3::DistanceSquared(MVector3 position1, MVector3 position2)
  180. {
  181.     return (position1 - position2).LengthSquared();
  182. }
  183. float MVector3::Distance2D(MVector3 position1, MVector3 position2)
  184. {
  185.     MVector3 pos1 = MVector3(position1.x, position1.y, 0);
  186.     MVector3 pos2 = MVector3(position2.x, position2.y, 0);
  187.     return (pos1 - pos2).Length();
  188. }
  189. float MVector3::DistanceSquared2D(MVector3 position1, MVector3 position2)
  190. {
  191.     MVector3 pos1 = MVector3(position1.x, position1.y, 0);
  192.     MVector3 pos2 = MVector3(position2.x, position2.y, 0);
  193.     return (pos1 - pos2).LengthSquared();
  194. }
  195. float MVector3::Angle(MVector3 from, MVector3 to)
  196. {
  197.     double dot = MVector3::Dot(from.Normalized(), to.Normalized());
  198.     return (float)(acosf((dot)) * (180.0 / M_PI));
  199. }
  200. float MVector3::SignedAngle(MVector3 from, MVector3 to, MVector3 planeNormal)
  201. {
  202.     MVector3 perpVector = MVector3::Cross(planeNormal, from);
  203.  
  204.     double angle = MVector3::Angle(from, to);
  205.     double dot = MVector3::Dot(perpVector, to);
  206.     if (dot < 0)
  207.     {
  208.         angle *= -1;
  209.     }
  210.  
  211.     return (float)angle;
  212. }
  213. float MVector3::ToHeading()
  214. {
  215.     return (float)((atan2f(x, -y) + M_PI) * (180.0 / M_PI));
  216. }
  217. MVector3 MVector3::Around(float distance)
  218. {
  219.     //return *this + MVector3::RandomXY() * distance;
  220. }
  221. MVector3 MVector3::RandomXY()
  222. {
  223.     MVector3 v;
  224.     double radian = _rand() * 2 * M_PI;
  225.  
  226.     v.x = (float)(cosf(radian));
  227.     v.y = (float)(sinf(radian));
  228.     v.Normalize();
  229.     return v;
  230. }
  231. MVector3 MVector3::RandomXYZ()
  232. {
  233.     MVector3 v;
  234.     double radian = _rand() * 2.0 * M_PI;
  235.     double cosTheta = (_rand() * 2.0) - 1.0;
  236.     double theta = acosf(cosTheta);
  237.  
  238.     v.x = (float)(sinf(theta) * cosf(radian));
  239.     v.y = (float)(sinf(theta) * sinf(radian));
  240.     v.z = (float)(cosf(theta));
  241.     v.Normalize();
  242.     return v;
  243. }
  244. MVector3 MVector3::Add(MVector3 left, MVector3 right)
  245. {
  246.     return MVector3(left.x + right.x, left.y + right.y, left.z + right.z);
  247. }
  248. MVector3 MVector3::Subtract(MVector3 left, MVector3 right)
  249. {
  250.     return MVector3(left.x - right.x, left.y - right.y, left.z - right.z);
  251. }
  252. MVector3 MVector3::Modulate(MVector3 left, MVector3 right)
  253. {
  254.     return MVector3(left.x * right.x, left.y * right.y, left.z * right.z);
  255. }
  256. MVector3 MVector3::Multiply(MVector3 value, float scale)
  257. {
  258.     return MVector3(value.x * scale, value.y * scale, value.z * scale);
  259. }
  260. MVector3 MVector3::Divide(MVector3 value, float scale)
  261. {
  262.     return MVector3(value.x / scale, value.y / scale, value.z / scale);
  263. }
  264. MVector3 MVector3::Negate(MVector3 value)
  265. {
  266.     return MVector3(-value.x, -value.y, -value.z);
  267. }
  268. MVector3 MVector3::Clamp(MVector3 value, MVector3 min, MVector3 max)
  269. {
  270.     float xx = value.x;
  271.     xx = (xx > max.x) ? max.x : xx;
  272.     xx = (xx < min.x) ? min.x : xx;
  273.  
  274.     float yy = value.y;
  275.     yy = (yy > max.y) ? max.y : yy;
  276.     yy = (yy < min.y) ? min.y : yy;
  277.  
  278.     float zz = value.z;
  279.     zz = (zz > max.z) ? max.z : zz;
  280.     zz = (zz < min.z) ? min.z : zz;
  281.  
  282.     return MVector3(xx, yy, zz);
  283. }
  284. MVector3 MVector3::Lerp(MVector3 start, MVector3 end, float factor)
  285. {
  286.     MVector3 vector;
  287.  
  288.     vector.x = start.x + ((end.x - start.x) * factor);
  289.     vector.y = start.y + ((end.y - start.y) * factor);
  290.     vector.z = start.z + ((end.z - start.z) * factor);
  291.  
  292.     return vector;
  293. }
  294. float MVector3::Dot(MVector3 left, MVector3 right)
  295. {
  296.     return (left.x * right.x + left.y * right.y + left.z * right.z);
  297. }
  298. MVector3 MVector3::Cross(MVector3 left, MVector3 right)
  299. {
  300.     MVector3 result;
  301.     result.x = left.y * right.z - left.z * right.y;
  302.     result.y = left.z * right.x - left.x * right.z;
  303.     result.z = left.x * right.y - left.y * right.x;
  304.     return result;
  305. }
  306. MVector3 MVector3::Project(MVector3 vector, MVector3 onNormal)
  307. {
  308.     return onNormal * Dot(vector, onNormal) / Dot(onNormal, onNormal);
  309. }
  310. MVector3 MVector3::ProjectOnPlane(MVector3 vector, MVector3 planeNormal)
  311. {
  312.     return (vector - Project(vector, planeNormal));
  313. }
  314. MVector3 MVector3::Reflect(MVector3 vector, MVector3 normal)
  315. {
  316.     MVector3 result;
  317.     float dot = ((vector.x * normal.x) + (vector.y * normal.y)) + (vector.x * normal.x);
  318.  
  319.     result.x = vector.x - ((2.0f * dot) * normal.x);
  320.     result.y = vector.y - ((2.0f * dot) * normal.y);
  321.     result.z = vector.z - ((2.0f * dot) * normal.z);
  322.  
  323.     return result;
  324. }
  325. MVector3 MVector3::Normalize(MVector3 vector)
  326. {
  327.     vector.Normalize();
  328.     return vector;
  329. }
  330. MVector3 MVector3::Minimize(MVector3 left, MVector3 right)
  331. {
  332.     MVector3 vector;
  333.     vector.x = (left.x < right.x) ? left.x : right.x;
  334.     vector.y = (left.y < right.y) ? left.y : right.y;
  335.     vector.z = (left.z < right.z) ? left.z : right.z;
  336.     return vector;
  337. }
  338. MVector3 MVector3::Maximize(MVector3 left, MVector3 right)
  339. {
  340.     MVector3 vector;
  341.     vector.x = (left.x > right.x) ? left.x : right.x;
  342.     vector.y = (left.y > right.y) ? left.y : right.y;
  343.     vector.z = (left.z > right.z) ? left.z : right.z;
  344.     return vector;
  345. }
  346.  
  347. /*MVector3::operator Vector3() const
  348. {
  349.     return (x, y, z);
  350. }
  351. inline MVector3 operator+(MVector3 lhs, const MVector3& rhs)
  352. {
  353.     lhs += rhs;
  354.     return lhs;
  355. }
  356. MVector3& MVector3::operator+=(const MVector3& rhs)
  357. {
  358.     x += rhs.x;
  359.     y += rhs.y;
  360.     z += rhs.z;
  361.     return *this;
  362. }*/
  363. MVector3 operator + (MVector3 left, MVector3 right)
  364. {
  365.     return MVector3(left.x + right.x, left.y + right.y, left.z + right.z);
  366. }
  367. MVector3 operator += (MVector3 left, MVector3 right)
  368. {
  369.     return MVector3(left.x += right.x, left.y += right.y, left.z += right.z);
  370. }
  371. MVector3 operator - (MVector3 left, MVector3 right)
  372. {
  373.     return MVector3(left.x - right.x, left.y - right.y, left.z - right.z);
  374. }
  375. MVector3 operator -= (MVector3 left, MVector3 right)
  376. {
  377.     return MVector3(left.x -= right.x, left.y -= right.y, left.z -= right.z);
  378. }
  379. MVector3 operator - (MVector3 value)
  380. {
  381.     return MVector3(-value.x, -value.y, -value.z);
  382. }
  383. MVector3 operator * (MVector3 value, float scale)
  384. {
  385.     return MVector3(value.x * scale, value.y * scale, value.z * scale);
  386. }
  387. MVector3 operator * (float scale, MVector3 vec)
  388. {
  389.     return vec * scale;
  390. }
  391. MVector3 operator / (MVector3 value, float scale)
  392. {
  393.     return MVector3(value.x / scale, value.y / scale, value.z / scale);
  394. }
  395. bool operator == (MVector3 left, MVector3 right)
  396. {
  397.     return MVector3::Equals(left, right);
  398. }
  399. bool operator != (MVector3 left, MVector3 right)
  400. {
  401.     return !MVector3::Equals(left, right);
  402. }
  403. char *MVector3::ToString()
  404. {
  405.     char data[55];
  406.     _snprintf(data, sizeof(data), "X:%.5f Y:%.5f Z:%.5f", x, y, z);
  407.     return data;
  408. }
  409. bool MVector3::Equals(MVector3 *value)
  410. {
  411.     if (value == NULL)
  412.         return false;
  413.  
  414.     return Equals(value);
  415. }
  416. bool MVector3::Equals(MVector3 value)
  417. {
  418.     return (x == value.x && y == value.y && z == value.z);
  419. }
  420. bool MVector3::Equals(MVector3 &value1, MVector3 &value2)
  421. {
  422.     return (value1.x == value2.x && value1.y == value2.y && value1.z == value2.z);
  423. }
  424.  
  425. #pragma pack(push, 1)
  426. typedef struct Vector3
  427. {
  428.     float x, y, z;
  429.  
  430.     operator MVector3() const
  431.     {
  432.         return MVector3(x, y, z);
  433.     }
  434.  
  435.     Vector3(float, float, float);
  436.     Vector3(float, int, float, int, float, int);
  437.     Vector3(float, int);
  438.     Vector3(float);
  439.     Vector3();
  440. } Vector3;
  441. #pragma pack(pop)
  442. Vector3::Vector3(float X, float Y, float Z) :
  443.     x(X),
  444.     y(Y),
  445.     z(Z)
  446. {}
  447. Vector3::Vector3(float X, int, float Y, int, float Z, int) :
  448.     x(X),
  449.     y(Y),
  450.     z(Z)
  451. {}
  452. Vector3::Vector3(float v, int) :
  453.     x(v),
  454.     y(v),
  455.     z(v)
  456. {}
  457. Vector3::Vector3(float v) :
  458.     x(v),
  459.     y(v),
  460.     z(v)
  461. {}
  462. Vector3::Vector3() :
  463.     x(0.f),
  464.     y(0.f),
  465.     z(0.f)
  466. {}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement