summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authoreug-vs <eugene@eug-vs.xyz>2024-12-15 13:30:57 +0100
committereug-vs <eugene@eug-vs.xyz>2024-12-15 13:30:57 +0100
commit5d8a34d0cf4f1c35f5fd714fbf7cd37502632b79 (patch)
tree8037fa20a262e966ccb2af648d3b837e50c43145
parent70afc5a7d871919776a64782e8b93404e6b0defd (diff)
downloadparticle-physics-5d8a34d0cf4f1c35f5fd714fbf7cd37502632b79.tar.gz
feat: allow watching particles
-rw-r--r--playground/src/main.rs53
1 files changed, 43 insertions, 10 deletions
diff --git a/playground/src/main.rs b/playground/src/main.rs
index 1cb8198..f585efa 100644
--- a/playground/src/main.rs
+++ b/playground/src/main.rs
@@ -1,9 +1,9 @@
use raylib::prelude::*;
+use physics::algebra::{Point, Scalar, Vector, N};
use physics::force::{drag::Drag, gravity::Gravity, spring::Spring};
use physics::nalgebra::Point as PointBase;
use physics::particle_system::{Particle, ParticleSystem};
-use physics::algebra::{Point, Scalar, Vector, N};
use physics::renderer::Camera;
use physics::solver::Solver;
@@ -42,7 +42,7 @@ fn main() {
Box::new(Spring {
particle_ids: [4, 2],
spring_constant: 0.75,
- damping_constant: 0.1,
+ damping_constant: 0.3,
rest_length: 20.0,
}),
],
@@ -58,6 +58,7 @@ fn main() {
system.add_beam_constraint([5, 4]);
let mut selected_particle_id = None;
+ let mut watch_particle_id = 0;
let (mut rl, thread) = raylib::init()
.size(640, 480)
@@ -83,7 +84,11 @@ fn main() {
Color::BLACK,
);
- let camera = Camera::new(Point::origin(), Vector::y(), Vector::x());
+ let camera = Camera::new(
+ system.particles[watch_particle_id].position,
+ Vector::y(),
+ Vector::x(),
+ );
match selected_particle_id {
Some(particle_id) => {
@@ -139,12 +144,23 @@ fn main() {
}
}
+ if d.is_mouse_button_pressed(MouseButton::MOUSE_BUTTON_RIGHT) {
+ let mouse_point =
+ PointBase::<f32, 2>::new(d.get_mouse_x() as f32, d.get_mouse_y() as f32);
+ let position = PointBase::<f32, 2>::new(position.x as f32, position.y as f32);
+ if (position - mouse_point).norm() < radius {
+ watch_particle_id = particle_id
+ }
+ }
+
d.draw_circle(
position.x,
position.y,
radius,
if selected_particle_id.is_some_and(|v| v == particle_id) {
Color::RED
+ } else if watch_particle_id == particle_id {
+ Color::YELLOW
} else {
Color::BLACK
},
@@ -153,8 +169,14 @@ fn main() {
for c in &system.constraints {
let particle_ids = c.get_particles();
if particle_ids.len() == 2 {
- let a = screen_space_to_raylib(system.particles[particle_ids[0]].position, &d);
- let b = screen_space_to_raylib(system.particles[particle_ids[1]].position, &d);
+ let a = screen_space_to_raylib(
+ camera.world_to_screen_space(system.particles[particle_ids[0]].position),
+ &d,
+ );
+ let b = screen_space_to_raylib(
+ camera.world_to_screen_space(system.particles[particle_ids[1]].position),
+ &d,
+ );
d.draw_line(a.x, a.y, b.x, b.y, Color::GRAY);
}
@@ -163,8 +185,14 @@ fn main() {
{
// Hard-coded spring
let particle_ids = [4, 2];
- let a = screen_space_to_raylib(system.particles[particle_ids[0]].position, &d);
- let b = screen_space_to_raylib(system.particles[particle_ids[1]].position, &d);
+ let a = screen_space_to_raylib(
+ camera.world_to_screen_space(system.particles[particle_ids[0]].position),
+ &d,
+ );
+ let b = screen_space_to_raylib(
+ camera.world_to_screen_space(system.particles[particle_ids[1]].position),
+ &d,
+ );
d.draw_line(a.x, a.y, b.x, b.y, Color::GREEN);
}
{
@@ -172,9 +200,14 @@ fn main() {
match selected_particle_id {
Some(id) => {
let mouse_particle_id = system.particles.len() - 1;
- let a = screen_space_to_raylib(system.particles[id].position, &d);
- let b =
- screen_space_to_raylib(system.particles[mouse_particle_id].position, &d);
+ let a = screen_space_to_raylib(
+ camera.world_to_screen_space(system.particles[id].position),
+ &d,
+ );
+ let b = screen_space_to_raylib(
+ camera.world_to_screen_space(system.particles[mouse_particle_id].position),
+ &d,
+ );
d.draw_line(a.x, a.y, b.x, b.y, Color::GREEN);
}