Author Topic: 2D - "Entity System"  (Read 1528 times)

Offline backslider

  • Community Developer
  • Prof. Inline
  • ******
  • Posts: 892
    • View Profile
2D - "Entity System"
« on: 2012-May-29 »
Ich bastel gerad sone Art Entity System in 2D (nicht GLB)...
Nun stehe ich irgendwie auf dem Schlauch! :(

Ich habe ein Entity, welches beliebig viele Entities als Kinder hat.
Nun möchte ich beim Bewegen oder Drehen alle Kinder Updaten und relativ zum Elternteil bewegen / drehen.
Könnt ihr mir mal einen Pseudo-Anstoß geben? :)

Code: GLBasic [Select]
//pseudo-schleife
foreach(Entity child in children)
{
child.Position = new Vector2(?, ?);
child.Rotation = ?;
}
 

Danke!

Offline Quentin

  • Prof. Inline
  • *****
  • Posts: 915
    • View Profile
Re: 2D - "Entity System"
« Reply #1 on: 2012-May-29 »
würde es so wie im 3D-ES machen. Stichwort Rekursion
(siehe Funktion EntityUpdateMatrix)

Pseudo-Code:
Code: GLBasic [Select]
function update2d( object2d )
{
    ....
    mach update Sachen mit object2d;
    für alle Kinder von object2d
    {
       update2d( object2d.child );
    }
}
 

oder so ähnlich

Offline backslider

  • Community Developer
  • Prof. Inline
  • ******
  • Posts: 892
    • View Profile
Re: 2D - "Entity System"
« Reply #2 on: 2012-May-29 »
Danke für die schnelle Antwort, aber um das Updaten an sich gehts mir gar nicht, das geht schon.

Es geht mir um eine Formel, wie ich die Position und die Rotation relativ zum Elternteil berechne. :)

Offline Slydog

  • Prof. Inline
  • *****
  • Posts: 929
  • KodeSource
    • View Profile
    • KodeSource
Re: 2D - "Entity System"
« Reply #3 on: 2012-May-29 »
I would only store the child's position and rotation relative to its parent.
This way you don't have to update the child's properties everytime its parent changes.
Just figure out the child's position and rotation when drawing.

Here's some pseudocode, mixing both 'C' and 'GLBasic'.  Hope it makes sense!
The SIN/COS commands may need to be reversed.
And this is just a quick guess, not sure if I'm even close! ha

And and this will only work for one level of parent / children.
You would have to figure this out all the way up the chain for more than 2 deep.
(or actually store the final calculated values, so children of children can use the previous calculations)

Code: GLBasic [Select]
parent.Parent = null; // New member 'Parent', which this object has none
parent.Position = new Vector2(100, 100);
parent.Rotation = 45;

child.Parent = parent
child.Position = new Vector2(0, -20); // Child is 20 pixels directly behind 'parent'
child.Rotation = 90; // Child is always rotated 90 degress relative to parent

parent.Draw() // Render 'parent'
child.Draw() // Render 'child'

TYPE Entity
        id%
        Parent AS Entity // Not really valid with GLBasic, TYPE can't have references to itself
        ..
        ..
 
        FUNCTION Draw:
                LOCAL position AS Vector2 = new Vector2(0, 0) // Default to (0,0)
                LOCAL rotation# = 0 // Default to no rotation
         
                // If object has parent . . .
                IF SELF.Parent <> null
                        // Start with parent's position and rotation
                        postition = SELF.Parent.position
                        rotation = SELF.Parent.rotation

                        // Adjust with this object's relative position
                        position.x = position.x + (SIN(rotation) * SELF.position.x)
                        position.y = position.y + (COS(rotation) * SELF.position.y)

                        // Add this object's rotation
                        rotation = rotation + SELF.rotation

                // No parent?  Then just use this object's values
                ELSE
                        postition = SELF.position
                        rotation = SELF.rotation
                ENDIF

                // Draw object
                RotateSprite(SELF.id, position.x, position.y, rotation) // Ha, or however you want to draw/rotation your sprite
        ENDFUNCTION
ENDTYPE
My current project (WIP) :: TwistedMaze <<  [Updated: 2015-11-25]