update deps; tweak constants

This commit is contained in:
Raptorox 2025-06-05 17:55:35 +02:00
parent 57e28360a0
commit de5f28b4f7
No known key found for this signature in database
GPG key ID: 8B3556FC3ED1F6D8
4 changed files with 101 additions and 55 deletions

View file

@ -1,9 +1,9 @@
use sfml::graphics::*;
use sfml::window::*;
use sfml::SfResult;
use sfml::system::{Vector2f, Clock};
use std::rc::Rc;
use sfml::graphics::*;
use sfml::system::{Clock, Vector2f};
use sfml::window::*;
use std::cell::RefCell;
use std::rc::Rc;
mod particle;
use particle::Particle;
@ -15,8 +15,8 @@ const GRAVITY: f32 = 100.;
fn populate_particles(particles: &mut Vec<Rc<RefCell<Particle>>>, num: u32, cols: u32) {
for i in 0..num {
let x_pos = 200. + 50. * (i % cols) as f32;
let y_pos = 100. + 50. * (i / cols) as f32;
let x_pos = 150. + 20. * (i % cols) as f32;
let y_pos = 80. + 20. * (i / cols) as f32;
let immovable = i < cols;
let particle = Particle::new(Vector2f::new(x_pos, y_pos), immovable);
particles.push(Rc::new(RefCell::new(particle)));
@ -31,16 +31,41 @@ fn populate_circles(circles: &mut Vec<CircleShape>, num: u32, rad: f32, pts: usi
}
}
fn populate_links(links: &mut Vec<Link>, particles: &Vec<Rc<RefCell<Particle>>>, num: u32) {
for i in 0..(num-1) {
/*fn populate_links(links: &mut Vec<Link>, particles: &[Rc<RefCell<Particle>>], num: u32) {
for i in 0..(num - 1) {
let p1 = &particles[i as usize];
let p2 = &particles[(i as usize)+1];
let p2 = &particles[(i as usize) + 1];
//let vec = p2.borrow().pos - p1.borrow().pos;
//let dist = (vec.x*vec.x + vec.y*vec.y).sqrt();
let link = Link::new(Rc::clone(p1), Rc::clone(p2), 100.);
links.push(link);
}
}*/
fn populate_links(
links: &mut Vec<Link>,
particles: &[Rc<RefCell<Particle>>],
width: usize,
height: usize,
) {
for y in 0..height {
for x in 0..width {
let i = y * width + x;
if x < width - 1 {
let right = i + 1;
let link = Link::new(Rc::clone(&particles[i]), Rc::clone(&particles[right]), 20.);
links.push(link);
}
if y < height - 1 {
let below = i + width;
let link = Link::new(Rc::clone(&particles[i]), Rc::clone(&particles[below]), 20.);
links.push(link);
}
}
}
}
/*fn populate_lines(lines: &mut Vec<RectangleShape>, circles: &Vec<CircleShape>, num: u32) {
@ -53,25 +78,31 @@ fn apply_forces(particles: &Vec<Rc<RefCell<Particle>>>) {
for particle in particles {
let mut borrowed = particle.borrow_mut();
borrowed.apply_force(Vector2f::new(0., GRAVITY));
}
}
fn update_particles(particles: &Vec<Rc<RefCell<Particle>>>, dt: f32) {
for particle in particles { particle.borrow_mut().update(dt); }
for particle in particles {
particle.borrow_mut().update(dt);
}
}
fn solve_links(links: &mut Vec<Link>) {
for link in links { link.solve(); }
for link in links {
link.solve();
}
}
fn update_positions(circles: &mut Vec<CircleShape>, particles: &Vec<Rc<RefCell<Particle>>>) {
fn update_positions(circles: &mut Vec<CircleShape>, particles: &[Rc<RefCell<Particle>>]) {
for (index, circle) in circles.iter_mut().enumerate() {
circle.set_position(particles[index].borrow().pos);
}
}
fn draw_all(window: &mut RenderWindow, circles: &Vec<CircleShape>, /* lines: &Vec<RectangleShape> */) {
fn draw_all(
window: &mut RenderWindow,
circles: &Vec<CircleShape>, /* lines: &Vec<RectangleShape> */
) {
for circle in circles {
window.draw(circle);
}
@ -82,27 +113,31 @@ fn draw_all(window: &mut RenderWindow, circles: &Vec<CircleShape>, /* lines: &Ve
}
fn main() -> SfResult<()> {
let mut window = RenderWindow::new(
(800, 600),
"Verlet",
Style::CLOSE,
&Default::default()
)?;
let mut window = RenderWindow::new((800, 600), "Verlet", Style::CLOSE, &Default::default())?;
window.set_framerate_limit(60);
let mut clock = Clock::start()?;
let particle_count: u32 = 60;
let column_count: u32 = 10;
let mouse_pos_prev = window.mouse_position();
let mut mouse_coords_prev = window.map_pixel_to_coords_current_view(mouse_pos_prev);
let particle_count: u32 = 600;
let column_count: u32 = 30;
let row_count: u32 = particle_count / column_count;
let mut particles: Vec<Rc<RefCell<Particle>>> = vec![];
populate_particles(&mut particles, particle_count, column_count);
let mut links: Vec<Link> = vec![];
populate_links(&mut links, &particles, particle_count);
populate_links(
&mut links,
&particles,
column_count as usize,
row_count as usize,
);
// let mut link3 = Link::new(Rc::clone(&(particles[0])), Rc::clone(&(particles[2])), 200.);
let radius: f32 = 32.;
let radius: f32 = 8.;
let point_count: usize = 100;
let mut circles: Vec<CircleShape> = vec![];
populate_circles(&mut circles, particle_count, radius, point_count);
@ -112,17 +147,27 @@ fn main() -> SfResult<()> {
while window.is_open() {
while let Some(event) = window.poll_event() {
#[allow(clippy::single_match)]
match event {
Event::Closed => window.close(),
_ => {}
}
}
let mouse_pos = window.mouse_position();
let mouse_coords = window.map_pixel_to_coords_current_view(mouse_pos);
let mouse_vel = mouse_coords - mouse_coords_prev;
mouse_coords_prev = mouse_coords;
if mouse::Button::is_pressed(mouse::Button::Left) {
let mouse_pos = window.mouse_position();
let mouse_coords = window.map_pixel_to_coords_current_view(mouse_pos);
let p_pos = particles[10].borrow().pos;
particles[10].borrow_mut().apply_force((mouse_coords - p_pos)*4.);
for particle in &particles {
let p_pos = particle.borrow().pos;
let dist_vec = mouse_coords - p_pos;
let dist = (dist_vec.x * dist_vec.x + dist_vec.y * dist_vec.y).sqrt();
if dist < 20. {
particle.borrow_mut().apply_force(mouse_vel * 128.);
}
}
}
let dt = clock.restart().as_seconds();
@ -137,7 +182,7 @@ fn main() -> SfResult<()> {
update_positions(&mut circles, &particles);
window.clear(Color::BLACK);
draw_all(&mut window, &circles, /* &lines */);
draw_all(&mut window, &circles /* &lines */);
window.display();
}