Skip to content

Commit

Permalink
feature: Add From impls for DistanceLimit.
Browse files Browse the repository at this point in the history
Add From<Scalar> and From<(Scalar, Scalar)> for DistanceLimit.
  • Loading branch information
shanecelis committed Sep 22, 2024
1 parent 2a018cd commit 93e5e6d
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 13 deletions.
2 changes: 1 addition & 1 deletion crates/avian2d/examples/distance_joint_2d.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
2 changes: 1 addition & 1 deletion crates/avian3d/examples/distance_joint_3d.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
);

Expand Down
32 changes: 21 additions & 11 deletions src/dynamics/solver/joints/distance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<DistanceLimit>) -> 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 {}
Expand Down
20 changes: 20 additions & 0 deletions src/dynamics/solver/joints/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,26 @@ pub struct DistanceLimit {
pub max: Scalar,
}

/// Convert the `limit` into a distance limit where _min = max = limit_.
impl From<Scalar> 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 };
Expand Down

0 comments on commit 93e5e6d

Please sign in to comment.