From b3e991afa5c3c362317616aad68e30889cf3ef40 Mon Sep 17 00:00:00 2001 From: max-001 Date: Tue, 5 Nov 2024 13:20:44 +0100 Subject: Changed examples a bit to work with new GameObject class --- src/example/rendering.cpp | 21 +++------------------ 1 file changed, 3 insertions(+), 18 deletions(-) (limited to 'src/example/rendering.cpp') diff --git a/src/example/rendering.cpp b/src/example/rendering.cpp index 1bf448c..9624093 100644 --- a/src/example/rendering.cpp +++ b/src/example/rendering.cpp @@ -23,19 +23,14 @@ int main() { dbg_trace(); - auto obj = GameObject(0, "name", "tag", 0); - auto obj1 = GameObject(1, "name", "tag", 0); - auto obj2 = GameObject(2, "name", "tag", 0); + auto obj = GameObject(0, "name", "tag", Point{0, 0}, 1, 1); + auto obj1 = GameObject(1, "name", "tag", Point{500, 0}, 1, 0.1); + auto obj2 = GameObject(2, "name", "tag", Point{800, 0}, 1, 0.1); auto & mgr = AssetManager::get_instance(); // Normal adding components { Color color(0, 0, 0, 0); - Point point = { - .x = 0, - .y = 0, - }; - obj.add_component(point, 1, 1); obj.add_component( make_shared("../asset/texture/img.png"), color, FlipSettings{true, true}); @@ -43,22 +38,12 @@ int main() { { Color color(0, 0, 0, 0); - Point point = { - .x = 500, - .y = 0, - }; - obj1.add_component(point, 0, 0.1); auto img = mgr.cache("../asset/texture/second.png"); obj1.add_component(img, color, FlipSettings{true, true}); } { Color color(0, 0, 0, 0); - Point point = { - .x = 800, - .y = 0, - }; - //obj.add_component(point, 0, 0.1); auto img = mgr.cache("../asset/texture/second.png"); obj2.add_component(img, color, FlipSettings{true, true}); } -- cgit v1.2.3 From 3fe7400de095756362b999908fd2a2ba3b71a848 Mon Sep 17 00:00:00 2001 From: Loek Le Blansch Date: Tue, 5 Nov 2024 18:48:46 +0100 Subject: merge #18 --- contributing.md | 31 +++++++++++++++++++++++++++++++ src/crepe/Component.cpp | 1 - src/crepe/Component.h | 2 +- src/crepe/ComponentManager.hpp | 3 ++- src/crepe/Metadata.cpp | 5 ++--- src/crepe/Metadata.h | 6 +++--- src/crepe/api/GameObject.cpp | 21 +++++++++++---------- src/crepe/api/GameObject.h | 5 ++--- src/crepe/api/Transform.cpp | 3 +-- src/crepe/api/Transform.h | 4 ++-- src/example/ecs.cpp | 16 ++++++++-------- src/example/rendering.cpp | 1 - 12 files changed, 63 insertions(+), 35 deletions(-) (limited to 'src/example/rendering.cpp') diff --git a/contributing.md b/contributing.md index 2fe46f7..e910dba 100644 --- a/contributing.md +++ b/contributing.md @@ -386,6 +386,37 @@ that you can click on to open them. #endif ``` +-
+ Variables that are being moved always use the fully qualified std::move +
GoodBad
+ + ```cpp + using namespace std; + string foo = "bar"; + ref_fn(std::move(foo)); + ``` + + + ```cpp + using namespace std; + string foo = "bar"; + ref_fn(move(foo)); + ``` +
+-
+ If possible, classes and structs are passed to functions by (const) reference +
GoodBad
+ + ```cpp + void foo(const Point & p); + ``` + + + ```cpp + void foo(Point & p); + void bar(Point p); + ``` +
## CMakeLists-specific diff --git a/src/crepe/Component.cpp b/src/crepe/Component.cpp index 78b47fa..41e7273 100644 --- a/src/crepe/Component.cpp +++ b/src/crepe/Component.cpp @@ -4,4 +4,3 @@ using namespace crepe; Component::Component(uint32_t id) : game_object_id(id), active(true) {} -int Component::get_instances_max() const { return -1; } diff --git a/src/crepe/Component.h b/src/crepe/Component.h index 039836e..8db9b2a 100644 --- a/src/crepe/Component.h +++ b/src/crepe/Component.h @@ -13,7 +13,7 @@ protected: public: virtual ~Component() = default; - virtual int get_instances_max() const; + virtual int get_instances_max() const { return -1; } public: uint32_t game_object_id; diff --git a/src/crepe/ComponentManager.hpp b/src/crepe/ComponentManager.hpp index f469d12..e74f2e9 100644 --- a/src/crepe/ComponentManager.hpp +++ b/src/crepe/ComponentManager.hpp @@ -36,12 +36,13 @@ T & ComponentManager::add_component(uint32_t id, Args &&... args) { // Check if the vector size is not greater than get_instances_max if (instance->get_instances_max() != -1 && components[type][id].size() >= instance->get_instances_max()) { + // TODO: Exception throw std::runtime_error( "Exceeded maximum number of instances for this component type"); } // store its unique_ptr in the vector<> - components[type][id].push_back(move(instance)); + components[type][id].push_back(std::move(instance)); return *instance; } diff --git a/src/crepe/Metadata.cpp b/src/crepe/Metadata.cpp index d362e0a..1ba150a 100644 --- a/src/crepe/Metadata.cpp +++ b/src/crepe/Metadata.cpp @@ -3,7 +3,6 @@ using namespace crepe; using namespace std; -Metadata::Metadata(uint32_t gameObjectId, string name, string tag) - : Component(gameObjectId), name(name), tag(tag) {} +Metadata::Metadata(uint32_t game_object_id, const string & name, const string & tag) + : Component(game_object_id), name(name), tag(tag) {} -int Metadata::get_instances_max() const { return 1; } diff --git a/src/crepe/Metadata.h b/src/crepe/Metadata.h index 2f08476..b946fd0 100644 --- a/src/crepe/Metadata.h +++ b/src/crepe/Metadata.h @@ -9,13 +9,13 @@ namespace crepe { class Metadata : public Component { public: - Metadata(uint32_t game_object_id, std::string name, std::string tag); - int get_instances_max() const; + Metadata(uint32_t game_object_id, const std::string & name, const std::string & tag); + virtual int get_instances_max() const { return 1; } public: std::string name; std::string tag; - uint32_t parent = UINT32_MAX; + uint32_t parent = -1; std::vector children; }; diff --git a/src/crepe/api/GameObject.cpp b/src/crepe/api/GameObject.cpp index 2592d2d..8a1a235 100644 --- a/src/crepe/api/GameObject.cpp +++ b/src/crepe/api/GameObject.cpp @@ -6,20 +6,21 @@ using namespace crepe; using namespace std; -GameObject::GameObject(uint32_t id, std::string name, std::string tag, - Point position, double rotation, double scale) - : id(id) { +GameObject::GameObject(uint32_t id, std::string name, std::string tag, const Point & position, double rotation, double scale) : id(id) { ComponentManager & mgr = ComponentManager::get_instance(); mgr.add_component(this->id, position, rotation, scale); mgr.add_component(this->id, name, tag); } -void GameObject::set_parent(GameObject & parent) { +void GameObject::set_parent(const GameObject & parent) { auto & mgr = ComponentManager::get_instance(); - vector> thisMetadata - = mgr.get_components_by_id(this->id); - vector> parentMetadata - = mgr.get_components_by_id(parent.id); - thisMetadata.at(0).get().parent = parent.id; - parentMetadata.at(0).get().children.push_back(this->id); + + // set parent on own Metadata component + vector> this_metadata = mgr.get_components_by_id(this->id); + this_metadata.at(0).get().parent = parent.id; + + // add own id to children list of parent's Metadata component + vector> parent_metadata = mgr.get_components_by_id(parent.id); + parent_metadata.at(0).get().children.push_back(this->id); } + diff --git a/src/crepe/api/GameObject.h b/src/crepe/api/GameObject.h index dcd33ad..4c87639 100644 --- a/src/crepe/api/GameObject.h +++ b/src/crepe/api/GameObject.h @@ -9,9 +9,8 @@ namespace crepe { class GameObject { public: - GameObject(uint32_t id, std::string name, std::string tag, Point position, - double rotation, double scale); - void set_parent(GameObject & parent); + GameObject(uint32_t id, std::string name, std::string tag, const Point & position, double rotation, double scale); + void set_parent(const GameObject & parent); template T & add_component(Args &&... args); diff --git a/src/crepe/api/Transform.cpp b/src/crepe/api/Transform.cpp index a80aff3..9c9bb06 100644 --- a/src/crepe/api/Transform.cpp +++ b/src/crepe/api/Transform.cpp @@ -8,11 +8,10 @@ using namespace crepe; -Transform::Transform(uint32_t game_id, Point point, double rot, double scale) +Transform::Transform(uint32_t game_id, const Point & point, double rot, double scale) : Component(game_id), position(point), rotation(rot), scale(scale) { dbg_trace(); } Transform::~Transform() { dbg_trace(); } -int Transform::get_instances_max() const { return 1; } diff --git a/src/crepe/api/Transform.h b/src/crepe/api/Transform.h index f918115..557061b 100644 --- a/src/crepe/api/Transform.h +++ b/src/crepe/api/Transform.h @@ -14,9 +14,9 @@ class Transform : public Component { // works similar (or the same) as those found in GLSL? public: - Transform(uint32_t id, Point, double, double); + Transform(uint32_t id, const Point &, double, double); ~Transform(); - int get_instances_max() const; + virtual int get_instances_max() const { return 1; } //! Translation (shift) Point position; //! Rotation, in radians diff --git a/src/example/ecs.cpp b/src/example/ecs.cpp index eb5eeba..6f9752e 100644 --- a/src/example/ecs.cpp +++ b/src/example/ecs.cpp @@ -12,16 +12,16 @@ int main() { // Create a few GameObjects try { GameObject body(0, "body", "person", Point{0, 0}, 0, 1); - GameObject rightLeg(1, "rightLeg", "person", Point{1, 1}, 0, 1); - GameObject leftLeg(2, "leftLeg", "person", Point{1, 1}, 0, 1); - GameObject rightFoot(3, "rightFoot", "person", Point{2, 2}, 0, 1); - GameObject leftFoot(4, "leftFoot", "person", Point{2, 2}, 0, 1); + GameObject right_leg(1, "rightLeg", "person", Point{1, 1}, 0, 1); + GameObject left_leg(2, "leftLeg", "person", Point{1, 1}, 0, 1); + GameObject right_foot(3, "rightFoot", "person", Point{2, 2}, 0, 1); + GameObject left_foot(4, "leftFoot", "person", Point{2, 2}, 0, 1); // Set the parent of each GameObject - rightFoot.set_parent(rightLeg); - leftFoot.set_parent(leftLeg); - rightLeg.set_parent(body); - leftLeg.set_parent(body); + right_foot.set_parent(right_leg); + left_foot.set_parent(left_leg); + right_leg.set_parent(body); + left_leg.set_parent(body); // Adding a second Transform component is not allowed and will invoke an exception body.add_component(Point{10, 10}, 0, 1); diff --git a/src/example/rendering.cpp b/src/example/rendering.cpp index f3d8a7d..3fe43d6 100644 --- a/src/example/rendering.cpp +++ b/src/example/rendering.cpp @@ -17,7 +17,6 @@ using namespace std; using namespace crepe; int main() { - dbg_trace(); auto obj = GameObject(0, "name", "tag", Point{0, 0}, 1, 1); -- cgit v1.2.3 From e2f3ace383b43cc3f140629e577b97c6f69c9856 Mon Sep 17 00:00:00 2001 From: jaroWMR Date: Thu, 7 Nov 2024 18:39:56 +0100 Subject: added physics system --- src/crepe/api/CMakeLists.txt | 4 +- src/crepe/api/Config.h | 10 ++++ src/crepe/api/Force.cpp | 20 ------- src/crepe/api/Force.h | 17 ------ src/crepe/api/GameObject.cpp | 2 +- src/crepe/api/GameObject.h | 4 +- src/crepe/api/Point.h | 11 ---- src/crepe/api/Rigidbody.cpp | 16 ++++-- src/crepe/api/Rigidbody.h | 102 ++++++++++++++++++++++++++------ src/crepe/api/Transform.cpp | 2 +- src/crepe/api/Transform.h | 6 +- src/crepe/api/Vector2.cpp | 59 +++++++++++++++++++ src/crepe/api/Vector2.h | 48 ++++++++++++++++ src/crepe/system/PhysicsSystem.cpp | 102 +++++++++++++++++++++----------- src/crepe/system/PhysicsSystem.h | 17 +++++- src/example/ecs.cpp | 12 ++-- src/example/particle.cpp | 4 +- src/example/physics.cpp | 23 ++++---- src/example/rendering.cpp | 8 +-- src/example/scene_manager.cpp | 16 +++--- src/example/script.cpp | 2 +- src/makefile | 19 +++--- src/test/CMakeLists.txt | 1 + src/test/PhysicsTest.cpp | 115 +++++++++++++++++++++++++++++++++++++ 24 files changed, 464 insertions(+), 156 deletions(-) delete mode 100644 src/crepe/api/Force.cpp delete mode 100644 src/crepe/api/Force.h delete mode 100644 src/crepe/api/Point.h create mode 100644 src/crepe/api/Vector2.cpp create mode 100644 src/crepe/api/Vector2.h create mode 100644 src/test/PhysicsTest.cpp (limited to 'src/example/rendering.cpp') diff --git a/src/crepe/api/CMakeLists.txt b/src/crepe/api/CMakeLists.txt index dbd6bf1..321343a 100644 --- a/src/crepe/api/CMakeLists.txt +++ b/src/crepe/api/CMakeLists.txt @@ -4,7 +4,6 @@ target_sources(crepe PUBLIC Script.cpp GameObject.cpp Rigidbody.cpp - Force.cpp ParticleEmitter.cpp Transform.cpp Color.cpp @@ -14,6 +13,7 @@ target_sources(crepe PUBLIC Metadata.cpp Scene.cpp SceneManager.cpp + Vector2.cpp ) target_sources(crepe PUBLIC FILE_SET HEADERS FILES @@ -26,7 +26,7 @@ target_sources(crepe PUBLIC FILE_SET HEADERS FILES GameObject.hpp Rigidbody.h Sprite.h - Point.h + Vector2.h Color.h Texture.h AssetManager.h diff --git a/src/crepe/api/Config.h b/src/crepe/api/Config.h index 22104a7..88220a7 100644 --- a/src/crepe/api/Config.h +++ b/src/crepe/api/Config.h @@ -35,6 +35,16 @@ public: */ bool color = true; } log; + + //! physics-related settings + struct { + /** + * \brief gravity value of physics system + * + * Gravity value of game. + */ + double gravity = 1; + } physics; }; } // namespace crepe diff --git a/src/crepe/api/Force.cpp b/src/crepe/api/Force.cpp deleted file mode 100644 index 63131ac..0000000 --- a/src/crepe/api/Force.cpp +++ /dev/null @@ -1,20 +0,0 @@ -#include - -#include "Force.h" - -namespace crepe { - -Force::Force(game_object_id_t id, uint32_t magnitude, uint32_t direction) : Component(id) { - // TODO: A standard angle unit should be established for the entire engine - // and assumed to be the default everywhere. Only conversion functions should - // explicitly contain the unit (i.e. `deg_to_rad()` & `rad_to_deg()`) - - // Convert direction from degrees to radians - float radian_direction = static_cast(direction) * (M_PI / 180.0f); - force_x = static_cast( - std::round(magnitude * std::cos(radian_direction))); - force_y = static_cast( - std::round(magnitude * std::sin(radian_direction))); -} - -} // namespace crepe diff --git a/src/crepe/api/Force.h b/src/crepe/api/Force.h deleted file mode 100644 index 4a4b5de..0000000 --- a/src/crepe/api/Force.h +++ /dev/null @@ -1,17 +0,0 @@ -#pragma once - -#include - -#include "../Component.h" - -namespace crepe { - -class Force : public Component { -public: - Force(game_object_id_t id, uint32_t magnitude, uint32_t direction); - - int32_t force_x; - int32_t force_y; -}; - -} // namespace crepe diff --git a/src/crepe/api/GameObject.cpp b/src/crepe/api/GameObject.cpp index 15330f3..d252e77 100644 --- a/src/crepe/api/GameObject.cpp +++ b/src/crepe/api/GameObject.cpp @@ -7,7 +7,7 @@ using namespace crepe; using namespace std; GameObject::GameObject(game_object_id_t id, const std::string & name, - const std::string & tag, const Point & position, + const std::string & tag, const Vector2 & position, double rotation, double scale) : id(id) { // Add Transform and Metadata components diff --git a/src/crepe/api/GameObject.h b/src/crepe/api/GameObject.h index 8389e6c..8dc102c 100644 --- a/src/crepe/api/GameObject.h +++ b/src/crepe/api/GameObject.h @@ -7,7 +7,7 @@ namespace crepe { -class Point; +class Vector2; /** * \brief Represents a GameObject @@ -30,7 +30,7 @@ public: * \param scale The scale of the GameObject */ GameObject(game_object_id_t id, const std::string & name, const std::string & tag, - const Point & position, double rotation, double scale); + const Vector2 & position, double rotation, double scale); /** * \brief Set the parent of this GameObject * diff --git a/src/crepe/api/Point.h b/src/crepe/api/Point.h deleted file mode 100644 index 575d624..0000000 --- a/src/crepe/api/Point.h +++ /dev/null @@ -1,11 +0,0 @@ -#pragma once - -namespace crepe { - -class Point { -public: - double x; - double y; -}; - -} // namespace crepe diff --git a/src/crepe/api/Rigidbody.cpp b/src/crepe/api/Rigidbody.cpp index 1e76346..cf07b0e 100644 --- a/src/crepe/api/Rigidbody.cpp +++ b/src/crepe/api/Rigidbody.cpp @@ -2,7 +2,15 @@ using namespace crepe; -Rigidbody::Rigidbody(game_object_id_t id, int mass, int gravity_scale, - BodyType bodyType) - : Component(id), mass(mass), gravity_scale(gravity_scale), - body_type(bodyType) {} +crepe::Rigidbody::Rigidbody(uint32_t game_object_id, + const RigidbodyData & data) : + Component(game_object_id), data(data){} + +void crepe::Rigidbody::add_force_linear(const Vector2 & force) { + this->data.linear_velocity += force; +} + +void crepe::Rigidbody::add_force_angular(double force) { + this->data.angular_velocity += force; +} + diff --git a/src/crepe/api/Rigidbody.h b/src/crepe/api/Rigidbody.h index 02ced2e..0c069d8 100644 --- a/src/crepe/api/Rigidbody.h +++ b/src/crepe/api/Rigidbody.h @@ -4,26 +4,96 @@ #include "../Component.h" -namespace crepe { +#include "Vector2.h" -// FIXME: can't this enum be defined inside the class declaration of Rigidbody? -enum class BodyType { - //! Does not move (e.g. walls, ground ...) - STATIC, - //! Moves and responds to forces (e.g. player, physics objects ...) - DYNAMIC, - //! Moves but does not respond to forces (e.g. moving platforms ...) - KINEMATIC, -}; +namespace crepe { +/** + * \brief Rigidbody class + * + * This class is used by the physics sytem and collision system. + * It configures how to system interact with the gameobject for movement and collisions. + */ class Rigidbody : public Component { public: - Rigidbody(game_object_id_t id, int mass, int gravity_scale, BodyType body_type); - int32_t velocity_x; - int32_t velocity_y; - int mass; - int gravity_scale; - BodyType body_type; + /** + * \brief BodyType enum + * + * This enum provides three bodytypes the physics sytem and collision system use. + */ + enum class BodyType { + //! Does not move (e.g. walls, ground ...) + STATIC, + //! Moves and responds to forces (e.g. player, physics objects ...) + DYNAMIC, + //! Moves but does not respond to forces (e.g. moving platforms ...) + KINEMATIC, + }; + /** + * \brief PhysicsConstraints to constrain movement + * + * This struct configures the movement constraint for this object. + * If a constraint is enabled the systems will not move the object. + */ + struct PhysicsConstraints { + //! X constraint + bool x = 0; + //! Y constraint + bool y = 0; + //! rotation constraint + bool rotation = 0; + }; +public: + /** + * This struct holds the data for the Rigidbody. + */ + struct RigidbodyData{ + //! objects mass + double mass = 0.0; + //! gravtiy scale + double gravity_scale = 0.0; + //! Changes if physics apply + BodyType body_type = BodyType::DYNAMIC; + //! linear velocity of object + Vector2 linear_velocity; + //! maximum linear velocity of object + Vector2 max_linear_velocity; + //! linear damping of object + Vector2 linear_damping; + //! angular velocity of object + double angular_velocity = 0.0; + //! max angular velocity of object + double max_angular_velocity = 0.0; + //! angular damping of object + double angular_damping = 0.0; + //! movements constraints of object + PhysicsConstraints constraints; + //! if gravity applies + bool use_gravity = true; + //! if object bounces + bool bounce = false; + }; +public: + /** + * \param game_object_id id of the gameobject the rigibody is added to. + * \param data struct to configure the rigidbody. + */ + Rigidbody(uint32_t game_object_id,const RigidbodyData& data); + //! struct to hold data of rigidbody + RigidbodyData data; +public: + /** + * \brief add a linear force to the Rigidbody. + * + * \param force Vector2 that is added to the linear force. + */ + void add_force_linear(const Vector2 & force); + /** + * \brief add a angular force to the Rigidbody. + * + * \param force Vector2 that is added to the angular force. + */ + void add_force_angular(double force); }; } // namespace crepe diff --git a/src/crepe/api/Transform.cpp b/src/crepe/api/Transform.cpp index 5fb66b3..a5f29ea 100644 --- a/src/crepe/api/Transform.cpp +++ b/src/crepe/api/Transform.cpp @@ -6,7 +6,7 @@ using namespace crepe; -Transform::Transform(game_object_id_t id, const Point & point, double rot, +Transform::Transform(game_object_id_t id, const Vector2 & point, double rot, double scale) : Component(id), position(point), rotation(rot), scale(scale) { dbg_trace(); diff --git a/src/crepe/api/Transform.h b/src/crepe/api/Transform.h index 1104e99..33e70f9 100644 --- a/src/crepe/api/Transform.h +++ b/src/crepe/api/Transform.h @@ -1,6 +1,6 @@ #pragma once -#include "api/Point.h" +#include "api/Vector2.h" #include "Component.h" @@ -20,7 +20,7 @@ public: * \param rot The rotation of the GameObject * \param scale The scale of the GameObject */ - Transform(game_object_id_t id, const Point & point, double rot, double scale); + Transform(game_object_id_t id, const Vector2 & point, double rot, double scale); /** * \brief Get the maximum number of instances for this component * @@ -30,7 +30,7 @@ public: public: //! Translation (shift) - Point position; + Vector2 position; //! Rotation, in radians double rotation; //! Multiplication factor diff --git a/src/crepe/api/Vector2.cpp b/src/crepe/api/Vector2.cpp new file mode 100644 index 0000000..8cbd9ad --- /dev/null +++ b/src/crepe/api/Vector2.cpp @@ -0,0 +1,59 @@ +#include "Vector2.h" + +namespace crepe { + + // Constructor with initial values + Vector2::Vector2(float x, float y) : x(x), y(y) {} + + // Subtracts another vector from this vector and returns the result. + Vector2 Vector2::operator-(const Vector2& other) const { + return {x - other.x, y - other.y}; + } + + // Adds another vector to this vector and returns the result. + Vector2 Vector2::operator+(const Vector2& other) const { + return {x + other.x, y + other.y}; + } + + // Multiplies this vector by a scalar and returns the result. + Vector2 Vector2::operator*(float scalar) const { + return {x * scalar, y * scalar}; + } + + // Multiplies this vector by another vector element-wise and updates this vector. + Vector2& Vector2::operator*=(const Vector2& other) { + x *= other.x; + y *= other.y; + return *this; + } + + // Adds another vector to this vector and updates this vector. + Vector2& Vector2::operator+=(const Vector2& other) { + x += other.x; + y += other.y; + return *this; + } + + // Adds a scalar value to both components of this vector and updates this vector. + Vector2& Vector2::operator+=(float other) { + x += other; + y += other; + return *this; + } + + // Returns the negation of this vector. + Vector2 Vector2::operator-() const { + return {-x, -y}; + } + + // Checks if this vector is equal to another vector. + bool Vector2::operator==(const Vector2& other) const { + return x == other.x && y == other.y; + } + + // Checks if this vector is not equal to another vector. + bool Vector2::operator!=(const Vector2& other) const { + return !(*this == other); + } + +} // namespace crepe diff --git a/src/crepe/api/Vector2.h b/src/crepe/api/Vector2.h new file mode 100644 index 0000000..d571209 --- /dev/null +++ b/src/crepe/api/Vector2.h @@ -0,0 +1,48 @@ +#pragma once + +namespace crepe { + + //! Vector2 class + class Vector2 { + public: + //! X component of the vector + float x; + //! Y component of the vector + float y; + + //! Default constructor + Vector2() = default; + + //! Constructor with initial values + Vector2(float x, float y); + + //! Subtracts another vector from this vector and returns the result. + Vector2 operator-(const Vector2& other) const; + + //! Adds another vector to this vector and returns the result. + Vector2 operator+(const Vector2& other) const; + + //! Multiplies this vector by a scalar and returns the result. + Vector2 operator*(float scalar) const; + + //! Multiplies this vector by another vector element-wise and updates this vector. + Vector2& operator*=(const Vector2& other); + + //! Adds another vector to this vector and updates this vector. + Vector2& operator+=(const Vector2& other); + + //! Adds a scalar value to both components of this vector and updates this vector. + Vector2& operator+=(float other); + + //! Returns the negation of this vector. + Vector2 operator-() const; + + //! Checks if this vector is equal to another vector. + bool operator==(const Vector2& other) const; + + //! Checks if this vector is not equal to another vector. + bool operator!=(const Vector2& other) const; + + }; + +} // namespace crepe diff --git a/src/crepe/system/PhysicsSystem.cpp b/src/crepe/system/PhysicsSystem.cpp index cea8062..1a323ee 100644 --- a/src/crepe/system/PhysicsSystem.cpp +++ b/src/crepe/system/PhysicsSystem.cpp @@ -1,16 +1,15 @@ -#include +#include #include "../ComponentManager.h" -#include "../api/Force.h" #include "../api/Rigidbody.h" #include "../api/Transform.h" +#include "../api/Vector2.h" +#include "../api/Config.h" #include "PhysicsSystem.h" using namespace crepe; -PhysicsSystem::PhysicsSystem() {} - void PhysicsSystem::update() { ComponentManager & mgr = ComponentManager::get_instance(); std::vector> rigidbodies @@ -18,41 +17,80 @@ void PhysicsSystem::update() { std::vector> transforms = mgr.get_components_by_type(); + double gravity = Config::get_instance().physics.gravity; for (Rigidbody & rigidbody : rigidbodies) { - - switch (rigidbody.body_type) { - case BodyType::DYNAMIC: + if(!rigidbody.active){continue;} + switch (rigidbody.data.body_type) { + case Rigidbody::BodyType::DYNAMIC: for (Transform & transform : transforms) { if (transform.game_object_id == rigidbody.game_object_id) { - rigidbody.velocity_x = 0; - rigidbody.velocity_y = 0; - std::vector> forces - = mgr.get_components_by_id( - rigidbody.game_object_id); - rigidbody.velocity_y - += rigidbody.gravity_scale * 1 * rigidbody.mass; - - for (Force & force : forces) { - rigidbody.velocity_x += force.force_x; - rigidbody.velocity_y += force.force_y; - } - - std::cout << "before transform.postion.x " - << transform.position.x << std::endl; - std::cout << "before transform.postion.y " - << transform.position.y << std::endl; - transform.position.x += rigidbody.velocity_x; - transform.position.y += rigidbody.velocity_y; - std::cout << "after transform.postion.x " - << transform.position.x << std::endl; - std::cout << "after transform.postion.y " - << transform.position.y << std::endl; + + // Add gravity + if(rigidbody.data.use_gravity) + { + rigidbody.data.linear_velocity.y += (rigidbody.data.mass * rigidbody.data.gravity_scale * gravity); + } + // Add damping + if(rigidbody.data.angular_damping != 0) + { + rigidbody.data.angular_velocity *= rigidbody.data.angular_damping; + } + if(rigidbody.data.linear_damping != Vector2{0,0}) + { + rigidbody.data.linear_velocity *= rigidbody.data.linear_damping; + } + + // Max velocity check + if(rigidbody.data.angular_velocity > rigidbody.data.max_angular_velocity) + { + rigidbody.data.angular_velocity = rigidbody.data.max_angular_velocity; + } + else if (rigidbody.data.angular_velocity < -rigidbody.data.max_angular_velocity) + { + rigidbody.data.angular_velocity = -rigidbody.data.max_angular_velocity; + } + + if(rigidbody.data.linear_velocity.x > rigidbody.data.max_linear_velocity.x) + { + rigidbody.data.linear_velocity.x = rigidbody.data.max_linear_velocity.x; + } + else if(rigidbody.data.linear_velocity.x < -rigidbody.data.max_linear_velocity.x) + { + rigidbody.data.linear_velocity.x = -rigidbody.data.max_linear_velocity.x; + } + + if(rigidbody.data.linear_velocity.y > rigidbody.data.max_linear_velocity.y) + { + rigidbody.data.linear_velocity.y = rigidbody.data.max_linear_velocity.y; + } + else if(rigidbody.data.linear_velocity.y < -rigidbody.data.max_linear_velocity.y) + { + rigidbody.data.linear_velocity.y = -rigidbody.data.max_linear_velocity.y; + } + + // Move object + if(!rigidbody.data.constraints.rotation) + { + transform.rotation += rigidbody.data.angular_velocity; + transform.rotation = std::fmod(transform.rotation, 360.0); + if (transform.rotation < 0) { + transform.rotation += 360.0; + } + } + if(!rigidbody.data.constraints.x) + { + transform.position.x += rigidbody.data.linear_velocity.x; + } + if(!rigidbody.data.constraints.y) + { + transform.position.y += rigidbody.data.linear_velocity.y; + } } } break; - case BodyType::KINEMATIC: + case Rigidbody::BodyType::KINEMATIC: break; //(scripts) - case BodyType::STATIC: + case Rigidbody::BodyType::STATIC: break; //(unmoveable objects) default: break; diff --git a/src/crepe/system/PhysicsSystem.h b/src/crepe/system/PhysicsSystem.h index 33b4072..cc13b70 100644 --- a/src/crepe/system/PhysicsSystem.h +++ b/src/crepe/system/PhysicsSystem.h @@ -1,10 +1,23 @@ #pragma once namespace crepe { - +/** + * \brief System that controls all physics + * + * This class is a physics system that uses a rigidbody and transform + * to add physics to a game object. + */ class PhysicsSystem { public: - PhysicsSystem(); + /** + * Constructor is default + */ + PhysicsSystem() = default; + /** + * \brief updates the physics system. + * + * It calculates new velocties and changes the postion in the transform. + */ void update(); }; diff --git a/src/example/ecs.cpp b/src/example/ecs.cpp index 0c64373..e61c398 100644 --- a/src/example/ecs.cpp +++ b/src/example/ecs.cpp @@ -11,11 +11,11 @@ using namespace std; int main() { // Create a few GameObjects try { - GameObject body(0, "body", "person", Point{0, 0}, 0, 1); - GameObject right_leg(1, "rightLeg", "person", Point{1, 1}, 0, 1); - GameObject left_leg(2, "leftLeg", "person", Point{1, 1}, 0, 1); - GameObject right_foot(3, "rightFoot", "person", Point{2, 2}, 0, 1); - GameObject left_foot(4, "leftFoot", "person", Point{2, 2}, 0, 1); + GameObject body(0, "body", "person", Vector2{0, 0}, 0, 1); + GameObject right_leg(1, "rightLeg", "person", Vector2{1, 1}, 0, 1); + GameObject left_leg(2, "leftLeg", "person", Vector2{1, 1}, 0, 1); + GameObject right_foot(3, "rightFoot", "person", Vector2{2, 2}, 0, 1); + GameObject left_foot(4, "leftFoot", "person", Vector2{2, 2}, 0, 1); // Set the parent of each GameObject right_foot.set_parent(right_leg); @@ -24,7 +24,7 @@ int main() { left_leg.set_parent(body); // Adding a second Transform component is not allowed and will invoke an exception - body.add_component(Point{10, 10}, 0, 1); + body.add_component(Vector2{10, 10}, 0, 1); } catch (const exception & e) { cerr << e.what() << endl; } diff --git a/src/example/particle.cpp b/src/example/particle.cpp index 607530d..0ab5632 100644 --- a/src/example/particle.cpp +++ b/src/example/particle.cpp @@ -7,7 +7,7 @@ #include #include #include -#include +#include #include #include @@ -26,7 +26,7 @@ int main(int argc, char * argv[]) { } GameObject * game_object[1]; - game_object[0] = new GameObject(0, "Name", "Tag", Point{0, 0}, 0, 1); + game_object[0] = new GameObject(0, "Name", "Tag", Vector2{0, 0}, 0, 1); // FIXME: all systems are singletons, so this shouldn't even compile. ParticleSystem particle_system; diff --git a/src/example/physics.cpp b/src/example/physics.cpp index c7db6ac..fd15c79 100644 --- a/src/example/physics.cpp +++ b/src/example/physics.cpp @@ -1,10 +1,5 @@ -#include -#include -#include - #include #include -#include #include #include #include @@ -14,13 +9,15 @@ using namespace crepe; using namespace std; int main(int argc, char * argv[]) { - PhysicsSystem physics_system; - GameObject * game_object[2]; - // not found not used - game_object[1] = new GameObject(2, "Name", "Tag", Point{0, 0}, 0, 0); - game_object[0] = new GameObject(5, "Name", "Tag", Point{0, 0}, 0, 0); - game_object[0]->add_component(1, 1, BodyType::DYNAMIC); - game_object[0]->add_component(1, 0); - physics_system.update(); + GameObject *game_object; + game_object = new GameObject(0, "Name", "Tag", Vector2{0,0},0,0); + game_object->add_component(Rigidbody::RigidbodyData{ + .mass = 1, + .gravity_scale = 1, + .body_type = Rigidbody::BodyType::DYNAMIC, + .constraints = {0,0,0}, + .use_gravity = true, + .bounce = false, + }); return 0; } diff --git a/src/example/rendering.cpp b/src/example/rendering.cpp index 3fe43d6..e1ff9da 100644 --- a/src/example/rendering.cpp +++ b/src/example/rendering.cpp @@ -5,7 +5,7 @@ #include #include -#include +#include #include #include #include @@ -19,9 +19,9 @@ using namespace crepe; int main() { dbg_trace(); - auto obj = GameObject(0, "name", "tag", Point{0, 0}, 1, 1); - auto obj1 = GameObject(1, "name", "tag", Point{500, 0}, 1, 0.1); - auto obj2 = GameObject(2, "name", "tag", Point{800, 0}, 1, 0.1); + auto obj = GameObject(0, "name", "tag", Vector2{0, 0}, 1, 1); + auto obj1 = GameObject(1, "name", "tag", Vector2{500, 0}, 1, 0.1); + auto obj2 = GameObject(2, "name", "tag", Vector2{800, 0}, 1, 0.1); auto & mgr = AssetManager::get_instance(); // Normal adding components diff --git a/src/example/scene_manager.cpp b/src/example/scene_manager.cpp index efbf2c2..1780c81 100644 --- a/src/example/scene_manager.cpp +++ b/src/example/scene_manager.cpp @@ -3,7 +3,7 @@ #include #include #include -#include +#include #include #include @@ -15,9 +15,9 @@ public: ConcreteScene1(string name) : Scene(name) {} void load_scene() { - GameObject object1(0, "scene_1", "tag_scene_1", Point{0, 0}, 0, 1); - GameObject object2(1, "scene_1", "tag_scene_1", Point{1, 0}, 0, 1); - GameObject object3(2, "scene_1", "tag_scene_1", Point{2, 0}, 0, 1); + GameObject object1(0, "scene_1", "tag_scene_1", Vector2{0, 0}, 0, 1); + GameObject object2(1, "scene_1", "tag_scene_1", Vector2{1, 0}, 0, 1); + GameObject object3(2, "scene_1", "tag_scene_1", Vector2{2, 0}, 0, 1); } }; @@ -26,10 +26,10 @@ public: ConcreteScene2(string name) : Scene(name) {} void load_scene() { - GameObject object1(0, "scene_2", "tag_scene_2", Point{0, 0}, 0, 1); - GameObject object2(1, "scene_2", "tag_scene_2", Point{0, 1}, 0, 1); - GameObject object3(2, "scene_2", "tag_scene_2", Point{0, 2}, 0, 1); - GameObject object4(3, "scene_2", "tag_scene_2", Point{0, 3}, 0, 1); + GameObject object1(0, "scene_2", "tag_scene_2", Vector2{0, 0}, 0, 1); + GameObject object2(1, "scene_2", "tag_scene_2", Vector2{0, 1}, 0, 1); + GameObject object3(2, "scene_2", "tag_scene_2", Vector2{0, 2}, 0, 1); + GameObject object4(3, "scene_2", "tag_scene_2", Vector2{0, 3}, 0, 1); } }; diff --git a/src/example/script.cpp b/src/example/script.cpp index 8ca4ceb..9e8b147 100644 --- a/src/example/script.cpp +++ b/src/example/script.cpp @@ -36,7 +36,7 @@ class MyScript : public Script { int main() { // Create game object with Transform and BehaviorScript components - auto obj = GameObject(0, "name", "tag", Point{1.2, 3.4}, 0, 1); + auto obj = GameObject(0, "name", "tag", Vector2{1.2, 3.4}, 0, 1); obj.add_component().set_script(); // Get ScriptSystem singleton instance (this would normally be done from the diff --git a/src/makefile b/src/makefile index be1548c..2251119 100644 --- a/src/makefile +++ b/src/makefile @@ -41,16 +41,14 @@ TODO += crepe/api/CircleCollider.h TODO += crepe/api/Color.cpp TODO += crepe/api/Color.h LOEK += crepe/api/Config.h -TODO += crepe/api/Force.cpp -TODO += crepe/api/Force.h MAX += crepe/api/GameObject.cpp MAX += crepe/api/GameObject.h MAX += crepe/api/GameObject.hpp TODO += crepe/api/ParticleEmitter.cpp TODO += crepe/api/ParticleEmitter.h -TODO += crepe/api/Point.h -TODO += crepe/api/Rigidbody.cpp -TODO += crepe/api/Rigidbody.h +TODO += crepe/api/Vector2.h +JARO += crepe/api/Rigidbody.cpp +JARO += crepe/api/Rigidbody.h LOEK += crepe/api/Script.cpp LOEK += crepe/api/Script.h LOEK += crepe/api/Script.hpp @@ -60,8 +58,6 @@ TODO += crepe/api/Texture.cpp TODO += crepe/api/Texture.h MAX += crepe/api/Transform.cpp MAX += crepe/api/Transform.h -TODO += crepe/facade/SDLApp.cpp -TODO += crepe/facade/SDLApp.h TODO += crepe/facade/SDLContext.cpp TODO += crepe/facade/SDLContext.h LOEK += crepe/facade/Sound.cpp @@ -72,8 +68,8 @@ TODO += crepe/system/CollisionSystem.cpp TODO += crepe/system/CollisionSystem.h TODO += crepe/system/ParticleSystem.cpp TODO += crepe/system/ParticleSystem.h -TODO += crepe/system/PhysicsSystem.cpp -TODO += crepe/system/PhysicsSystem.h +JARO += crepe/system/PhysicsSystem.cpp +JARO += crepe/system/PhysicsSystem.h TODO += crepe/system/RenderSystem.cpp TODO += crepe/system/RenderSystem.h LOEK += crepe/system/ScriptSystem.cpp @@ -91,13 +87,14 @@ TODO += example/components_internal.cpp MAX += example/ecs.cpp LOEK += example/log.cpp TODO += example/particle.cpp -TODO += example/physics.cpp +JARO += example/physics.cpp TODO += example/rendering.cpp LOEK += example/script.cpp LOEK += test/audio.cpp LOEK += test/dummy.cpp +JARO += test/PhysicsTest.cpp -FMT := $(MAX) #<<< CHANGE THIS TO YOUR NAME FOR STEP 2 +FMT := $(JARO) #<<< CHANGE THIS TO YOUR NAME FOR STEP 2 format: FORCE clang-tidy -p build/compile_commands.json --fix-errors $(FMT) diff --git a/src/test/CMakeLists.txt b/src/test/CMakeLists.txt index 0d316d6..0e4eaed 100644 --- a/src/test/CMakeLists.txt +++ b/src/test/CMakeLists.txt @@ -1,5 +1,6 @@ target_sources(test_main PUBLIC dummy.cpp # audio.cpp + PhysicsTest.cpp ) diff --git a/src/test/PhysicsTest.cpp b/src/test/PhysicsTest.cpp new file mode 100644 index 0000000..5ad5d01 --- /dev/null +++ b/src/test/PhysicsTest.cpp @@ -0,0 +1,115 @@ +#include +#include +#include +#include +#include +#include +#include + +using namespace std; +using namespace std::chrono_literals; +using namespace crepe; + +class PhysicsTest : public ::testing::Test { +protected: + GameObject* game_object; + PhysicsSystem physics_system; + void SetUp() override { + ComponentManager & mgr = ComponentManager::get_instance(); + std::vector> transforms = mgr.get_components_by_id(0); + if (transforms.empty()) { + game_object = new GameObject(0,"","",Vector2{0,0},0,0); + game_object->add_component(Rigidbody::RigidbodyData{ + .mass = 1, + .gravity_scale = 1, + .body_type = Rigidbody::BodyType::DYNAMIC, + .max_linear_velocity = Vector2{10,10}, + .max_angular_velocity = 10, + .constraints = {0, 0}, + .use_gravity = true, + .bounce = false + }); + } + transforms = mgr.get_components_by_id(0); + Transform& transform = transforms.front().get(); + transform.position.x = 0.0; + transform.position.y = 0.0; + transform.rotation = 0.0; + std::vector> rigidbodies = mgr.get_components_by_id(0); + Rigidbody& rigidbody = rigidbodies.front().get(); + rigidbody.data.angular_velocity = 0; + rigidbody.data.linear_velocity.x = 0; + rigidbody.data.linear_velocity.y = 0; + } +}; + +TEST_F(PhysicsTest, gravity) { + Config::get_instance().physics.gravity = 1; + ComponentManager & mgr = ComponentManager::get_instance(); + std::vector> transforms = mgr.get_components_by_id(0); + const Transform& transform = transforms.front().get(); + ASSERT_FALSE(transforms.empty()); + EXPECT_EQ(transform.position.y, 0); + physics_system.update(); + EXPECT_EQ(transform.position.y, 1); + physics_system.update(); + EXPECT_EQ(transform.position.y, 3); +} + +TEST_F(PhysicsTest, max_velocity) { + ComponentManager & mgr = ComponentManager::get_instance(); + std::vector> rigidbodies = mgr.get_components_by_id(0); + Rigidbody& rigidbody = rigidbodies.front().get(); + ASSERT_FALSE(rigidbodies.empty()); + EXPECT_EQ(rigidbody.data.linear_velocity.y, 0); + rigidbody.add_force_linear({100,100}); + rigidbody.add_force_angular(100); + physics_system.update(); + EXPECT_EQ(rigidbody.data.linear_velocity.y, 10); + EXPECT_EQ(rigidbody.data.linear_velocity.x, 10); + EXPECT_EQ(rigidbody.data.angular_velocity, 10); + rigidbody.add_force_linear({-100,-100}); + rigidbody.add_force_angular(-100); + physics_system.update(); + EXPECT_EQ(rigidbody.data.linear_velocity.y, -10); + EXPECT_EQ(rigidbody.data.linear_velocity.x, -10); + EXPECT_EQ(rigidbody.data.angular_velocity, -10); +} + +TEST_F(PhysicsTest, movement) { + Config::get_instance().physics.gravity = 0; + ComponentManager & mgr = ComponentManager::get_instance(); + std::vector> rigidbodies = mgr.get_components_by_id(0); + Rigidbody& rigidbody = rigidbodies.front().get(); + std::vector> transforms = mgr.get_components_by_id(0); + const Transform& transform = transforms.front().get(); + ASSERT_FALSE(rigidbodies.empty()); + ASSERT_FALSE(transforms.empty()); + rigidbody.add_force_linear({1,1}); + rigidbody.add_force_angular(1); + physics_system.update(); + EXPECT_EQ(transform.position.x, 1); + EXPECT_EQ(transform.position.y, 1); + EXPECT_EQ(transform.rotation, 1); + rigidbody.data.constraints = {1,1,1}; + EXPECT_EQ(transform.position.x, 1); + EXPECT_EQ(transform.position.y, 1); + EXPECT_EQ(transform.rotation, 1); + rigidbody.data.linear_damping.x = 0.5; + rigidbody.data.linear_damping.y = 0.5; + rigidbody.data.angular_damping = 0.5; + physics_system.update(); + EXPECT_EQ(rigidbody.data.linear_velocity.x, 0.5); + EXPECT_EQ(rigidbody.data.linear_velocity.y, 0.5); + EXPECT_EQ(rigidbody.data.angular_velocity, 0.5); + rigidbody.data.constraints = {1,1,0}; + rigidbody.data.angular_damping = 0; + rigidbody.data.max_angular_velocity = 1000; + rigidbody.data.angular_velocity = 360; + physics_system.update(); + EXPECT_EQ(transform.rotation, 1); + rigidbody.data.angular_velocity = -360; + physics_system.update(); + EXPECT_EQ(transform.rotation, 1); +} + -- cgit v1.2.3 From f49c93a5dfc0aeb5c0087cc9adfeb68526a2b0a7 Mon Sep 17 00:00:00 2001 From: max-001 Date: Thu, 7 Nov 2024 19:24:24 +0100 Subject: Make format --- src/crepe/api/GameObject.h | 6 +-- src/crepe/api/ParticleEmitter.cpp | 17 +++--- src/crepe/api/Rigidbody.cpp | 6 +-- src/crepe/api/Rigidbody.h | 13 +++-- src/crepe/api/Transform.h | 3 +- src/crepe/api/Vector2.cpp | 104 ++++++++++++++++++------------------- src/crepe/api/Vector2.h | 61 +++++++++++----------- src/crepe/system/PhysicsSystem.cpp | 96 ++++++++++++++++++---------------- src/example/physics.cpp | 8 +-- src/example/rendering.cpp | 2 +- src/example/scene_manager.cpp | 2 +- src/test/PhysicsTest.cpp | 94 +++++++++++++++++---------------- 12 files changed, 210 insertions(+), 202 deletions(-) (limited to 'src/example/rendering.cpp') diff --git a/src/crepe/api/GameObject.h b/src/crepe/api/GameObject.h index 8dc102c..d703730 100644 --- a/src/crepe/api/GameObject.h +++ b/src/crepe/api/GameObject.h @@ -1,6 +1,5 @@ #pragma once -#include #include #include "types.h" @@ -29,8 +28,9 @@ public: * \param rotation The rotation of the GameObject * \param scale The scale of the GameObject */ - GameObject(game_object_id_t id, const std::string & name, const std::string & tag, - const Vector2 & position, double rotation, double scale); + GameObject(game_object_id_t id, const std::string & name, + const std::string & tag, const Vector2 & position, + double rotation, double scale); /** * \brief Set the parent of this GameObject * diff --git a/src/crepe/api/ParticleEmitter.cpp b/src/crepe/api/ParticleEmitter.cpp index 6094732..3b2e2f2 100644 --- a/src/crepe/api/ParticleEmitter.cpp +++ b/src/crepe/api/ParticleEmitter.cpp @@ -6,15 +6,14 @@ using namespace crepe; -ParticleEmitter::ParticleEmitter(game_object_id_t id, - uint32_t max_particles, uint32_t emission_rate, - uint32_t speed, uint32_t speed_offset, - uint32_t angle, uint32_t angleOffset, - float begin_lifespan, float end_lifespan) - : Component(id), max_particles(max_particles), - emission_rate(emission_rate), speed(speed), speed_offset(speed_offset), - position{0, 0}, begin_lifespan(begin_lifespan), - end_lifespan(end_lifespan) { +ParticleEmitter::ParticleEmitter(game_object_id_t id, uint32_t max_particles, + uint32_t emission_rate, uint32_t speed, + uint32_t speed_offset, uint32_t angle, + uint32_t angleOffset, float begin_lifespan, + float end_lifespan) + : Component(id), max_particles(max_particles), emission_rate(emission_rate), + speed(speed), speed_offset(speed_offset), position{0, 0}, + begin_lifespan(begin_lifespan), end_lifespan(end_lifespan) { std::srand( static_cast(std::time(nullptr))); // initialize random seed std::cout << "Create emitter" << std::endl; diff --git a/src/crepe/api/Rigidbody.cpp b/src/crepe/api/Rigidbody.cpp index cf07b0e..0de9846 100644 --- a/src/crepe/api/Rigidbody.cpp +++ b/src/crepe/api/Rigidbody.cpp @@ -2,9 +2,8 @@ using namespace crepe; -crepe::Rigidbody::Rigidbody(uint32_t game_object_id, - const RigidbodyData & data) : - Component(game_object_id), data(data){} +crepe::Rigidbody::Rigidbody(uint32_t game_object_id, const RigidbodyData & data) + : Component(game_object_id), data(data) {} void crepe::Rigidbody::add_force_linear(const Vector2 & force) { this->data.linear_velocity += force; @@ -13,4 +12,3 @@ void crepe::Rigidbody::add_force_linear(const Vector2 & force) { void crepe::Rigidbody::add_force_angular(double force) { this->data.angular_velocity += force; } - diff --git a/src/crepe/api/Rigidbody.h b/src/crepe/api/Rigidbody.h index 0c069d8..e1abd46 100644 --- a/src/crepe/api/Rigidbody.h +++ b/src/crepe/api/Rigidbody.h @@ -37,17 +37,18 @@ public: */ struct PhysicsConstraints { //! X constraint - bool x = 0; + bool x = 0; //! Y constraint - bool y = 0; + bool y = 0; //! rotation constraint - bool rotation = 0; + bool rotation = 0; }; + public: /** * This struct holds the data for the Rigidbody. */ - struct RigidbodyData{ + struct RigidbodyData { //! objects mass double mass = 0.0; //! gravtiy scale @@ -73,14 +74,16 @@ public: //! if object bounces bool bounce = false; }; + public: /** * \param game_object_id id of the gameobject the rigibody is added to. * \param data struct to configure the rigidbody. */ - Rigidbody(uint32_t game_object_id,const RigidbodyData& data); + Rigidbody(uint32_t game_object_id, const RigidbodyData & data); //! struct to hold data of rigidbody RigidbodyData data; + public: /** * \brief add a linear force to the Rigidbody. diff --git a/src/crepe/api/Transform.h b/src/crepe/api/Transform.h index 33e70f9..805553e 100644 --- a/src/crepe/api/Transform.h +++ b/src/crepe/api/Transform.h @@ -20,7 +20,8 @@ public: * \param rot The rotation of the GameObject * \param scale The scale of the GameObject */ - Transform(game_object_id_t id, const Vector2 & point, double rot, double scale); + Transform(game_object_id_t id, const Vector2 & point, double rot, + double scale); /** * \brief Get the maximum number of instances for this component * diff --git a/src/crepe/api/Vector2.cpp b/src/crepe/api/Vector2.cpp index 8cbd9ad..09bb59b 100644 --- a/src/crepe/api/Vector2.cpp +++ b/src/crepe/api/Vector2.cpp @@ -2,58 +2,56 @@ namespace crepe { - // Constructor with initial values - Vector2::Vector2(float x, float y) : x(x), y(y) {} - - // Subtracts another vector from this vector and returns the result. - Vector2 Vector2::operator-(const Vector2& other) const { - return {x - other.x, y - other.y}; - } - - // Adds another vector to this vector and returns the result. - Vector2 Vector2::operator+(const Vector2& other) const { - return {x + other.x, y + other.y}; - } - - // Multiplies this vector by a scalar and returns the result. - Vector2 Vector2::operator*(float scalar) const { - return {x * scalar, y * scalar}; - } - - // Multiplies this vector by another vector element-wise and updates this vector. - Vector2& Vector2::operator*=(const Vector2& other) { - x *= other.x; - y *= other.y; - return *this; - } - - // Adds another vector to this vector and updates this vector. - Vector2& Vector2::operator+=(const Vector2& other) { - x += other.x; - y += other.y; - return *this; - } - - // Adds a scalar value to both components of this vector and updates this vector. - Vector2& Vector2::operator+=(float other) { - x += other; - y += other; - return *this; - } - - // Returns the negation of this vector. - Vector2 Vector2::operator-() const { - return {-x, -y}; - } - - // Checks if this vector is equal to another vector. - bool Vector2::operator==(const Vector2& other) const { - return x == other.x && y == other.y; - } - - // Checks if this vector is not equal to another vector. - bool Vector2::operator!=(const Vector2& other) const { - return !(*this == other); - } +// Constructor with initial values +Vector2::Vector2(float x, float y) : x(x), y(y) {} + +// Subtracts another vector from this vector and returns the result. +Vector2 Vector2::operator-(const Vector2 & other) const { + return {x - other.x, y - other.y}; +} + +// Adds another vector to this vector and returns the result. +Vector2 Vector2::operator+(const Vector2 & other) const { + return {x + other.x, y + other.y}; +} + +// Multiplies this vector by a scalar and returns the result. +Vector2 Vector2::operator*(float scalar) const { + return {x * scalar, y * scalar}; +} + +// Multiplies this vector by another vector element-wise and updates this vector. +Vector2 & Vector2::operator*=(const Vector2 & other) { + x *= other.x; + y *= other.y; + return *this; +} + +// Adds another vector to this vector and updates this vector. +Vector2 & Vector2::operator+=(const Vector2 & other) { + x += other.x; + y += other.y; + return *this; +} + +// Adds a scalar value to both components of this vector and updates this vector. +Vector2 & Vector2::operator+=(float other) { + x += other; + y += other; + return *this; +} + +// Returns the negation of this vector. +Vector2 Vector2::operator-() const { return {-x, -y}; } + +// Checks if this vector is equal to another vector. +bool Vector2::operator==(const Vector2 & other) const { + return x == other.x && y == other.y; +} + +// Checks if this vector is not equal to another vector. +bool Vector2::operator!=(const Vector2 & other) const { + return !(*this == other); +} } // namespace crepe diff --git a/src/crepe/api/Vector2.h b/src/crepe/api/Vector2.h index d571209..741951b 100644 --- a/src/crepe/api/Vector2.h +++ b/src/crepe/api/Vector2.h @@ -2,47 +2,46 @@ namespace crepe { - //! Vector2 class - class Vector2 { - public: - //! X component of the vector - float x; - //! Y component of the vector - float y; +//! Vector2 class +class Vector2 { +public: + //! X component of the vector + float x; + //! Y component of the vector + float y; - //! Default constructor - Vector2() = default; + //! Default constructor + Vector2() = default; - //! Constructor with initial values - Vector2(float x, float y); + //! Constructor with initial values + Vector2(float x, float y); - //! Subtracts another vector from this vector and returns the result. - Vector2 operator-(const Vector2& other) const; + //! Subtracts another vector from this vector and returns the result. + Vector2 operator-(const Vector2 & other) const; - //! Adds another vector to this vector and returns the result. - Vector2 operator+(const Vector2& other) const; + //! Adds another vector to this vector and returns the result. + Vector2 operator+(const Vector2 & other) const; - //! Multiplies this vector by a scalar and returns the result. - Vector2 operator*(float scalar) const; + //! Multiplies this vector by a scalar and returns the result. + Vector2 operator*(float scalar) const; - //! Multiplies this vector by another vector element-wise and updates this vector. - Vector2& operator*=(const Vector2& other); + //! Multiplies this vector by another vector element-wise and updates this vector. + Vector2 & operator*=(const Vector2 & other); - //! Adds another vector to this vector and updates this vector. - Vector2& operator+=(const Vector2& other); + //! Adds another vector to this vector and updates this vector. + Vector2 & operator+=(const Vector2 & other); - //! Adds a scalar value to both components of this vector and updates this vector. - Vector2& operator+=(float other); + //! Adds a scalar value to both components of this vector and updates this vector. + Vector2 & operator+=(float other); - //! Returns the negation of this vector. - Vector2 operator-() const; + //! Returns the negation of this vector. + Vector2 operator-() const; - //! Checks if this vector is equal to another vector. - bool operator==(const Vector2& other) const; + //! Checks if this vector is equal to another vector. + bool operator==(const Vector2 & other) const; - //! Checks if this vector is not equal to another vector. - bool operator!=(const Vector2& other) const; - - }; + //! Checks if this vector is not equal to another vector. + bool operator!=(const Vector2 & other) const; +}; } // namespace crepe diff --git a/src/crepe/system/PhysicsSystem.cpp b/src/crepe/system/PhysicsSystem.cpp index 1a323ee..1bd2171 100644 --- a/src/crepe/system/PhysicsSystem.cpp +++ b/src/crepe/system/PhysicsSystem.cpp @@ -1,10 +1,10 @@ #include #include "../ComponentManager.h" +#include "../api/Config.h" #include "../api/Rigidbody.h" #include "../api/Transform.h" #include "../api/Vector2.h" -#include "../api/Config.h" #include "PhysicsSystem.h" @@ -19,71 +19,77 @@ void PhysicsSystem::update() { double gravity = Config::get_instance().physics.gravity; for (Rigidbody & rigidbody : rigidbodies) { - if(!rigidbody.active){continue;} + if (!rigidbody.active) continue; + switch (rigidbody.data.body_type) { case Rigidbody::BodyType::DYNAMIC: for (Transform & transform : transforms) { if (transform.game_object_id == rigidbody.game_object_id) { - - // Add gravity - if(rigidbody.data.use_gravity) - { - rigidbody.data.linear_velocity.y += (rigidbody.data.mass * rigidbody.data.gravity_scale * gravity); + + // Add gravity + if (rigidbody.data.use_gravity) { + rigidbody.data.linear_velocity.y + += (rigidbody.data.mass + * rigidbody.data.gravity_scale * gravity); } // Add damping - if(rigidbody.data.angular_damping != 0) - { - rigidbody.data.angular_velocity *= rigidbody.data.angular_damping; + if (rigidbody.data.angular_damping != 0) { + rigidbody.data.angular_velocity + *= rigidbody.data.angular_damping; } - if(rigidbody.data.linear_damping != Vector2{0,0}) - { - rigidbody.data.linear_velocity *= rigidbody.data.linear_damping; + if (rigidbody.data.linear_damping != Vector2{0, 0}) { + rigidbody.data.linear_velocity + *= rigidbody.data.linear_damping; } // Max velocity check - if(rigidbody.data.angular_velocity > rigidbody.data.max_angular_velocity) - { - rigidbody.data.angular_velocity = rigidbody.data.max_angular_velocity; - } - else if (rigidbody.data.angular_velocity < -rigidbody.data.max_angular_velocity) - { - rigidbody.data.angular_velocity = -rigidbody.data.max_angular_velocity; + if (rigidbody.data.angular_velocity + > rigidbody.data.max_angular_velocity) { + rigidbody.data.angular_velocity + = rigidbody.data.max_angular_velocity; + } else if (rigidbody.data.angular_velocity + < -rigidbody.data.max_angular_velocity) { + rigidbody.data.angular_velocity + = -rigidbody.data.max_angular_velocity; } - if(rigidbody.data.linear_velocity.x > rigidbody.data.max_linear_velocity.x) - { - rigidbody.data.linear_velocity.x = rigidbody.data.max_linear_velocity.x; - } - else if(rigidbody.data.linear_velocity.x < -rigidbody.data.max_linear_velocity.x) - { - rigidbody.data.linear_velocity.x = -rigidbody.data.max_linear_velocity.x; + if (rigidbody.data.linear_velocity.x + > rigidbody.data.max_linear_velocity.x) { + rigidbody.data.linear_velocity.x + = rigidbody.data.max_linear_velocity.x; + } else if (rigidbody.data.linear_velocity.x + < -rigidbody.data.max_linear_velocity.x) { + rigidbody.data.linear_velocity.x + = -rigidbody.data.max_linear_velocity.x; } - if(rigidbody.data.linear_velocity.y > rigidbody.data.max_linear_velocity.y) - { - rigidbody.data.linear_velocity.y = rigidbody.data.max_linear_velocity.y; - } - else if(rigidbody.data.linear_velocity.y < -rigidbody.data.max_linear_velocity.y) - { - rigidbody.data.linear_velocity.y = -rigidbody.data.max_linear_velocity.y; + if (rigidbody.data.linear_velocity.y + > rigidbody.data.max_linear_velocity.y) { + rigidbody.data.linear_velocity.y + = rigidbody.data.max_linear_velocity.y; + } else if (rigidbody.data.linear_velocity.y + < -rigidbody.data.max_linear_velocity.y) { + rigidbody.data.linear_velocity.y + = -rigidbody.data.max_linear_velocity.y; } - // Move object - if(!rigidbody.data.constraints.rotation) - { - transform.rotation += rigidbody.data.angular_velocity; - transform.rotation = std::fmod(transform.rotation, 360.0); + // Move object + if (!rigidbody.data.constraints.rotation) { + transform.rotation + += rigidbody.data.angular_velocity; + transform.rotation + = std::fmod(transform.rotation, 360.0); if (transform.rotation < 0) { transform.rotation += 360.0; } } - if(!rigidbody.data.constraints.x) - { - transform.position.x += rigidbody.data.linear_velocity.x; + if (!rigidbody.data.constraints.x) { + transform.position.x + += rigidbody.data.linear_velocity.x; } - if(!rigidbody.data.constraints.y) - { - transform.position.y += rigidbody.data.linear_velocity.y; + if (!rigidbody.data.constraints.y) { + transform.position.y + += rigidbody.data.linear_velocity.y; } } } diff --git a/src/example/physics.cpp b/src/example/physics.cpp index 9a5e5f9..a7e94f6 100644 --- a/src/example/physics.cpp +++ b/src/example/physics.cpp @@ -9,16 +9,16 @@ using namespace crepe; using namespace std; int main(int argc, char * argv[]) { - GameObject *game_object; - game_object = new GameObject(0, "Name", "Tag", Vector2{0,0},0,0); + GameObject * game_object; + game_object = new GameObject(0, "Name", "Tag", Vector2{0, 0}, 0, 0); game_object->add_component(Rigidbody::RigidbodyData{ .mass = 1, .gravity_scale = 1, .body_type = Rigidbody::BodyType::DYNAMIC, - .constraints = {0,0,0}, + .constraints = {0, 0, 0}, .use_gravity = true, .bounce = false, - }); + }); delete game_object; return 0; } diff --git a/src/example/rendering.cpp b/src/example/rendering.cpp index e1ff9da..d554a8a 100644 --- a/src/example/rendering.cpp +++ b/src/example/rendering.cpp @@ -5,10 +5,10 @@ #include #include -#include #include #include #include +#include #include #include diff --git a/src/example/scene_manager.cpp b/src/example/scene_manager.cpp index 1780c81..f46dc36 100644 --- a/src/example/scene_manager.cpp +++ b/src/example/scene_manager.cpp @@ -3,9 +3,9 @@ #include #include #include -#include #include #include +#include using namespace crepe; using namespace std; diff --git a/src/test/PhysicsTest.cpp b/src/test/PhysicsTest.cpp index 5ad5d01..6b8c4d8 100644 --- a/src/test/PhysicsTest.cpp +++ b/src/test/PhysicsTest.cpp @@ -1,10 +1,10 @@ -#include #include +#include #include #include #include #include -#include +#include using namespace std; using namespace std::chrono_literals; @@ -12,42 +12,44 @@ using namespace crepe; class PhysicsTest : public ::testing::Test { protected: - GameObject* game_object; - PhysicsSystem physics_system; - void SetUp() override { - ComponentManager & mgr = ComponentManager::get_instance(); - std::vector> transforms = mgr.get_components_by_id(0); - if (transforms.empty()) { - game_object = new GameObject(0,"","",Vector2{0,0},0,0); - game_object->add_component(Rigidbody::RigidbodyData{ - .mass = 1, - .gravity_scale = 1, - .body_type = Rigidbody::BodyType::DYNAMIC, - .max_linear_velocity = Vector2{10,10}, - .max_angular_velocity = 10, - .constraints = {0, 0}, - .use_gravity = true, - .bounce = false - }); - } - transforms = mgr.get_components_by_id(0); - Transform& transform = transforms.front().get(); - transform.position.x = 0.0; - transform.position.y = 0.0; - transform.rotation = 0.0; - std::vector> rigidbodies = mgr.get_components_by_id(0); - Rigidbody& rigidbody = rigidbodies.front().get(); - rigidbody.data.angular_velocity = 0; - rigidbody.data.linear_velocity.x = 0; - rigidbody.data.linear_velocity.y = 0; - } + GameObject * game_object; + PhysicsSystem physics_system; + void SetUp() override { + ComponentManager & mgr = ComponentManager::get_instance(); + std::vector> transforms + = mgr.get_components_by_id(0); + if (transforms.empty()) { + game_object = new GameObject(0, "", "", Vector2{0, 0}, 0, 0); + game_object->add_component(Rigidbody::RigidbodyData{ + .mass = 1, + .gravity_scale = 1, + .body_type = Rigidbody::BodyType::DYNAMIC, + .max_linear_velocity = Vector2{10, 10}, + .max_angular_velocity = 10, + .constraints = {0, 0}, + .use_gravity = true, + .bounce = false}); + } + transforms = mgr.get_components_by_id(0); + Transform & transform = transforms.front().get(); + transform.position.x = 0.0; + transform.position.y = 0.0; + transform.rotation = 0.0; + std::vector> rigidbodies + = mgr.get_components_by_id(0); + Rigidbody & rigidbody = rigidbodies.front().get(); + rigidbody.data.angular_velocity = 0; + rigidbody.data.linear_velocity.x = 0; + rigidbody.data.linear_velocity.y = 0; + } }; TEST_F(PhysicsTest, gravity) { Config::get_instance().physics.gravity = 1; ComponentManager & mgr = ComponentManager::get_instance(); - std::vector> transforms = mgr.get_components_by_id(0); - const Transform& transform = transforms.front().get(); + std::vector> transforms + = mgr.get_components_by_id(0); + const Transform & transform = transforms.front().get(); ASSERT_FALSE(transforms.empty()); EXPECT_EQ(transform.position.y, 0); physics_system.update(); @@ -58,17 +60,18 @@ TEST_F(PhysicsTest, gravity) { TEST_F(PhysicsTest, max_velocity) { ComponentManager & mgr = ComponentManager::get_instance(); - std::vector> rigidbodies = mgr.get_components_by_id(0); - Rigidbody& rigidbody = rigidbodies.front().get(); + std::vector> rigidbodies + = mgr.get_components_by_id(0); + Rigidbody & rigidbody = rigidbodies.front().get(); ASSERT_FALSE(rigidbodies.empty()); EXPECT_EQ(rigidbody.data.linear_velocity.y, 0); - rigidbody.add_force_linear({100,100}); + rigidbody.add_force_linear({100, 100}); rigidbody.add_force_angular(100); physics_system.update(); EXPECT_EQ(rigidbody.data.linear_velocity.y, 10); EXPECT_EQ(rigidbody.data.linear_velocity.x, 10); EXPECT_EQ(rigidbody.data.angular_velocity, 10); - rigidbody.add_force_linear({-100,-100}); + rigidbody.add_force_linear({-100, -100}); rigidbody.add_force_angular(-100); physics_system.update(); EXPECT_EQ(rigidbody.data.linear_velocity.y, -10); @@ -79,19 +82,21 @@ TEST_F(PhysicsTest, max_velocity) { TEST_F(PhysicsTest, movement) { Config::get_instance().physics.gravity = 0; ComponentManager & mgr = ComponentManager::get_instance(); - std::vector> rigidbodies = mgr.get_components_by_id(0); - Rigidbody& rigidbody = rigidbodies.front().get(); - std::vector> transforms = mgr.get_components_by_id(0); - const Transform& transform = transforms.front().get(); + std::vector> rigidbodies + = mgr.get_components_by_id(0); + Rigidbody & rigidbody = rigidbodies.front().get(); + std::vector> transforms + = mgr.get_components_by_id(0); + const Transform & transform = transforms.front().get(); ASSERT_FALSE(rigidbodies.empty()); ASSERT_FALSE(transforms.empty()); - rigidbody.add_force_linear({1,1}); + rigidbody.add_force_linear({1, 1}); rigidbody.add_force_angular(1); physics_system.update(); EXPECT_EQ(transform.position.x, 1); EXPECT_EQ(transform.position.y, 1); EXPECT_EQ(transform.rotation, 1); - rigidbody.data.constraints = {1,1,1}; + rigidbody.data.constraints = {1, 1, 1}; EXPECT_EQ(transform.position.x, 1); EXPECT_EQ(transform.position.y, 1); EXPECT_EQ(transform.rotation, 1); @@ -102,7 +107,7 @@ TEST_F(PhysicsTest, movement) { EXPECT_EQ(rigidbody.data.linear_velocity.x, 0.5); EXPECT_EQ(rigidbody.data.linear_velocity.y, 0.5); EXPECT_EQ(rigidbody.data.angular_velocity, 0.5); - rigidbody.data.constraints = {1,1,0}; + rigidbody.data.constraints = {1, 1, 0}; rigidbody.data.angular_damping = 0; rigidbody.data.max_angular_velocity = 1000; rigidbody.data.angular_velocity = 360; @@ -112,4 +117,3 @@ TEST_F(PhysicsTest, movement) { physics_system.update(); EXPECT_EQ(transform.rotation, 1); } - -- cgit v1.2.3