Today I prepared a somewhat useful function, but first, big thanks to Dirk! Thank you very much, man, for your support!
If you used attachTo before you probably noticed that attaching one object to another makes first object to reset its orientation. So if you have 2 objects and you want them remain visually exactly the same but only get attached to each other, you cannot do it with the simple attachTo command. While using obj1 attachTo [obj2] will preserve objects relative positioning, obj1 will recieve relative default orientation against obj2. In other words it will look as if someone additionally executed obj1 setVectorDirAndUp [[0,1,0],[0,0,1]].
What can be done about it? I’m thinking it would not be hard for BIS to provide scripters with attachToRelative command, but doubt this could happen any time soon. So I was looking for SQF workaround. I have to say I tried several times unsuccessfully to solve this problem before. But with every failure one normally learns something :).
Instead of trying to make my own vector transformation, I decided to use worldToModel function. But, but it is designed to be used with position??? True, but what if directional vector is treated as position. Basically if you move everything to [0,0,0] then there is no difference what is transformed, as long as it has x, y and z.
Here is the code to get relative vectorDir and vectorUp of obj1 against obj2:
As you can see, all the complicated transformation is done by existing methods. So going back to attachTo problem, if we have relative orientation vector we can now set it on attached object that will set its orientation correctly:
To test, spawn a car, turn towards it and execute the following code, you will get attached to the acr as you stand:
Ideally, setVectorDir would have been enough for everything, however attachTo just doesn’t work with it alone, so setVectorDirAndUp is needed. An extra vector calculation, not really the end of the world.
Enjoy,
KK
EDIT: Found another use for newly acquired knowledge, relative direction of a weapon: