aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJAROWMR <jarorutjes07@gmail.com>2024-12-02 17:17:21 +0100
committerJAROWMR <jarorutjes07@gmail.com>2024-12-02 17:17:21 +0100
commiteeb66130e2cb94c94e1748576f98f78ce0f1ee86 (patch)
treef3a8a09b9f9245f559d1c924a7eccead5434f979
parentd06e05d0d8dbe1ace63b439231ccd05a975e9834 (diff)
changed some values because of feedback and merged from master
-rw-r--r--src/crepe/Collider.cpp2
-rw-r--r--src/crepe/Collider.h2
-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/Rigidbody.h14
-rw-r--r--src/crepe/system/CollisionSystem.cpp18
-rw-r--r--src/crepe/system/CollisionSystem.h8
-rw-r--r--src/crepe/system/PhysicsSystem.cpp10
-rw-r--r--src/test/CMakeLists.txt2
-rw-r--r--src/test/CollisionTest.cpp5
-rw-r--r--src/test/Profiling.cpp42
13 files changed, 52 insertions, 59 deletions
diff --git a/src/crepe/Collider.cpp b/src/crepe/Collider.cpp
index 80a944d..9d94152 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, vec2 offset) : Component(id), offset(offset) {}
+Collider::Collider(game_object_id_t id, const vec2& offset) : Component(id), offset(offset) {}
diff --git a/src/crepe/Collider.h b/src/crepe/Collider.h
index e1624b4..15d3a24 100644
--- a/src/crepe/Collider.h
+++ b/src/crepe/Collider.h
@@ -7,7 +7,7 @@ namespace crepe {
class Collider : public Component {
public:
- Collider(game_object_id_t id, vec2 offset);
+ Collider(game_object_id_t id, const vec2& offset);
public:
//! Offset of the collider relative to rigidbody position
diff --git a/src/crepe/api/BoxCollider.cpp b/src/crepe/api/BoxCollider.cpp
index 6034837..1069e90 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,vec2 offset, float width, float height) : Collider(game_object_id,offset), width(width), height(height) {}
+BoxCollider::BoxCollider(game_object_id_t game_object_id,const vec2& offset, float width, float 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 2ce1ee8..c83d54a 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,vec2 offset, float width, float height);
+ BoxCollider(game_object_id_t game_object_id,const vec2& offset, float width, float height);
//! Width of box collider
float width;
diff --git a/src/crepe/api/CircleCollider.cpp b/src/crepe/api/CircleCollider.cpp
index 497ebba..473734e 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,vec2 offset, float radius) : Collider(game_object_id,offset), radius(radius) {}
+CircleCollider::CircleCollider(game_object_id_t game_object_id,const vec2& offset, float radius) : Collider(game_object_id,offset), radius(radius) {}
diff --git a/src/crepe/api/CircleCollider.h b/src/crepe/api/CircleCollider.h
index c61f027..bbcc330 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,vec2 offset, float radius);
+ CircleCollider(game_object_id_t game_object_id,const vec2& offset, float radius);
//! Radius of the circle collider.
float radius;
diff --git a/src/crepe/api/Rigidbody.h b/src/crepe/api/Rigidbody.h
index 446c5dd..63b1b51 100644
--- a/src/crepe/api/Rigidbody.h
+++ b/src/crepe/api/Rigidbody.h
@@ -54,7 +54,7 @@ public:
//! objects mass
float mass = 0.0;
//! gravtiy scale
- float gravity_scale = 0.0;
+ float gravity_scale = 0;
//! Changes if physics apply
BodyType body_type = BodyType::DYNAMIC;
//! linear velocity of object
@@ -62,20 +62,16 @@ public:
//! maximum linear velocity of object
vec2 max_linear_velocity = {INFINITY ,INFINITY};
//! linear damping of object
- vec2 linear_damping;
+ vec2 linear_velocity_factor;
//! angular velocity of object
- float angular_velocity = 0.0;
+ float angular_velocity = 1;
//! max angular velocity of object
float max_angular_velocity = INFINITY;
//! angular damping of object
- float angular_damping = 0.0;
+ float angular_velocity_factor = 1;
//! movements constraints of object
PhysicsConstraints constraints;
- //! if gravity applies
- bool use_gravity = true;
- //! if object bounces
- bool bounce = false;
- //! bounce factor of material
+ //! bounce factor of material. 0.0 <= means all velocity is lost, 1.0 means it gets the same momentum but the mirrored direction. 0.5 is half of the velocity is saved.
float elastisity = 0.0;
//! offset of all colliders relative to transform position
vec2 offset;
diff --git a/src/crepe/system/CollisionSystem.cpp b/src/crepe/system/CollisionSystem.cpp
index 2fe3621..fbb16e8 100644
--- a/src/crepe/system/CollisionSystem.cpp
+++ b/src/crepe/system/CollisionSystem.cpp
@@ -146,10 +146,10 @@ std::pair<vec2,CollisionSystem::Direction> CollisionSystem::collision_handler(Co
resolution.x = data1.rigidbody.data.linear_velocity.x * (resolution.y/data1.rigidbody.data.linear_velocity.y);
}
- return {resolution,resolution_direction};
+ return std::make_pair(resolution,resolution_direction);
}
-vec2 CollisionSystem::get_box_box_resolution(const BoxCollider& box_collider1,const BoxCollider& box_collider2,vec2 final_position1,vec2 final_position2) const
+vec2 CollisionSystem::get_box_box_resolution(const BoxCollider& box_collider1,const BoxCollider& box_collider2,const vec2& final_position1,const vec2& final_position2) const
{
vec2 resolution; // Default resolution vector
vec2 delta = final_position2 - final_position1;
@@ -183,7 +183,7 @@ vec2 CollisionSystem::get_box_box_resolution(const BoxCollider& box_collider1,co
return resolution;
}
-vec2 CollisionSystem::get_circle_circle_resolution(const CircleCollider& circle_collider1, const CircleCollider& circle_collider2, vec2 final_position1, vec2 final_position2) const
+vec2 CollisionSystem::get_circle_circle_resolution(const CircleCollider& circle_collider1, const CircleCollider& circle_collider2, const vec2& final_position1, const vec2& final_position2) const
{
vec2 delta = final_position2 - final_position1;
@@ -205,7 +205,7 @@ vec2 CollisionSystem::get_circle_circle_resolution(const CircleCollider& circle_
return resolution;
}
-vec2 CollisionSystem::get_circle_box_resolution(const CircleCollider& circle_collider, const BoxCollider& box_collider, vec2 circle_position, vec2 box_position) const
+vec2 CollisionSystem::get_circle_box_resolution(const CircleCollider& circle_collider, const BoxCollider& box_collider, const vec2& circle_position, const vec2& box_position) const
{
vec2 delta = circle_position - box_position;
@@ -252,7 +252,7 @@ void CollisionSystem::static_collision_handler(CollisionInfo& info){
info.first_transform.position += info.resolution;
// If bounce is enabled mirror velocity
- if(info.first_rigidbody.data.bounce) {
+ if(info.first_rigidbody.data.elastisity > 0) {
if(info.resolution_direction == Direction::BOTH)
{
info.first_rigidbody.data.linear_velocity.y = -info.first_rigidbody.data.linear_velocity.y * info.first_rigidbody.data.elastisity;
@@ -335,12 +335,12 @@ CollisionSystem::get_active_transform_and_rigidbody(game_object_id_t game_object
RefVector<Transform> transforms = this->component_manager.get_components_by_id<Transform>(game_object_id);
if (transforms.empty()) return std::nullopt;
+ Transform& transform = transforms.front().get();
+ if (!transform.active) return std::nullopt;
+
RefVector<Rigidbody> rigidbodies = this->component_manager.get_components_by_id<Rigidbody>(game_object_id);
if (rigidbodies.empty()) return std::nullopt;
- Transform& transform = transforms.front().get();
- if (!transform.active) return std::nullopt;
-
Rigidbody& rigidbody = rigidbodies.front().get();
if (!rigidbody.active) return std::nullopt;
@@ -453,7 +453,7 @@ bool CollisionSystem::get_circle_circle_collision(const CircleCollider& circle1,
return distance_squared <= radius_sum * radius_sum;
}
-vec2 CollisionSystem::get_current_position(vec2 collider_offset, const Transform& transform, const Rigidbody& rigidbody) const {
+vec2 CollisionSystem::get_current_position(const vec2& collider_offset, const Transform& transform, const Rigidbody& rigidbody) const {
// Get the rotation in radians
float radians1 = transform.rotation * (M_PI / 180.0);
diff --git a/src/crepe/system/CollisionSystem.h b/src/crepe/system/CollisionSystem.h
index 3f24db1..34c03ae 100644
--- a/src/crepe/system/CollisionSystem.h
+++ b/src/crepe/system/CollisionSystem.h
@@ -104,7 +104,7 @@ private:
* \param rigidbody The Rigidbody of the associated game object.
* \return The calculated position of the collider.
*/
- vec2 get_current_position(vec2 collider_offset, const Transform& transform, const Rigidbody& rigidbody) const;
+ vec2 get_current_position(const vec2& collider_offset, const Transform& transform, const Rigidbody& rigidbody) const;
private:
@@ -141,7 +141,7 @@ private:
* \param position2 The position of the second BoxCollider.
* \return The resolution vector for the collision.
*/
- vec2 get_box_box_resolution(const BoxCollider& box_collider1,const BoxCollider& box_collider2,vec2 position1,vec2 position2) const;
+ vec2 get_box_box_resolution(const BoxCollider& box_collider1,const BoxCollider& box_collider2,const vec2& position1,const vec2& position2) const;
/**
* \brief Calculates the resolution vector for two CircleCollider.
@@ -154,7 +154,7 @@ private:
* \param position2 The position of the second CircleCollider.
* \return The resolution vector for the collision.
*/
- vec2 get_circle_circle_resolution(const CircleCollider& circle_collider1, const CircleCollider& circle_collider2, vec2 final_position1, vec2 final_position2) const;
+ vec2 get_circle_circle_resolution(const CircleCollider& circle_collider1, const CircleCollider& circle_collider2, const vec2& final_position1, const vec2& final_position2) const;
/**
* \brief Calculates the resolution vector for two CircleCollider.
@@ -167,7 +167,7 @@ private:
* \param box_position The position of the BocCollider.
* \return The resolution vector for the collision.
*/
- vec2 get_circle_box_resolution(const CircleCollider& circle_collider, const BoxCollider& box_collider, vec2 circle_position, vec2 box_position) const;
+ vec2 get_circle_box_resolution(const CircleCollider& circle_collider, const BoxCollider& box_collider, const vec2& circle_position, const vec2& box_position) const;
/**
* \brief Determines the appropriate collision handler for a collision.
diff --git a/src/crepe/system/PhysicsSystem.cpp b/src/crepe/system/PhysicsSystem.cpp
index 514a4b3..8f21727 100644
--- a/src/crepe/system/PhysicsSystem.cpp
+++ b/src/crepe/system/PhysicsSystem.cpp
@@ -25,17 +25,17 @@ void PhysicsSystem::update() {
if (transform.game_object_id == rigidbody.game_object_id) {
// Add gravity
- if (rigidbody.data.use_gravity) {
+ if (rigidbody.data.gravity_scale > 0) {
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_velocity_factor != 1 && rigidbody.data.angular_velocity_factor > 0) {
+ rigidbody.data.angular_velocity *= rigidbody.data.angular_velocity_factor;
}
- if (rigidbody.data.linear_damping != vec2{0, 0}) {
- rigidbody.data.linear_velocity *= rigidbody.data.linear_damping;
+ if (rigidbody.data.linear_velocity_factor != vec2{1, 1} && rigidbody.data.linear_velocity_factor.x > 0 && rigidbody.data.linear_velocity_factor.y > 0) {
+ rigidbody.data.linear_velocity *= rigidbody.data.linear_velocity_factor;
}
// Max velocity check
diff --git a/src/test/CMakeLists.txt b/src/test/CMakeLists.txt
index 616e238..4555c0b 100644
--- a/src/test/CMakeLists.txt
+++ b/src/test/CMakeLists.txt
@@ -13,5 +13,5 @@ target_sources(test_main PUBLIC
# ValueBrokerTest.cpp
# DBTest.cpp
# Vector2Test.cpp
- # Profiling.cpp
+ Profiling.cpp
)
diff --git a/src/test/CollisionTest.cpp b/src/test/CollisionTest.cpp
index ed40b1b..92ff7ba 100644
--- a/src/test/CollisionTest.cpp
+++ b/src/test/CollisionTest.cpp
@@ -65,7 +65,6 @@ public:
world.add_component<Rigidbody>(Rigidbody::Data{
// TODO: remove unrelated properties:
.body_type = Rigidbody::BodyType::STATIC,
- .bounce = false,
.offset = {0,0},
});
// Create a box with an inner size of 10x10 units
@@ -80,8 +79,6 @@ public:
.body_type = Rigidbody::BodyType::DYNAMIC,
.linear_velocity = {0,0},
.constraints = {0, 0, 0},
- .use_gravity = true,
- .bounce = true,
.elastisity = 1,
.offset = {0,0},
});
@@ -96,8 +93,6 @@ public:
.body_type = Rigidbody::BodyType::DYNAMIC,
.linear_velocity = {0,0},
.constraints = {0, 0, 0},
- .use_gravity = true,
- .bounce = true,
.elastisity = 1,
.offset = {0,0},
});
diff --git a/src/test/Profiling.cpp b/src/test/Profiling.cpp
index 2549c57..a88bf85 100644
--- a/src/test/Profiling.cpp
+++ b/src/test/Profiling.cpp
@@ -79,7 +79,8 @@ public:
void SetUp() override {
GameObject do_not_use = mgr.new_object("DO_NOT_USE","",{0,0});
- do_not_use.add_component<Camera>(Color::WHITE);
+ do_not_use.add_component<Camera>(Color::WHITE, ivec2{1080, 720},
+ vec2{2000, 2000}, 1.0f);
// initialize systems here:
//calls init
script_sys.update();
@@ -110,14 +111,19 @@ public:
// Print timings of all functions
void log_timings() const {
- std::stringstream ss;
- ss << "\nFunction timings:\n";
- for (const auto& [name, duration] : timings) {
- ss << name << " took " << duration.count() / 1000.0 / average << " ms (" << duration.count() / average << " µs).\n";
- }
- ss << "Total time: " << this->total_time.count() / 1000.0 / average << " ms (" << this->total_time.count() / average << " µs)\n";
- ss << "Amount of gameobjects: " << game_object_count << "\n";
- GTEST_LOG_(INFO) << ss.str();
+ std::string result = "\nFunction timings:\n";
+
+ for (const auto& [name, duration] : timings) {
+ result += name + " took " + std::to_string(duration.count() / 1000.0 / average) + " ms (" +
+ std::to_string(duration.count() / average) + " µs).\n";
+ }
+
+ result += "Total time: " + std::to_string(this->total_time.count() / 1000.0 / average) + " ms (" +
+ std::to_string(this->total_time.count() / average) + " µs)\n";
+
+ result += "Amount of gameobjects: " + std::to_string(game_object_count) + "\n";
+
+ GTEST_LOG_(INFO) << result;
}
void clear_timings() {
@@ -158,15 +164,14 @@ TEST_F(Profiling, Profiling_2) {
//define gameobject used for testing
GameObject gameobject = mgr.new_object("gameobject","",{static_cast<float>(game_object_count*2),0});
gameobject.add_component<Rigidbody>(Rigidbody::Data{
- .body_type = Rigidbody::BodyType::STATIC,
- .use_gravity = false,
+ .gravity_scale = 0.0,
+ .body_type = Rigidbody::BodyType::STATIC,
});
gameobject.add_component<BoxCollider>(vec2{0, 0}, 1, 1);
gameobject.add_component<BehaviorScript>().set_script<TestScript>();
Color color(0, 0, 0, 0);
- gameobject.add_component<Sprite>(
- make_shared<Texture>("asset/texture/green_square.png"), color,
- FlipSettings{true, true});
+ auto img = Texture("asset/texture/green_square.png");
+ Sprite & test_sprite = gameobject.add_component<Sprite>(img, color, Sprite::FlipSettings{false, false}, 1, 1, 500);
}
this->game_object_count++;
@@ -190,17 +195,14 @@ TEST_F(Profiling, Profiling_3) {
//define gameobject used for testing
GameObject gameobject = mgr.new_object("gameobject","",{static_cast<float>(game_object_count*2),0});
gameobject.add_component<Rigidbody>(Rigidbody::Data{
+ .gravity_scale = 0,
.body_type = Rigidbody::BodyType::STATIC,
- .use_gravity = false,
});
gameobject.add_component<BoxCollider>(vec2{0, 0}, 1, 1);
gameobject.add_component<BehaviorScript>().set_script<TestScript>();
Color color(0, 0, 0, 0);
- gameobject.add_component<Sprite>(
- make_shared<Texture>("asset/texture/green_square.png"), color,
- FlipSettings{true, true});
- Sprite & test_sprite = gameobject.add_component<Sprite>(
- make_shared<Texture>("asset/texture/img.png"), color, FlipSettings{false, false});
+ auto img = Texture("asset/texture/green_square.png");
+ Sprite & test_sprite = gameobject.add_component<Sprite>(img, color, Sprite::FlipSettings{false, false}, 1, 1, 500);
auto & test = gameobject.add_component<ParticleEmitter>(ParticleEmitter::Data{
.max_particles = 10,
.emission_rate = 100,