mirror of
https://github.com/zoriya/Bomberman.git
synced 2025-12-21 22:05:10 +00:00
Adding a position componentn and a vector3
This commit is contained in:
157
lib/wal/sources/Models/Vector3.hpp
Normal file
157
lib/wal/sources/Models/Vector3.hpp
Normal file
@@ -0,0 +1,157 @@
|
||||
//
|
||||
// Created by Zoe Roux on 5/17/21.
|
||||
//
|
||||
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <iostream>
|
||||
#include <cmath>
|
||||
|
||||
namespace WAL
|
||||
{
|
||||
//! @brief A Vector3 data type. (templated to allow any kind of vector3)
|
||||
template<typename T>
|
||||
class Vector3
|
||||
{
|
||||
public:
|
||||
//! @brief The x value of the vector
|
||||
T x;
|
||||
//! @brief The y value of the vector
|
||||
T y;
|
||||
//! @brief The y value of the vector
|
||||
T z;
|
||||
|
||||
//! @brief Create a new nil vector3.
|
||||
Vector3<T>()
|
||||
: x(0), y(0), z(0)
|
||||
{}
|
||||
|
||||
//! @brief Create a new vector3 representing a specific coordinate.
|
||||
Vector3<T>(T x, T y, T z)
|
||||
: x(x), y(y), z(z)
|
||||
{}
|
||||
|
||||
//! @brief A default destructor
|
||||
~Vector3() = default;
|
||||
|
||||
template<typename T2>
|
||||
Vector3<T> &operator+=(const Vector3<T2> &vec)
|
||||
{
|
||||
this->x += vec.x;
|
||||
this->y += vec.y;
|
||||
this->z += vec.z;
|
||||
return *this;
|
||||
}
|
||||
|
||||
template<typename T2>
|
||||
Vector3<T> operator+(const Vector3<T2> &vec) const
|
||||
{
|
||||
return Vector3<T>(this->x + vec.x, this->y + vec.y, this->z + vec.z);
|
||||
}
|
||||
|
||||
template<typename T2>
|
||||
Vector3<T> &operator-=(const Vector3<T2> &vec)
|
||||
{
|
||||
this->x -= vec.x;
|
||||
this->y -= vec.y;
|
||||
this->z -= vec.z;
|
||||
return *this;
|
||||
}
|
||||
|
||||
template<typename T2>
|
||||
Vector3<T> &operator*=(T2 d)
|
||||
{
|
||||
this->x *= d;
|
||||
this->y *= d;
|
||||
this->z *= d;
|
||||
return *this;
|
||||
}
|
||||
|
||||
template<typename T2>
|
||||
Vector3<T> operator*(T2 d) const
|
||||
{
|
||||
return Vector2<T>(this->x * d, this->y * d, this->z * d);
|
||||
}
|
||||
|
||||
template<typename T2>
|
||||
Vector3<T> operator*(Vector3<T2> &b) const
|
||||
{
|
||||
return Vector3<T>(this->x * b.x, this->y * b.y, this->z * b.z);
|
||||
}
|
||||
|
||||
template<typename T2>
|
||||
Vector3<T> operator/=(Vector3<T2> &b)
|
||||
{
|
||||
this->x /= b.x;
|
||||
this->y /= b.y;
|
||||
this->z /= b.z;
|
||||
return this;
|
||||
}
|
||||
|
||||
template<typename T2>
|
||||
Vector3<T> operator/(Vector3<T2> &b) const
|
||||
{
|
||||
return Vector3<T>(this->x / b.x, this->y / b.y, this->z / b.z);
|
||||
}
|
||||
|
||||
template<typename T2>
|
||||
Vector3<T> operator/=(T2 b)
|
||||
{
|
||||
this->x /= b;
|
||||
this->y /= b;
|
||||
this->z /= b;
|
||||
return this;
|
||||
}
|
||||
|
||||
template<typename T2>
|
||||
Vector3<T> operator/(T2 b) const
|
||||
{
|
||||
return Vector3<T>(this->x / b, this->y / b, this->z / b);
|
||||
}
|
||||
|
||||
template<typename T2>
|
||||
double distance(const Vector3<T2> &o) const
|
||||
{
|
||||
return std::sqrt(std::pow(this->x - o.x, 2) + std::pow(this->y - o.y, 2) + std::pow(this->z - o.z, 2));
|
||||
}
|
||||
|
||||
double magnitude() const
|
||||
{
|
||||
return (std::sqrt(std::pow(this->x, 2) + std::pow(this->y, 2), std::pow(this->z, 2)));
|
||||
}
|
||||
|
||||
Vector3<T> normalize()
|
||||
{
|
||||
double mag = this->magnitude();
|
||||
|
||||
this->x /= mag;
|
||||
this->y /= mag;
|
||||
this->z /= mag;
|
||||
return *this;
|
||||
}
|
||||
|
||||
Vector3<T> normalized() const
|
||||
{
|
||||
T mag = this->magnitude();
|
||||
|
||||
return Vector3<T>(this->x / mag, this->y / mag, this->z / mag);
|
||||
}
|
||||
|
||||
Vector3<T> projection(const Vector3<T> &point) const
|
||||
{
|
||||
return (point * this) / std::pow(this->magnitude(), 2) * this;
|
||||
}
|
||||
};
|
||||
|
||||
typedef Vector3<float> Vector3f;
|
||||
typedef Vector3<unsigned> Vector3u;
|
||||
typedef Vector3<int> Vector3i;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
std::ostream &operator<<(std::ostream &s, const WAL::Vector3<T> &v)
|
||||
{
|
||||
s << v.x << " " << v.y << " " << v.z;
|
||||
return s;
|
||||
}
|
||||
Reference in New Issue
Block a user