From 09fd0514f6ece247715ee17fbbf72720339df97e Mon Sep 17 00:00:00 2001 From: Dennis Kobert Date: Mon, 4 Jan 2021 01:34:26 +0100 Subject: [PATCH] Calculate the difference between bounding boxes --- rask-engine/src/engine/mod.rs | 15 +++++++++- rask-engine/src/engine/systems.rs | 49 ++++++++++++++++++++++++++++--- 2 files changed, 59 insertions(+), 5 deletions(-) diff --git a/rask-engine/src/engine/mod.rs b/rask-engine/src/engine/mod.rs index 9152c85e..97ca56c7 100644 --- a/rask-engine/src/engine/mod.rs +++ b/rask-engine/src/engine/mod.rs @@ -95,9 +95,22 @@ impl GameEngine for RaskEngine { entity: _background, }) .build(); + let _ground = world + .create_entity() + .with(SubCollider { + collider: crate::boxes::AABox { + pos: Vec2::new(0.0, -1.0), + size: Vec2::new(2.0, 0.5), + } + .into(), + }) + .with(Parent { + entity: _background, + }) + .build(); let _char = world .create_entity() - .with(Pos(Vec2::new(0.0, 0.8))) + .with(Pos(Vec2::new(0.0, 0.0))) .with(Vel(Vec2::new(0.0, 0.0))) .with(Speed(0.2)) .with(Mass(1.0)) diff --git a/rask-engine/src/engine/systems.rs b/rask-engine/src/engine/systems.rs index d165cd0a..3a24acb8 100644 --- a/rask-engine/src/engine/systems.rs +++ b/rask-engine/src/engine/systems.rs @@ -141,10 +141,12 @@ impl<'a> System<'a> for UpdateAnimationSystem { WriteStorage<'a, Parent>, ReadStorage<'a, Present>, ReadStorage<'a, Scale>, - ReadStorage<'a, Pos>, + WriteStorage<'a, Pos>, + WriteStorage<'a, Vel>, Entities<'a>, ReadExpect<'a, Hierarchy>, Read<'a, ElapsedTime>, + Read<'a, DeltaTime>, ); fn run( @@ -160,18 +162,21 @@ impl<'a> System<'a> for UpdateAnimationSystem { mut parent, present, scale, - pos, + mut pos, + mut vel, entities, hierarchy, elapsed, + dt, ): Self::SystemData, ) { let res = &mut *resources::RESOURCE_TABLE.write(); - for (mut animation, collider, scale, pos, entity, _) in ( + for (mut animation, collider, scale, pos, vel, entity, _) in ( &mut animations, &collider, &scale, - &pos, + &mut pos, + &mut vel, &entities, &present, ) @@ -257,6 +262,42 @@ impl<'a> System<'a> for UpdateAnimationSystem { .flatten(), ); + use crate::boxes::AABox; + let f = |old: AABox, new: AABox, dv, c: fn(Vec2) -> f32| { + let dv = c(dv) > 0.0; + let (new, old) = if dv { + (c(new.pos + new.size), c(old.pos + old.size)) + } else { + (c(old.pos), c(new.pos)) + }; + if dv == (new - old > 0.0) { + new - old + } else { + 0.0 + } + }; + + let x = f(aabb, new_aabb, vel.0, Vec2::x); + let y = f(aabb, new_aabb, vel.0, Vec2::y); + if x.is_finite() && y.is_finite() { + let diff = Vec2::new(x, y); + pos.0 += diff; + vel.0 -= diff / dt.0.as_secs_f32(); + log::debug!("x: {}, y: {}, vel: {:?}", x, y, vel.0); + for e in hierarchy.children(entity) { + if let Some(trans) = mat3.get_mut(*e) { + trans.mat3 *= Mat3::translation(diff.x(), diff.y()); + } + if let Some(sub) = sub.get_mut(*e) { + match sub.collider { + Collidable::AABox(mut a) => a.pos += diff, + Collidable::RBox(mut r) => r.pos += diff, + Collidable::Point(mut p) => p += diff, + }; + } + } + } + // modify position to avoid collisions } }