aboutsummaryrefslogtreecommitdiff
path: root/src/crepe
diff options
context:
space:
mode:
authorjaroWMR <jarorutjes07@gmail.com>2024-11-07 18:39:56 +0100
committerjaroWMR <jarorutjes07@gmail.com>2024-11-07 18:39:56 +0100
commite2f3ace383b43cc3f140629e577b97c6f69c9856 (patch)
tree690eb146e11a4bc134840ec82bfbea4805daba61 /src/crepe
parent156906dca0b84d3fd3c889e1bcda12308b8fe793 (diff)
added physics system
Diffstat (limited to 'src/crepe')
-rw-r--r--src/crepe/api/CMakeLists.txt4
-rw-r--r--src/crepe/api/Config.h10
-rw-r--r--src/crepe/api/Force.cpp20
-rw-r--r--src/crepe/api/Force.h17
-rw-r--r--src/crepe/api/GameObject.cpp2
-rw-r--r--src/crepe/api/GameObject.h4
-rw-r--r--src/crepe/api/Point.h11
-rw-r--r--src/crepe/api/Rigidbody.cpp16
-rw-r--r--src/crepe/api/Rigidbody.h102
-rw-r--r--src/crepe/api/Transform.cpp2
-rw-r--r--src/crepe/api/Transform.h6
-rw-r--r--src/crepe/api/Vector2.cpp59
-rw-r--r--src/crepe/api/Vector2.h48
-rw-r--r--src/crepe/system/PhysicsSystem.cpp102
-rw-r--r--src/crepe/system/PhysicsSystem.h17
15 files changed, 309 insertions, 111 deletions
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 <cmath>
-
-#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<float>(direction) * (M_PI / 180.0f);
- force_x = static_cast<int32_t>(
- std::round(magnitude * std::cos(radian_direction)));
- force_y = static_cast<int32_t>(
- 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 <cstdint>
-
-#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 <iostream>
+#include <cmath>
#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<std::reference_wrapper<Rigidbody>> rigidbodies
@@ -18,41 +17,80 @@ void PhysicsSystem::update() {
std::vector<std::reference_wrapper<Transform>> transforms
= mgr.get_components_by_type<Transform>();
+ 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<std::reference_wrapper<Force>> forces
- = mgr.get_components_by_id<Force>(
- 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();
};