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:

//vDirAndUp = [obj1,obj2] call KK_fnc_vectorDirAndUpRelative; KK_fnc_vectorDirAndUpRelative = { private ["_o1","_o2","_v"]; _o1 = _this select 0; _o2 = _this select 1; _v = _o2 worldToModelVisual [0,0,0]; [ _o2 worldToModelVisual vectorDirVisual _o1 vectorDiff _v, _o2 worldToModelVisual vectorUpVisual _o1 vectorDiff _v ] };

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:

//[obj1,obj2] call KK_fnc_attachToRelative; KK_fnc_attachToRelative = { private ["_o","_v"]; _o = _this select 0; _v = _this call KK_fnc_vectorDirAndUpRelative; _o attachTo [_this select 1]; _o setVectorDirAndUp _v; };

To test, spawn a car, turn towards it and execute the following code, you will get attached to the acr as you stand:

[player, car] call KK_fnc_attachToRelative;

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.


EDIT: Found another use for newly acquired knowledge, relative direction of a weapon:

//vectorDirRel = [veh, wep] call KK_fnc_weaponDirRelative; KK_fnc_weaponDirRelative = { private "_veh"; _veh = _this select 0; _veh worldToModelVisual (_veh weaponDirection (_this select 1)) vectorDiff (_veh worldToModelVisual [0,0,0]) }; //To test with M2A4 hint str ([tank, "cannon_105mm"] call KK_fnc_weaponDirRelative);