Commit 68aaed47 authored by Leon's avatar Leon

Extend engine contents

parent 11763fb8
*.vcxproj
*.vcxproj.*
.vs/
.vscode/
x64/
\ No newline at end of file
#ifndef INCLUDE_BOUNDS_H
#define INCLUDE_BOUNDS_H
#include "Vector2D.hpp"
namespace duodim
{
class Bounds
{
public:
static Bounds fromCenterAndExtents(Vector2D<float> center, const Vector2D<float> extents);
static Bounds asUnion(Bounds a, Bounds b);
Bounds(const Vector2D<float> min, const Vector2D<float> max);
Vector2D<float> getMin();
Vector2D<float> getMax();
Vector2D<float> getCenter();
Vector2D<float> getExtents();
Bounds expandBy(const float factor);
float getPerimeter();
bool intersects(const Bounds o);
bool contains(const Bounds o);
private:
Vector2D<float> min;
Vector2D<float> max;
};
}
#endif
\ No newline at end of file
#ifndef INCLUDE_MATRIX2_H
#define INCLUDE_MATRIX2_H
#include "Vector2D.h"
#ifndef INCLUDE_MATRIX2_HPP
#define INCLUDE_MATRIX2_HPP
#include "Vector2D.hpp"
namespace duodim
{
template <typename T>
......@@ -8,6 +8,7 @@ namespace duodim
{
static_assert(std::is_floating_point<T>::value, "Matrix2 must be instantiated with a floating-point template argument.");
public:
static Matrix2<T> fromRadians(const T a);
Matrix2(const T x0, const T x1, const T y0, const T y1);
Matrix2(const Matrix2<T>& a);
Matrix2();
......@@ -18,8 +19,6 @@ namespace duodim
Matrix2<T> transpose();
T operator [](const int x);
void setFromRadians(const T radians);
private:
T[2][2] fields;
};
......
#ifndef INCLUDE_VECTOR2D_H
#define INCLUDE_VECTOR2D_H
#ifndef INCLUDE_VECTOR2D_HPP
#define INCLUDE_VECTOR2D_HPP
#include <type_traits>
namespace duodim
{
......@@ -8,25 +8,35 @@ namespace duodim
{
static_assert(std::is_floating_point<T>::value, "Vector2D must be instantiated with a floating-point template argument.");
public:
static Vector2D<T> min(const Vector2D<T> a, const Vector2D<T> b);
static Vector2D<T> max(const Vector2D<T> a, const Vector2D<T> b);
Vector2D(const T x, const T y);
Vector2D(const Vector2D<T>& a);
Vector2D();
~Vector2D();
Vector2D<T> operator +(const Vector2D<T> b);
Vector2D<T> operator -(const Vector2D<T> b);
Vector2D<T> operator *(const T a);
Vector2D<T> operator /(const T a);
Vector2D<T> crossMult(const T a);
Vector2D<T> iCrossMult(const T a);
Vector2D<T> normalized();
T operator *(const Vector2D<T> b);
T getX();
T getY();
T magnitude();
T squaredMagnitude();
T crossMult(const Vector2D<T> b);
T dotMult(const Vector2D<T> b);
void crossMult(const T a);
void crossMultInvert(const T a);
void normalize();
void scalarMult(const T a);
void setX(const T x);
void setY(const T y);
bool operator ==(Vector2D<T> b);
void setX(const T a);
void setY(const T a);
private:
T x;
T y;
......
#ifndef INCLUDE_CIRCLE_H
#define INCLUDE_CIRCLE_H
#ifndef INCLUDE_CIRCLE_HPP
#define INCLUDE_CIRCLE_HPP
#include "Shape.h"
#include "Shape.hpp"
#include "Bounds.hpp"
namespace duodim
{
......@@ -12,8 +13,13 @@ namespace duodim
Circle(float radius);
~Circle();
Circle* setRadius(float radius);
Bounds getBounds();
float getMass();
float getRadius();
void setRadius(float r);
protected:
void updateName();
private:
float radius;
};
......
#ifndef INCLUDE_CONVEX_POLYGON_H
#define INCLUDE_CONVEX_POLYGON_H
#include "Shape.h"
#include "Vector2D.h"
namespace duodim
{
class ConvexPolygon : public Shape
{
public:
ConvexPolygon();
ConvexPolygon(Vector2D<float>* vertices, Vector2D<float>* normals);
ConvexPolygon(int vertices, int normals);
~ConvexPolygon();
ConvexPolygon* setVertices(Vector2D<float>* vertices);
ConvexPolygon* setNormals(Vector2D<float>* normals);
Vector2D<float>* getVertices();
Vector2D<float>* getNormals();
private:
Vector2D<float>* vertices;
Vector2D<float>* normals;
};
}
#endif
\ No newline at end of file
#ifndef INCLUDE_CONVEX_POLYGON_HPP
#define INCLUDE_CONVEX_POLYGON_HPP
#include <list>
#include "Shape.hpp"
using namespace std;
namespace duodim
{
class ConvexPolygon : public Shape
{
public:
ConvexPolygon();
ConvexPolygon(list<Vector2D<float>> vertices, list<Vector2D<float>> normals);
~ConvexPolygon();
list<Vector2D<float>> getVertices();
list<Vector2D<float>> getNormals();
float getMass();
void setVertices(list<Vector2D<float>> v);
void setNormals(list<Vector2D<float>> n);
protected:
void updateName();
private:
list<Vector2D<float>> vertices;
list<Vector2D<float>> normals;
};
}
#endif
\ No newline at end of file
#ifndef INCLUDE_RECTANGLE_H
#define INCLUDE_RECTANGLE_H
#ifndef INCLUDE_RECTANGLE_HPP
#define INCLUDE_RECTANGLE_HPP
#include "Shape.h"
#include "Shape.hpp"
namespace duodim
{
......@@ -13,12 +13,14 @@ namespace duodim
Rectangle(float dim);
~Rectangle();
Rectangle* setHeight(float height);
Rectangle* setWidth(float width);
float getHeight();
float getWidth();
float getMass();
bool isQuadratic();
void setHeight(float h);
void setWidth(float w);
protected:
void updateName();
private:
float height;
float width;
......
#ifndef INCLUDE_SHAPE_H
#define INCLUDE_SHAPE_H
namespace duodim
{
class Shape
{
};
}
#endif
\ No newline at end of file
#ifndef INCLUDE_SHAPE_HPP
#define INCLUDE_SHAPE_HPP
#include <string>
#include "Bounds.hpp"
using namespace std;
namespace duodim
{
class Shape
{
public:
virtual Bounds getBounds();
virtual float getMass();
string getName();
void setName(string n);
protected:
virtual void updateName();
string name;
};
}
#endif
\ No newline at end of file
#ifndef INCLUDE_BODY_H
#define INCLUDE_BODY_H
#include "Vector2D.h"
#include "Shape.h"
#ifndef INCLUDE_BODY_HPP
#define INCLUDE_BODY_HPP
#include "Transform.hpp"
#include "Bounds.hpp"
#include "Material.hpp"
#include "Vector2D.hpp"
#include "Shape.hpp"
namespace duodim
{
class Body
......@@ -10,31 +13,43 @@ namespace duodim
Body();
Body(Vector2D<float> position, Shape shape);
Body(float posX, float posY, Shape shape);
float rotation;
float velocity;
float angularVelocity;
float mass = 1.f;
float inertia;
float coeffFriction;
float coeffRestitution;
Body* setPosition(Vector2D<float> position);
Body* setPosition(float posX, float posY);
Body* setShape(Shape shape);
Vector2D<float> getPosition();
Shape getShape();
float getRotation();
void addForce(Vector2D<float> f);
void setPosition(Vector2D<float> p);
void setPosition(float posX, float posY);
void setRotation(float r);
void setShape(Shape s);
void update(float deltaTime);
void transformLocalToWorldSpace();
void transformWorldToLocalSpace();
private:
Vector2D<float> position;
Transform transform;
Vector2D<float> velocity;
Vector2D<float> force;
Shape shape;
float torque;
Bounds bounds;
Material material;
float rotation = 0.f;
float angularVelocity = 0.f;
float torque = 0.f;
float mass = 1.f;
float inertia = 1.f;
float inv_mass = 1.f;
float inv_inertia = 1.f;
float coeffFriction = 1.f;
float coeffRestitution = 1.f;
};
}
#endif
\ No newline at end of file
#ifndef INCLUDE_MATERIAL_H
#define INCLUDE_MATERIAL_H
namespace duodim
{
class Material
{
public:
Material(float friction, float restitution);
float getFriction();
float getRestitution();
private:
float friction;
float restitution;
};
}
#endif
\ No newline at end of file
#ifndef INCLUDE_TRANSFORM_H
#define INCLUDE_TRANSFORM_H
#include "Matrix2.hpp"
#include "Vector2D.hpp"
namespace duodim
{
class Transform
{
public:
Transform();
Transform(Vector2D<float> position);
Transform(Vector2D<float> position, float rotation);
~Transform();
Vector2D<float> getPosition();
Vector2D<float> getWorldPosition(Vector2D<float> localPosition);
Vector2D<float> getWorldDirection(Vector2D<float> localDirection);
Vector2D<float> getLocalPosition(Vector2D<float> worldPosition);
Vector2D<float> getLocalDirection(Vector2D<float> worldDirection);
float getRotation();
void setPosition(const Vector2D<float> p);
void setRotation(const float r);
private:
Matrix2<float> rotMatrix;
Vector2D<float> position;
float rotation;
};
}
#endif
\ No newline at end of file
#ifndef INCLUDE_WORLD_H
#define INCLUDE_WORLD_H
#ifndef INCLUDE_WORLD_HPP
#define INCLUDE_WORLD_HPP
#include <list>
#include "Body.h"
#include "Body.hpp"
using namespace std;
......
#include "Body.h"
#include "Vector2D.h"
#include "Shape.h"
#include "World.h"
using namespace duodim;
Body::Body() : position(Vector2D<float>()) {}
Body::Body(Vector2D<float> position, Shape shape)
: position(position), shape(shape) {}
Body::Body(float posX, float posY, Shape shape)
: position(Vector2D<float>(posX, posY)), shape(shape) {}
Body* Body::setPosition(Vector2D<float> position)
{
this->position = position;
return this;
}
Body* Body::setPosition(float posX, float posY)
{
this->position = Vector2D(posX, posY);
return this;
}
Body* Body::setShape(Shape shape)
{
this->shape = shape;
return this;
}
Vector2D<float> Body::getPosition()
{
return this->position;
}
Shape Body::getShape()
{
return this->shape;
}
void Body::update(float deltaTime)
{
if (this->mass == 0.f) {
throw "Mass can't be zero";
}
Vector2D<float> acceleration = Vector2D<float>(0, World::gravityConst * -1);
this->velocity += acceleration.dotMult(dt);
}
void Body::transformLocalToWorldSpace()
{
}
void Body::transformWorldToLocalSpace()
{
}
\ No newline at end of file
#include "Bounds.hpp"
#include "Vector2D.hpp"
using namespace duodim;
Bounds Bounds::fromCenterAndExtents(Vector2D<float> center, const Vector2D<float> extents)
{
return Bounds((center - extents), (center + extents));
}
Bounds Bounds::asUnion(Bounds a, Bounds b)
{
return Bounds(
Vector2D<float>::min(a.getMin(), b.getMin()),
Vector2D<float>::max(a.getMax(), b.getMax())
);
}
Bounds::Bounds(const Vector2D<float> min, const Vector2D<float> max) : min(min), max(max) {}
Vector2D<float> Bounds::getMin()
{
return min;
}
Vector2D<float> Bounds::getMax()
{
return max;
}
Vector2D<float> Bounds::getCenter()
{
return (max + min) * .5f;
}
Vector2D<float> Bounds::getExtents()
{
return (max - min) * .5f;
}
Bounds Bounds::expandBy(const float factor)
{
return Bounds::fromCenterAndExtents(getCenter(), (getExtents() * factor));
}
float Bounds::getPerimeter()
{
return 2.f * (max.getX() - min.getX() + max.getY() - min.getY());
}
bool Bounds::intersects(const Bounds o)
{
Vector2D<float> oMin = o.min;
Vector2D<float> oMax = o.max;
return !(max.getX() < oMin.getX() || min.getX() > oMax.getX())
&& !(max.getY() < oMin.getY() || min.getY() > oMax.getY)
;
}
bool Bounds::contains(const Bounds o)
{
return Vector2D<float>::min(min, o.min) == min && Vector2D<float>::max(max, o.max) == max;
}
\ No newline at end of file
#include <math.h>
#include "Matrix2.h"
#include "Matrix2.hpp"
using namespace duodim;
template <typename T>
Matrix2<T> Matrix2<T>::fromRadians(const T a)
{
T c = T(cos(a));
T s = T(sin(a));
return Matrix2<T>(c, s * (-1), s, c);
}
template <typename T>
Matrix2<T>::Matrix2(const T x0, const T x1, const T y0, const T y1)
{
......@@ -65,17 +73,6 @@ T Matrix2<T>::operator [](const int x)
return this->fields[x];
}
template <typename T>
void Matrix2<T>::setFromRadians(const T radians)
{
T c = cos(radians);
T s = sin(radians);
this[0][0] = c;
this[0][1] = s * (-1);
this[1][0] = s;
this[1][1] = c;
}
template class Matrix2<float>;
template class Matrix2<double>;
template class Matrix2<long double>;
\ No newline at end of file
#include <math.h>
#include "Vector2D.h"
#include "Vector2D.hpp"
using namespace duodim;
template <typename T>
Vector2D<T> Vector2D<T>::min(const Vector2D<T> a, const Vector2D<T> b)
{
return Vector2D<T>(T(min(a.getX(), b.getX())), T(min(a.getY(), b.getY())));
}
template <typename T>
Vector2D<T> Vector2D<T>::max(const Vector2D<T> a, const Vector2D<T> b)
{
return Vector2D<T>(T(max(a.getX(), b.getX())), T(max(a.getY(), b.getY())));
}
template <typename T>
Vector2D<T>::Vector2D(const T x, const T y) : x(x), y(y) {}
......@@ -16,84 +28,105 @@ template <typename T>
Vector2D<T>::~Vector2D() {}
template <typename T>
T Vector2D<T>::getX()
Vector2D<T> Vector2D<T>::operator +(const Vector2D<T> b)
{
return this.x;
return Vector2D<T>(T(x + b.getX()), T(y + b.getY()));
}
template <typename T>
T Vector2D<T>::getY()
Vector2D<T> Vector2D<T>::operator -(const Vector2D<T> b)
{
return this.y;
return Vector2D<T>(T(x - b.getX()), T(y - b.getY()));
}
template <typename T>
T Vector2D<T>::magnitude()
Vector2D<T> Vector2D<T>::operator *(const T a)
{
return sqrt(this->squaredMagnitude());
return Vector2D<T>(T(x * a), T(y * a));
}
template <typename T>
T Vector2D<T>::squaredMagnitude()
Vector2D<T> Vector2D<T>::operator /(const T a)
{
return pow(this->x, 2) + pow(this->y, 2);
if (b == T(0)) {
throw "Cannot divide by zero";
}
return Vector2D<T>(T(x / a), T(y / a));
}
template <typename T>
T Vector2D<T>::crossMult(const Vector2D<T> b)
Vector2D<T> Vector2D<T>::crossMult(const T a)
{
return Vector2D<T>(T(a * y), T(a * (-1) * x));
}
template <typename T>
Vector2D<T> Vector2D<T>::iCrossMult(const T a)
{
return T(this->x * b->y - this->y * b->x);
return Vector2D<T>(T(a * (-1) * y), T(a * x));
}
template <typename T>
T Vector2D<T>::dotMult(const Vector2D<T> b)
Vector2D<T> Vector2D<T>::normalized()
{
return T(this->x * b->x + this->y * b->y);
T mag = magnitude();
T a = magnitude > T(0) ? T(1 / mag) : mag;
return Vector2D<T>(a, a);
}
template <typename T>
void Vector2D<T>::crossMult(const T a)
T Vector2D<T>::getX()
{
T nx = a * this->y;
T ny = a * (-1) * this->x;
this->x = nx;
this->ny = ny;
return x;
}
template <typename T>
void Vector2D<T>::crossMultInvert(const T a)
T Vector2D<T>::getY()
{
return y;
}
template <typename T>
T Vector2D<T>::magnitude()
{
T sMag = squaredMagnitude();
return sMag == T(0) ? sMag : sqrt(sMag);
}
template <typename T>
T Vector2D<T>::squaredMagnitude()
{
return T(pow(x, 2) + pow(y, 2));