Building an Efficient 2D Scene Graph for Object Transformation and Hierarchical Management
Posted: Sun Dec 08, 2024 10:21 am
### Task: Implement a Basic Scene Graph for 2D Game Engine
#### Description:
Create a simple **scene graph** system that organizes and manages game objects in a hierarchical structure. The nodes of the graph represent various objects in the game world (such as characters, items, terrain), and each node can have one or more children. Transformations such as translation, scaling, and rotation should be inherited by child objects from their parent nodes.
#### Requirements:
1. **SceneNode Class**:
- Implement a `SceneNode` class representing a game object.
- Each node should have the following properties:
- Position (x, y).
- Scale (width, height).
- Rotation (angle in degrees).
- A list of child nodes (children).
- Implement methods to:
- Apply transformations (translation, scaling, rotation) to the node.
- Add child nodes to the current node.
- Print the current transformations of the node (position, scale, rotation).
- Print transformations of all child nodes.
2. **SceneGraph Class**:
- Implement a `SceneGraph` class that manages the root node of the scene.
- The class should allow applying transformations to the root node.
- The transformations should propagate to all child nodes.
3. **Program Logic**:
- Create a scene graph with multiple nodes (e.g., a root node and several child nodes).
- Apply transformations to the root node (e.g., moving, scaling, rotating).
- Output the transformations of the root node and all its children.
- Demonstrate how child nodes inherit transformations from their parent node.
#### Example Output:
Before transformations:
```
Root Node:
- Position: (0, 0), Scale: (1, 1), Rotation: 0°
Child Node 1:
- Position: (10, 10), Scale: (1, 1), Rotation: 0°
Child Node 2:
- Position: (20, 20), Scale: (2, 2), Rotation: 45°
```
After applying transformation (translate root by (10, 10)):
```
Root Node:
- Position: (10, 10), Scale: (1, 1), Rotation: 0°
Child Node 1:
- Position: (20, 20), Scale: (1, 1), Rotation: 0°
Child Node 2:
- Position: (30, 30), Scale: (2, 2), Rotation: 45°
```
#### Requirements:
- Use object-oriented principles in your implementation.
- Provide methods for adding and transforming child nodes.
- Apply the transformations to the parent node and ensure child nodes automatically inherit the parent's transformations.
#### Description:
Create a simple **scene graph** system that organizes and manages game objects in a hierarchical structure. The nodes of the graph represent various objects in the game world (such as characters, items, terrain), and each node can have one or more children. Transformations such as translation, scaling, and rotation should be inherited by child objects from their parent nodes.
#### Requirements:
1. **SceneNode Class**:
- Implement a `SceneNode` class representing a game object.
- Each node should have the following properties:
- Position (x, y).
- Scale (width, height).
- Rotation (angle in degrees).
- A list of child nodes (children).
- Implement methods to:
- Apply transformations (translation, scaling, rotation) to the node.
- Add child nodes to the current node.
- Print the current transformations of the node (position, scale, rotation).
- Print transformations of all child nodes.
2. **SceneGraph Class**:
- Implement a `SceneGraph` class that manages the root node of the scene.
- The class should allow applying transformations to the root node.
- The transformations should propagate to all child nodes.
3. **Program Logic**:
- Create a scene graph with multiple nodes (e.g., a root node and several child nodes).
- Apply transformations to the root node (e.g., moving, scaling, rotating).
- Output the transformations of the root node and all its children.
- Demonstrate how child nodes inherit transformations from their parent node.
#### Example Output:
Before transformations:
```
Root Node:
- Position: (0, 0), Scale: (1, 1), Rotation: 0°
Child Node 1:
- Position: (10, 10), Scale: (1, 1), Rotation: 0°
Child Node 2:
- Position: (20, 20), Scale: (2, 2), Rotation: 45°
```
After applying transformation (translate root by (10, 10)):
```
Root Node:
- Position: (10, 10), Scale: (1, 1), Rotation: 0°
Child Node 1:
- Position: (20, 20), Scale: (1, 1), Rotation: 0°
Child Node 2:
- Position: (30, 30), Scale: (2, 2), Rotation: 45°
```
#### Requirements:
- Use object-oriented principles in your implementation.
- Provide methods for adding and transforming child nodes.
- Apply the transformations to the parent node and ensure child nodes automatically inherit the parent's transformations.