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

Commit f185ef7

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

File tree

2 files changed

+56
-5
lines changed

2 files changed

+56
-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: 42 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,39 @@ 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 u = |d, b| if b { d } else { 0.0 };
268+
if c(dv) > 0.0 {
269+
let d = c(old.pos + old.size) - c(new.pos + new.size);
270+
u(d, d < 0.0)
271+
} else {
272+
let d = c(old.pos - new.pos);
273+
u(d, d > 0.0)
274+
}
275+
};
276+
277+
let x = f(aabb, new_aabb, vel.0, Vec2::x);
278+
let y = f(aabb, new_aabb, vel.0, Vec2::y);
279+
if x.is_finite() && y.is_finite() {
280+
let diff = Vec2::new(x, y);
281+
pos.0 += diff;
282+
vel.0 -= diff / dt.0.as_secs_f32();
283+
log::debug!("x: {}, y: {}, vel: {:?}", x, y, vel.0);
284+
for e in hierarchy.children(entity) {
285+
if let Some(trans) = mat3.get_mut(*e) {
286+
trans.mat3 *= Mat3::translation(diff.x(), diff.y());
287+
}
288+
if let Some(sub) = sub.get_mut(*e) {
289+
match sub.collider {
290+
Collidable::AABox(mut a) => a.pos += diff,
291+
Collidable::RBox(mut r) => r.pos += diff,
292+
Collidable::Point(mut p) => p += diff,
293+
};
294+
}
295+
}
296+
}
297+
260298
// modify position to avoid collisions
261299
}
262300
}

0 commit comments

Comments
 (0)