aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorjaroWMR <jarorutjes07@gmail.com>2024-10-20 16:58:13 +0200
committerjaroWMR <jarorutjes07@gmail.com>2024-10-20 16:58:13 +0200
commitcead795b7ff7135e13caf9ad3b76628070391458 (patch)
treef91f5e9f6927727367324961e585d049d2947364
parentd8483cfab70b5aca3baae6e0588924da8b54e090 (diff)
added collision system and circlecollider
-rw-r--r--src/crepe/CMakeLists.txt6
-rw-r--r--src/crepe/CircleCollider.h12
-rw-r--r--src/crepe/Collider.cpp2
-rw-r--r--src/crepe/Collider.h2
-rw-r--r--src/crepe/CollisionSystem.cpp13
-rw-r--r--src/crepe/CollisionSystem.h15
-rw-r--r--src/crepe/Component.h1
-rw-r--r--src/crepe/Force.cpp14
-rw-r--r--src/crepe/Force.h17
-rw-r--r--src/crepe/GameObject.cpp7
-rw-r--r--src/crepe/PhysicsSystem.cpp37
-rw-r--r--src/crepe/Rigidbody.h3
-rw-r--r--src/crepe/Transform.cpp4
-rw-r--r--src/crepe/Transform.h16
-rw-r--r--src/example/Physics.cpp8
15 files changed, 143 insertions, 14 deletions
diff --git a/src/crepe/CMakeLists.txt b/src/crepe/CMakeLists.txt
index ae9d51d..cce25c4 100644
--- a/src/crepe/CMakeLists.txt
+++ b/src/crepe/CMakeLists.txt
@@ -16,6 +16,9 @@ target_sources(crepe PUBLIC
ScriptSystem.cpp
Script.cpp
PhysicsSystem.cpp
+ Transform.cpp
+ Force.cpp
+ CollisionSystem.cpp
)
target_sources(crepe PUBLIC FILE_SET HEADERS FILES
@@ -37,6 +40,9 @@ target_sources(crepe PUBLIC FILE_SET HEADERS FILES
System.h
ScriptSystem.h
PhysicsSystem.h
+ Transform.h
+ Force.h
+ CollisionSystem.h
)
add_subdirectory(api)
diff --git a/src/crepe/CircleCollider.h b/src/crepe/CircleCollider.h
new file mode 100644
index 0000000..922477b
--- /dev/null
+++ b/src/crepe/CircleCollider.h
@@ -0,0 +1,12 @@
+#pragma once
+#include "Collider.h"
+
+namespace crepe {
+
+class CircleCollider : public Collider {
+public:
+ CircleCollider(uint32_t gameObjectId,int radius) : Collider(gameObjectId), radius(radius) {}
+ int radius;
+};
+
+} // namespace crepe
diff --git a/src/crepe/Collider.cpp b/src/crepe/Collider.cpp
index 3d3f7cb..311ffb3 100644
--- a/src/crepe/Collider.cpp
+++ b/src/crepe/Collider.cpp
@@ -2,4 +2,4 @@
using namespace crepe;
-Collider::Collider(uint32_t gameObjectId , int size) : Component(gameObjectId), size(size){}
+Collider::Collider(uint32_t gameObjectId) : Component(gameObjectId){}
diff --git a/src/crepe/Collider.h b/src/crepe/Collider.h
index 69bb0c3..cfc044c 100644
--- a/src/crepe/Collider.h
+++ b/src/crepe/Collider.h
@@ -6,7 +6,7 @@ namespace crepe {
class Collider : public Component {
public:
- Collider(uint32_t gameObjectId,int size);
+ Collider(uint32_t gameObjectId);
int size;
};
diff --git a/src/crepe/CollisionSystem.cpp b/src/crepe/CollisionSystem.cpp
new file mode 100644
index 0000000..a0c6dca
--- /dev/null
+++ b/src/crepe/CollisionSystem.cpp
@@ -0,0 +1,13 @@
+#include "CollisionSystem.h"
+#include "ComponentManager.h"
+#include <iostream>
+
+using namespace crepe;
+
+CollisionSystem::CollisionSystem() {
+
+}
+
+void CollisionSystem::update() {
+
+}
diff --git a/src/crepe/CollisionSystem.h b/src/crepe/CollisionSystem.h
new file mode 100644
index 0000000..70d7237
--- /dev/null
+++ b/src/crepe/CollisionSystem.h
@@ -0,0 +1,15 @@
+#pragma once
+
+namespace crepe {
+
+class CollisionSystem
+{
+ private:
+ /* data */
+ public:
+ CollisionSystem(/* args */);
+ void update();
+};
+
+}
+
diff --git a/src/crepe/Component.h b/src/crepe/Component.h
index c8fb6bc..a23de61 100644
--- a/src/crepe/Component.h
+++ b/src/crepe/Component.h
@@ -6,6 +6,7 @@ namespace crepe {
class Component {
public:
Component(uint32_t id);
+ virtual ~Component() {}
// TODO: shouldn't this constructor be deleted because this class will never
// directly be instantiated?
//changed so it sets the id (jaro)
diff --git a/src/crepe/Force.cpp b/src/crepe/Force.cpp
new file mode 100644
index 0000000..b314f49
--- /dev/null
+++ b/src/crepe/Force.cpp
@@ -0,0 +1,14 @@
+#include "Force.h"
+#include <cmath>
+
+namespace crepe {
+
+Force::Force(uint32_t gameObjectId, uint32_t forceMagnitude, uint32_t direction): Component(gameObjectId)
+{
+ // 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(forceMagnitude * std::cos(radian_direction)));
+ force_y = static_cast<int32_t>(std::round(forceMagnitude * std::sin(radian_direction)));
+}
+
+} // namespace crepe
diff --git a/src/crepe/Force.h b/src/crepe/Force.h
new file mode 100644
index 0000000..a121ec2
--- /dev/null
+++ b/src/crepe/Force.h
@@ -0,0 +1,17 @@
+#pragma once
+
+#include "Component.h"
+#include <cstdint>
+#include <utility>
+
+namespace crepe {
+
+class Force : public Component {
+public:
+ Force(uint32_t gameObjectId, uint32_t forceMagnitude, uint32_t direction);
+
+ int32_t force_x;
+ int32_t force_y;
+};
+
+} // namespace crepe
diff --git a/src/crepe/GameObject.cpp b/src/crepe/GameObject.cpp
index de3beb6..36eb886 100644
--- a/src/crepe/GameObject.cpp
+++ b/src/crepe/GameObject.cpp
@@ -1,7 +1,12 @@
#include "GameObject.h"
+#include "Transform.h"
using namespace crepe;
GameObject::GameObject(uint32_t id, std::string name, std::string tag,
int layer)
- : id(id), name(name), tag(tag), active(true), layer(layer) {}
+ : id(id), name(name), tag(tag), active(true), layer(layer)
+{
+ Position pos = {0,0};
+ this->add_component<Transform>(pos, 0, 0);
+}
diff --git a/src/crepe/PhysicsSystem.cpp b/src/crepe/PhysicsSystem.cpp
index c24afa2..7d16044 100644
--- a/src/crepe/PhysicsSystem.cpp
+++ b/src/crepe/PhysicsSystem.cpp
@@ -2,6 +2,7 @@
#include "ComponentManager.h"
#include "Rigidbody.h"
#include "Transform.h"
+#include "Force.h"
#include <iostream>
using namespace crepe;
@@ -14,7 +15,41 @@ void PhysicsSystem::update() {
ComponentManager& mgr = ComponentManager::get_instance();
std::vector<std::reference_wrapper<Rigidbody>> rigidbodies = mgr.get_components_by_type<Rigidbody>();
std::vector<std::reference_wrapper<Transform>> transforms = mgr.get_components_by_type<Transform>();
+
for (Rigidbody& rigidbody : rigidbodies) {
- std::cout << rigidbody.gameObjectId << std::endl;
+
+ switch (rigidbody.body_type)
+ {
+ case BodyType::Dynamic :
+ for (Transform& transform : transforms) {
+ if(transform.gameObjectId == rigidbody.gameObjectId)
+ {
+ rigidbody.velocity_x = 0;
+ rigidbody.velocity_y = 0;
+ std::vector<std::reference_wrapper<Force>> Forces = mgr.get_components_by_id<Force>(rigidbody.gameObjectId);
+ 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.postion.x << std::endl;
+ std::cout << "before transform.postion.y " << transform.postion.y << std::endl;
+ transform.postion.x += rigidbody.velocity_x;
+ transform.postion.y += rigidbody.velocity_y;
+ std::cout << "after transform.postion.x " << transform.postion.x << std::endl;
+ std::cout << "after transform.postion.y " << transform.postion.y << std::endl;
+ }
+ }
+ break;
+ case BodyType::Kinematic :
+ break; //(scripts)
+ case BodyType::Static :
+ break; //(unmoveable objects)
+ default:
+ break;
+ }
}
}
diff --git a/src/crepe/Rigidbody.h b/src/crepe/Rigidbody.h
index 7018e83..a03346f 100644
--- a/src/crepe/Rigidbody.h
+++ b/src/crepe/Rigidbody.h
@@ -14,7 +14,8 @@ enum class BodyType {
class Rigidbody : public Component {
public:
Rigidbody(uint32_t gameObjectId,int mass, int gravityScale, BodyType bodyType);
-
+ int32_t velocity_x;
+ int32_t velocity_y;
int mass;
int gravity_scale;
BodyType body_type;
diff --git a/src/crepe/Transform.cpp b/src/crepe/Transform.cpp
index 2fde448..2c39523 100644
--- a/src/crepe/Transform.cpp
+++ b/src/crepe/Transform.cpp
@@ -2,5 +2,5 @@
using namespace crepe;
-Transform::Transform(uint32_t gameObjectId,int mass, int gravityScale, int bodyType)
- : Component(gameObjectId), mass(mass), gravity_scale(gravityScale), body_type(bodyType) {}
+Transform::Transform(uint32_t gameObjectId,Position position, int rotation, int scale)
+ : Component(gameObjectId), postion(postion), rotation(rotation), scale(scale) {}
diff --git a/src/crepe/Transform.h b/src/crepe/Transform.h
index 96da1a4..3e8d142 100644
--- a/src/crepe/Transform.h
+++ b/src/crepe/Transform.h
@@ -4,13 +4,19 @@
namespace crepe {
+struct Position
+{
+ int x;
+ int y;
+};
+
+
class Transform : public Component {
public:
- Transform(uint32_t gameObjectId,int mass, int gravityScale, int bodyType);
-
- int mass;
- int gravity_scale;
- int body_type;
+ Transform(uint32_t gameObjectId,Position position, int rotation, int scale);
+ Position postion;
+ int rotation;
+ int scale;
};
} // namespace crepe
diff --git a/src/example/Physics.cpp b/src/example/Physics.cpp
index 61a3bf2..9ad6da0 100644
--- a/src/example/Physics.cpp
+++ b/src/example/Physics.cpp
@@ -1,7 +1,9 @@
#include <iostream>
#include <thread>
#include <chrono>
+#include "Transform.h"
#include "Rigidbody.h"
+#include "Force.h"
#include "PhysicsSystem.h"
#include <crepe/Component.h>
#include <crepe/ComponentManager.h>
@@ -13,9 +15,11 @@ using namespace std;
int main(int argc, char* argv[]) {
PhysicsSystem physicsSystem;
- GameObject * game_object[1];
+ GameObject * game_object[2];
+ game_object[1] = new GameObject(2, "Name", "Tag", 0); // not found not used
game_object[0] = new GameObject(5, "Name", "Tag", 0);
- game_object[0]->add_component<Rigidbody>(10, 11 , BodyType::Dynamic);
+ game_object[0]->add_component<Rigidbody>(1, 1 , BodyType::Dynamic);
+ game_object[0]->add_component<Force>(1 , 0);
physicsSystem.update();
return 0;
}