GetElementMatrix: Difference between revisions

From Multi Theft Auto: Wiki
Jump to navigation Jump to search
No edit summary
No edit summary
Line 16: Line 16:


==Example==  
==Example==  
This example creates a utility function that gets the position that is in front of a specified player.  Note that it assumes you have the [http://lua-users.org/wiki/LuaMatrix Lua Matrix] library installed.
This example creates a utility function that turns an offset into a position that is relative to the specified element.
<syntaxhighlight lang="lua">
<syntaxhighlight lang="lua">
function getPositionInFrontOfElement(element)
function getPositionFromElementOffset(element,offX,offY,offZ)
--Get the position 5 units in front
local m = getElementMatrix ( element )  -- Get the matrix
local transformed = matrix{0,5,0} * matrix(getElementMatrix(element))
local x = offX * m[1][1] + offY * m[2][1] + offZ * m[3][1] + m[4][1]  -- Apply transform
--Get our offset components
local y = offX * m[1][2] + offY * m[2][2] + offZ * m[3][2] + m[4][2]
local offX,offY,offZ = transformed[1][1],transformed[2][1],transformed[3][1]
local z = offX * m[1][3] + offY * m[2][3] + offZ * m[3][3] + m[4][3]
--Return the transformed point
return x, y, z                              -- Return the transformed point
return offX, offY, offZ
end
end
-- Get the position of a point 3 units to the left of the element:
x,y,z = getPositionFromElementOffset(element,3,0,0)
-- Get the position of a point 2 units in front of the element:
x,y,z = getPositionFromElementOffset(element,0,2,0)
-- Get the position of a point 1 unit above the element:
x,y,z = getPositionFromElementOffset(element,0,0,1)
</syntaxhighlight>
</syntaxhighlight>


This example is exactly the same as above, but without using the Lua Matrix library.
This example creates some more matrix utility functions
<syntaxhighlight lang="lua">
<syntaxhighlight lang="lua">
function getPositionInFrontOfElement(element)
function getMatrixLeft(m)
-- Get the matrix
        return m[1][1], m[1][2], m[1][3]
local matrix = getElementMatrix ( element )
-- Get the transformation of a point 5 units in front of the element
local offX = 0 * matrix[1][1] + 5 * matrix[2][1] + 0 * matrix[3][1] + 1 * matrix[4][1]
local offY = 0 * matrix[1][2] + 5 * matrix[2][2] + 0 * matrix[3][2] + 1 * matrix[4][2]
local offZ = 0 * matrix[1][3] + 5 * matrix[2][3] + 0 * matrix[3][3] + 1 * matrix[4][3]
--Return the transformed point
return offX, offY, offZ
end
end
function getMatrixForward(m)
        return m[2][1], m[2][2], m[2][3]
end
function getMatrixUp(m)
        return m[3][1], m[3][2], m[3][3]
end
function getMatrixPosition(m)
        return m[4][1], m[4][2], m[4][3]
end
local mat = getElementMatrix(element)  -- Get the matrix
x,y,z = getMatrixLeft(mat)    -- Get the matrix left direction
x,y,z = getMatrixForward(mat)  -- Get the matrix forward direction
x,y,z = getMatrixUp(mat)      -- Get the matrix up direction
</syntaxhighlight>
</syntaxhighlight>


==See Also==
==See Also==
{{Client_element_functions}}
{{Client_element_functions}}

Revision as of 03:57, 22 April 2012

This function gets an element's transform matrix. This contains 16 float values that multiplied to a point will give you the point transformed. It is most useful for matrix calculations such as calculating offsets. For further information, please refer to a tutorial of matrices in computer graphics programming.

Note that the matrix returned by this function is not setup correctly for some calculations.

Syntax

table getElementMatrix ( element theElement )       

Required Arguments

  • theElement: The element which you wish to retrieve the matrix for

Returns

Returns a multi-dimensional array containing a 4x4 matrix.

Example

This example creates a utility function that turns an offset into a position that is relative to the specified element.

function getPositionFromElementOffset(element,offX,offY,offZ)
	local m = getElementMatrix ( element )  -- Get the matrix
	local x = offX * m[1][1] + offY * m[2][1] + offZ * m[3][1] + m[4][1]  -- Apply transform
	local y = offX * m[1][2] + offY * m[2][2] + offZ * m[3][2] + m[4][2]
	local z = offX * m[1][3] + offY * m[2][3] + offZ * m[3][3] + m[4][3]
	return x, y, z                               -- Return the transformed point
end

-- Get the position of a point 3 units to the left of the element:
x,y,z = getPositionFromElementOffset(element,3,0,0)

-- Get the position of a point 2 units in front of the element:
x,y,z = getPositionFromElementOffset(element,0,2,0)

-- Get the position of a point 1 unit above the element:
x,y,z = getPositionFromElementOffset(element,0,0,1)

This example creates some more matrix utility functions

function getMatrixLeft(m)
        return m[1][1], m[1][2], m[1][3]
end
function getMatrixForward(m)
        return m[2][1], m[2][2], m[2][3]
end
function getMatrixUp(m)
        return m[3][1], m[3][2], m[3][3]
end
function getMatrixPosition(m)
        return m[4][1], m[4][2], m[4][3]
end

local mat = getElementMatrix(element)  -- Get the matrix
x,y,z = getMatrixLeft(mat)     -- Get the matrix left direction
x,y,z = getMatrixForward(mat)  -- Get the matrix forward direction
x,y,z = getMatrixUp(mat)       -- Get the matrix up direction

See Also