From 93e5e6d27ed2efb5b4f6fed453645e42c3920113 Mon Sep 17 00:00:00 2001 From: Shane Celis Date: Sat, 21 Sep 2024 21:33:02 -0400 Subject: [PATCH] feature: Add From impls for DistanceLimit. Add From and From<(Scalar, Scalar)> for DistanceLimit. --- crates/avian2d/examples/distance_joint_2d.rs | 2 +- crates/avian3d/examples/distance_joint_3d.rs | 2 +- src/dynamics/solver/joints/distance.rs | 32 +++++++++++++------- src/dynamics/solver/joints/mod.rs | 20 ++++++++++++ 4 files changed, 43 insertions(+), 13 deletions(-) diff --git a/crates/avian2d/examples/distance_joint_2d.rs b/crates/avian2d/examples/distance_joint_2d.rs index 77aa85d0..f2bde85b 100644 --- a/crates/avian2d/examples/distance_joint_2d.rs +++ b/crates/avian2d/examples/distance_joint_2d.rs @@ -51,7 +51,7 @@ fn setup(mut commands: Commands) { DistanceJoint::new(anchor, object) .with_local_anchor_1(Vector::ZERO) .with_local_anchor_2(Vector::ZERO) - .with_rest_length(100.0) + .with_length_limits(100.0) .with_linear_velocity_damping(0.1) .with_angular_velocity_damping(1.0) .with_compliance(0.00000001), diff --git a/crates/avian3d/examples/distance_joint_3d.rs b/crates/avian3d/examples/distance_joint_3d.rs index 8a617960..594654f2 100644 --- a/crates/avian3d/examples/distance_joint_3d.rs +++ b/crates/avian3d/examples/distance_joint_3d.rs @@ -52,7 +52,7 @@ fn setup( commands.spawn( DistanceJoint::new(static_cube, dynamic_cube) .with_local_anchor_2(0.5 * Vector::ONE) - .with_rest_length(1.5) + .with_length_limits(1.5) .with_compliance(1.0 / 400.0), ); diff --git a/src/dynamics/solver/joints/distance.rs b/src/dynamics/solver/joints/distance.rs index 665af1e0..4c5ad52a 100644 --- a/src/dynamics/solver/joints/distance.rs +++ b/src/dynamics/solver/joints/distance.rs @@ -167,22 +167,32 @@ impl DistanceJoint { self.compute_force(self.lagrange, dir, dt) } - /// Sets the minimum and maximum distances between the attached bodies. - pub fn with_limits(self, min: Scalar, max: Scalar) -> Self { + /// Returns self with the minimum and maximum distances between the attached + /// bodies. + /// + /// `` + /// # #[cfg(feature = "2d")] + /// # use avian2d::prelude::*; + /// # #[cfg(feature = "3d")] + /// # use avian3d::prelude::*; + /// # use bevy::perlude::*;` + /// # fn new_joint() -> DistanceJoint { DistanceJoint::new(Entity::PLACEHOLDER, Entity::PLACEHOLDER) } + /// let j: DistanceJoint = new_joint(); + /// let a = j.with_length_limits(DistanceLimit { min: 0.0, max: 1.0 }); + /// let b = j.with_length_limits((0.0, 1.0)); + /// assert_eq!(a, b); + /// + /// let c = j.with_length_limits(DistanceLimit { min: 0.5, max: 0.5 }); + /// let d = j.with_length_limits(0.5); + /// assert_eq!(c, d); + /// ``` + pub fn with_length_limits(self, limit: impl Into) -> Self { Self { - length_limits: DistanceLimit::new(min, max), + length_limits: limit.into(), ..self } } - /// Sets the joint's minimum and maximum length limit to `rest_length`, or - /// distance the bodies will be kept at. - pub fn with_rest_length(self, rest_length: Scalar) -> Self { - Self { - length_limits: DistanceLimit::new(rest_length, rest_length), - ..self - } - } } impl PositionConstraint for DistanceJoint {} diff --git a/src/dynamics/solver/joints/mod.rs b/src/dynamics/solver/joints/mod.rs index 6fa37350..b0cfb396 100644 --- a/src/dynamics/solver/joints/mod.rs +++ b/src/dynamics/solver/joints/mod.rs @@ -194,6 +194,26 @@ pub struct DistanceLimit { pub max: Scalar, } +/// Convert the `limit` into a distance limit where _min = max = limit_. +impl From for DistanceLimit { + fn from(limit: Scalar) -> DistanceLimit { + DistanceLimit { + min: limit, + max: limit + } + } +} + +/// Convert the `(min, max)` pair into a distance limit. +impl From<(Scalar, Scalar)> for DistanceLimit { + fn from((min, max): (Scalar, Scalar)) -> DistanceLimit { + DistanceLimit { + min, + max + } + } +} + impl DistanceLimit { /// A `DistanceLimit` with `min` and `max` set to zero. pub const ZERO: Self = Self { min: 0.0, max: 0.0 };