Skip to main content

propagate_transforms

Function propagate_transforms 

Source
pub fn propagate_transforms(world: &mut World)
Expand description

Propagates transforms through the entity hierarchy (3D).

This function updates GlobalTransform for all entities with Transform:

  • Root entities: GlobalTransform = Transform (direct copy)
  • Child entities: GlobalTransform = parent’s GlobalTransform * local Transform

§Requirements

For correct propagation:

  • Parent entities must have both Transform and GlobalTransform
  • Parent entities must have a Children component listing their children
  • Child entities must have both Transform, GlobalTransform, and Parent

§Example

use goud_engine::ecs::World;
use goud_engine::ecs::components::{Transform, GlobalTransform, Parent, Children};
use goud_engine::ecs::components::propagation::propagate_transforms;
use goud_engine::core::math::Vec3;

let mut world = World::new();

// Create parent at (10, 0, 0)
let parent = world.spawn_empty();
world.insert(parent, Transform::from_position(Vec3::new(10.0, 0.0, 0.0)));
world.insert(parent, GlobalTransform::IDENTITY);

// Create child at local (5, 0, 0)
let child = world.spawn_empty();
world.insert(child, Transform::from_position(Vec3::new(5.0, 0.0, 0.0)));
world.insert(child, GlobalTransform::IDENTITY);
world.insert(child, Parent::new(parent));

// Set up parent's children list
let mut children = Children::new();
children.push(child);
world.insert(parent, children);

// Propagate transforms
propagate_transforms(&mut world);

// Child's global position should be (15, 0, 0)
if let Some(global) = world.get::<GlobalTransform>(child) {
    let pos = global.translation();
    assert!((pos.x - 15.0).abs() < 0.001);
}