egui-circles/src/circle.rs
2024-07-03 22:22:53 +02:00

59 lines
1.3 KiB
Rust

extern crate emath;
extern crate rand;
use self::emath::Vec2;
use self::emath::Pos2;
use self::emath::Rect;
use self::rand::Rng;
pub static FRICTION: f32 = 0.995;
pub struct Circle {
pub v: Vec2,
pub c: Pos2,
pub r: f32,
}
impl Default for Circle {
fn default() -> Self {
let r = rand::thread_rng().gen_range(20.0..50.0);
Self {
r: r,
c: Pos2 {
x: rand::thread_rng().gen_range(r..400.0-r),
y: rand::thread_rng().gen_range(r..400.0-r)},
v: Vec2 {
x: rand::thread_rng().gen_range(-2.0..2.0),
y: rand::thread_rng().gen_range(-2.0..2.0)},
}
}
}
impl Circle {
pub fn apply_force(&mut self, bb: &Rect) {
self.v *= FRICTION;
self.c += self.v;
if self.v.x > 0.0 {
if self.c.x + self.r > bb.right() {
self.v.x *= -1.0
}
} else {
if self.c.x - self.r < bb.left() {
self.v.x *= -1.0
}
}
if self.v.y > 0.0 {
if self.c.y + self.r > bb.bottom() {
self.v.y *= -1.0
}
} else {
if self.c.y - self.r < bb.top() {
self.v.y *= -1.0
}
}
}
}