Page MenuHomeFeedback Tracker

Code donation attachToSelectionRelative
New, NormalPublic

Description

/*
 * Author: Ampersand
 * Attaches the child object to a selection on a parent object, at the current relative position and orientation.
 *
 * Arguments:
 * 0: Child <OBJECT>
 * 1: Parent <OBJECT>
 * 2: Selection <STRING>
 * 3: Relative <BOOLEAN> True to maintain current relative orientation, false to match selection orientation
 *
 * Return Value:
 * 0: Selection Position and Orientation <ARRAY> For saving 
 *     0: Selection
 *     1: Attachment - For example, place objects in editor, run this code in init, save attachment data for later use.
 *
 * Example:
 * [_child, _parent, _bonename, _isRelative] call compile preprocessFileLineNumbers "attachToSelection.sqf";
 * [_rocket, _tank, "recoil_axis", false] call compile preprocessFileLineNumbers "attachToSelection.sqf"; // Matches barrel direction
 * [_flower, player, "weapon"] call compile preprocessFileLineNumbers "attachToSelection.sqf";
 *
 * Tutorial: https://www.youtube.com/watch?v=c-no1LJnf5E
 */

#define LOD_MEMORY 1e15

params ["_child", "_parent", "_selection", ["_isRelative", true]];

if (!isNull attachedTo _child) then {detach _child;};

private _childPos = _parent worldToModel ASLToAGL getPosWorldVisual _child;
private _childY = _parent vectorWorldToModelVisual vectorDirVisual _child;
private _childZ = _parent vectorWorldToModelVisual vectorUpVisual _child;
private _childX = _childY vectorCrossProduct _childZ;

private _selectionPos = _parent selectionPosition [_selection, LOD_MEMORY];
private _offset = _childPos vectorDiff _selectionPos;
_parent selectionVectorDirAndUp [_selection, LOD_MEMORY] params ["_selectionY", "_selectionZ"];
private _selectionX = _selectionY vectorCrossProduct _selectionZ;

private _m = matrixTranspose [_selectionX, _selectionY, _selectionZ];
private _pos = flatten ([_offset] matrixMultiply _m);
_child attachTo [_parent, _pos, _selection, true];
[_childX, _childY, _childZ] matrixMultiply _m params ["", "_vY", "_vZ"];

if (_isRelative) then {
    _child setVectorDirAndUp [_vY, _vZ];
};

[
    [_selectionPos, _selectionY, _selectionZ],
    [_pos, _vY, _vZ]
]

Details

Severity
Feature
Resolution
Open
Reproducibility
N/A
Operating System
Windows 10 x64
Category
Scripting

Event Timeline

This comment was removed by BIS_fnc_KK.