diff options
author | JAROWMR <jarorutjes07@gmail.com> | 2024-11-15 16:10:45 +0100 |
---|---|---|
committer | JAROWMR <jarorutjes07@gmail.com> | 2024-11-15 16:10:45 +0100 |
commit | b019b401c3a1de0ffea7e6776242ae73599651ef (patch) | |
tree | 473c90cd71a8ee9943d8d333e11f6d40e68a8287 /src | |
parent | e6bc46a30152a7585dda24fde66622575349d25e (diff) |
collision handeling example
Diffstat (limited to 'src')
-rw-r--r-- | src/crepe/api/Event.h | 1 | ||||
-rw-r--r-- | src/crepe/api/Rigidbody.h | 5 | ||||
-rw-r--r-- | src/crepe/api/Script.h | 2 | ||||
-rw-r--r-- | src/crepe/system/CollisionSystem.cpp | 2 | ||||
-rw-r--r-- | src/crepe/system/CollisionSystem.h | 2 | ||||
-rw-r--r-- | src/crepe/system/ScriptSystem.cpp | 6 | ||||
-rw-r--r-- | src/example/collision.cpp | 39 |
7 files changed, 46 insertions, 11 deletions
diff --git a/src/crepe/api/Event.h b/src/crepe/api/Event.h index 3431f29..bd6a541 100644 --- a/src/crepe/api/Event.h +++ b/src/crepe/api/Event.h @@ -49,7 +49,6 @@ public: crepe::CollisionSystem::CollisionInfo info; CollisionEvent(const crepe::CollisionSystem::CollisionInfo& collisionInfo) : info(collisionInfo) {} - }; class TextSubmitEvent : public Event { public: diff --git a/src/crepe/api/Rigidbody.h b/src/crepe/api/Rigidbody.h index b9edec2..a45623f 100644 --- a/src/crepe/api/Rigidbody.h +++ b/src/crepe/api/Rigidbody.h @@ -1,5 +1,6 @@ #pragma once +#include <cmath> #include <cstdint> #include "../Component.h" @@ -60,13 +61,13 @@ public: //! linear velocity of object Vector2 linear_velocity; //! maximum linear velocity of object - Vector2 max_linear_velocity; + Vector2 max_linear_velocity = {INFINITY ,INFINITY}; //! 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; + double max_angular_velocity = INFINITY; //! angular damping of object double angular_damping = 0.0; //! movements constraints of object diff --git a/src/crepe/api/Script.h b/src/crepe/api/Script.h index 49e625f..0a10848 100644 --- a/src/crepe/api/Script.h +++ b/src/crepe/api/Script.h @@ -28,7 +28,7 @@ protected: template <typename T> std::vector<std::reference_wrapper<T>> get_components(); -private: +public: friend class crepe::BehaviorScript; BehaviorScript * parent = nullptr; }; diff --git a/src/crepe/system/CollisionSystem.cpp b/src/crepe/system/CollisionSystem.cpp index 56f07cc..b9366df 100644 --- a/src/crepe/system/CollisionSystem.cpp +++ b/src/crepe/system/CollisionSystem.cpp @@ -93,7 +93,7 @@ void CollisionSystem::call_collision_handler(const CollidedInfoStor& data1,const { *collider2, data2.transform, data2.rigidbody } }; CollisionEvent data(collision_info); - EventManager::get_instance().trigger_event<CollisionEvent>(data, 0); + EventManager::get_instance().trigger_event<CollisionEvent>(data, first); } } diff --git a/src/crepe/system/CollisionSystem.h b/src/crepe/system/CollisionSystem.h index c7e8e0b..a31ac48 100644 --- a/src/crepe/system/CollisionSystem.h +++ b/src/crepe/system/CollisionSystem.h @@ -27,7 +27,7 @@ public: void update(); private: void call_collision_handler(const CollidedInfoStor& data1,const CollidedInfoStor& data2); - std::vector<std::pair<CollidedInfoStor,CollidedInfoStor>> check_collisions(const std::vector<std::reference_wrapper<BoxCollider>>& boxcolliders, const std::vector<std::reference_wrapper<CircleCollider>>& circlecolliders); + std::vector<std::pair<CollidedInfoStor,CollidedInfoStor>> check_collisions(const std::vector<std::reference_wrapper<BoxCollider>>& boxcolliders, const std::vector<std::reference_wrapper<CircleCollider>>& circlecolliders); bool check_box_box_collision(const BoxCollider& box1, const BoxCollider& box2, const Transform& transform1, const Transform& transform2, const Rigidbody& rigidbody1, const Rigidbody& rigidbody2); bool check_box_circle_collision(const BoxCollider& box1, const CircleCollider& circle2, const Transform& transform1, const Transform& transform2, const Rigidbody& rigidbody1, const Rigidbody& rigidbody2); bool check_circle_circle_collision(const CircleCollider& circle1, const CircleCollider& circle2, const Transform& transform1, const Transform& transform2, const Rigidbody& rigidbody1, const Rigidbody& rigidbody2); diff --git a/src/crepe/system/ScriptSystem.cpp b/src/crepe/system/ScriptSystem.cpp index f2673e7..e41961e 100644 --- a/src/crepe/system/ScriptSystem.cpp +++ b/src/crepe/system/ScriptSystem.cpp @@ -17,7 +17,11 @@ void ScriptSystem::update() { dbg_trace(); forward_list<Script *> scripts = this->get_scripts(); - for (Script * script : scripts) script->update(); + for (Script * script : scripts) + { + script->init(); + script->update(); + }; } forward_list<Script *> ScriptSystem::get_scripts() { diff --git a/src/example/collision.cpp b/src/example/collision.cpp index e82b493..45ed0b0 100644 --- a/src/example/collision.cpp +++ b/src/example/collision.cpp @@ -1,5 +1,6 @@ #include "api/BoxCollider.h" #include "system/CollisionSystem.h" +#include <crepe/system/ScriptSystem.h> #include <crepe/Component.h> #include <crepe/ComponentManager.h> #include <crepe/api/GameObject.h> @@ -10,12 +11,15 @@ #include <crepe/system/RenderSystem.h> #include <crepe/util/log.h> +#include <crepe/api/Script.h> #include <crepe/api/AssetManager.h> #include <crepe/api/Color.h> #include <crepe/api/Sprite.h> #include <crepe/api/Texture.h> #include <crepe/api/Transform.h> #include <crepe/api/Vector2.h> +#include <crepe/api/Event.h> +#include <crepe/api/EventManager.h> #include <chrono> #include <memory> @@ -23,6 +27,22 @@ using namespace crepe; using namespace std; +class MyScript : public Script { + static bool oncollision(const CollisionEvent& test) { + std::cout << "test collision: " << test.info.first.collider.game_object_id << std::endl; + return true; + } + void init() { + EventManager::get_instance().subscribe<CollisionEvent>(oncollision, this->parent->game_object_id); + } + void update() { + // Retrieve component from the same GameObject this script is on + + } + + +}; + int main(int argc, char * argv[]) { Color color(0, 0, 0, 0); @@ -33,16 +53,19 @@ int main(int argc, char * argv[]) { .body_type = Rigidbody::BodyType::DYNAMIC, .constraints = {0, 0, 0}, .use_gravity = true, - .bounce = false, + .bounce = true, .offset = {0,0} }); game_object1.add_component<BoxCollider>(Vector2{5, 5}, 5, 5); + game_object1.add_component<BehaviorScript>().set_script<MyScript>(); + game_object1.add_component<BehaviorScript>().set_script<MyScript>(); + // game_object1.add_component<Sprite>( // make_shared<Texture>("/home/jaro/crepe/asset/texture/img.png"), color, // FlipSettings{true, true}); - GameObject game_object2(1, "Name", "Tag", Vector2{20, 0}, 90, 1); + GameObject game_object2(1, "Name", "Tag", Vector2{10, 10}, 0, 1); game_object2.add_component<Rigidbody>(Rigidbody::Data{ .mass = 1, .gravity_scale = 1, @@ -53,16 +76,24 @@ int main(int argc, char * argv[]) { .offset = {0,0} }); game_object2.add_component<BoxCollider>(Vector2{5, 5}, 5, 5); + game_object2.add_component<BehaviorScript>().set_script<MyScript>(); // game_object2.add_component<Sprite>( // make_shared<Texture>("/home/jaro/crepe/asset/texture/img.png"), color, // FlipSettings{true, true}); - CollisionSystem coltest; - coltest.update(); + + ScriptSystem sys; + // Update all scripts. This should result in MyScript::update being called + sys.update(); // auto & sys = crepe::RenderSystem::get_instance(); // auto start = std::chrono::steady_clock::now(); // while (std::chrono::steady_clock::now() - start < std::chrono::seconds(5)) { // sys.update(); // } + CollisionSystem coltest; + coltest.update(); + + + return 0; } |