AttachElementsOffsets: Difference between revisions

From Multi Theft Auto: Wiki
Jump to navigation Jump to search
m (To be checked...)
mNo edit summary
 
(5 intermediate revisions by 2 users not shown)
Line 1: Line 1:
'''This article concerns the note left in [[attachElements]].'''
'''This article concerns the note left in [[attachElements]].'''


==Explanation==
==Problem==
The offset coordinates reflect the object space, not the world space. This means that you cannot simply visualize the attachment in the map editor and calculate the offsets between the 2 sets of world coordinates between "theElement" and "theAttachToObject".
The offset coordinates reflect the object space, not the world space. This means that you cannot simply visualize the attachment in the map editor and calculate the offsets between the 2 sets of world coordinates for "theElement" and "theAttachToObject".


==Example==
For example, if "theAttachToElement" has XYZ rotations, then "theElement" will inherit these rotations. The specified rotation offsets will then be performed from these starting rotation points. Simply put, "theElement" will be rotated twice.
Start freeroam resource and paste this into the console:
<syntaxhighlight lang="lua">setpos 1904.7426757813 -2411.1262207031 13.53911781311</syntaxhighlight>


Now, use the following script and look at the rocket. See how it is off center? Despite the fact you found the positions in the map editor and calculated the offsets. If you commented out [[attachElements]] you will see that it is in the correct position in terms of world coordinates.
==Solution==
The following code shows how to use offsets calculated in the map editor with 'attachElements':
<syntaxhighlight lang="lua">addEventHandler( "onResourceStart", resourceRoot,
    function()
        -- Postion and rotations from the map editor:
        local mainPos = { -756, 995, 14 }
        local mainRot = { 0, 0, 90 }            -- Two rotations are zero. See note in attachRotationAdjusted


If you make "rocketRZ = 0" you will have the correct coordinates. Again, because the offsets are based off "theAttachToElement". If you kept any rotations in the object, it would change the XYZ coordinates "theElement" would need to be attached at.
        local subPos = { -756, 999, 24 }
        local subRot = { 89, 0, 177 }          -- One rotation is zero. See note in attachRotationAdjusted


<syntaxhighlight lang="lua">
        -- Create the objects
--This example illustrates the problem with using the world coordinate math to calculate offsets.
        mainObject = createObject ( 17050, mainPos[1], mainPos[2], mainPos[3], mainRot[1], mainRot[2], mainRot[3] )
--Change rocketRZ to 0 to get the correct position.
        subObject = createVehicle ( 519, subPos[1], subPos[2], subPos[3], subRot[1], subRot[2], subRot[3] )


local rocketX = 1925.022705
        -- Attach so they look like what they do in the map editor
local rocketY = -2450.944336
        attachRotationAdjusted ( subObject, mainObject )
local rocketZ = 18.053474
    end
local rocketRX = 0
)
local rocketRY = 0
local rocketRZ = 69.61437


local vehX = 1925.112793 --more is forward
local vehY = -2446.260742 -- less is forward
local vehZ = 26.692139
local vehRX = 89.38142
local vehRY = 0.030000
local vehRZ = 177.90356


shuttletable = {}
function attachRotationAdjusted ( from, to )
shuttletable[1] = createObject ( 17050, rocketX, rocketY, rocketZ, rocketRX, rocketRY, rocketRZ )
    -- Note: Objects being attached to ('to') should have at least two of their rotations set to zero
shuttletable[2] = createVehicle ( 519, vehX, vehY, vehZ, vehRX, vehRY, vehRZ )
    --       Objects being attached ('from') should have at least one of their rotations set to zero
    -- Otherwise it will look all funny
attachOffsetX = vehX-rocketX
attachOffsetY = vehY-rocketY
attachOffsetZ = vehZ-rocketZ
attachOffsetRX = vehRX-rocketRX
attachOffsetRY = vehRY-rocketRY
attachOffsetRZ = vehRZ-rocketRZ


attachElements( shuttletable[2], shuttletable[1], attachOffsetX, attachOffsetY, attachOffsetZ, attachOffsetRX, attachOffsetRY, attachOffsetRZ )</syntaxhighlight>
    local frPosX, frPosY, frPosZ = getElementPosition( from )
    local frRotX, frRotY, frRotZ = getElementRotation( from )
    local toPosX, toPosY, toPosZ = getElementPosition( to )
    local toRotX, toRotY, toRotZ = getElementRotation( to )
    local offsetPosX = frPosX - toPosX
    local offsetPosY = frPosY - toPosY
    local offsetPosZ = frPosZ - toPosZ
    local offsetRotX = frRotX - toRotX
    local offsetRotY = frRotY - toRotY
    local offsetRotZ = frRotZ - toRotZ
 
    offsetPosX, offsetPosY, offsetPosZ = applyInverseRotation ( offsetPosX, offsetPosY, offsetPosZ, toRotX, toRotY, toRotZ )
 
    attachElements( from, to, offsetPosX, offsetPosY, offsetPosZ, offsetRotX, offsetRotY, offsetRotZ )
