diff --git a/include/main.h b/include/main.h index efab70116a5388e1c74ba9649b6a8dede035f864..729d69e8475e1bb4a40ec91fc40851dc355b785a 100644 --- a/include/main.h +++ b/include/main.h @@ -6,7 +6,8 @@ enum GenerationMethod { - Random + Random, + Cave }; #include "save-manager.h" @@ -15,5 +16,6 @@ enum GenerationMethod #include "world-map-3d.h" #include "chunk.h" #include "logger.h" +#include "maths/maths.h" #endif // MAIN_H diff --git a/include/maths/maths.h b/include/maths/maths.h new file mode 100644 index 0000000000000000000000000000000000000000..fea450c34d4a3c6ca63296c54f7043743956ca1d --- /dev/null +++ b/include/maths/maths.h @@ -0,0 +1,6 @@ +#ifndef __MATHEMATICS_INCLUDE_H__ +#define __MATHEMATICS_INCLUDE_H__ + +#include "maths/vec2.h" + +#endif // __MATHEMATICS_INCLUDE_H__ diff --git a/include/maths/vec2.h b/include/maths/vec2.h new file mode 100644 index 0000000000000000000000000000000000000000..e01f0f7b9c4172905bb6e0e114b2530983b771cd --- /dev/null +++ b/include/maths/vec2.h @@ -0,0 +1,27 @@ +#ifndef __MATHS__VEC2_H__ +#define __MATHS__VEC2_H__ + +#include <cmath> + +class Vec2 +{ + public: + float x = 0, y = 0; + Vec2(); + Vec2( float xIn, float yIn ); + + bool operator == ( const Vec2 rhs ) const; + Vec2 operator + ( const Vec2 rhs ) const; + Vec2 operator * ( const Vec2 rhs ) const; + Vec2 operator - ( const Vec2 rhs ) const; + Vec2 operator * ( const float scale ) const; + void operator += ( const Vec2 & rhs ); + void operator -= ( const Vec2 & rhs ); + void operator *= ( const Vec2 & rhs ); + void operator *= ( const float rhs ); + + float length(); + void normalise(); +}; + +#endif // __MATHS__VEC2_H__ diff --git a/src/maths/vec2.cpp b/src/maths/vec2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..aee6719eca9998cfe71c7094beeecd8925494870 --- /dev/null +++ b/src/maths/vec2.cpp @@ -0,0 +1,32 @@ +#include "maths/vec2.h" + +Vec2::Vec2() + : Vec2( 0.0, 0.0 ) {} +Vec2::Vec2( float xIn, float yIn ) + : x( xIn ), y( yIn ) +{}; + +bool Vec2::operator == ( const Vec2 rhs ) const { + return ( + abs( this->x - rhs.x ) < 0.01 + ) && ( + abs( this->y - rhs.y ) < 0.01 + ); +} +Vec2 Vec2::operator + ( const Vec2 rhs ) const { return Vec2( this->x + rhs.x, this->y + rhs.y ); } +Vec2 Vec2::operator - ( const Vec2 rhs ) const { return Vec2( this->x - rhs.x, this->y - rhs.y ); } +Vec2 Vec2::operator * ( const Vec2 rhs ) const { return Vec2( this->x * rhs.x, this->y * rhs.y ); } +Vec2 Vec2::operator * ( const float scale ) const { return Vec2( this->x * scale, this->y * scale ); } +void Vec2::operator += ( const Vec2 & rhs ) { this->x += rhs.x; this->y += rhs.y; } +void Vec2::operator -= ( const Vec2 & rhs ) { this->x -= rhs.x; this->y -= rhs.y; } +void Vec2::operator *= ( const Vec2 & rhs ) { this->x *= rhs.x; this->y *= rhs.y; } +void Vec2::operator *= ( const float scale ) { this->x *= scale; this->y *= scale; } + +float Vec2::length() + { return sqrtf( this->x*this->x + this->y*this->y ); } +void Vec2::normalise() +{ + float mag = this->length(); + this->x /= mag; + this->y /= mag; +}