PDA

View Full Version : Entities: Attach or setParent?



ReneMiner
09-03-2013, 12:17
I want to move a camera around and also one light - so I use entities for both. Now I wonder how would be the best way to perform this:

Camera + directional Light shall always be at same position and face the same target - now which would be faster?

a) Leave camera and light two seperate entities and place/arrange them each with SetPos/SetTarget
b) Attach the light to the camera (will it follow the SetTarget and "look" to same direction then?)
c) set the camera as the lights parent or vice versa (will it follow SetTarget?)
d) another method?

Petr Schreiber
09-03-2013, 12:25
Hi Rene,

a) Possible, but there is better way
b) Yes, yes :) - this is the way
c) Basically the same as b)
d) I think the b) is okay :P

So the basic setup would be:


TBGL_EntityCreateCamera(%sScene, %eCamera) ' -- Creates camera
TBGL_EntityCreateLight(%sScene, %eLight, %eCamera) ' -- Creates light and camera is set to be the parent



Petr

ReneMiner
09-03-2013, 12:28
thank you
now I think I got it: Attach would be "set child"

Petr Schreiber
09-03-2013, 15:26
Great! :)

Just to add more background, there are two more functions to achieve the same effect in phase after creation of the objects:

TBGL_EntitySetParent
TBGL_EntityAttach


The first one just changes the relationship between two objects - the child object inherits the basic transform from the parent.
So - when object A is at (1, 1, 1) and object B is at (2, 2, 2), making:


TBGL_EntitySetParent(Scene, B, A)


Will make the object A parent of B, which means that the absolute position of object B becomes (1+2, 1+2, 1+2) = (3, 3, 3). The original vector (2,2,2) will become the offset from the parent.

After setting parent of B back to zero, the object would return to (2, 2, 2), even if the A moved in the meantime.

TBGL_EntityDetach allows nice trick, that is, disconnecting the objects while baking the transformation to the original child. That is, if you would do TBGL_EntityDetach(Scene, B, %TBGL_PRESERVE), the position of object B in global space would be preserved. That is, if A would move to (5,5,5) in the meantime, the B would have the original (2,2,2), the global position of B would be (7,7,7). After using TBGL_EntityDetach(Scene, B, %TBGL_PRESERVE), the B would not go back to (2,2,2), but it would stay at (7,7,7).


Petr