diff --git a/lyra-scene/src/lib.rs b/lyra-scene/src/lib.rs index b737e5c..0e1b1b1 100644 --- a/lyra-scene/src/lib.rs +++ b/lyra-scene/src/lib.rs @@ -22,7 +22,7 @@ pub struct SceneNodeFlag; #[derive(Component)] pub struct SceneNodeRoot; -/// A SceneGraph is a Graph of nodes that represents the hierarchy of a scene. +/// A Graph of nodes that represents the hierarchy of a Scene. /// /// This SceneGraph is special in the sense that it is literally just an ECS world with methods /// implemented for it that make it easier to use for a SceneGraph. diff --git a/lyra-scene/src/node.rs b/lyra-scene/src/node.rs index a09004f..f72e3f9 100644 --- a/lyra-scene/src/node.rs +++ b/lyra-scene/src/node.rs @@ -2,9 +2,9 @@ use lyra_ecs::{query::Entities, relation::{ChildOf, RelationOriginComponent}, Bu use crate::SceneGraph; +/// A node inside of the Scene #[derive(Clone, PartialEq, Eq)] pub struct SceneNode { - //world: WorldLock, parent: Option, entity: Entity } @@ -54,28 +54,6 @@ impl SceneNode { pub fn add_node(&self, graph: &mut SceneGraph, bundle: B) -> SceneNode { graph.add_node_under(self, bundle) } - - /* pub fn local_transform(&self, graph: &SceneGraph) -> Transform { - let v = graph.world.view_one::<&Transform>(self.entity); - - let t = v.get().expect("Somehow the SceneNode is missing its Transform!"); - t.clone() - } - - /// Retrieves the world transform of the Node. - /// - /// This traverses up the SceneGraph from this node. - pub fn world_transform(&self, graph: &SceneGraph) -> Transform { - match self.parent(graph) { - Some(parent) => { - let pt = parent.world_transform(graph); - pt + self.local_transform(graph) - }, - None => { - self.local_transform(graph) - } - } - } */ } #[cfg(test)] diff --git a/lyra-scene/src/world_transform.rs b/lyra-scene/src/world_transform.rs index 3eb54c7..143dd2a 100644 --- a/lyra-scene/src/world_transform.rs +++ b/lyra-scene/src/world_transform.rs @@ -4,6 +4,13 @@ use lyra_ecs::{query::{filter::{Has, Not}, Entities, View}, relation::{ChildOf, use lyra_math::Transform; use crate::lyra_engine; +/// The world transform of an entity. +/// +/// A Transform represents the relative position of the entity to its parent entity, while +/// a world transform is the position relative to the World. When wanting to move an entity, +/// you should use its [`Transform`]. You cannot mutate [`WorldTransform`] as its managed completey +/// by the [`system_update_world_transforms`] system. For the WorldTransform to work properly, you +/// must have both a [`Transform`] and [`WorldTransform`] on the entities in the scene. #[derive(Debug, Copy, Clone, PartialEq, Default, Component)] pub struct WorldTransform(pub(crate) Transform); @@ -18,14 +25,11 @@ impl Deref for WorldTransform { /// A system that updates the [`WorldTransform`]'s for entities and their children. /// /// For entities without parents, this will update world transform to match local transform. -/// For any children entities, their [`GlobalTransform`]s will be updated to reflect the changes +/// For any children entities, their [`WorldTransform`]s will be updated to reflect the changes /// of its parent entity. pub fn system_update_world_transforms(world: &World, view: View<(Entities, &mut WorldTransform, &Transform, Not>>)>) -> anyhow::Result<()> { - for (en, mut world_tran, tran, _) in view.into_iter() { world_tran.0 = *tran; - println!("Found entity {:?} at {:?}", en, world_tran.translation); - recurse_update_trans(world, &world_tran, en); } @@ -47,8 +51,7 @@ fn recurse_update_trans(world: &World, parent_transform: &WorldTransform, entity next_entities.push((en, world_tran.0.clone())); } - next_entities.reverse(); - while let Some((en, pos)) = next_entities.pop() { + for (en, pos) in next_entities.into_iter() { recurse_update_trans(world, &WorldTransform(pos), en); } }