diff --git a/include/_circle.h b/include/_circle.h new file mode 100644 index 0000000..397ccd5 --- /dev/null +++ b/include/_circle.h @@ -0,0 +1,22 @@ +#ifndef CIRCLE_H +#define CIRCLE_H + +#include +#include "physics_object.h" + +class Circle : public PhysicsObject { +private: + float radius; + sf::CircleShape shape; + ShapeID ID = ShapeID::CIRCLE_SHAPE; + +public: + Circle(float r, sf::Vector2f position); + + void update(sf::Time deltaTime) override; + void draw(sf::RenderWindow& window) override; + + float getRadius() const; +}; + +#endif diff --git a/include/physics_object.h b/include/physics_object.h index 5e1e0b2..d0f2dc8 100644 --- a/include/physics_object.h +++ b/include/physics_object.h @@ -1,4 +1,6 @@ #include +#include +#pragma once struct PhysicalAttributes { float mass; @@ -30,7 +32,7 @@ class PhysicsObject { PhysicalAttributes attributes; public: - virtual ~PhysicsObject(); + virtual ~PhysicsObject() = default; // virtual get.set; // apply force diff --git a/src/circle.cpp b/src/circle.cpp new file mode 100644 index 0000000..a981a09 --- /dev/null +++ b/src/circle.cpp @@ -0,0 +1,29 @@ +#include "_circle.h" + +Circle::Circle(float r, sf::Vector2f pos) : PhysicsObject(pos), radius(r) { + shape.setRadius(radius); + shape.setOrigin(radius, radius); + shape.setPosition(pos); +} + +void Circle::update(sf::Time dt) { + sf::Vector2f pos = getPosition(); + sf::Vector2f vel = getVelocity(); + sf::Vector2f acc = getAcceleration(); + + float dts = dt.asSeconds(); + pos += vel * dts + acc * (0.5f * dts * dts); + vel += acc * dts; + + shape.setPosition(pos); + setVelocity(vel); + setPosition(pos); +} + +void Circle::draw(sf::RenderWindow& window) { + window.draw(shape); +} + +float Circle::getRadius() const { + return radius; +}