Reputation: 1351
Developing a game using Rust / Bevy (version 0.11)/ Bevy Rapier 3D (version 0.22.0) I have a problem with rigid bodies going haywire if containing child joints and spawning the parent with a transforms other than origo... Any idea why or how to fix?
The code looks as follows. It works as expected as long as the rigid body parent entity is spawned at 0,0,0
. But if spawning at for instance 10.,10.,10
(as in the code example below) I have issues with my rigid body entering the scene spinning around exaggeratedly.
A less technical way to describe the symptom is that it seems like there is "tension" between joints and the parent, which then "snaps" like a rubber band after one frame when spawning, consequently causing the spin.
pub fn spawn_thing(
mut commands: Commands,
) {
let parent_entity = commands
.spawn((
TransformBundle::from(Transform::from_xyz(10., 10., 10.)),
RigidBody::Dynamic,
))
.with_children(|child_builder| {
child_builder.spawn((
TransformBundle::from(Transform::from_xyz(0., 2., 0.)),
Collider::cuboid(0.2, 2., 0.2),
));
child_builder.spawn((
TransformBundle::from(Transform::from_xyz(0., 0., 0.)),
Collider::cuboid(0.7, 0.3, 0.7),
));
})
.id();
let joint_positions= [
[-0.8, 0., 0.8],
[0.8, 0., 0.8],
[-0.8, 0., -0.8],
[0.8, 0., -0.8],
[0., 2., 0.],
];
for join_position in joint_positions {
let position = Vec3::from_array(join_position);
let joint = FixedJointBuilder::new().local_anchor1(position);
commands
.spawn((
TransformBundle::from(Transform::from_translation(position)),
RigidBody::Dynamic,
Collider::ball(0.5),
CollisionGroups::new(Group::NONE, Group::NONE),
ImpulseJoint::new(parent_entity, joint),
));
}
}
Upvotes: 0
Views: 439
Reputation: 1351
As explained by @aceeri on the bevy rapier discord, the solution is to spawn the child rigid bodies with joints, in locations relative to the parent, otherwise it will need to apply and opposite force to correct its position for the joint.
let parent_pos = Vec3::new(10., 10., 10.);
//...
commands
.spawn((
TransformBundle::from(Transform::from_translation(parent_pos + position)),
RigidBody::Dynamic,
Collider::ball(0.5),
CollisionGroups::new(Group::NONE, Group::NONE),
ImpulseJoint::new(parent_entity, joint),
));
//...
Upvotes: 0