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

Commit c1387cb

Browse files
committed
Debug strange velocity behaviour
1 parent f185ef7 commit c1387cb

File tree

3 files changed

+34
-11
lines changed

3 files changed

+34
-11
lines changed

rask-engine/src/engine/components.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,10 @@ impl PParent for Parent {
4848
#[storage(VecStorage)]
4949
pub struct Vel(pub Vec2);
5050

51+
#[derive(Debug, Clone, Copy, Component)]
52+
#[storage(VecStorage)]
53+
pub struct Vel_(pub Vec2);
54+
5155
#[derive(Debug, Clone, Copy, Component)]
5256
#[storage(VecStorage)]
5357
pub struct Pos(pub Vec2);

rask-engine/src/engine/mod.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,12 @@ impl GameEngine for RaskEngine {
8484
.build();
8585
let _ground = world
8686
.create_entity()
87+
.with(Pos(Vec2::new(-2.0, -1.0)))
88+
.with(Scale(Vec2::new(6.0, 0.15)))
89+
.with(Sprite {
90+
id: registry::EMPTY.id,
91+
sub_id: 0,
92+
})
8793
.with(SubCollider {
8894
collider: crate::boxes::AABox {
8995
pos: Vec2::new(-2.0, -1.0),
@@ -97,6 +103,12 @@ impl GameEngine for RaskEngine {
97103
.build();
98104
let _ground = world
99105
.create_entity()
106+
.with(Sprite {
107+
id: registry::EMPTY.id,
108+
sub_id: 0,
109+
})
110+
.with(Pos(Vec2::new(1.0, -1.0)))
111+
.with(Scale(Vec2::new(1.0, 0.5)))
100112
.with(SubCollider {
101113
collider: crate::boxes::AABox {
102114
pos: Vec2::new(0.0, -1.0),
@@ -112,6 +124,7 @@ impl GameEngine for RaskEngine {
112124
.create_entity()
113125
.with(Pos(Vec2::new(0.0, 0.0)))
114126
.with(Vel(Vec2::new(0.0, 0.0)))
127+
.with(Vel_(Vec2::zero()))
115128
.with(Speed(0.2))
116129
.with(Mass(1.0))
117130
.with(Animation {

rask-engine/src/engine/systems.rs

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ impl<'a> System<'a> for VelocitySystem {
4343
type SystemData = (
4444
WriteStorage<'a, Pos>,
4545
WriteStorage<'a, Vel>,
46+
ReadStorage<'a, Vel_>,
4647
ReadStorage<'a, Mass>,
4748
ReadStorage<'a, Collider>,
4849
WriteStorage<'a, Transform>,
@@ -53,16 +54,17 @@ impl<'a> System<'a> for VelocitySystem {
5354
Read<'a, DeltaTime>,
5455
);
5556

57+
#[rustfmt::skip]
5658
fn run(
5759
&mut self,
58-
(mut pos, mut vel, mass, collider, mut transform, sub, terrain, entities, hierarchy, dt): Self::SystemData,
60+
(mut pos, mut vel, vel_, mass, collider, mut transform, sub, terrain, entities, hierarchy, dt): Self::SystemData,
5961
) {
60-
let reset_values: Vec<_> = (&collider, &vel, !&terrain, &pos, &mass, &entities)
62+
let reset_values: Vec<_> = (&collider, &vel, (&vel_).maybe(), !&terrain, &pos, &mass, &entities)
6163
.par_join()
62-
.map(|(_col1, vel, _, _pos1, _mass, entity1)| {
64+
.map(|(_col1, vel, vel_, _, _pos1, _mass, entity1)| {
6365
let mut percent = -1.0;
6466
let mut ids = (entity1.id(), 0);
65-
let v = vel.0 * dt.0.as_secs_f32();
67+
let v = vel.0 * dt.0.as_secs_f32() + vel_.map(|x| x.0).unwrap_or_else(Vec2::zero);
6668
for (_, _, _pos2, entity2) in (&collider, &terrain, &pos, &entities).join() {
6769
for e1 in hierarchy.children(entity1) {
6870
for e2 in hierarchy.children(entity2) {
@@ -81,6 +83,7 @@ impl<'a> System<'a> for VelocitySystem {
8183
}
8284
}
8385
}
86+
log::debug!("woop {:?}", percent);
8487
if percent == -1.0 {
8588
(ids.0, core::u32::MAX, v)
8689
} else {
@@ -121,10 +124,8 @@ impl<'a> System<'a> for GravitationSystem {
121124

122125
fn run(&mut self, (mut vel, mass, present, g, dt): Self::SystemData) {
123126
let dt = dt.0.as_secs_f32();
124-
if dt < 0.5 {
125-
for (vel, _, _) in (&mut vel, &mass, &present).join() {
126-
vel.0 += g.0 * dt;
127-
}
127+
for (vel, _, _) in (&mut vel, &mass, &present).join() {
128+
vel.0 += g.0 * dt;
128129
}
129130
}
130131
}
@@ -143,6 +144,7 @@ impl<'a> System<'a> for UpdateAnimationSystem {
143144
ReadStorage<'a, Scale>,
144145
WriteStorage<'a, Pos>,
145146
WriteStorage<'a, Vel>,
147+
WriteStorage<'a, Vel_>,
146148
Entities<'a>,
147149
ReadExpect<'a, Hierarchy<Parent>>,
148150
Read<'a, ElapsedTime>,
@@ -164,19 +166,21 @@ impl<'a> System<'a> for UpdateAnimationSystem {
164166
scale,
165167
mut pos,
166168
mut vel,
169+
mut vel_,
167170
entities,
168171
hierarchy,
169172
elapsed,
170173
dt,
171174
): Self::SystemData,
172175
) {
173176
let res = &mut *resources::RESOURCE_TABLE.write();
174-
for (mut animation, collider, scale, pos, vel, entity, _) in (
177+
for (mut animation, collider, scale, pos, vel, mut vel__, entity, _) in (
175178
&mut animations,
176179
&collider,
177180
&scale,
178181
&mut pos,
179182
&mut vel,
183+
&mut vel_,
180184
&entities,
181185
&present,
182186
)
@@ -268,6 +272,8 @@ impl<'a> System<'a> for UpdateAnimationSystem {
268272
if c(dv) > 0.0 {
269273
let d = c(old.pos + old.size) - c(new.pos + new.size);
270274
u(d, d < 0.0)
275+
} else if c(dv) == 0.0 {
276+
0.0
271277
} else {
272278
let d = c(old.pos - new.pos);
273279
u(d, d > 0.0)
@@ -279,8 +285,8 @@ impl<'a> System<'a> for UpdateAnimationSystem {
279285
if x.is_finite() && y.is_finite() {
280286
let diff = Vec2::new(x, y);
281287
pos.0 += diff;
282-
vel.0 -= diff / dt.0.as_secs_f32();
283-
log::debug!("x: {}, y: {}, vel: {:?}", x, y, vel.0);
288+
vel__.0 = diff;
289+
log::debug!("pos: {:?}, x: {}, y: {}, vel: {:?}", pos.0, x, y, vel.0);
284290
for e in hierarchy.children(entity) {
285291
if let Some(trans) = mat3.get_mut(*e) {
286292
trans.mat3 *= Mat3::translation(diff.x(), diff.y());

0 commit comments

Comments
 (0)