Compare commits

..

No commits in common. "4b1933f3f8462387f340211afd8c91a56b8e0524" and "a4408e624c16e422117d53e30e6be5edcddb41c8" have entirely different histories.

5 changed files with 87 additions and 196 deletions

16
Cargo.lock generated
View file

@ -4,21 +4,21 @@ version = 4
[[package]] [[package]]
name = "autocfg" name = "autocfg"
version = "1.5.0" version = "1.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c08606f8c3cbf4ce6ec8e28fb0014a2c086708fe954eaa885384a6165172e7e8" checksum = "ace50bade8e6234aa140d9a2f552bbee1db4d353f69b8217bc503490fc1a9f26"
[[package]] [[package]]
name = "bitflags" name = "bitflags"
version = "2.9.1" version = "2.9.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1b8e56985ec62d17e9c1001dc89c88ecd7dc08e47eba5ec7c29c7b5eeecde967" checksum = "5c8214115b7bf84099f1309324e63141d4c5d7cc26862f97a0a857dbefe165bd"
[[package]] [[package]]
name = "cc" name = "cc"
version = "1.2.31" version = "1.2.20"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c3a42d84bb6b69d3a8b3eaacf0d88f179e1929695e1ad012b6cf64d9caaa5fd2" checksum = "04da6a0d40b948dfc4fa8f5bbf402b0fc1a64a28dbf7d12ffd683550f2c1b63a"
dependencies = [ dependencies = [
"shlex", "shlex",
] ]
@ -34,9 +34,9 @@ dependencies = [
[[package]] [[package]]
name = "libc" name = "libc"
version = "0.2.174" version = "0.2.172"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1171693293099992e19cddea4e8b849964e9846f4acee11b3948bcc337be8776" checksum = "d750af042f7ef4f724306de029d18836c26c1765a54a6a3f094cbd23a7267ffa"
[[package]] [[package]]
name = "libflac-sys" name = "libflac-sys"

View file

@ -1,31 +1,29 @@
use crate::math;
use crate::particle::Particle; use crate::particle::Particle;
use std::cell::RefCell;
use std::rc::Rc; use std::rc::Rc;
use std::cell::RefCell;
pub struct Link { pub struct Link {
p1: Rc<RefCell<Particle>>, p1: Rc<RefCell<Particle>>,
p2: Rc<RefCell<Particle>>, p2: Rc<RefCell<Particle>>,
length: f32, length: f32
} }
impl Link { impl Link {
pub fn new(p1: Rc<RefCell<Particle>>, p2: Rc<RefCell<Particle>>, length: f32) -> Self { pub fn new(p1: Rc<RefCell<Particle>>, p2: Rc<RefCell<Particle>>, length: f32) -> Self {
Link { p1, p2, length } Link {
p1: p1,
p2: p2,
length: length
}
} }
pub fn solve(&mut self) { pub fn solve(&mut self) {
let vec = self.p2.borrow().pos - self.p1.borrow().pos; let vec = self.p2.borrow().pos - self.p1.borrow().pos;
let vec_len = math::vec_len(vec); let vec_len = (vec.x*vec.x + vec.y*vec.y).sqrt();
let vec_norm = vec/vec_len;
let vec_scaled = vec_norm * (self.length-vec_len) * 9.;
if vec_len > self.length { self.p1.borrow_mut().apply_force(-vec_scaled);
let vec_norm = vec / vec_len; self.p2.borrow_mut().apply_force(vec_scaled);
let displacement = self.length - vec_len;
let vec_scaled = vec_norm * displacement / 2.;
self.p1.borrow_mut().apply_vel(-vec_scaled);
self.p2.borrow_mut().apply_vel(vec_scaled);
}
} }
} }

View file

@ -1,11 +1,9 @@
use sfml::SfResult;
use sfml::graphics::*; use sfml::graphics::*;
use sfml::system::{Clock, Vector2f};
use sfml::window::*; use sfml::window::*;
use std::cell::RefCell; use sfml::SfResult;
use sfml::system::{Vector2f, Clock};
use std::rc::Rc; use std::rc::Rc;
use std::cell::RefCell;
mod math;
mod particle; mod particle;
use particle::Particle; use particle::Particle;
@ -13,148 +11,67 @@ use particle::Particle;
mod link; mod link;
use link::Link; use link::Link;
const WIN_WIDTH: u32 = 800; //const GRAVITY: f32 = 100.;
const WIN_HEIGHT: u32 = 600;
const GRAVITY: Vector2f = Vector2f::new(0., 1500.); fn populate_particles(mut particles: Vec<Rc<RefCell<Particle>>>, num: u32) {
for i in 0..=num {
const SUBSTEP_COUNT: u32 = 16; particles.push(Rc::new(RefCell::new(Particle::new(Vector2f::new(100. * i as f32, 300.)))));
fn populate_particles(particles: &mut Vec<Rc<RefCell<Particle>>>, num: u32, cols: u32) {
for i in 0..num {
const OFFSET: u32 = 10;
let x_pos = (/* WIN_WIDTH/2 - cols/2*OFFSET */120 + OFFSET * (i % cols)) as f32;
let y_pos = (/* WIN_HEIGHT/2 - (num/cols)/2*OFFSET */40 + OFFSET * (i / cols)) as f32;
let immovable = i == 0 || i == cols - 1 || i == (cols / 2);
let particle = Particle::new((x_pos, y_pos), immovable);
particles.push(Rc::new(RefCell::new(particle)));
} }
} }
fn populate_circles(circles: &mut Vec<CircleShape>, num: u32, rad: f32, pts: usize) { fn populate_links(links: Vec<Link>, particles: Vec<Rc<RefCell<Particle>>>) {
for _ in 0..num {
let mut circle = CircleShape::new(rad, pts);
circle.set_origin((rad, rad));
circles.push(circle);
}
} }
fn populate_links( fn apply_forces(particles: Vec<Rc<RefCell<Particle>>>) {
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 dist = math::vec_len(particles[i].borrow().pos - particles[right].borrow().pos);
let link = Link::new(Rc::clone(&particles[i]), Rc::clone(&particles[right]), dist);
links.push(link);
}
if y < height - 1 {
let below = i + width;
let dist = math::vec_len(particles[i].borrow().pos - particles[below].borrow().pos);
let link = Link::new(Rc::clone(&particles[i]), Rc::clone(&particles[below]), dist);
links.push(link);
}
}
}
}
/*fn populate_lines(lines: &mut Vec<RectangleShape>, circles: &Vec<CircleShape>, num: u32) {
for i in 0..num {
let rect = RectangleShape::with_size(Vector2f::new(100., 100.));
}
}*/
fn apply_forces(particles: &Vec<Rc<RefCell<Particle>>>) {
for particle in particles { for particle in particles {
let velocity = particle.borrow().vel;
let mut borrowed = particle.borrow_mut(); let mut borrowed = particle.borrow_mut();
borrowed.apply_force(GRAVITY); // borrowed.apply_force(Vector2f::new(0., GRAVITY));
borrowed.apply_force(-(velocity * 0.5)); borrowed.apply_force(Vector2f::new(1., 1.));
} }
} }
fn update_particles(particles: &Vec<Rc<RefCell<Particle>>>, dt: f32) { fn update_particles(particles: Vec<Rc<RefCell<Particle>>>, dt: f32) {
for particle in particles { for particle in particles { particle.borrow_mut().update(dt); }
particle.borrow_mut().update(dt);
}
} }
fn solve_links(links: &mut Vec<Link>) { fn solve_links(links: Vec<Link>) {
for link in links { for mut link in links { link.solve(); }
link.solve();
}
} }
fn update_particle_derivatives(particles: &Vec<Rc<RefCell<Particle>>>, dt: f32) { fn update_positions(circles: Vec<CircleShape>) {
for particle in particles {
particle.borrow_mut().update_derivatives(dt);
}
}
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> */
) {
for circle in circles {
window.draw(circle);
}
// for line in lines {
// window.draw(line);
// }
} }
fn main() -> SfResult<()> { fn main() -> SfResult<()> {
let mut window = RenderWindow::new( let mut window = RenderWindow::new(
(WIN_WIDTH, WIN_HEIGHT), (800, 600),
"Verlet", "Verlet",
Style::CLOSE, Style::CLOSE,
&Default::default(), &Default::default()
)?; )?;
window.set_framerate_limit(60); window.set_framerate_limit(60);
let mut clock = Clock::start()?; let mut clock = Clock::start()?;
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 = 2000;
let column_count: u32 = 50;
let row_count: u32 = particle_count / column_count;
let mut particles: Vec<Rc<RefCell<Particle>>> = vec![]; let mut particles: Vec<Rc<RefCell<Particle>>> = vec![];
populate_particles(&mut particles, particle_count, column_count); populate_particles(particles, 3);
let particle1 = Rc::new(RefCell::new(Particle::new(Vector2f::new(200., 300.))));
let particle2 = Rc::new(RefCell::new(Particle::new(Vector2f::new(300., 300.))));
let particle3 = Rc::new(RefCell::new(Particle::new(Vector2f::new(400., 300.))));
let mut links: Vec<Link> = vec![]; let mut link1 = Link::new(Rc::clone(&particle1), Rc::clone(&particle2), 100.);
populate_links( let mut link2 = Link::new(Rc::clone(&particle2), Rc::clone(&particle3), 100.);
&mut links, let mut link3 = Link::new(Rc::clone(&particle1), Rc::clone(&particle3), 200.);
&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 = 1.; let radius = 32.;
let point_count: usize = 100; let mut circle1 = CircleShape::new(radius, 100);
let mut circles: Vec<CircleShape> = vec![]; let mut circle2 = CircleShape::new(radius, 100);
populate_circles(&mut circles, particle_count, radius, point_count); let mut circle3 = CircleShape::new(radius, 100);
circle1.set_origin((radius, radius));
// let mut lines: Vec<RectangleShape> = vec![]; circle2.set_origin((radius, radius));
// populate_lines(&mut lines, &circles, particle_count); circle3.set_origin((radius, radius));
while window.is_open() { while window.is_open() {
while let Some(event) = window.poll_event() { while let Some(event) = window.poll_event() {
@ -164,37 +81,33 @@ fn main() -> SfResult<()> {
} }
} }
if mouse::Button::is_pressed(mouse::Button::Left) {
let mouse_pos = window.mouse_position(); let mouse_pos = window.mouse_position();
let mouse_coords = window.map_pixel_to_coords_current_view(mouse_pos); let mouse_coords = window.map_pixel_to_coords_current_view(mouse_pos);
let mouse_vel = mouse_coords - mouse_coords_prev; let p_pos = particle1.borrow().pos;
mouse_coords_prev = mouse_coords; particle2.borrow_mut().apply_force(mouse_coords - p_pos);
if mouse::Button::is_pressed(mouse::Button::Left) {
for particle in &particles {
let p_pos = particle.borrow().pos;
let dist_vec = mouse_coords - p_pos;
let dist = math::vec_len(dist_vec);
if dist < 40. {
particle.borrow_mut().apply_force(mouse_vel * 800.);
}
}
} }
let dt = clock.restart().as_seconds(); let dt = clock.restart().as_seconds();
let substep_dt = dt / SUBSTEP_COUNT as f32;
//for _ in 0..SUBSTEP_COUNT { //particle.apply_force(Vector2f::new(0., GRAVITY));
apply_forces(&particles);
update_particles(&particles, dt); particle1.borrow_mut().update(dt);
for _ in 0..SUBSTEP_COUNT { particle2.borrow_mut().update(dt);
solve_links(&mut links); particle3.borrow_mut().update(dt);
}
update_particle_derivatives(&particles, dt); link1.solve();
update_positions(&mut circles, &particles); link2.solve();
//} link3.solve();
circle1.set_position(particle1.borrow().pos);
circle2.set_position(particle2.borrow().pos);
circle3.set_position(particle3.borrow().pos);
window.clear(Color::BLACK); window.clear(Color::BLACK);
draw_all(&mut window, &circles /* &lines */); window.draw(&circle1);
window.draw(&circle2);
window.draw(&circle3);
window.display(); window.display();
} }

View file

@ -1,5 +0,0 @@
use sfml::system::Vector2f;
pub fn vec_len(vec: Vector2f) -> f32 {
(vec.x * vec.x + vec.y * vec.y).sqrt()
}

View file

@ -3,43 +3,28 @@ use sfml::system::Vector2f;
pub struct Particle { pub struct Particle {
pub pos: Vector2f, pub pos: Vector2f,
prev_pos: Vector2f, prev_pos: Vector2f,
pub vel: Vector2f, accel: Vector2f
accel: Vector2f,
immovable: bool,
} }
impl Particle { impl Particle {
pub fn new<P: Into<Vector2f>>(pos: P, immovable: bool) -> Self { pub fn new(pos: Vector2f) -> Self {
let pos = pos.into();
Particle { Particle {
pos, pos: pos,
prev_pos: pos, prev_pos: pos,
vel: Vector2f::default(), accel: Vector2f::default()
accel: Vector2f::default(),
immovable,
} }
} }
pub fn apply_force<F: Into<Vector2f>>(&mut self, force: F) { pub fn apply_force(&mut self, force: Vector2f) {
self.accel += force.into(); self.accel += force;
} }
pub fn update(&mut self, dt: f32) { pub fn update(&mut self, dt: f32) {
if self.immovable {return} let vel = (self.pos - self.prev_pos) * 0.99;
self.prev_pos = self.pos; let new_pos = self.pos + vel + self.accel * (dt * dt);
self.vel += self.accel * dt;
self.pos += self.vel * dt;
}
pub fn update_derivatives(&mut self, dt: f32) { self.prev_pos = self.pos;
self.vel = (self.pos - self.prev_pos) / dt; self.pos = new_pos;
self.accel = Vector2f::default(); self.accel = Vector2f::default();
} }
pub fn apply_vel(&mut self, vel: Vector2f) {
if self.immovable {return}
self.pos += vel;
}
} }