Jump to content

[HELP] attachToBones* get attached element's rotation


#Dv^

Recommended Posts

Hi everyone!, I have a query regarding this script. I am wanting to rotate an object obtaining its rotation through the function getElementBoneAttachmentDetails and it returns a matrix, of which I do not know how to use it. Would you give me a guide to try to understand? 

https://forum.mtasa.com/topic/128821-rel-new-bone-attach-using-onclientpedsprocessed-and-useful-function-attachelementtobone/?tab=comments#comment-992075

I don't know how to get the integers rx, ry, rz of the rotation of the object through a matrix

Thanks for the help

Link to post

Although have no experience with matrix and I'm a noob in mathematics :) I have found something interesting in python and convert it to Lua, not sure it will work try converting the matrix with this function:

function convertRotationMatrixToEulerAngles(matrix)
	local roll, pitch, yaw = false, false, false;
	local pi = math.pi;
	local atan2 = math.atan2;
	local m_cos = math.cos;
	local m_sin = math.sin;
	if matrix then
		local m11, m12, m13 = matrix[1][1], matrix[1][2], matrix[1][3];
		local m21, m22, m23 = matrix[2][1], matrix[2][2], matrix[2][3];
		local m31, m32, m33 = matrix[3][1], matrix[3][2], matrix[3][3];
		if m31 ~= 1 and m31 ~= -1 then
			pitch_1 = -1*m_sin(m31);
			pitch_2 = pi - pitch_1;
			roll_1 = m_atan2(m32/m_cos(pitch_1), m33/m_cos(pitch_1));
			roll_2 = m_atan2(m33/m_cos(pitch_2), m33/m_cos(pitch_2));
			yaw_1 = m_atan2(m21/m_cos(pitch_1), m11/m_cos(pitch_1));
			yaw_2 = m_atan2(m21/m_cos(pitch_2), m11/m_cos(pitch_2));
			pitch = pitch_1;
			roll = roll_1;
			yaw = yaw_1;
		else
			if m31 == -1 then
				pitch = pi/2;
				roll = yaw + m_atan2(m12, m13);
			else
				pitch = -pi/2;
				roll = -1*yaw + m_atan2(-1*m12,-1*m13);
			end
			yaw = 0;
		end
		roll = roll*180/pi;
		pitch = pitch*180/pi;
		yaw = yaw*180/pi;
		return roll, pitch, yaw;
	end
end

It will return (in theory) rotX, rotY, rotZ in degrees

Hope that helps!

  • Thanks 1
Link to post
  • Patrick changed the title to [HELP] bone_attach get attached element's rotation
2 hours ago, Patrick said:

It isn't a matrix, 6-7-8th arguments are rx, ry, rz .

image.png.3b33b0538af2dd46f032336df1056077.png

He’s talking about attachToBones I have a pm from him also if you visit the link it’s attachToBones

Link to post

@Tekken Thank you very much for your help! I have used its helpful, but when obtaining the data it gives me wrong numbers. If I rotate X = 90, I should rotate the same with the matrix but it returns other numbers :( 

Link to post

I use this code to do what I want

exports.bone_attach:setElementBoneRotationOffset(object, 180, 40, 0)

local data = exports.bone_attach:getElementBoneAttachmentDetails(object)
local rx, ry, rz = convertRotationMatrixToEulerAngles(data[6])
print(rx, ry, rz) -- return -140, -5.3751111671239e-15, 180
Link to post
  • Patrick changed the title to [HELP] attachToBones* get attached element's rotation

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...