Skip to content

Commit 0bdb07c

Browse files
committed
Demo scenes
1 parent c89c0d2 commit 0bdb07c

File tree

8 files changed

+510
-368
lines changed

8 files changed

+510
-368
lines changed

Cargo.lock

Lines changed: 17 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

blobs/src/physics.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -339,8 +339,7 @@ impl Physics {
339339
let displacement = (body.position - body.position_old) * (dt / self.old_dt);
340340
self.old_dt = dt;
341341

342-
self.spatial_hash
343-
.move_point(idx.to_bits(), displacement);
342+
self.spatial_hash.move_point(idx.to_bits(), displacement);
344343

345344
body.position_old = body.position;
346345
body.position += displacement + body.acceleration * dt * dt;

demo/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ macroquad = "0.3.25"
1313
glam = "0.23.0"
1414
egui-macroquad = "0.15.0"
1515
egui = "0.21.0"
16+
hecs = "0.10.3"
1617

1718
rapier2d = { version = "0.17.2", optional = true }
1819

demo/src/demos/balls.rs

Lines changed: 202 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,202 @@
1+
use crate::*;
2+
3+
pub struct BallsDemo {
4+
pub enable_autospawn: bool,
5+
pub random_radius: bool,
6+
7+
pub sim: Simulation,
8+
pub drag: Option<DragState>,
9+
pub hover: Option<HoverState>,
10+
}
11+
12+
impl BallsDemo {
13+
pub fn new() -> Self {
14+
// let gravity = vec2(0.0, -30.0);
15+
let gravity = vec2(0.0, 0.0);
16+
17+
// let mut sim = Simulation::new(Box::new(rapier_physics));
18+
19+
let mut sim = make_world(gravity);
20+
21+
let a = sim.balls.insert(TestObject {
22+
position: Vec2::ZERO,
23+
color: PINK,
24+
});
25+
26+
let mouse_rbd = sim.spawn_ball(
27+
RigidBodyDesc {
28+
position: Vec2::ZERO,
29+
body_type: RigidBodyType::Static,
30+
collision_groups: groups(0, 0),
31+
radius: 0.1,
32+
// gravity_mod: 0.0,
33+
..Default::default()
34+
},
35+
RED,
36+
);
37+
38+
// let pos = vec2(5.0, 5.0);
39+
//
40+
// let rbd = RigidBody {
41+
// position: pos,
42+
// position_old: pos,
43+
// gravity_mod: 1.0,
44+
// mass: 1.0,
45+
// velocity_request: Some(vec2(1.0, 0.)),
46+
// calculated_velocity: Vec2::ZERO,
47+
// acceleration: Vec2::ZERO,
48+
// rotation: 0.0,
49+
// scale: Vec2::ONE,
50+
// // angular_velocity: 0.0,
51+
// colliders: vec![],
52+
// user_data: 0,
53+
// connected_joints: vec![],
54+
// body_type: RigidBodyType::KinematicVelocityBased,
55+
// collision_groups: groups(0, 0),
56+
// };
57+
//
58+
// let rbd_handle = sim.physics.insert_rbd(rbd);
59+
60+
let a = sim.balls.insert(TestObject {
61+
position: Vec2::ZERO,
62+
color: GREEN,
63+
});
64+
65+
// let torque_test_rbd = spawn_rbd_entity(
66+
// &mut sim.physics,
67+
// a,
68+
// RigidBodyDesc {
69+
// position: vec2(-2.0, 2.0),
70+
// body_type: RigidBodyType::Dynamic,
71+
// collision_groups: groups(0, 0),
72+
// radius: 0.1,
73+
// // gravity_mod: 0.0,
74+
// ..Default::default()
75+
// },
76+
// );
77+
78+
Self {
79+
enable_autospawn: true,
80+
random_radius: true,
81+
82+
sim,
83+
drag: None,
84+
hover: None,
85+
}
86+
}
87+
88+
pub fn physics(&self) -> Ref<Physics> {
89+
self.sim.physics.borrow()
90+
}
91+
92+
pub fn physics_mut(&self) -> RefMut<Physics> {
93+
self.sim.physics.borrow_mut()
94+
}
95+
}
96+
97+
impl Demo for BallsDemo {
98+
fn update(&mut self, c: &mut DemoContext) {
99+
if is_key_down(KeyCode::Key1) {
100+
self.enable_autospawn = !self.enable_autospawn;
101+
}
102+
let mut physics = self.physics_mut();
103+
let physics = &mut *physics;
104+
105+
physics.fixed_step(c.delta);
106+
107+
let mut wants_ball = false;
108+
let position = random_around(vec2(1.0, 1.0), 0.1, 0.2);
109+
110+
// for (index, object) in sim.balls.iter() {
111+
// let collider = sim.physics.col_set.arena.get(index).unwrap();
112+
// let rbd_handle = collider.parent.unwrap();
113+
//
114+
// let mut hovered = false;
115+
//
116+
// if mouse_rbd != rbd_handle {
117+
// if mouse_world.distance(collider.absolute_translation()) < collider.radius {
118+
// hovered = true;
119+
//
120+
// hover = Some(HoverState {
121+
// index: rbd_handle,
122+
// position: collider.absolute_translation(),
123+
// });
124+
// }
125+
// }
126+
//
127+
// let color = if hovered {
128+
// RED.mix(object.color, 0.2)
129+
// } else {
130+
// object.color
131+
// };
132+
//
133+
// let rbd = sim.physics.get_rbd(rbd_handle).unwrap();
134+
//
135+
// draw_circle(collider.absolute_translation(), collider.radius, color);
136+
// let a = collider.absolute_translation();
137+
// let b = a + vec2(rbd.rotation.cos(), rbd.rotation.sin()) * 0.4;
138+
// draw_line(a.x, a.y, b.x, b.y, 0.05, YELLOW);
139+
//
140+
// let r = collider.radius;
141+
//
142+
// draw_texture_ex(
143+
// texture,
144+
// collider.absolute_translation().x - r,
145+
// collider.absolute_translation().y - r,
146+
// color.alpha(0.4),
147+
// DrawTextureParams {
148+
// dest_size: Some(macroquad::prelude::vec2(
149+
// collider.radius * 2.0,
150+
// collider.radius * 2.0,
151+
// )),
152+
// rotation: rbd.rotation,
153+
// ..Default::default()
154+
// },
155+
// );
156+
// }
157+
158+
if physics.rbd_set.len() < 200 && self.enable_autospawn {
159+
if c.cooldowns.can_use("ball", 0.1) {
160+
wants_ball = true;
161+
}
162+
}
163+
164+
if wants_ball {
165+
spawn_rbd_entity(
166+
physics,
167+
// Just temporarily for now
168+
thunderdome::Index::from_bits(1 << 32).unwrap(),
169+
RigidBodyDesc {
170+
position,
171+
initial_velocity: Some(random_circle(3.0)),
172+
radius: if self.random_radius {
173+
gen_range(0.05, 0.2)
174+
} else {
175+
gen_range(0.05, 0.1)
176+
},
177+
mass: 1.0,
178+
is_sensor: false,
179+
..Default::default()
180+
},
181+
);
182+
183+
// physics.spawn_kinematic_ball(
184+
// world,
185+
// c.commands,
186+
// if random_radius {
187+
// gen_range(0.05, 0.2)
188+
// } else {
189+
// gen_range(0.05, 0.1)
190+
// },
191+
// position,
192+
// Some(random_vec(1.0, 50.0)),
193+
// groups(1, 1),
194+
// (Sprite::new("1px".to_string(), splat(0.0), 0, RED),),
195+
// );
196+
}
197+
}
198+
199+
fn debug_data(&self) -> DebugData {
200+
self.physics().debug_data()
201+
}
202+
}

demo/src/demos/joints.rs

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
2+
{
3+
let mut force = None;
4+
5+
if is_key_down(KeyCode::W) {
6+
force = Some(vec2(0.0, 1.0));
7+
}
8+
if is_key_down(KeyCode::S) {
9+
force = Some(vec2(0.0, -1.0));
10+
}
11+
if is_key_down(KeyCode::A) {
12+
force = Some(vec2(-1.0, 0.0));
13+
}
14+
if is_key_down(KeyCode::D) {
15+
force = Some(vec2(1.0, 0.0));
16+
}
17+
18+
let rbd = self
19+
.physics
20+
.borrow_mut()
21+
.get_mut_rbd(torque_test_rbd)
22+
.unwrap();
23+
24+
let force_point = if is_key_down(KeyCode::LeftShift) {
25+
mouse_world
26+
} else {
27+
rbd.position
28+
};
29+
30+
if let Some(force) = force {
31+
rbd.apply_force_at_point(force, force_point);
32+
33+
let a = force_point;
34+
let b = a + force;
35+
36+
draw_line(a.x, a.y, b.x, b.y, 0.2, RED);
37+
}
38+
}
39+
40+
// draw_circle(mouse_world, 0.3, WHITE);
41+
42+
43+
// draw_circle(vec2(1.0, 1.0), 1.0, RED);
44+
// draw_circle(vec2(1.0, -1.0), 1.0, GREEN);
45+
// draw_circle(vec2(-1.0, -1.0), 1.0, BLUE);
46+
// draw_circle(vec2(-1.0, 1.0), 1.0, YELLOW);
47+
//
48+
49+
// if !ctx.wants_pointer_input() {
50+
// if is_mouse_button_down(MouseButton::Left) {
51+
// if cooldowns.can_use("ball", 0.005) {
52+
// wants_ball = true;
53+
// random_radius = true;
54+
// position = mouse_world;
55+
// }
56+
// }
57+
58+
if let Some(hover) = hover {
59+
if drag.is_none() && is_mouse_button_pressed(MouseButton::Left) {
60+
let spring = sim.physics.springs.insert(Spring {
61+
rigid_body_a: hover.index,
62+
rigid_body_b: mouse_rbd,
63+
rest_length: 1.0,
64+
stiffness: 1000.0,
65+
damping: 50.0,
66+
});
67+
68+
drag = Some(DragState {
69+
spring: SpringHandle(spring),
70+
index: hover.index,
71+
start: hover.position,
72+
offset: mouse_world - hover.position,
73+
});
74+
}
75+
}
76+
77+
if is_mouse_button_released(MouseButton::Left) {
78+
if let Some(drag) = drag {
79+
sim.physics.springs.remove(*drag.spring);
80+
}
81+
82+
drag = None;
83+
}
84+
85+
sim.physics.rbd_set.get_mut(mouse_rbd).unwrap().position = mouse_world;
86+
87+
if let Some(_drag) = drag {
88+
if is_mouse_button_down(MouseButton::Left) {
89+
// let blobs = sim.cast_physics::<blobs::Physics>();
90+
91+
// let rbd = blobs.rbd_set.arena.get_mut(drag.index.0).unwrap();
92+
93+
// rbd.position = drag.start + mouse_world - drag.offset;
94+
// rbd.position = mouse_world;
95+
}
96+
}
97+
98+
if is_mouse_button_pressed(MouseButton::Right) {
99+
random_radius = false;
100+
wants_ball = true;
101+
}
102+
103+
// if is_mouse_button_pressed(macroquad::prelude::MouseButton::Left) {
104+
// sim.spawn_ball(
105+
// RigidBodyDesc {
106+
// position: vec2(gen_range(-2.0, 2.0), gen_range(-2.0, 2.0)),
107+
// initial_velocity: Some(vec2(5.0, 2.0)),
108+
// radius: 0.5,
109+
// mass: 1.0,
110+
// is_sensor: false,
111+
// ..Default::default()
112+
// },
113+
// RED,
114+
// );
115+
//
116+
// // spawn_rbd_entity(
117+
// // &mut physics,
118+
// // );
119+
// }
120+
// }

demo/src/demos/mod.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
use crate::*;
2+
3+
mod balls;
4+
5+
pub use balls::*;
6+
7+
pub trait Demo {
8+
fn update(&mut self, c: &mut DemoContext);
9+
fn debug_data(&self) -> blobs::DebugData;
10+
}

0 commit comments

Comments
 (0)