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/test/CMakeLists.txt | 1 + src/test/PhysicsTest.cpp | 115 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 116 insertions(+) create mode 100644 src/test/PhysicsTest.cpp (limited to 'src/test') 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/test') 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 From 3d2428af8e8d9d49b4ade52d4806a7dae4cf1ab8 Mon Sep 17 00:00:00 2001 From: Loek Le Blansch Date: Thu, 7 Nov 2024 20:03:14 +0100 Subject: merge #25 + nitpicking --- contributing.md | 17 +++++++++++++++++ src/crepe/api/Rigidbody.cpp | 2 +- src/crepe/api/Rigidbody.h | 13 ++++++------- src/crepe/api/Sprite.cpp | 4 ++-- src/crepe/api/Sprite.h | 7 ++++--- src/crepe/api/Transform.cpp | 8 +++----- src/crepe/api/Transform.h | 4 ++-- src/crepe/system/PhysicsSystem.cpp | 2 +- src/crepe/types.h | 1 - src/example/physics.cpp | 2 +- src/test/PhysicsTest.cpp | 5 +++-- 11 files changed, 40 insertions(+), 25 deletions(-) (limited to 'src/test') diff --git a/contributing.md b/contributing.md index ba8ad68..8799057 100644 --- a/contributing.md +++ b/contributing.md @@ -635,6 +635,23 @@ that you can click on to open them. } ``` +-
+ Assigning booleans should be done with the + true/false literals instead of + 0/1 +
GoodBad
+ + ```cpp + bool foo = true; + bool bar = false; + ``` + + + ```cpp + bool foo = 1; + bool bar = 0; + ``` +
## CMakeLists-specific diff --git a/src/crepe/api/Rigidbody.cpp b/src/crepe/api/Rigidbody.cpp index 0de9846..cbf1325 100644 --- a/src/crepe/api/Rigidbody.cpp +++ b/src/crepe/api/Rigidbody.cpp @@ -2,7 +2,7 @@ using namespace crepe; -crepe::Rigidbody::Rigidbody(uint32_t game_object_id, const RigidbodyData & data) +crepe::Rigidbody::Rigidbody(uint32_t game_object_id, const Data & data) : Component(game_object_id), data(data) {} void crepe::Rigidbody::add_force_linear(const Vector2 & force) { diff --git a/src/crepe/api/Rigidbody.h b/src/crepe/api/Rigidbody.h index b12cf1d..68481f4 100644 --- a/src/crepe/api/Rigidbody.h +++ b/src/crepe/api/Rigidbody.h @@ -37,21 +37,20 @@ public: */ struct PhysicsConstraints { //! X constraint - bool x = 0; + bool x = false; //! Y constraint - bool y = 0; + bool y = false; //! rotation constraint - bool rotation = 0; + bool rotation = false; }; public: /** - * * \brief struct for Rigidbody data * * This struct holds the data for the Rigidbody. */ - struct RigidbodyData { + struct Data { //! objects mass double mass = 0.0; //! gravtiy scale @@ -83,9 +82,9 @@ 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 Data & data); //! struct to hold data of rigidbody - RigidbodyData data; + Data data; public: /** diff --git a/src/crepe/api/Sprite.cpp b/src/crepe/api/Sprite.cpp index 7e810b5..d3465c7 100644 --- a/src/crepe/api/Sprite.cpp +++ b/src/crepe/api/Sprite.cpp @@ -10,8 +10,8 @@ using namespace std; using namespace crepe; -Sprite::Sprite(game_object_id_t id, shared_ptr image, const Color & color, - const FlipSettings & flip) +Sprite::Sprite(game_object_id_t id, shared_ptr image, + const Color & color, const FlipSettings & flip) : Component(id), color(color), flip(flip), sprite_image(image) { dbg_trace(); } diff --git a/src/crepe/api/Sprite.h b/src/crepe/api/Sprite.h index 28078cd..00dcb27 100644 --- a/src/crepe/api/Sprite.h +++ b/src/crepe/api/Sprite.h @@ -12,14 +12,15 @@ namespace crepe { struct FlipSettings { - bool flip_x = 1; - bool flip_y = 1; + bool flip_x = true; + bool flip_y = true; }; class Sprite : public Component { public: - Sprite(game_object_id_t id, std::shared_ptr image, const Color & color, const FlipSettings & flip); + Sprite(game_object_id_t id, std::shared_ptr image, + const Color & color, const FlipSettings & flip); ~Sprite(); std::shared_ptr sprite_image; Color color; diff --git a/src/crepe/api/Transform.cpp b/src/crepe/api/Transform.cpp index a5f29ea..a244bc5 100644 --- a/src/crepe/api/Transform.cpp +++ b/src/crepe/api/Transform.cpp @@ -1,13 +1,11 @@ -#include - #include "util/log.h" #include "Transform.h" using namespace crepe; -Transform::Transform(game_object_id_t id, const Vector2 & point, double rot, - double scale) - : Component(id), position(point), rotation(rot), scale(scale) { +Transform::Transform(game_object_id_t id, const Vector2 & point, + double rotation, double scale) + : Component(id), position(point), rotation(rotation), scale(scale) { dbg_trace(); } diff --git a/src/crepe/api/Transform.h b/src/crepe/api/Transform.h index 805553e..d7a5b8a 100644 --- a/src/crepe/api/Transform.h +++ b/src/crepe/api/Transform.h @@ -17,10 +17,10 @@ public: /** * \param id The id of the GameObject this component belongs to * \param point The position of the GameObject - * \param rot The rotation of the GameObject + * \param rotation The rotation of the GameObject * \param scale The scale of the GameObject */ - Transform(game_object_id_t id, const Vector2 & point, double rot, + Transform(game_object_id_t id, const Vector2 & point, double rotation, double scale); /** * \brief Get the maximum number of instances for this component diff --git a/src/crepe/system/PhysicsSystem.cpp b/src/crepe/system/PhysicsSystem.cpp index 1bd2171..eb54ad3 100644 --- a/src/crepe/system/PhysicsSystem.cpp +++ b/src/crepe/system/PhysicsSystem.cpp @@ -20,7 +20,7 @@ void PhysicsSystem::update() { double gravity = Config::get_instance().physics.gravity; for (Rigidbody & rigidbody : rigidbodies) { if (!rigidbody.active) continue; - + switch (rigidbody.data.body_type) { case Rigidbody::BodyType::DYNAMIC: for (Transform & transform : transforms) { diff --git a/src/crepe/types.h b/src/crepe/types.h index 5ae2c81..0d459e8 100644 --- a/src/crepe/types.h +++ b/src/crepe/types.h @@ -7,4 +7,3 @@ namespace crepe { typedef uint32_t game_object_id_t; } - diff --git a/src/example/physics.cpp b/src/example/physics.cpp index a7e94f6..848f857 100644 --- a/src/example/physics.cpp +++ b/src/example/physics.cpp @@ -11,7 +11,7 @@ using namespace std; int main(int argc, char * argv[]) { GameObject * game_object; game_object = new GameObject(0, "Name", "Tag", Vector2{0, 0}, 0, 0); - game_object->add_component(Rigidbody::RigidbodyData{ + game_object->add_component(Rigidbody::Data{ .mass = 1, .gravity_scale = 1, .body_type = Rigidbody::BodyType::DYNAMIC, diff --git a/src/test/PhysicsTest.cpp b/src/test/PhysicsTest.cpp index 6b8c4d8..5385962 100644 --- a/src/test/PhysicsTest.cpp +++ b/src/test/PhysicsTest.cpp @@ -20,7 +20,7 @@ protected: = 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{ + game_object->add_component(Rigidbody::Data{ .mass = 1, .gravity_scale = 1, .body_type = Rigidbody::BodyType::DYNAMIC, @@ -28,7 +28,8 @@ protected: .max_angular_velocity = 10, .constraints = {0, 0}, .use_gravity = true, - .bounce = false}); + .bounce = false, + }); } transforms = mgr.get_components_by_id(0); Transform & transform = transforms.front().get(); -- cgit v1.2.3