|
1 | 1 | # Transforms |
2 | | -Calculates 3D transformations for objects within a hierarchy. |
3 | 2 |
|
4 | | -### Dependencies |
5 | | -* [Simulation](https://github.com/simulation-tree/simulation) |
| 3 | +For representing a hierarchy of objects in 3D space. |
6 | 4 |
|
7 | 5 | ### Behaviour |
8 | | -Entities with an `IsTransform` component will have a `LocalToWorld` component built |
9 | | -using data from these components (relative to the parent entity), whenever the `TransformUpdate` event |
10 | | -is polled: |
| 6 | + |
| 7 | +Entities with the `IsTransform` tag will have a `LocalToWorld` component calculated |
| 8 | +using data from these components, relative to the parent: |
11 | 9 | * `Position` |
12 | 10 | * `Rotation` |
13 | 11 | * `Scale` |
| 12 | +* `Anchor` |
| 13 | +* `Pivot` |
| 14 | + |
| 15 | +### Axis convention |
| 16 | + |
| 17 | +This is only relevant for rotations when working with them manually: |
| 18 | +* X axis is right (pitch) |
| 19 | +* Y axis is up (yaw) |
| 20 | +* Z axis is forward (roll) |
14 | 21 |
|
15 | | -### Example |
| 22 | +### Anchoring to corners |
| 23 | + |
| 24 | +The `Anchor` component is able to describe both relative and absolute values. The example |
| 25 | +below makes the child transform have a 10 pixel border inside the parent: |
16 | 26 | ```cs |
17 | | -eint parentObj = world.CreateEntity(); |
18 | | -world.AddComponent(parentObj, new IsTransform()); |
19 | | -world.AddComponent(parentObj, new Scale(2, 2, 2)); |
| 27 | +Transform parent = new(world); |
| 28 | +parent.LocalScale = new(100, 100, 0); |
| 29 | +parent.LocalPosition = new(50, 50, 0); |
20 | 30 |
|
21 | | -eint obj = world.CreateEntity(parentObj); |
22 | | -world.AddComponent(obj, new IsTransform()); |
23 | | -world.AddComponent(obj, new Position(0, 5, 0)); |
| 31 | +Transform child = new(world); |
| 32 | +child.Anchor = new(10, 10, 0, 10, 10, 0, Anchor.Relativeness.X | Anchor.Relativeness.Y); |
24 | 33 |
|
25 | | -world.Submit(new TransformUpdate()); |
26 | | -world.Poll(); |
| 34 | +//after simulating, child will be at position (60, 60) with size (80, 80) |
| 35 | +``` |
27 | 36 |
|
28 | | -LocalToWorld ltw = world.GetComponent<LocalToWorld>(obj)); |
29 | | -Vector3 worldPosition = ltw.Position; |
30 | | -Matrix4x4 matrix = ltw.value; |
| 37 | +### Reading world state |
| 38 | + |
| 39 | +Accessing world position, rotation or scale is can be done through properties of a transform |
| 40 | +entity: |
| 41 | +```cs |
| 42 | +Transform a = ... |
| 43 | +Vector3 position = a.WorldPosition; |
| 44 | +Quaternion rotation = a.WorldRotation; |
| 45 | +Vector3 scale = a.WorldScale; |
31 | 46 | ``` |
| 47 | + |
| 48 | +And optionally, can be accessed directly through components. However, accessing |
| 49 | +the world rotation is not possible through the `LocalToWorld` component and must |
| 50 | +be done through `WorldRotation`: |
| 51 | +```cs |
| 52 | +uint a = ... |
| 53 | +LocalToWorld ltw = world.GetComponent<LocalToWorld>(a); |
| 54 | +Vector3 position = ltw.Position; |
| 55 | +Vector3 scale = ltw.Scale; |
| 56 | +Quaternion rotation = world.GetComponent<WorldRotation>(a).value; |
| 57 | +``` |
0 commit comments