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

Commit 09fd051

Browse files
committed
Calculate the difference between bounding boxes
1 parent dd0a350 commit 09fd051

File tree

2 files changed

+59
-5
lines changed

2 files changed

+59
-5
lines changed

rask-engine/src/engine/mod.rs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,9 +95,22 @@ impl GameEngine for RaskEngine {
9595
entity: _background,
9696
})
9797
.build();
98+
let _ground = world
99+
.create_entity()
100+
.with(SubCollider {
101+
collider: crate::boxes::AABox {
102+
pos: Vec2::new(0.0, -1.0),
103+
size: Vec2::new(2.0, 0.5),
104+
}
105+
.into(),
106+
})
107+
.with(Parent {
108+
entity: _background,
109+
})
110+
.build();
98111
let _char = world
99112
.create_entity()
100-
.with(Pos(Vec2::new(0.0, 0.8)))
113+
.with(Pos(Vec2::new(0.0, 0.0)))
101114
.with(Vel(Vec2::new(0.0, 0.0)))
102115
.with(Speed(0.2))
103116
.with(Mass(1.0))

rask-engine/src/engine/systems.rs

Lines changed: 45 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -141,10 +141,12 @@ impl<'a> System<'a> for UpdateAnimationSystem {
141141
WriteStorage<'a, Parent>,
142142
ReadStorage<'a, Present>,
143143
ReadStorage<'a, Scale>,
144-
ReadStorage<'a, Pos>,
144+
WriteStorage<'a, Pos>,
145+
WriteStorage<'a, Vel>,
145146
Entities<'a>,
146147
ReadExpect<'a, Hierarchy<Parent>>,
147148
Read<'a, ElapsedTime>,
149+
Read<'a, DeltaTime>,
148150
);
149151

150152
fn run(
@@ -160,18 +162,21 @@ impl<'a> System<'a> for UpdateAnimationSystem {
160162
mut parent,
161163
present,
162164
scale,
163-
pos,
165+
mut pos,
166+
mut vel,
164167
entities,
165168
hierarchy,
166169
elapsed,
170+
dt,
167171
): Self::SystemData,
168172
) {
169173
let res = &mut *resources::RESOURCE_TABLE.write();
170-
for (mut animation, collider, scale, pos, entity, _) in (
174+
for (mut animation, collider, scale, pos, vel, entity, _) in (
171175
&mut animations,
172176
&collider,
173177
&scale,
174-
&pos,
178+
&mut pos,
179+
&mut vel,
175180
&entities,
176181
&present,
177182
)
@@ -257,6 +262,42 @@ impl<'a> System<'a> for UpdateAnimationSystem {
257262
.flatten(),
258263
);
259264

265+
use crate::boxes::AABox;
266+
let f = |old: AABox, new: AABox, dv, c: fn(Vec2) -> f32| {
267+
let dv = c(dv) > 0.0;
268+
let (new, old) = if dv {
269+
(c(new.pos + new.size), c(old.pos + old.size))
270+
} else {
271+
(c(old.pos), c(new.pos))
272+
};
273+
if dv == (new - old > 0.0) {
274+
new - old
275+
} else {
276+
0.0
277+
}
278+
};
279+
280+
let x = f(aabb, new_aabb, vel.0, Vec2::x);
281+
let y = f(aabb, new_aabb, vel.0, Vec2::y);
282+
if x.is_finite() && y.is_finite() {
283+
let diff = Vec2::new(x, y);
284+
pos.0 += diff;
285+
vel.0 -= diff / dt.0.as_secs_f32();
286+
log::debug!("x: {}, y: {}, vel: {:?}", x, y, vel.0);
287+
for e in hierarchy.children(entity) {
288+
if let Some(trans) = mat3.get_mut(*e) {
289+
trans.mat3 *= Mat3::translation(diff.x(), diff.y());
290+
}
291+
if let Some(sub) = sub.get_mut(*e) {
292+
match sub.collider {
293+
Collidable::AABox(mut a) => a.pos += diff,
294+
Collidable::RBox(mut r) => r.pos += diff,
295+
Collidable::Point(mut p) => p += diff,
296+
};
297+
}
298+
}
299+
}
300+
260301
// modify position to avoid collisions
261302
}
262303
}

0 commit comments

Comments
 (0)