gh push for access

This commit is contained in:
Raptorox 2025-05-15 10:13:50 +02:00
parent cbcdcea940
commit 7a290c9998
5 changed files with 282 additions and 0 deletions

29
src/link.rs Normal file
View file

@ -0,0 +1,29 @@
use crate::particle::Particle;
use std::rc::Rc;
use std::cell::RefCell;
pub struct Link {
p1: Rc<RefCell<Particle>>,
p2: Rc<RefCell<Particle>>,
length: f32
}
impl Link {
pub fn new(p1: Rc<RefCell<Particle>>, p2: Rc<RefCell<Particle>>, length: f32) -> Self {
Link {
p1: p1,
p2: p2,
length: length
}
}
pub fn solve(&mut self) {
let vec = self.p2.borrow().pos - self.p1.borrow().pos;
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.;
self.p1.borrow_mut().apply_force(-vec_scaled);
self.p2.borrow_mut().apply_force(vec_scaled);
}
}

108
src/main.rs Normal file
View file

@ -0,0 +1,108 @@
use sfml::graphics::*;
use sfml::window::*;
use sfml::SfResult;
use sfml::system::{Vector2f, Clock};
use std::rc::Rc;
use std::cell::RefCell;
mod particle;
use particle::Particle;
mod link;
use link::Link;
//const GRAVITY: f32 = 100.;
fn populate_particles(particles: Vec<Particle>) {
}
fn populate_links(links: Vec<Link>) {
}
fn apply_forces(particles: Vec<Particle>) {
}
fn update_particles(particles: Vec<Particle>) {
}
fn solve_links(links: Vec<Link>) {
}
fn update_positions(circles: Vec<CircleShape>) {
}
fn main() -> SfResult<()> {
let mut window = RenderWindow::new(
(800, 600),
"Verlet",
Style::CLOSE,
&Default::default()
)?;
window.set_framerate_limit(60);
let mut clock = Clock::start()?;
let mut particles: Vec<Particle> = vec![];
populate_particles(particles);
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 link1 = Link::new(Rc::clone(&particle1), Rc::clone(&particle2), 100.);
let mut link2 = Link::new(Rc::clone(&particle2), Rc::clone(&particle3), 100.);
let mut link3 = Link::new(Rc::clone(&particle1), Rc::clone(&particle3), 200.);
let radius = 32.;
let mut circle1 = CircleShape::new(radius, 100);
let mut circle2 = CircleShape::new(radius, 100);
let mut circle3 = CircleShape::new(radius, 100);
circle1.set_origin((radius, radius));
circle2.set_origin((radius, radius));
circle3.set_origin((radius, radius));
while window.is_open() {
while let Some(event) = window.poll_event() {
match event {
Event::Closed => window.close(),
_ => {}
}
}
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 = particle1.borrow().pos;
particle2.borrow_mut().apply_force(mouse_coords - p_pos);
}
let dt = clock.restart().as_seconds();
//particle.apply_force(Vector2f::new(0., GRAVITY));
particle1.borrow_mut().update(dt);
particle2.borrow_mut().update(dt);
particle3.borrow_mut().update(dt);
link1.solve();
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.draw(&circle1);
window.draw(&circle2);
window.draw(&circle3);
window.display();
}
Ok(())
}

30
src/particle.rs Normal file
View file

@ -0,0 +1,30 @@
use sfml::system::Vector2f;
pub struct Particle {
pub pos: Vector2f,
prev_pos: Vector2f,
accel: Vector2f
}
impl Particle {
pub fn new(pos: Vector2f) -> Self {
Particle {
pos: pos,
prev_pos: pos,
accel: Vector2f::default()
}
}
pub fn apply_force(&mut self, force: Vector2f) {
self.accel += force;
}
pub fn update(&mut self, dt: f32) {
let vel = (self.pos - self.prev_pos) * 0.99;
let new_pos = self.pos + vel + self.accel * (dt * dt);
self.prev_pos = self.pos;
self.pos = new_pos;
self.accel = Vector2f::default();
}
}