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

108
Cargo.lock generated Normal file
View file

@ -0,0 +1,108 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 4
[[package]]
name = "autocfg"
version = "1.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ace50bade8e6234aa140d9a2f552bbee1db4d353f69b8217bc503490fc1a9f26"
[[package]]
name = "bitflags"
version = "2.9.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5c8214115b7bf84099f1309324e63141d4c5d7cc26862f97a0a857dbefe165bd"
[[package]]
name = "cc"
version = "1.2.20"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "04da6a0d40b948dfc4fa8f5bbf402b0fc1a64a28dbf7d12ffd683550f2c1b63a"
dependencies = [
"shlex",
]
[[package]]
name = "cmake"
version = "0.1.54"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e7caa3f9de89ddbe2c607f4101924c5abec803763ae9534e4f4d7d8f84aa81f0"
dependencies = [
"cc",
]
[[package]]
name = "libc"
version = "0.2.172"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d750af042f7ef4f724306de029d18836c26c1765a54a6a3f094cbd23a7267ffa"
[[package]]
name = "libflac-sys"
version = "0.3.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "01b01d08e4f670c184ffe3c3e5efbf0bd6e88c8cca796c278c86bbf49583545c"
dependencies = [
"cmake",
"libc",
]
[[package]]
name = "link-cplusplus"
version = "1.0.10"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4a6f6da007f968f9def0d65a05b187e2960183de70c160204ecfccf0ee330212"
dependencies = [
"cc",
]
[[package]]
name = "num-traits"
version = "0.2.19"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "071dfc062690e90b734c0b2273ce72ad0ffa95f0c74596bc250dcfd960262841"
dependencies = [
"autocfg",
]
[[package]]
name = "pkg-config"
version = "0.3.32"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7edddbd0b52d732b21ad9a5fab5c704c14cd949e5e9a1ec5929a24fded1b904c"
[[package]]
name = "sfml"
version = "0.24.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "941169c8be33fd81c006591c0dff6056f94bca0bff6057a262ba946bdb6dc0ed"
dependencies = [
"bitflags",
"cc",
"cmake",
"libflac-sys",
"link-cplusplus",
"num-traits",
"pkg-config",
"widestring",
]
[[package]]
name = "shlex"
version = "1.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0fda2ff0d084019ba4d7c6f371c95d8fd75ce3524c3cb8fb653a3023f6323e64"
[[package]]
name = "verlet"
version = "0.1.0"
dependencies = [
"sfml",
]
[[package]]
name = "widestring"
version = "1.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dd7cf3379ca1aac9eea11fba24fd7e315d621f8dfe35c8d7d2be8b793726e07d"

7
Cargo.toml Normal file
View file

@ -0,0 +1,7 @@
[package]
name = "verlet"
version = "0.1.0"
edition = "2024"
[dependencies]
sfml = "0.24.0"

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();
}
}