end
 
 
function applyInverseRotation ( x,y,z, rx,ry,rz )
    -- Degress to radians
    local DEG2RAD = (math.pi * 2) / 360
    rx = rx * DEG2RAD
    ry = ry * DEG2RAD
    rz = rz * DEG2RAD
 
    -- unrotate each axis
    local tempY = y
    y =  math.cos ( rx ) * tempY + math.sin ( rx ) * z
    z = -math.sin ( rx ) * tempY + math.cos ( rx ) * z
 
    local tempX = x
    x =  math.cos ( ry ) * tempX - math.sin ( ry ) * z
    z =  math.sin ( ry ) * tempX + math.cos ( ry ) * z
 
    tempX = x
    x =  math.cos ( rz ) * tempX + math.sin ( rz ) * y
    y = -math.sin ( rz ) * tempX + math.cos ( rz ) * y
 
    return x, y, z
end</syntaxhighlight>
 
[[Category:Scripting Concepts]]

Latest revision as of 10:42, 13 July 2024

This article concerns the note left in attachElements.

Problem

The offset coordinates reflect the object space, not the world space. This means that you cannot simply visualize the attachment in the map editor and calculate the offsets between the 2 sets of world coordinates for "theElement" and "theAttachToObject".

For example, if "theAttachToElement" has XYZ rotations, then "theElement" will inherit these rotations. The specified rotation offsets will then be performed from these starting rotation points. Simply put, "theElement" will be rotated twice.

Solution

The following code shows how to use offsets calculated in the map editor with 'attachElements':

addEventHandler( "onResourceStart", resourceRoot,
    function()
        -- Postion and rotations from the map editor:
        local mainPos = { -756, 995, 14 }
        local mainRot = { 0, 0, 90 }            -- Two rotations are zero. See note in attachRotationAdjusted

        local subPos = { -756, 999, 24 }
        local subRot = { 89, 0, 177 }           -- One rotation is zero. See note in attachRotationAdjusted

        -- Create the objects
        mainObject = createObject ( 17050, mainPos[1], mainPos[2], mainPos[3], mainRot[1], mainRot[2], mainRot[3] )
        subObject = createVehicle ( 519, subPos[1], subPos[2], subPos[3], subRot[1], subRot[2], subRot[3] )

        -- Attach so they look like what they do in the map editor
        attachRotationAdjusted ( subObject, mainObject )
    end
)


function attachRotationAdjusted ( from, to )
    -- Note: Objects being attached to ('to') should have at least two of their rotations set to zero
    --       Objects being attached ('from') should have at least one of their rotations set to zero
    -- Otherwise it will look all funny

    local frPosX, frPosY, frPosZ = getElementPosition( from )
    local frRotX, frRotY, frRotZ = getElementRotation( from )
    local toPosX, toPosY, toPosZ = getElementPosition( to )
    local toRotX, toRotY, toRotZ = getElementRotation( to )
    local offsetPosX = frPosX - toPosX
    local offsetPosY = frPosY - toPosY
    local offsetPosZ = frPosZ - toPosZ
    local offsetRotX = frRotX - toRotX
    local offsetRotY = frRotY - toRotY
    local offsetRotZ = frRotZ - toRotZ

    offsetPosX, offsetPosY, offsetPosZ = applyInverseRotation ( offsetPosX, offsetPosY, offsetPosZ, toRotX, toRotY, toRotZ )

    attachElements( from, to, offsetPosX, offsetPosY, offsetPosZ, offsetRotX, offsetRotY, offsetRotZ )
end


function applyInverseRotation ( x,y,z, rx,ry,rz )
    -- Degress to radians
    local DEG2RAD = (math.pi * 2) / 360
    rx = rx * DEG2RAD
    ry = ry * DEG2RAD
    rz = rz * DEG2RAD

    -- unrotate each axis
    local tempY = y
    y =  math.cos ( rx ) * tempY + math.sin ( rx ) * z
    z = -math.sin ( rx ) * tempY + math.cos ( rx ) * z

    local tempX = x
    x =  math.cos ( ry ) * tempX - math.sin ( ry ) * z
    z =  math.sin ( ry ) * tempX + math.cos ( ry ) * z

    tempX = x
    x =  math.cos ( rz ) * tempX + math.sin ( rz ) * y
    y = -math.sin ( rz ) * tempX + math.cos ( rz ) * y

    return x, y, z
end