use crate::prelude::*;
use bevy::prelude::*;
#[derive(Clone, Copy, Debug, PartialEq, Reflect)]
#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "serialize", reflect(Serialize, Deserialize))]
#[reflect(Debug, PartialEq)]
pub enum MotorModel {
SpringDamper {
frequency: Scalar,
damping_ratio: Scalar,
},
ForceBased {
stiffness: Scalar,
damping: Scalar,
},
AccelerationBased {
stiffness: Scalar,
damping: Scalar,
},
}
impl Default for MotorModel {
fn default() -> Self {
Self::DEFAULT
}
}
impl MotorModel {
pub const DEFAULT: Self = Self::SpringDamper {
frequency: 5.0,
damping_ratio: 1.0,
};
}
#[derive(Clone, Copy, Debug, PartialEq, Reflect)]
#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "serialize", reflect(Serialize, Deserialize))]
#[reflect(Debug, PartialEq)]
pub struct AngularMotor {
pub enabled: bool,
pub target_velocity: Scalar,
pub target_position: Scalar,
pub max_torque: Scalar,
pub motor_model: MotorModel,
}
impl Default for AngularMotor {
fn default() -> Self {
Self::new(MotorModel::DEFAULT)
}
}
impl AngularMotor {
#[inline]
pub const fn new(motor_model: MotorModel) -> Self {
Self {
enabled: true,
target_velocity: 0.0,
target_position: 0.0,
max_torque: Scalar::MAX,
motor_model,
}
}
#[inline]
pub const fn new_disabled(motor_model: MotorModel) -> Self {
Self {
enabled: false,
..Self::new(motor_model)
}
}
#[inline]
pub const fn set_enabled(&mut self, enabled: bool) -> &mut Self {
self.enabled = enabled;
self
}
#[inline]
pub const fn with_target_velocity(mut self, velocity: Scalar) -> Self {
self.target_velocity = velocity;
self
}
#[inline]
pub const fn with_target_position(mut self, target_position: Scalar) -> Self {
self.target_position = target_position;
self
}
#[inline]
pub const fn with_max_torque(mut self, max_torque: Scalar) -> Self {
self.max_torque = max_torque;
self
}
#[inline]
pub const fn with_motor_model(mut self, motor_model: MotorModel) -> Self {
self.motor_model = motor_model;
self
}
}
#[derive(Clone, Copy, Debug, PartialEq, Reflect)]
#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "serialize", reflect(Serialize, Deserialize))]
#[reflect(Debug, PartialEq)]
pub struct LinearMotor {
pub enabled: bool,
pub target_velocity: Scalar,
pub target_position: Scalar,
pub max_force: Scalar,
pub motor_model: MotorModel,
}
impl Default for LinearMotor {
fn default() -> Self {
Self::new(MotorModel::DEFAULT)
}
}
impl LinearMotor {
#[inline]
pub const fn new(motor_model: MotorModel) -> Self {
Self {
enabled: true,
target_velocity: 0.0,
target_position: 0.0,
max_force: Scalar::MAX,
motor_model,
}
}
#[inline]
pub const fn new_disabled(motor_model: MotorModel) -> Self {
Self {
enabled: false,
..Self::new(motor_model)
}
}
#[inline]
pub const fn set_enabled(&mut self, enabled: bool) -> &mut Self {
self.enabled = enabled;
self
}
#[inline]
pub const fn with_target_velocity(mut self, velocity: Scalar) -> Self {
self.target_velocity = velocity;
self
}
#[inline]
pub const fn with_target_position(mut self, target_position: Scalar) -> Self {
self.target_position = target_position;
self
}
#[inline]
pub const fn with_max_force(mut self, max_force: Scalar) -> Self {
self.max_force = max_force;
self
}
#[inline]
pub const fn with_motor_model(mut self, motor_model: MotorModel) -> Self {
self.motor_model = motor_model;
self
}
}