Commit f9404f27 authored by Leon's avatar Leon

Add Vector2D, Matrix2 headers and source; WIP Add Rigidbody2D header

parent 53f54349
*.vcxproj
*.vcxproj.*
.vs/
x64/
\ No newline at end of file

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.29519.87
MinimumVisualStudioVersion = 10.0.40219.1
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "DuoDim", "DuoDim.vcxproj", "{6D870D6C-2739-422A-AF9C-8F5B62C1CACD}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|x64 = Debug|x64
Debug|x86 = Debug|x86
Release|x64 = Release|x64
Release|x86 = Release|x86
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{6D870D6C-2739-422A-AF9C-8F5B62C1CACD}.Debug|x64.ActiveCfg = Debug|x64
{6D870D6C-2739-422A-AF9C-8F5B62C1CACD}.Debug|x64.Build.0 = Debug|x64
{6D870D6C-2739-422A-AF9C-8F5B62C1CACD}.Debug|x86.ActiveCfg = Debug|Win32
{6D870D6C-2739-422A-AF9C-8F5B62C1CACD}.Debug|x86.Build.0 = Debug|Win32
{6D870D6C-2739-422A-AF9C-8F5B62C1CACD}.Release|x64.ActiveCfg = Release|x64
{6D870D6C-2739-422A-AF9C-8F5B62C1CACD}.Release|x64.Build.0 = Release|x64
{6D870D6C-2739-422A-AF9C-8F5B62C1CACD}.Release|x86.ActiveCfg = Release|Win32
{6D870D6C-2739-422A-AF9C-8F5B62C1CACD}.Release|x86.Build.0 = Release|Win32
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {2A474D2C-B130-485C-8892-E9280F5486C8}
EndGlobalSection
EndGlobal
#include <math.h>
#include "Matrix2.h"
using namespace duodim;
template <typename T>
Matrix2<T>::Matrix2(const T x0, const T x1, const T y0, const T y1)
{
this->fields[0][0] = x0;
this->fields[0][1] = x1;
this->fields[1][0] = y0;
this->fields[1][1] = y1;
}
template <typename T>
Matrix2<T>::Matrix2(const Matrix2<T>& a)
{
this->fields[0][0] = a->fields[0][0];
this->fields[0][1] = a->fields[0][1];
this->fields[1][0] = a->fields[1][0];
this->fields[1][1] = a->fields[1][1];
}
template <typename T>
Matrix2<T>::Matrix2()
{
this->fields[0][0] = T(0);
this->fields[0][1] = T(0);
this->fields[1][0] = T(0);
this->fields[1][1] = T(0);
}
template <typename T>
Matrix2<T>::~Matrix2() {}
template <typename T>
Vector2D<T> Matrix2<T>::operator *(const Vector2D<T> b)
{
return Vector2D(
(this[0][0] * b.getX() + this[0][1] * b.getY()),
(this[1][0] * b.getX() + this[1][1] * b.getY())
);
}
template <typename T>
Matrix2<T> Matrix2<T>::operator *(const Matrix2<T> b)
{
return Matrix2(
(this[0][0] * b[0][0] + this[0][1] * b[1][0]),
(this[0][0] * b[0][1] + this[0][1] * b[1][1]),
(this[1][0] * b[0][0] + this[1][1] * b[1][0]),
(this[1][0] * b[0][1] + this[1][1] * b[1][1])
);
}
template <typename T>
Matrix2<T> Matrix2<T>::transpose()
{
return Matrix2(this[0][0], this[1][0], this[0][1], this[1][1]);
}
template <typename T>
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
#ifndef INCLUDE_MATRIX2_H
#define INCLUDE_MATRIX2_H
#include "Vector2D.h"
namespace duodim
{
template <typename T>
class Matrix2
{
static_assert(std::is_floating_point<T>::value, "Matrix2 must be instantiated with a floating-point template argument.");
public:
Matrix2(const T x0, const T x1, const T y0, const T y1);
Matrix2(const Matrix2<T>& a);
Matrix2();
~Matrix2();
Vector2D<T> operator *(const Vector2D<T> b);
Matrix2<T> operator *(const Matrix2<T> b);
Matrix2<T> transpose();
T operator [](const int x);
void setFromRadians(const T radians);
private:
T[2][2] fields;
};
}
#endif
\ No newline at end of file
#ifndef INCLUDE_RIGIDBODY2D_H
#define INCLUDE_RIGIDBODY2D_H
#include "Vector2D.h"
namespace duodim
{
class Rigidbody2D
{
public:
Vector2D<float> position;
float rotation;
float velocity;
float angularVelocity;
float mass;
float inertia;
float coeffFriction;
float coeffRestitution;
void transformLocalToWorldSpace();
void transformWorldToLocalSpace();
private:
Vector2D<float> force;
float torque;
};
}
#endif
\ No newline at end of file
#include <math.h>
#include "Vector2D.h"
using namespace duodim;
template <typename T>
Vector2D<T>::Vector2D(const T x, const T y) : x(x), y(y) {}
template <typename T>
Vector2D<T>::Vector2D(const Vector2D<T>& a) : x(a.x), y(a.y) {}
template <typename T>
Vector2D<T>::Vector2D() : x(T(0)), y(T(0)) {}
template <typename T>
Vector2D<T>::~Vector2D() {}
template <typename T>
T Vector2D<T>::getX()
{
return this.x;
}
template <typename T>
T Vector2D<T>::getY()
{
return this.y;
}
template <typename T>
T Vector2D<T>::magnitude()
{
return sqrt(this->squaredMagnitude());
}
template <typename T>
T Vector2D<T>::squaredMagnitude()
{
return pow(this->x, 2) + pow(this->y, 2);
}
template <typename T>
T Vector2D<T>::crossMult(const Vector2D<T> b)
{
return T(this->x * b->y - this->y * b->x);
}
template <typename T>
T Vector2D<T>::dotMult(const Vector2D<T> b)
{
return T(this->x * b->x + this->y * b->y);
}
template <typename T>
void Vector2D<T>::crossMult(const T a)
{
T nx = a * this->y;
T ny = a * (-1) * this->x;
this->x = nx;
this->ny = ny;
}
template <typename T>
void Vector2D<T>::crossMultInvert(const T a)
{
T nx = a * (-1) * this->y;
T ny = a * this->x;
this->x = nx;
this->ny = ny;
}
template <typename T>
void Vector2D<T>::normalize()
{
T a = 1 / this->magnitude();
this->x = a;
this->y = a;
}
template <typename T>
void Vector2D<T>::scalarMult(const T a)
{
this->x *= a;
this->y *= a;
}
template <typename T>
void Vector2D<T>::setX(const T a)
{
this->x = a;
}
template <typename T>
void Vector2D<T>::setY(const T a)
{
this->y = a;
}
template class Vector2D<float>;
template class Vector2D<double>;
template class Vector2D<long double>;
\ No newline at end of file
#ifndef INCLUDE_VECTOR2D_H
#define INCLUDE_VECTOR2D_H
#include <type_traits>
namespace duodim
{
template <typename T>
class Vector2D
{
static_assert(std::is_floating_point<T>::value, "Vector2D must be instantiated with a floating-point template argument.");
public:
Vector2D(const T x, const T y);
Vector2D(const Vector2D<T>& a);
Vector2D();
~Vector2D();
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);
private:
T x;
T y;
};
}
#endif
\ No newline at end of file
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment