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

Commit f2c8fc5

Browse files
committed
Bring the collision system into a somewhat usable state
1 parent c1387cb commit f2c8fc5

File tree

4 files changed

+57
-28
lines changed

4 files changed

+57
-28
lines changed

rask-engine/src/collide.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,23 @@ impl Collide for Collidable {
3838
}
3939
}
4040

41+
impl Collidable {
42+
pub fn shift(&self, dv: Vec2) -> Collidable {
43+
match self {
44+
Collidable::AABox(a) => Collidable::AABox(AABox {
45+
pos: a.pos + dv,
46+
size: a.size,
47+
}),
48+
Collidable::RBox(r) => Collidable::RBox(RBox {
49+
pos: r.pos + dv,
50+
v1: r.v1,
51+
v2: r.v2,
52+
}),
53+
Collidable::Point(p) => Collidable::Point(*p + dv),
54+
}
55+
}
56+
}
57+
4158
/// calculate the minimal axis aligned bounding box that contains all `Collidable`s of a given set
4259
pub fn calculate_aabb<'a, I: Iterator<Item = &'a Collidable>>(vals: I) -> AABox {
4360
let [(mut minx, mut maxx), (mut miny, mut maxy)] = [(f32::INFINITY, f32::NEG_INFINITY); 2];

rask-engine/src/engine/components.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,15 +44,15 @@ impl PParent for Parent {
4444
}
4545
}
4646

47-
#[derive(Debug, Clone, Copy, Component)]
47+
#[derive(Debug, Clone, Copy, Default, Component)]
4848
#[storage(VecStorage)]
4949
pub struct Vel(pub Vec2);
5050

51-
#[derive(Debug, Clone, Copy, Component)]
51+
#[derive(Debug, Clone, Copy, Default, Component)]
5252
#[storage(VecStorage)]
5353
pub struct Vel_(pub Vec2);
5454

55-
#[derive(Debug, Clone, Copy, Component)]
55+
#[derive(Debug, Clone, Copy, Default, Component)]
5656
#[storage(VecStorage)]
5757
pub struct Pos(pub Vec2);
5858

rask-engine/src/engine/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ impl GameEngine for RaskEngine {
125125
.with(Pos(Vec2::new(0.0, 0.0)))
126126
.with(Vel(Vec2::new(0.0, 0.0)))
127127
.with(Vel_(Vec2::zero()))
128-
.with(Speed(0.2))
128+
.with(Speed(0.4))
129129
.with(Mass(1.0))
130130
.with(Animation {
131131
id: registry::CHAR.id,

rask-engine/src/engine/systems.rs

Lines changed: 36 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -57,14 +57,15 @@ impl<'a> System<'a> for VelocitySystem {
5757
#[rustfmt::skip]
5858
fn run(
5959
&mut self,
60-
(mut pos, mut vel, vel_, mass, collider, mut transform, sub, terrain, entities, hierarchy, dt): Self::SystemData,
60+
(mut pos, mut vel, vel_, mass, collider, mut transform, mut sub, terrain, entities, hierarchy, dt): Self::SystemData,
6161
) {
62-
let reset_values: Vec<_> = (&collider, &vel, (&vel_).maybe(), !&terrain, &pos, &mass, &entities)
62+
let reset_values: Vec<_> = (&collider, &pos, &vel, (&vel_).maybe(), !&terrain, &pos, &mass, &entities)
6363
.par_join()
64-
.map(|(_col1, vel, vel_, _, _pos1, _mass, entity1)| {
64+
.map(|(_col1, &pos_, vel, vel_, _, _pos1, _mass, entity1)| {
6565
let mut percent = -1.0;
6666
let mut ids = (entity1.id(), 0);
67-
let v = vel.0 * dt.0.as_secs_f32() + vel_.map(|x| x.0).unwrap_or_else(Vec2::zero);
67+
let v_ = vel_.map(|x|x.0).unwrap_or_default();
68+
let v = vel.0 * dt.0.as_secs_f32() + v_;
6869
for (_, _, _pos2, entity2) in (&collider, &terrain, &pos, &entities).join() {
6970
for e1 in hierarchy.children(entity1) {
7071
for e2 in hierarchy.children(entity2) {
@@ -73,6 +74,8 @@ impl<'a> System<'a> for VelocitySystem {
7374
Some(SubCollider { collider: c2 }),
7475
) = (sub.get(*e1), sub.get(*e2))
7576
{
77+
let c1 = c1.shift(pos_.0 - v_);
78+
//log::debug!("c1:{:?}", pos_.0 - v);
7679
if let Some(move_out) = c1.collide_after(c2, v) {
7780
if move_out > percent {
7881
percent = move_out;
@@ -83,30 +86,36 @@ impl<'a> System<'a> for VelocitySystem {
8386
}
8487
}
8588
}
86-
log::debug!("woop {:?}", percent);
8789
if percent == -1.0 {
88-
(ids.0, core::u32::MAX, v)
90+
(ids.0, core::u32::MAX, v - v_)
8991
} else {
90-
(ids.0, ids.1, v * (1.0 - percent))
92+
log::debug!("rv {:?}, v_: {:?}", v, v_);
93+
(ids.0, ids.1, v * (1.0 - percent) - v_)
9194
}
9295
})
9396
.collect();
9497
for (e1, e2, rv) in reset_values {
9598
let e1 = entities.entity(e1);
9699
if let Some(pos) = pos.get_mut(e1) {
97100
pos.0 += rv;
98-
}
99-
if let Some(vel) = vel.get_mut(e1) {
100-
if e2 != core::u32::MAX {
101-
vel.0 = Vec2::zero();
102-
}
103-
}
104-
for e in hierarchy.children(e1) {
105-
if let Some(trans) = transform.get_mut(*e) {
106-
trans.mat3 *= Mat3::translation(rv.x(), rv.y()); // * trans.mat3;
101+
//log::debug!("new vel {:?}, new pos: {:?}", rv, pos.0);
102+
let trans_mat = Mat3::translation(pos.0.x(), pos.0.y());
103+
if let Some(vel) = vel.get_mut(e1) {
104+
if e2 != core::u32::MAX {
105+
vel.0 = Vec2::zero();
106+
}
107107
}
108-
if let Some(sub) = sub.get(*e) {
109-
//sub. += Mat3::translation(rv.x(), rv.y()) * trans.mat3;
108+
for e in hierarchy.children(e1) {
109+
if let Some(trans) = transform.get_mut(*e) {
110+
trans.mat3 = trans_mat * trans.mat3;
111+
}
112+
if let Some(sub) = sub.get_mut(*e) {
113+
match sub.collider {
114+
Collidable::AABox(mut a) => a.pos += rv,
115+
Collidable::RBox(mut r) => r.pos += rv,
116+
Collidable::Point(mut p) => p += rv,
117+
};
118+
}
110119
}
111120
}
112121
}
@@ -217,6 +226,7 @@ impl<'a> System<'a> for UpdateAnimationSystem {
217226
for (i, s) in sprites.enumerate() {
218227
let s = s.unwrap();
219228
let n_transform = trans * scale * s.transform;
229+
let n_transform = scale * s.transform;
220230
let (new_mat3, new_sprite, new_sub) = (
221231
Transform { mat3: n_transform },
222232
Sprite {
@@ -284,19 +294,21 @@ impl<'a> System<'a> for UpdateAnimationSystem {
284294
let y = f(aabb, new_aabb, vel.0, Vec2::y);
285295
if x.is_finite() && y.is_finite() {
286296
let diff = Vec2::new(x, y);
287-
pos.0 += diff;
288-
vel__.0 = diff;
289-
log::debug!("pos: {:?}, x: {}, y: {}, vel: {:?}", pos.0, x, y, vel.0);
297+
let diff = Vec2::new(0.0, y);
298+
//let diff = Vec2::new(0.0, 0.001);
299+
//pos.0 += diff;
300+
vel__.0 = diff * -1.1;
301+
//log::debug!("pos: {:?}, x: {}, y: {}, vel: {:?}", pos.0, x, y, vel.0);
290302
for e in hierarchy.children(entity) {
291303
if let Some(trans) = mat3.get_mut(*e) {
292-
trans.mat3 *= Mat3::translation(diff.x(), diff.y());
304+
//trans.mat3 *= Mat3::translation(diff.x(), diff.y());
293305
}
294306
if let Some(sub) = sub.get_mut(*e) {
295-
match sub.collider {
307+
/*match sub.collider {
296308
Collidable::AABox(mut a) => a.pos += diff,
297309
Collidable::RBox(mut r) => r.pos += diff,
298310
Collidable::Point(mut p) => p += diff,
299-
};
311+
};*/
300312
}
301313
}
302314
}

0 commit comments

Comments
 (0)