How to get the absolute position of a child entity in bevy?

2.2k Views Asked by At

I am using the Bevy game engine.

The ability to have transforms be propagated to children in Bevy is handy, but when I am performing collision checks in my game, I have been using the object's Translation to compute its location. Now that I have some parent-child hierarchies in my scene, the Translation of each child entity is relative to its parent.

Is there a way to get the position of an entity relative to the world origin as opposed to the entity's parent?

1

There are 1 best solutions below

2
On BEST ANSWER

The "world" position is stored in the GlobalTransform component. Transforms internally are 4x4 matrices where the translation() function returns the position. You can access it like this:

fn system(global_transform: &GlobalTransform) {
  let position = global_transform.translation(); 
}