#Rapier: position of child colliders not scaling with parent

6 messages · Page 1 of 1 (latest)

jolly hedge
#

Hi all, I'm struggling with scaling a body in rapier 2d (which has been really great so far). The body has a main sprite & collider(a core part) and a number of child sprites & colliders (additional parts that the player can add). When I change the scale of the parent transform everything scales down nicely, including the child collider sizes.

However, the only thing that does not scale down as well are the collider positions. You can see the result in the pictures. Both the colliders and sprites use the same Transform, so it kind of surprises me that the colliders behave differently.

Does anyone have an idea whether this is a bug in rapier or expected behavior?

uneven igloo
jolly hedge
#

Thanks, that appears to be a similar issue. I'll see if I can get it working the same way!

jolly hedge
jolly hedge
#

I got a workaround that works more or less but is quite ugly:

#
pub fn update_collider_positions(
    mut rapier: ResMut<RapierContext>,
    ship_query: Query<(&Children, &Transform), With<Ship>>,
    part_transform_query: Query<&Transform, With<Part>>,
) {
    for (children, ship_transform) in &ship_query {
        for child in children.iter() {
            let handle = if let Some(handle) = rapier.entity2collider().get(child) {
                handle.clone()
            } else {
                continue;
            };
            match part_transform_query.get(*child) {
                Ok(transform) => {
                    rapier
                        .colliders
                        .get_mut(handle)
                        .unwrap()
                        .set_position_wrt_parent(
                            (
                                transform.translation.truncate() / PHYSICS_PIXELS_PER_METER
                                    * ship_transform.scale.x,
                                transform.rotation.to_euler(EulerRot::ZYX).0,
                            )
                                .into(),
                        );
                }
                _ => continue,
            }
        }
    }
}

The key here is to scale the collider's transform translation (which is a child entity of the (ship) body using the parent transform scale.