bgnerdclub / birb

6 stars 0 forks source link

Transform and Parent Child Relations #11

Open jw2476 opened 6 months ago

jw2476 commented 6 months ago

This issue only concerns the transforms module and 2D transforms, it doesn't include 3D transforms as the maths library isn't in place yet.

Transforms will need to include translation, rotation, and scale, we've already agreed to represent rotations as 2 vectors from which a rotor can be derived, I think translations and scale should be stored as vectors.

I think for transforms to be easy to use we will need some form of parent-child inheritance, this makes things difficult as entities need to store their transforms for caching reasons. I think the easiest approach would be to store an entity ID of the entity the parent transform is contained in, and then fetching the parent transform would look something like:

let parent_transform = app.get_entity::<ParentEntity>(self.transform.parent).unwrap().transform;

This works for getting a parent transformation but if you want to get a local-to-world transform you'd need to go all the way up the chain of transforms, which is difficult due to the fact the parent types have to be known the whole way to access their transforms. I think the best solution for this would be for Transform to have a related trait with a required getter method and some utility functions we provide that depend on that required getter, I'm not going to write out the trait yet but here is what the usage would be like:

let parent_transform = app.get_entity::<dyn Transform>(self.transform.parent).unwrap().get_transform(); // You can do this
let parent_transform = self.get_parent_transform(); // Or this is nicer, using a provided method from the Transform trait
let local_to_world = self.get_local_to_world_transform().unwrap();  // And you can do this too

I need to check if you can cast downcast to a trait object but I don't see why you couldn't. Also, the entity ID system this is hanging off isn't implemented yet, and neither is the get_entity function, but those can be implemented in a separate issue that I'll create shortly.

This architecture is just a suggestion, if anyone has any other ideas or suggestions please let me know. Any feedback would be much appreciated

jw2476 commented 6 months ago

@Alex-Programs @NaughtyDog6000 @mavic7 @camsoftworks2018 opinions?