Skip to content
This repository has been archived by the owner on Mar 26, 2024. It is now read-only.

Commit

Permalink
Calculate the difference between bounding boxes
Browse files Browse the repository at this point in the history
  • Loading branch information
TrueDoctor committed Jan 4, 2021
1 parent dd0a350 commit 09fd051
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 5 deletions.
15 changes: 14 additions & 1 deletion rask-engine/src/engine/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
49 changes: 45 additions & 4 deletions rask-engine/src/engine/systems.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Parent>>,
Read<'a, ElapsedTime>,
Read<'a, DeltaTime>,
);

fn run(
Expand All @@ -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,
)
Expand Down Expand Up @@ -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
}
}
Expand Down

0 comments on commit 09fd051

Please sign in to comment.