Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixed dynamic bodies not waking when their joint motor changed #328

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/data/graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,13 @@ impl<N, E> Graph<N, E> {
self.nodes.get(a.index()).map(|n| &n.weight)
}

/// Access the weight for node `a` mutably.
///
/// Also available with indexing syntax: `&mut graph[a]`.
pub fn node_weight_mut(&mut self, a: NodeIndex) -> Option<&mut N> {
self.nodes.get_mut(a.index()).map(|n| &mut n.weight)
}

/// Access the weight for edge `a`.
///
/// Also available with indexing syntax: `&graph[a]`.
Expand Down
4 changes: 4 additions & 0 deletions src/dynamics/joint/generic_joint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,8 @@ pub struct GenericJoint {
///
/// Note that the mostor must also be explicitly enabled by the `motors` bitmask.
pub motors: [JointMotor; SPATIAL_DIM],
/// The flag indicating that motor settings were changed from outside
pub motor_changed: bool,
Comment on lines +209 to +210
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While I appreciate the "low number of lines" this PR ends up, this would add state to an otherwise "configuration" or "data oriented" struct, I'm not sure this is the right place to add that.

}

impl Default for GenericJoint {
Expand All @@ -219,6 +221,7 @@ impl Default for GenericJoint {
coupled_axes: JointAxesMask::empty(),
limits: [JointLimits::default(); SPATIAL_DIM],
motors: [JointMotor::default(); SPATIAL_DIM],
motor_changed: false,
}
}
}
Expand Down Expand Up @@ -419,6 +422,7 @@ impl GenericJoint {
self.motors[i].target_pos = target_pos;
self.motors[i].stiffness = stiffness;
self.motors[i].damping = damping;
self.motor_changed = true;
self
}
}
Expand Down
31 changes: 31 additions & 0 deletions src/dynamics/joint/impulse_joint/impulse_joint_set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,37 @@ impl ImpulseJointSet {
}
}

/// Wake up bodies that are attached with joint that has a motor and it was changed in this frame
pub(crate) fn wake_up_bodies_with_active_motor(
&mut self,
islands: &mut IslandManager,
bodies: &mut RigidBodySet
)
{
let mut to_wake_up = vec![];
for (i, edge) in self.joint_graph.graph.edges.iter().enumerate() {
let joint = &edge.weight;

let rb1 = &bodies[joint.body1];
let rb2 = &bodies[joint.body2];

if (rb1.is_sleeping() || rb2.is_sleeping()) && joint.data.motor_changed {
to_wake_up.push((edge.source(), i));
to_wake_up.push((edge.target(), i));
}
}

for (node_id, joint_id) in to_wake_up {
if let Some(rb_handle) = self.joint_graph.graph.node_weight_mut(node_id) {
islands.wake_up(bodies, *rb_handle, true);
}

if let Some(mut joint) = self.joint_graph.graph.edges.get_mut(joint_id) {
joint.weight.data.motor_changed = false;
}
}
}

/// Removes a joint from this set.
///
/// If `wake_up` is set to `true`, then the bodies attached to this joint will be
Expand Down
5 changes: 5 additions & 0 deletions src/pipeline/physics_pipeline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -391,6 +391,11 @@ impl PhysicsPipeline {
&mut modified_colliders,
);

impulse_joints.wake_up_bodies_with_active_motor(
islands,
bodies
);

// TODO: do this only on user-change.
// TODO: do we want some kind of automatic inverse kinematics?
for multibody in &mut multibody_joints.multibodies {
Expand Down