Commit 11763fb8 authored by Leon Willens's avatar Leon Willens

Add shapes; Add Body and world

parent 7bd2536d
#ifndef INCLUDE_RIGIDBODY2D_H
#define INCLUDE_RIGIDBODY2D_H
#ifndef INCLUDE_BODY_H
#define INCLUDE_BODY_H
#include "Vector2D.h"
#include "Shape.h"
namespace duodim
{
class Rigidbody2D
class Body
{
public:
Vector2D<float> position;
Body();
Body(Vector2D<float> position, Shape shape);
Body(float posX, float posY, Shape shape);
float rotation;
float velocity;
float angularVelocity;
float mass;
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();
void update(float deltaTime);
void transformLocalToWorldSpace();
void transformWorldToLocalSpace();
private:
Vector2D<float> position;
Vector2D<float> force;
Shape shape;
float torque;
};
}
......
#ifndef INCLUDE_WORLD_H
#define INCLUDE_WORLD_H
#include <list>
#include "Body.h"
using namespace std;
namespace duodim
{
class World
{
public:
static float gravityConst;
World();
~World();
bool addBody(Body body);
bool removeBody(Body body);
void update(float deltaTime);
private:
list<Body> bodies;
};
}
#endif
\ No newline at end of file
#ifndef INCLUDE_CIRCLE_H
#define INCLUDE_CIRCLE_H
#include "Shape.h"
namespace duodim
{
class Circle : public Shape
{
public:
Circle();
Circle(float radius);
~Circle();
Circle* setRadius(float radius);
float getRadius();
private:
float radius;
};
}
#endif
\ No newline at end of file
#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_RECTANGLE_H
#define INCLUDE_RECTANGLE_H
#include "Shape.h"
namespace duodim
{
class Rectangle : public Shape
{
public:
Rectangle();
Rectangle(float height, float width);
Rectangle(float dim);
~Rectangle();
Rectangle* setHeight(float height);
Rectangle* setWidth(float width);
float getHeight();
float getWidth();
bool isQuadratic();
private:
float height;
float width;
};
}
#endif
\ No newline at end of file
#ifndef INCLUDE_SHAPE_H
#define INCLUDE_SHAPE_H
namespace duodim
{
class Shape
{
};
}
#endif
\ No newline at end of file
#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 <list>
#include "World.h"
#include "Body.h"
using namespace duodim;
using namespace std;
World::World() : bodies({}) {}
World::~World() {}
float World::gravityConst = 9.81f;
bool World::addBody(Body body)
{
this->bodies.push_back(body);
return true;
}
bool World::removeBody(Body body)
{
this->bodies.remove(body);
return true;
}
void World::update(float deltaTime)
{
for(auto b : this->bodies)
{
b.update(deltaTime);
}
}
\ No newline at end of file
#include "Circle.h"
using namespace duodim;
Circle::Circle(): radius(0.f) {}
Circle::Circle(float radius) : radius(radius) {}
Circle::~Circle() {}
Circle* Circle::setRadius(float radius)
{
this->radius = radius;
return this;
}
float Circle::getRadius()
{
return this->radius;
}
\ No newline at end of file
#include "ConvexPolygon.h"
#include "Vector2D.h"
using namespace duodim;
ConvexPolygon::ConvexPolygon()
: vertices(new Vector2D<float>[0]), normals(new Vector2D<float>[0]) {}
ConvexPolygon::ConvexPolygon(Vector2D<float>* vertices, Vector2D<float>* normals) : vertices(vertices), normals(normals) {}
ConvexPolygon::ConvexPolygon(int vertices, int normals)
: vertices(new Vector2D<float>[vertices]), normals(new Vector2D<float>[normals]) {}
ConvexPolygon::~ConvexPolygon()
{
delete this->vertices;
delete this->normals;
}
ConvexPolygon* ConvexPolygon::setVertices(Vector2D<float>* vertices)
{
this->vertices = vertices;
return this;
}
ConvexPolygon* ConvexPolygon::setNormals(Vector2D<float>* normals)
{
this->normals = normals;
return this;
}
Vector2D<float>* ConvexPolygon::getVertices()
{
return this->vertices;
}
Vector2D<float>* ConvexPolygon::getNormals()
{
return this->normals;
}
\ No newline at end of file
#include "Rectangle.h"
using namespace duodim;
Rectangle::Rectangle() : height(0.f), width(0.f) {}
Rectangle::Rectangle(float height, float width) : height(height), width(width) {}
Rectangle::Rectangle(float dim) : height(dim), width(dim) {}
Rectangle::~Rectangle() {}
Rectangle* Rectangle::setHeight(float height)
{
this->height = height;
return this;
}
Rectangle* Rectangle::setWidth(float width)
{
this->height = height;
return this;
}
float Rectangle::getHeight()
{
return this->height;
}
float Rectangle::getWidth()
{
return this->width;
}
bool Rectangle::isQuadratic()
{
return this->height == this->width;
}
\ No newline at end of file
#include "Shape.h"
using namespace duodim;
\ 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