use crate::dynamics::{GenericJoint, GenericJointBuilder, JointAxesMask};
use crate::dynamics::{JointAxis, MotorModel};
use crate::math::{Real, Vect};
use super::TypedJoint;
#[cfg_attr(feature = "serde-serialize", derive(Serialize, Deserialize))]
#[derive(Copy, Clone, Debug, PartialEq)]
#[repr(transparent)]
pub struct SpringJoint {
pub data: GenericJoint,
}
impl SpringJoint {
pub fn new(rest_length: Real, stiffness: Real, damping: Real) -> Self {
let data = GenericJointBuilder::new(JointAxesMask::empty())
.coupled_axes(JointAxesMask::LIN_AXES)
.motor_position(JointAxis::LinX, rest_length, stiffness, damping)
.motor_model(JointAxis::LinX, MotorModel::ForceBased)
.build();
Self { data }
}
pub fn contacts_enabled(&self) -> bool {
self.data.contacts_enabled()
}
pub fn set_contacts_enabled(&mut self, enabled: bool) -> &mut Self {
self.data.set_contacts_enabled(enabled);
self
}
#[must_use]
pub fn local_anchor1(&self) -> Vect {
self.data.local_anchor1()
}
pub fn set_local_anchor1(&mut self, anchor1: Vect) -> &mut Self {
self.data.set_local_anchor1(anchor1);
self
}
#[must_use]
pub fn local_anchor2(&self) -> Vect {
self.data.local_anchor2()
}
pub fn set_local_anchor2(&mut self, anchor2: Vect) -> &mut Self {
self.data.set_local_anchor2(anchor2);
self
}
pub fn set_spring_model(&mut self, model: MotorModel) -> &mut Self {
self.data.set_motor_model(JointAxis::LinX, model);
self
}
}
impl From<SpringJoint> for GenericJoint {
fn from(val: SpringJoint) -> GenericJoint {
val.data
}
}
#[cfg_attr(feature = "serde-serialize", derive(Serialize, Deserialize))]
#[derive(Copy, Clone, Debug, PartialEq)]
pub struct SpringJointBuilder(pub SpringJoint);
impl SpringJointBuilder {
pub fn new(rest_length: Real, stiffness: Real, damping: Real) -> Self {
Self(SpringJoint::new(rest_length, stiffness, damping))
}
#[must_use]
pub fn contacts_enabled(mut self, enabled: bool) -> Self {
self.0.set_contacts_enabled(enabled);
self
}
#[must_use]
pub fn local_anchor1(mut self, anchor1: Vect) -> Self {
self.0.set_local_anchor1(anchor1);
self
}
#[must_use]
pub fn local_anchor2(mut self, anchor2: Vect) -> Self {
self.0.set_local_anchor2(anchor2);
self
}
#[must_use]
pub fn spring_model(mut self, model: MotorModel) -> Self {
self.0.set_spring_model(model);
self
}
#[must_use]
pub fn build(self) -> SpringJoint {
self.0
}
}
impl From<SpringJointBuilder> for TypedJoint {
fn from(joint: SpringJointBuilder) -> TypedJoint {
joint.0.into()
}
}
impl From<SpringJoint> for TypedJoint {
fn from(joint: SpringJoint) -> TypedJoint {
TypedJoint::SpringJoint(joint)
}
}