GetMatElement MatToEuler

Blitz3D Forums/Blitz3D Programming/GetMatElement MatToEuler

Bobysait(Posted 2007) [#1]
here is my conversion from Entity's Matrix to Euler angles

Graphics3D 800,600,0,2
SetBuffer BackBuffer()

; ======================== MATRIX CONVERT ====================
Dim Matrix#(4,4)
Global G_Pit#
Global G_yaw#
Global G_Rol#

Function MatToEuler()
	Local xz#=Sqr(Matrix(2,0)*Matrix(2,0)+Matrix(2,2)*Matrix(2,2))
	G_Pit=-ATan2( Matrix(2,1) , xz )
	G_Yaw=-ATan2( Matrix(2,0) , Matrix(2,2) )
	G_Rol=+ATan2( Matrix(0,1) , Matrix(1,1) )
End Function
; =============================================================



Test()
End

Function Test()
	Local Spc#=StringWidth(" ")
	Local colstep%=1
	Local Cam%
	Local mesh%	=	CreateRepere():HideEntity mesh

	cam=	CreateCamera	()
			CameraProjMode	(cam,2)
			CameraZoom		(cam,.1)
			PositionEntity	(cam,0,0,-2)

	; test entity
	piv	=	CreatePivot		()
			TranslateEntity	(piv,10.1,20.2,30.3,1)

	; re0 => rotate with pitch/yaw/roll from pivot
	re0	=	CopyMesh	(mesh)
			MoveEntity	(re0,-3.333,0,0)
	; re1 => rotate with matrix coords returned
	re1	=	CopyMesh	(mesh)
			MoveEntity	(re1,+3.333,0,0)

	Repeat
		If KeyHit(1) Exit
		If MouseDown(1)
			msx=MouseXSpeed()
			msy=MouseYSpeed()
			TurnEntity piv,msy,msx,0
		EndIf
		RenderWorld
			Color 0,255,0
			Rect 5,5,790,70,0
			Line 400,5,400,75
			Text 202,10,"---------- Configuration ---------",1
			Text 202,30,"Mouse 1          => Turn Pivot",1
			Text 202,45,"Arrows L/R + U/D => Move Pivot",1
			px#=EntityX(piv)
			py#=EntityY(piv)
			pz#=EntityZ(piv)
			rx#=EntityPitch(piv)
			ry#=EntityYaw(piv)
			rz#=EntityRoll(piv)
			RotateEntity re0,rx,ry,rz,1
			; print matrix
			For r= 0 To 3
				For c = 0 To 3
					Matrix(r,c)=GetMatElement(piv,r,c)
				Next
			Next
			mx=400-(Float(39)*spc)/2
			y=100
			For r=0 To 3
			For c=0 To 3
				For r1=r To 3
				For c1=0 To 3
					If c1<c And (r<>r1 And c<>c1)
						If Abs(Matrix(r,c)-Matrix(r1,c1))<.0001
							Color 255,0,0
							x0=mx+10+3*Spc+c*9*Spc+Spc*3
							x1=mx+10+3*Spc+c1*9*Spc+Spc*3
							y0=y+30+r*12
							y1=y+30+r1*12
							Line x0,y0,x1,y1
							colstep=(r Mod(2)+c Mod(2))/2
							Select colstep
								Case 0:Color 50,70,90
								Case 1:Color 20,30,50
							End Select
							x0=mx+10+3*Spc+c*9*Spc-5
							y0=y+23+r*12
							x1=mx+10+3*Spc+c1*9*Spc-5
							y1=y+23+r1*12
							w=8*Spc+10
							h=14
							Rect x0,y0,w,h,1
							Rect x1,y1,w,h,1
							Color 255,255,0
							Rect x0,y0,w,h,0
							Rect x1,y1,w,h,0
						EndIf
					EndIf
				Next
				Next
			Next
			Next

			Color 255,180,000
			Text mx+10+5,y-12,"_______________________________________"
			Text mx+10+0,y+00,"| Matrix View                          |"
			Text mx+10+0,y+12,"|                                      |"
			y=y+12
			For r= 0 To 3
				l$="|"+String(r,1)
				For c= 0 To 3
					l=l+" "+formatNumber(Matrix(r,c),8)
				Next
				y=y+12 
				Text mx+10,y,l+" |"
			Next
			y=y+12
			Text mx+10+0,y,"|      0        1        2        3    |"
			Text mx+10+5,y,"_______________________________________"

		; Calcul Matrix
			MatToEuler()
			RotateEntity re1,G_Pit,G_Yaw,G_Rol,1
		; general Infos
			Text 602,10,"---------- Rotations ------------",1
			pivRL$		=		rx+" / "+ry+" / "+rz
			pivRM$		=		G_Pit+" / "+G_Yaw+" / "+G_Rol
			Text 602,30,"Local  Rot :"+pivRL,1
			Text 602,45,"Matrix Rot :"+pivRM,1

			Rect 140,200,250,200,0
			Rect 410,200,250,200,0
			Rect 140,400,250,030,0
			Rect 410,400,250,030,0
			Text 265,415,"TurnEntity",1,1
			Text 535,415,"Matrix Convert",1,1
			Color 128,128,128
			Line 265,250,265,350
			Line 535,250,535,350
			Line 160,300,370,300
			Line 430,300,640,300
		Flip
	Forever
End Function

; ========================= PRIVATE ==========================
Function CreateRepere%()
	Mdl	=	CreateMesh	()
	Local b%[3]
	b[0]=CreateBrush(255,000,000)
	b[1]=CreateBrush(000,255,000)
	b[2]=CreateBrush(000,000,255)
	axeX=	CreateCube	()
			ScaleMesh	(axeX,1,.05,.05)
			PositionMesh(axeX,1,0,0)
	axeY=	CreateCube	()
			ScaleMesh	(axeY,.05,1,.05)
			PositionMesh(axeY,0,1,0)
	axeZ=	CreateCube	()
			ScaleMesh	(axeZ,.05,.05,1)
			PositionMesh(axeZ,0,0,1)
	PaintMesh axeX,b[0]:FreeBrush b[0]
	PaintMesh axeY,b[1]:FreeBrush b[1]
	PaintMesh axeZ,b[2]:FreeBrush b[2]
	AddMesh axeX,Mdl:FreeEntity axeX
	AddMesh axeY,Mdl:FreeEntity axeY
	AddMesh axeZ,Mdl:FreeEntity axeZ
	Return Mdl
End Function


Function formatNumber$(number#,lenght%=8)
	Local l_$
	l_=String(number,1)
	; supprime le "-"
	If Number<0 l_=Right(l_,Len(l_)-1)
	; si trop grand => tronque la fin ( -1 pour le signe )
	If Len(l_)>lenght-1
		l_=Left(l_,lenght-1)
	Else
		; si trop petit rajoute des "0" devant
		Repeat
			If Len(l_)>=lenght-1 Exit
			l_="0"+l_
		Forever
	EndIf
	; rajoute le signe "+/-"
	If Number>=0
		l_="+"+l_
	Else
		l_="-"+l_
	EndIf
	Return l_
End Function


Now,I 'm looking for a way to understand how the matrix datas are setted...

I searched within quaternions, matrix and else, but no way to get the same result. Anyone have any ideas on how it exactly run ?