aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorJAROWMR <jarorutjes07@gmail.com>2024-11-22 15:36:14 +0100
committerJAROWMR <jarorutjes07@gmail.com>2024-11-22 15:36:14 +0100
commit3f63143b4005936da446fb2cdbbd1072b47fc8c1 (patch)
tree480484822cf915e035120fdd4db88e6f1c389b38 /src
parent503c1d15d35fc06ac36febc72a8aa8abe38707ce (diff)
merge with master
Diffstat (limited to 'src')
-rw-r--r--src/crepe/Collider.cpp2
-rw-r--r--src/crepe/Collider.h5
-rw-r--r--src/crepe/api/BoxCollider.cpp2
-rw-r--r--src/crepe/api/BoxCollider.h2
-rw-r--r--src/crepe/api/CircleCollider.cpp2
-rw-r--r--src/crepe/api/CircleCollider.h2
-rw-r--r--src/crepe/api/LoopManager.cpp1
-rw-r--r--src/crepe/api/Rigidbody.h2
-rw-r--r--src/crepe/system/CollisionSystem.cpp79
-rw-r--r--src/crepe/system/CollisionSystem.h8
-rw-r--r--src/example/CMakeLists.txt1
-rw-r--r--src/example/game.cpp30
12 files changed, 71 insertions, 65 deletions
diff --git a/src/crepe/Collider.cpp b/src/crepe/Collider.cpp
index 0706371..80a944d 100644
--- a/src/crepe/Collider.cpp
+++ b/src/crepe/Collider.cpp
@@ -2,4 +2,4 @@
using namespace crepe;
-Collider::Collider(game_object_id_t id, Vector2 offset) : Component(id), offset(offset) {}
+Collider::Collider(game_object_id_t id, vec2 offset) : Component(id), offset(offset) {}
diff --git a/src/crepe/Collider.h b/src/crepe/Collider.h
index 0157324..5b26af5 100644
--- a/src/crepe/Collider.h
+++ b/src/crepe/Collider.h
@@ -3,16 +3,17 @@
#include "api/Vector2.h"
#include "Component.h"
+#include "types.h"
namespace crepe {
class Collider : public Component {
public:
- Collider(game_object_id_t id, Vector2 offset);
+ Collider(game_object_id_t id, vec2 offset);
public:
//! Offset of the collider relative to rigidbody position
- Vector2 offset;
+ vec2 offset;
};
} // namespace crepe
diff --git a/src/crepe/api/BoxCollider.cpp b/src/crepe/api/BoxCollider.cpp
index eafbdb2..83fb632 100644
--- a/src/crepe/api/BoxCollider.cpp
+++ b/src/crepe/api/BoxCollider.cpp
@@ -4,4 +4,4 @@
using namespace crepe;
-BoxCollider::BoxCollider(game_object_id_t game_object_id,Vector2 offset, double width, double height) : Collider(game_object_id,offset), width(width), height(height) {}
+BoxCollider::BoxCollider(game_object_id_t game_object_id,vec2 offset, double width, double height) : Collider(game_object_id,offset), width(width), height(height) {}
diff --git a/src/crepe/api/BoxCollider.h b/src/crepe/api/BoxCollider.h
index 7f51cba..6135954 100644
--- a/src/crepe/api/BoxCollider.h
+++ b/src/crepe/api/BoxCollider.h
@@ -12,7 +12,7 @@ namespace crepe {
*/
class BoxCollider : public Collider {
public:
- BoxCollider(game_object_id_t game_object_id,Vector2 offset, double width, double height);
+ BoxCollider(game_object_id_t game_object_id,vec2 offset, double width, double height);
//! Width of box collider
double width;
diff --git a/src/crepe/api/CircleCollider.cpp b/src/crepe/api/CircleCollider.cpp
index 04a4995..43de991 100644
--- a/src/crepe/api/CircleCollider.cpp
+++ b/src/crepe/api/CircleCollider.cpp
@@ -3,4 +3,4 @@
using namespace crepe;
-CircleCollider::CircleCollider(game_object_id_t game_object_id,Vector2 offset, int radius) : Collider(game_object_id,offset), radius(radius) {}
+CircleCollider::CircleCollider(game_object_id_t game_object_id,vec2 offset, int radius) : Collider(game_object_id,offset), radius(radius) {}
diff --git a/src/crepe/api/CircleCollider.h b/src/crepe/api/CircleCollider.h
index 4e04fa4..843547f 100644
--- a/src/crepe/api/CircleCollider.h
+++ b/src/crepe/api/CircleCollider.h
@@ -14,7 +14,7 @@ namespace crepe {
class CircleCollider : public Collider {
public:
- CircleCollider(game_object_id_t game_object_id,Vector2 offset, int radius);
+ CircleCollider(game_object_id_t game_object_id,vec2 offset, int radius);
//! Radius of the circle collider.
double radius;
diff --git a/src/crepe/api/LoopManager.cpp b/src/crepe/api/LoopManager.cpp
index 586919d..10b59c8 100644
--- a/src/crepe/api/LoopManager.cpp
+++ b/src/crepe/api/LoopManager.cpp
@@ -63,6 +63,7 @@ void LoopManager::setup() {
this->game_running = true;
LoopTimer::get_instance().start();
LoopTimer::get_instance().set_fps(60);
+ this->scene_manager.load_next_scene();
}
void LoopManager::render() {
diff --git a/src/crepe/api/Rigidbody.h b/src/crepe/api/Rigidbody.h
index b96b463..7939563 100644
--- a/src/crepe/api/Rigidbody.h
+++ b/src/crepe/api/Rigidbody.h
@@ -78,7 +78,7 @@ public:
//! bounce factor of material
double elastisity = 0.0;
//! offset of all colliders relative to transform position
- Vector2 offset;
+ vec2 offset;
};
public:
diff --git a/src/crepe/system/CollisionSystem.cpp b/src/crepe/system/CollisionSystem.cpp
index 9ca8b3a..8d9b356 100644
--- a/src/crepe/system/CollisionSystem.cpp
+++ b/src/crepe/system/CollisionSystem.cpp
@@ -15,6 +15,7 @@
#include "ComponentManager.h"
#include "CollisionSystem.h"
#include "Collider.h"
+#include "types.h"
using namespace crepe;
@@ -39,7 +40,7 @@ void CollisionSystem::collision_handler(CollidedInfoStor& data1,CollidedInfoStor
// Data needed for collision handler info
const Collider* collider1 = nullptr;
const Collider* collider2 = nullptr;
- Vector2 move_back;
+ vec2 move_back;
// Check collision type and get values for handler
if (std::holds_alternative<std::reference_wrapper<BoxCollider>>(data1.collider)) {
@@ -53,8 +54,8 @@ void CollisionSystem::collision_handler(CollidedInfoStor& data1,CollidedInfoStor
// TODO: send with the collider info to this function because this is calculated previously
// Get the current position of the collider
- Vector2 final_position1 = current_position(box_collider1,data1.transform,data1.rigidbody);
- Vector2 final_position2 = current_position(box_collider2,data2.transform,data2.rigidbody);
+ vec2 final_position1 = current_position(box_collider1,data1.transform,data1.rigidbody);
+ vec2 final_position2 = current_position(box_collider2,data2.transform,data2.rigidbody);
// Determine move_back value for smallest overlap (x or y)
move_back = box_box_collision_move_back(box_collider1,box_collider2,final_position1,final_position2);
@@ -112,20 +113,20 @@ void CollisionSystem::collision_handler(CollidedInfoStor& data1,CollidedInfoStor
}
-Vector2 CollisionSystem::box_box_collision_move_back(const BoxCollider& box_collider1,const BoxCollider& box_collider2,Vector2 final_position1,Vector2 final_position2)
+vec2 CollisionSystem::box_box_collision_move_back(const BoxCollider& box_collider1,const BoxCollider& box_collider2,vec2 final_position1,vec2 final_position2)
{
- Vector2 resolution; // Default resolution vector
- Vector2 delta = final_position2 - final_position1;
+ vec2 resolution; // Default resolution vector
+ vec2 delta = final_position2 - final_position1;
// Compute half-dimensions of the boxes
- double half_width1 = box_collider1.width / 2.0;
- double half_height1 = box_collider1.height / 2.0;
- double half_width2 = box_collider2.width / 2.0;
- double half_height2 = box_collider2.height / 2.0;
+ float half_width1 = box_collider1.width / 2.0;
+ float half_height1 = box_collider1.height / 2.0;
+ float half_width2 = box_collider2.width / 2.0;
+ float half_height2 = box_collider2.height / 2.0;
// Calculate overlaps along X and Y axes
- double overlap_x = (half_width1 + half_width2) - std::abs(delta.x);
- double overlap_y = (half_height1 + half_height2) - std::abs(delta.y);
+ float overlap_x = (half_width1 + half_width2) - std::abs(delta.x);
+ float overlap_y = (half_height1 + half_height2) - std::abs(delta.y);
// Check if there is a collision
if (overlap_x > 0 && overlap_y > 0) {//should always be true check if this can be removed
@@ -288,14 +289,14 @@ std::vector<std::pair<CollisionSystem::CollidedInfoStor,CollisionSystem::Collide
bool CollisionSystem::check_box_box_collision(const BoxCollider& box1, const BoxCollider& box2, const Transform& transform1, const Transform& transform2, const Rigidbody& rigidbody1, const Rigidbody& rigidbody2)
{
// Get current positions of colliders
- Vector2 final_position1 = current_position(box1,transform1,rigidbody1);
- Vector2 final_position2 = current_position(box2,transform2,rigidbody2);
+ vec2 final_position1 = current_position(box1,transform1,rigidbody1);
+ vec2 final_position2 = current_position(box2,transform2,rigidbody2);
// Calculate half-extents (half width and half height)
- double half_width1 = box1.width / 2.0;
- double half_height1 = box1.height / 2.0;
- double half_width2 = box2.width / 2.0;
- double half_height2 = box2.height / 2.0;
+ float half_width1 = box1.width / 2.0;
+ float half_height1 = box1.height / 2.0;
+ float half_width2 = box2.width / 2.0;
+ float half_height2 = box2.height / 2.0;
// Check if the boxes overlap along the X and Y axes
return !(final_position1.x + half_width1 <= final_position2.x - half_width2 || // box1 is left of box2
@@ -306,21 +307,21 @@ bool CollisionSystem::check_box_box_collision(const BoxCollider& box1, const Box
bool CollisionSystem::check_box_circle_collision(const BoxCollider& box1, const CircleCollider& circle2, const Transform& transform1, const Transform& transform2, const Rigidbody& rigidbody1, const Rigidbody& rigidbody2) {
// Get current positions of colliders
- Vector2 final_position1 = current_position(box1, transform1, rigidbody1);
- Vector2 final_position2 = current_position(circle2, transform2, rigidbody2);
+ vec2 final_position1 = current_position(box1, transform1, rigidbody1);
+ vec2 final_position2 = current_position(circle2, transform2, rigidbody2);
// Calculate box half-extents
- double half_width = box1.width / 2.0;
- double half_height = box1.height / 2.0;
+ float half_width = box1.width / 2.0;
+ float half_height = box1.height / 2.0;
// Find the closest point on the box to the circle's center
- double closest_x = std::max(final_position1.x - half_width, std::min(final_position2.x, final_position1.x + half_width));
- double closest_y = std::max(final_position1.y - half_height, std::min(final_position2.y, final_position1.y + half_height));
+ float closest_x = std::max(final_position1.x - half_width, std::min(final_position2.x, final_position1.x + half_width));
+ float closest_y = std::max(final_position1.y - half_height, std::min(final_position2.y, final_position1.y + half_height));
// Calculate the distance squared between the circle's center and the closest point on the box
- double distance_x = final_position2.x - closest_x;
- double distance_y = final_position2.y - closest_y;
- double distance_squared = distance_x * distance_x + distance_y * distance_y;
+ float distance_x = final_position2.x - closest_x;
+ float distance_y = final_position2.y - closest_y;
+ float distance_squared = distance_x * distance_x + distance_y * distance_y;
// Compare distance squared with the square of the circle's radius
return distance_squared <= circle2.radius * circle2.radius;
@@ -328,32 +329,32 @@ bool CollisionSystem::check_box_circle_collision(const BoxCollider& box1, const
bool CollisionSystem::check_circle_circle_collision(const CircleCollider& circle1, const CircleCollider& circle2, const Transform& transform1, const Transform& transform2, const Rigidbody& rigidbody1, const Rigidbody& rigidbody2) {
// Get current positions of colliders
- Vector2 final_position1 = current_position(circle1,transform1,rigidbody1);
- Vector2 final_position2 = current_position(circle2,transform2,rigidbody2);
+ vec2 final_position1 = current_position(circle1,transform1,rigidbody1);
+ vec2 final_position2 = current_position(circle2,transform2,rigidbody2);
- double distance_x = final_position1.x - final_position2.x;
- double distance_y = final_position1.y - final_position2.y;
- double distance_squared = distance_x * distance_x + distance_y * distance_y;
+ float distance_x = final_position1.x - final_position2.x;
+ float distance_y = final_position1.y - final_position2.y;
+ float distance_squared = distance_x * distance_x + distance_y * distance_y;
// Calculate the sum of the radii
- double radius_sum = circle1.radius + circle2.radius;
+ float radius_sum = circle1.radius + circle2.radius;
// Check if the distance between the centers is less than or equal to the sum of the radii
return distance_squared <= radius_sum * radius_sum;
}
-Vector2 CollisionSystem::current_position(const Collider& collider, const Transform& transform, const Rigidbody& rigidbody) {
+vec2 CollisionSystem::current_position(const Collider& collider, const Transform& transform, const Rigidbody& rigidbody) {
// Get the rotation in radians
- double radians1 = transform.rotation * (M_PI / 180.0);
+ float radians1 = transform.rotation * (M_PI / 180.0);
// Calculate total offset with scale
- Vector2 total_offset = (rigidbody.data.offset + collider.offset) * transform.scale;
+ vec2 total_offset = (rigidbody.data.offset + collider.offset) * transform.scale;
// Rotate
- double rotated_total_offset_x1 = total_offset.x * cos(radians1) - total_offset.y * sin(radians1);
- double rotated_total_offset_y1 = total_offset.x * sin(radians1) + total_offset.y * cos(radians1);
+ float rotated_total_offset_x1 = total_offset.x * cos(radians1) - total_offset.y * sin(radians1);
+ float rotated_total_offset_y1 = total_offset.x * sin(radians1) + total_offset.y * cos(radians1);
// Final positions considering scaling and rotation
- return(transform.position + Vector2(rotated_total_offset_x1, rotated_total_offset_y1));
+ return(transform.position + vec2(rotated_total_offset_x1, rotated_total_offset_y1));
}
diff --git a/src/crepe/system/CollisionSystem.h b/src/crepe/system/CollisionSystem.h
index 69d1d23..44fff4e 100644
--- a/src/crepe/system/CollisionSystem.h
+++ b/src/crepe/system/CollisionSystem.h
@@ -65,7 +65,7 @@ public:
struct CollisionInfo{
ColliderInfo first;
ColliderInfo second;
- Vector2 move_back_value;
+ vec2 move_back_value;
Direction move_back_direction = Direction::NONE;
};
@@ -98,7 +98,7 @@ private:
* \param final_position2 The final position of the second box collider.
* \return The move-back vector to resolve the collision.
*/
- Vector2 box_box_collision_move_back(const BoxCollider& box_collider1,const BoxCollider& box_collider2,Vector2 position1,Vector2 position2);
+ vec2 box_box_collision_move_back(const BoxCollider& box_collider1,const BoxCollider& box_collider2,vec2 position1,vec2 position2);
/**
* \brief Determines the appropriate collision handler based on the rigidbody types of the colliding objects.
@@ -185,9 +185,9 @@ private: //detection
* \param collider The collider whose position is being determined.
* \param transform The transform component associated with the collider.
* \param rigidbody The rigidbody component associated with the collider.
- * \return The current position of the collider as a Vector2.
+ * \return The current position of the collider as a vec2.
*/
- Vector2 current_position(const Collider& collider, const Transform& transform, const Rigidbody& rigidbody);
+ vec2 current_position(const Collider& collider, const Transform& transform, const Rigidbody& rigidbody);
};
} // namespace crepe
diff --git a/src/example/CMakeLists.txt b/src/example/CMakeLists.txt
index 560e2bc..3ec5e43 100644
--- a/src/example/CMakeLists.txt
+++ b/src/example/CMakeLists.txt
@@ -20,4 +20,5 @@ add_example(asset_manager)
add_example(savemgr)
add_example(rendering_particle)
add_example(gameloop)
+add_example(game)
diff --git a/src/example/game.cpp b/src/example/game.cpp
index a557be7..f6b580a 100644
--- a/src/example/game.cpp
+++ b/src/example/game.cpp
@@ -18,17 +18,18 @@ 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;
+ Log::logf("Box {} script on_collision()", test.info.first.collider.game_object_id);
return true;
}
void init() {
- EventManager::get_instance().subscribe<CollisionEvent>(oncollision, this->get_game_object_id());
+ EventManager::get_instance().subscribe<CollisionEvent>(oncollision, 0);
}
void update() {
// Retrieve component from the same GameObject this script is on
}
};
+
class ConcreteScene1 : public Scene {
public:
using Scene::Scene;
@@ -37,11 +38,11 @@ public:
ComponentManager & mgr = this->component_manager;
Color color(0, 0, 0, 0);
- double screen_size_width = 640;
- double screen_size_height = 480;
- double world_collider = 1000;
+ float screen_size_width = 640;
+ float screen_size_height = 480;
+ float world_collider = 1000;
//define playable world
- GameObject world = mgr.new_object("Name", "Tag", Vector2{screen_size_width/2, screen_size_height/2}, 0, 1);
+ GameObject world = mgr.new_object("Name", "Tag", vec2{screen_size_width/2, screen_size_height/2}, 0, 1);
world.add_component<Rigidbody>(Rigidbody::Data{
.mass = 0,
.gravity_scale = 0,
@@ -51,13 +52,13 @@ public:
.bounce = false,
.offset = {0,0}
});
- world.add_component<BoxCollider>(Vector2{0, 0-(screen_size_height/2+world_collider/2)}, world_collider, world_collider);; // Top
- world.add_component<BoxCollider>(Vector2{0, screen_size_height/2+world_collider/2}, world_collider, world_collider); // Bottom
- world.add_component<BoxCollider>(Vector2{0-(screen_size_width/2+world_collider/2), 0}, world_collider, world_collider); // Left
- world.add_component<BoxCollider>(Vector2{screen_size_width/2+world_collider/2, 0}, world_collider, world_collider); // right
+ world.add_component<BoxCollider>(vec2{0, 0-(screen_size_height/2+world_collider/2)}, world_collider, world_collider);; // Top
+ world.add_component<BoxCollider>(vec2{0, screen_size_height/2+world_collider/2}, world_collider, world_collider); // Bottom
+ world.add_component<BoxCollider>(vec2{0-(screen_size_width/2+world_collider/2), 0}, world_collider, world_collider); // Left
+ world.add_component<BoxCollider>(vec2{screen_size_width/2+world_collider/2, 0}, world_collider, world_collider); // right
- GameObject game_object1 = mgr.new_object("Name", "Tag", Vector2{screen_size_width/2, screen_size_height/2}, 0, 1);
+ GameObject game_object1 = mgr.new_object("Name", "Tag", vec2{screen_size_width/2, screen_size_height/2}, 0, 1);
game_object1.add_component<Rigidbody>(Rigidbody::Data{
.mass = 1,
.gravity_scale = 0.01,
@@ -69,20 +70,21 @@ public:
.elastisity = 1,
.offset = {0,0},
});
- game_object1.add_component<BoxCollider>(Vector2{0, 0}, 20, 20);
+ game_object1.add_component<BoxCollider>(vec2{0, 0}, 20, 20);
game_object1.add_component<BehaviorScript>().set_script<MyScript>();
game_object1.add_component<Sprite>(
make_shared<Texture>("/home/jaro/crepe/asset/texture/green_square.png"), color,
FlipSettings{true, true});
game_object1.add_component<Camera>(Color::WHITE);
}
+
+ string get_name() const { return "scene1"; }
};
int main(int argc, char * argv[]) {
LoopManager gameloop;
- gameloop.scene_manager.add_scene<ConcreteScene1>("scene1");
- gameloop.scene_manager.load_next_scene();
+ gameloop.add_scene<ConcreteScene1>();
gameloop.start();
return 0;
}