use crate::{
collision::shape::Shape,
forces::force::Force,
vec::vec_2d::{vec2, Vec2d},
};
#[derive(Debug, Clone, Copy)]
pub struct Object2d {
pub vec: Vec2d,
pub density: f32,
pub mass: f32,
pub velocity: Vec2d,
pub acceleration: Vec2d,
pub radius: f32,
pub shape: Shape,
}
impl Object2d {
#[must_use]
pub fn new(
vec: Vec2d,
density: f32,
mass: f32,
velocity: Vec2d,
acceleration: Vec2d,
radius: f32,
shape: Shape,
) -> Self {
Object2d {
vec,
density,
mass,
velocity,
acceleration,
radius,
shape,
}
}
pub fn apply<T: Force>(&mut self, force: &T) {
force.apply_2d(self);
}
}
#[deprecated(since = "0.1.3", note = "Use the Object2dBuilder instead")]
#[inline]
pub fn obj2d<V: Into<Vec2d>>(
vec: V,
density: f32,
mass: f32,
velocity: V,
acceleration: V,
radius: f32,
shape: Shape,
) -> Object2d {
Object2d::new(
vec.into(),
density,
mass,
velocity.into(),
acceleration.into(),
radius,
shape,
)
}
#[derive(Debug)]
pub struct Object2dBuilder {
position: Vec2d,
density: f32,
mass: f32,
velocity: Vec2d,
acceleration: Vec2d,
radius: f32,
shape: Shape,
}
impl Object2dBuilder {
#[must_use]
pub fn new() -> Self {
Object2dBuilder {
position: vec2(0.0, 0.0),
density: 0.0,
mass: 0.0,
velocity: vec2(0.0, 0.0),
acceleration: vec2(0.0, 0.0),
radius: 0.0,
shape: Shape::None,
}
}
#[must_use]
pub fn position(mut self, vec: Vec2d) -> Self {
self.position = vec;
self
}
#[must_use]
pub fn density(mut self, density: f32) -> Self {
self.density = density;
self
}
#[must_use]
pub fn mass(mut self, mass: f32) -> Self {
self.mass = mass;
self
}
#[must_use]
pub fn velocity(mut self, vec: Vec2d) -> Self {
self.velocity = vec;
self
}
#[must_use]
pub fn acceleration(mut self, vec: Vec2d) -> Self {
self.acceleration = vec;
self
}
#[must_use]
pub fn radius(mut self, radius: f32) -> Self {
self.radius = radius;
self
}
#[must_use]
pub fn shape(mut self, shape: Shape) -> Self {
self.shape = shape;
self
}
#[must_use]
pub fn build(self) -> Object2d {
Object2d {
vec: self.position,
density: self.density,
mass: self.mass,
velocity: self.velocity,
acceleration: self.acceleration,
radius: self.radius,
shape: self.shape,
}
}
}
impl Default for Object2dBuilder {
fn default() -> Self {
Self::new()
}
}