CBA_A3 icon indicating copy to clipboard operation
CBA_A3 copied to clipboard

Function to get relative position and orientation of one object to another.

Open Tinter opened this issue 4 years ago • 2 comments

As per the other issue, this is something I stumbled upon working on my own project, which involves placing furniture in buildings. Getting the position and orientation of one object, could be a piece of furniture called A, to another, could be a house called B, is not trivial, at least I spent a long time figuring this out. Neither is setting the world position and orientation of A so that it is the correct position and orientation relative to B.

The following functions are what I and Nigel, who was an immense help, have found to be the most consistent way of doing this.

tint_fnc_getRelPosAndOrientation = {
  param["_objA", "_objB"];
  _pos = getPosWorld _objA;
  if (!(surfaceIsWater _pos)) then {
    _pos = ASLToATL _pos;
  };
  _relativePos = _objB worldToModel _pos;
  _relativeOrientation = ([_objA, _objB] call BIS_fnc_vectorDirAndUpRelative);

  [_relativePos, _relativeOrientation]
};

tint_fnc_setRelPosAndOrientation = {
  param["_objA", "_objB", "_relPosAndOrientation"];
  _relPos = _relPosAndOrientation#0;
  _relOrientation = _relPosAndOrientation#1;
  _relDir = _relOrientation#0;
  _relUp = _relOrientation#1;
  
  _objA setVectorDirAndUp [_objB vectorModelToWorld _relDir, _objB vectorModelToWorld _relUp];
  _objA setPosWorld (_objB modelToWorldWorld _relPos);
};

These are just for clarification's sake, I don't know whether the return type is good or if these should be split up into separate functions, but the point of confusion is that there are many different commands to set and get positions and it is only this combination of them that seems to work.

Tinter avatar Jul 06 '20 22:07 Tinter

getPos and getDir have alt syntaxes that do this, no?

commy2 avatar Jul 06 '20 23:07 commy2

The specific application is when working in model space and rotation not just in the Z axis, but the X and Y axis as well.

Tinter avatar Jul 07 '20 00:07 Tinter