Good evening, Community!
I need some help calculating an element's rotation based on the following matrix calculation:
'Point_B' is basically the point of which our player or vehicle element is facing towards, we draw a green "+" to show point_B's position on screen. Using processLineOfSight, we are able to show exactly where we're pointing to in the 3D world.
The problem is, or what I would really like achieve in this example snippet is, rotating our test object (AK-47) so that it is pointing directly at "Point_B".
-- client.lua
-- OOP true
setElementPosition( localPlayer, -281.52420, 1539.13477, 75.35938 ) -- Test location
local theObject = createObject( 355, -281.52420, 1539.13477, 75.35938, 0, 0, 0 ) -- Test object (AK-47)
addEventHandler( "onClientRender", root, function ( )
-- CALCULATE "POINT_A" / "POINT_B"
----------------------------------------
local matrix = localPlayer:getMatrix( 0, 0, 0 )
local point_A = matrix:transformPosition( 0, 0, 0 )
local point_B = matrix:transformPosition( 0, 500, 0 )
local hit, x, y, z, element = processLineOfSight( point_A, point_B, true, true, true, false, false, false, false, true, localPlayer )
local hit_start = Vector3( x, y, z )
local point_B = hit and hit_start or point_B
local scr = Vector2( getScreenFromWorldPosition( point_B ) )
----------------------------------------
-- CALCULATE OBJECT ROTATION:
-- Point at which I've lost my brain-cells
-----------------------------------
local rotX, rotY, rotZ = math.random( 0, 360 ), math.random( 0, 360 ), math.random( 0, 360 ) -- current position of the final brain cell
setElementRotation( theObject, rotX, rotY, rotZ ) -- rotate Test object (AK-47) to point to: "Point_B"
----------------------------------------
-- DRAW GREEN TARGET POINT AT: "Point_B"
----------------------------------------
if ( scr.length > 0 ) then
dxDrawText( "+", scr - Vector2 ( 0, 0 ), 0, 0, tocolor ( 0, 255, 0, 255 ), 2, "default" )
end
end )