SetRelativeFromAbsolute
Last updated
Was this helpful?
Last updated
Was this helpful?
This method can be used to "update" relative values (rotation and position) of a child after modifying its absolute rotation or position values.
Although child absolute values are considered read-only (see ), this does not necessarily mean that the actual variable itself is read-only. Rather, before drawing occurs, the absolute values of all children are overwritten by their attachment logic. However, prior to drawing occurring, absolute values can be set and reset and used *within that same frame*. We can take advantage of this behavior by modifying the absolute values, then using those values to update the child's relative values so that the overwriting that occurs later will not end up changing the absolute values. In other words, we can use the SetRelativeFromAbsolute to reposition a child object in absolute space, then make those changes "stick".
This example creates two and attaches one to the other. The child will then be repositioned in absolute coordinates, then SetRelativeFromAbsolute will be called to modify its relative values so that the child remains in the position that it was placed: Add the following using statements:
Add the following to Initialize after initializing FlatRedBall:
This code essentially makes the absolute values temporarily writable. The end result is the same as:
Detaching a child
Repositioning the child
Reattaching the child and passing true as the second (changeRelative) value