Making child entities

Hello
I’m trying to make entities get affected by position changes of main entity. I would like to ask for any example of usage of Parent in entity, because i seem to be unable to make it work.

Here is a way to do it:

So in this example you have an entity which can have a parent (or not) and his position depends on the parent’s position. And if you want to set a specific postion to the entity you have to substract the position of the parent.

public class Entity
{
    public Entity(Entity parent, Vector position)
    {
         [...]
    }
    
    private Entity parent;
    
    private Vector2 _position;
    public Vector2 Position 
    {
           get 
            {
                    return parent != null ? parent.Position + _position : _position;
             }
            set
            {
                    _position = parent != null ? value - parent.Position : value ;
             }

     }
}

But in this case you have two position for the entity, his global position in the world (or the window) and his position related to his parents. So Position is the WorldPosition and _position is the LocalPosition. (ofc in a case where this entity have a parent :slight_smile: )

Resume:

(I dont know if you will understand well but i can give you more explanation :slight_smile: )

2 Likes

Thank you for your answer. It seems clear and pretty much shows the idea.
However I am trying to use monogame.extended entity class, which has the Parent implemented from Transform2D and I’m just unsure what am I missing to make it work.

Oop, sry i didnt saw the Extended categorie :stuck_out_tongue: Im not very familiar with Monogame.Extended but anyway i will give you the answer later in the afternoon (i dont have the time now) :slight_smile:

I wanted to say that I found way to achiev what I wanted by adding several sprite components to same entity ( it sounds so stupid obvious now but yeah, didn’t think about it ).
Still, I believe I might need to use parent entity in future, so example will be welcomed :slight_smile:

ok nice :slight_smile: i will make u an example when i have the time (maybe tonight i think)

you’re really busy i see :rofl: