diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/crepe/system/CollisionSystem.cpp | 72 | ||||
-rw-r--r-- | src/example/game.cpp | 23 |
2 files changed, 59 insertions, 36 deletions
diff --git a/src/crepe/system/CollisionSystem.cpp b/src/crepe/system/CollisionSystem.cpp index 44a0431..496224e 100644 --- a/src/crepe/system/CollisionSystem.cpp +++ b/src/crepe/system/CollisionSystem.cpp @@ -192,13 +192,15 @@ CollisionSystem::collision_handler(CollisionInternal & data1, CollisionInternal resolution_direction = Direction::BOTH; } else if (resolution.x != 0) { resolution_direction = Direction::X_DIRECTION; - if (data1.rigidbody.data.linear_velocity.y != 0) - resolution.y = data1.rigidbody.data.linear_velocity.y + //checks if the other velocity has a value and if this object moved + if (data1.rigidbody.data.linear_velocity.x != 0 && data1.rigidbody.data.linear_velocity.y != 0) + resolution.y = -data1.rigidbody.data.linear_velocity.y * (resolution.x / data1.rigidbody.data.linear_velocity.x); } else if (resolution.y != 0) { resolution_direction = Direction::Y_DIRECTION; - if (data1.rigidbody.data.linear_velocity.x != 0) - resolution.x = data1.rigidbody.data.linear_velocity.x + //checks if the other velocity has a value and if this object moved + if (data1.rigidbody.data.linear_velocity.x != 0 && data1.rigidbody.data.linear_velocity.y != 0) + resolution.x = -data1.rigidbody.data.linear_velocity.x * (resolution.y / data1.rigidbody.data.linear_velocity.y); } @@ -314,28 +316,46 @@ void CollisionSystem::static_collision_handler(CollisionInfo & info) { // Move object back using calculate move back value info.this_transform.position += info.resolution; - // If bounce is enabled mirror velocity - if (info.this_rigidbody.data.elastisity_coefficient > 0) { - if (info.resolution_direction == Direction::BOTH) { - info.this_rigidbody.data.linear_velocity.y - = -info.this_rigidbody.data.linear_velocity.y - * info.this_rigidbody.data.elastisity_coefficient; - info.this_rigidbody.data.linear_velocity.x - = -info.this_rigidbody.data.linear_velocity.x - * info.this_rigidbody.data.elastisity_coefficient; - } else if (info.resolution_direction == Direction::Y_DIRECTION) { - info.this_rigidbody.data.linear_velocity.y - = -info.this_rigidbody.data.linear_velocity.y - * info.this_rigidbody.data.elastisity_coefficient; - } else if (info.resolution_direction == Direction::X_DIRECTION) { - info.this_rigidbody.data.linear_velocity.x - = -info.this_rigidbody.data.linear_velocity.x - * info.this_rigidbody.data.elastisity_coefficient; - } - } - // Stop movement if bounce is disabled - else { - info.this_rigidbody.data.linear_velocity = {0, 0}; + switch (info.resolution_direction) { + case Direction::BOTH: + //bounce + if (info.this_rigidbody.data.elastisity_coefficient > 0) { + info.this_rigidbody.data.linear_velocity = -info.this_rigidbody.data.linear_velocity * info.this_rigidbody.data.elastisity_coefficient; + } + //stop movement + else { + info.this_rigidbody.data.linear_velocity = {0, 0}; + } + break; + case Direction::Y_DIRECTION: + // Bounce + if (info.this_rigidbody.data.elastisity_coefficient > 0) { + info.this_rigidbody.data.linear_velocity.y + = -info.this_rigidbody.data.linear_velocity.y + * info.this_rigidbody.data.elastisity_coefficient; + } + // Stop movement + else { + info.this_rigidbody.data.linear_velocity.y = 0; + info.this_transform.position.x -= info.resolution.x; + } + break; + case Direction::X_DIRECTION: + // Bounce + if (info.this_rigidbody.data.elastisity_coefficient > 0) { + info.this_rigidbody.data.linear_velocity.x + = -info.this_rigidbody.data.linear_velocity.x + * info.this_rigidbody.data.elastisity_coefficient; + } + // Stop movement + else { + info.this_rigidbody.data.linear_velocity.x = 0; + info.this_transform.position.y -= info.resolution.y; + } + break; + case Direction::NONE: + // Not possible + break; } } diff --git a/src/example/game.cpp b/src/example/game.cpp index 16fe18f..8ea50ea 100644 --- a/src/example/game.cpp +++ b/src/example/game.cpp @@ -29,23 +29,23 @@ class MyScript1 : public Script { Log::logf("Box script keypressed()"); switch (test.key) { case Keycode::A: { - Transform & tf = this->get_component<Transform>(); - tf.position.x -= 1; + Rigidbody & tf = this->get_component<Rigidbody>(); + tf.data.linear_velocity.x -= 1; break; } case Keycode::W: { - Transform & tf = this->get_component<Transform>(); - tf.position.y -= 1; + Rigidbody & tf = this->get_component<Rigidbody>(); + tf.data.linear_velocity.y -= 1; break; } case Keycode::S: { - Transform & tf = this->get_component<Transform>(); - tf.position.y += 1; + Rigidbody & tf = this->get_component<Rigidbody>(); + tf.data.linear_velocity.y += 1; break; } case Keycode::D: { - Transform & tf = this->get_component<Transform>(); - tf.position.x += 1; + Rigidbody & tf = this->get_component<Rigidbody>(); + tf.data.linear_velocity.x += 1; break; } case Keycode::E: { @@ -85,7 +85,10 @@ class MyScript1 : public Script { [this](const KeyPressEvent & ev) -> bool { return this->keypressed(ev); }); } void update() { - // Retrieve component from the same GameObject this script is on + Rigidbody & tf = this->get_component<Rigidbody>(); + Log::logf("linear_velocity.x {}", tf.data.linear_velocity.x); + Log::logf("linear_velocity.y {}", tf.data.linear_velocity.y); + // tf.data.linear_velocity = {0,0}; } }; @@ -200,7 +203,7 @@ public: .mass = 1, .gravity_scale = 1, .body_type = Rigidbody::BodyType::DYNAMIC, - .linear_velocity = {0, 0}, + .linear_velocity = {0, 1}, .constraints = {0, 0, 0}, .elastisity_coefficient = 1, .offset = {0, 0}, |