Skip to content

Add Transform-preserving hierarchy update method #5475

@nicopap

Description

@nicopap

What problem does this solve or what need does it fill?

It is currently cumbersome and error-prone to update an entity's parent while keeping its GlobalTransform static.

What solution would you like?

I'd like a method on EntityCommands that allows updating both the parent and the transform of an entity so that they keep their GlobalTransform.

What alternative(s) have you considered?

It is possible to reparent keeping the same transform with the following code:

fn transform_relative_to(point: &GlobalTransform, reference: &GlobalTransform) -> Transform {
    let relative_affine = reference.affine().inverse() * point.affine();
    let (scale, rotation, translation) = relative_affine.to_scale_rotation_translation();
    Transform { translation, rotation, scale }
}

Then, the following to set both the parent and the transform in order to preserve the same GlobalTransform through the hierarchy change.

let new_transform = transform_relative_to(entity_transform, parent_transform);
commands.entity(entity).set_parent(new_parent).insert(new_transform);

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-TransformTranslations, rotations and scalesC-UsabilityA targeted quality-of-life change that makes Bevy easier to use

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions