A part ça j'ai voulu essayer et j'obtiens les bons angles si je ne fais que des rotations en x ou / et z, ou en y seul, si je mets les 3 ensembles le résultat n'est pas bon.
Code : Tout sélectionner
; Fichiers Include
IncludePath "..\Include\"
IncludeFile "d3dx9.pbi"
IncludeFile "dreamotion3d.pbi"
IncludeFile "PhysX.pbi"
; Globales
Global anglex.f, angley.f, flagXDown.w
Global mox.f, omx.f, moy.l, omy.l
Global angle_y.f,angle_x.f, angle_z.f
Global *camera.CEntity
Global Dim *roue.CEntity(4)
Global Dim *axis.CEntity(4)
Global *font.CFont
Global *caisse
Global *joint.NxJoint
Global *joint2.NxJoint
Global mat.D3DXMATRIX
Macro NEW_X(x, Angle, Distance)
((x) + Cos((Angle) * 0.0174533) * (Distance))
EndMacro
Macro NEW_Z(z, Angle, Distance)
((z) - Sin((Angle) * 0.0174533) * (Distance))
EndMacro
;- Declaration des procédures
Declare.f CurveValue(actuelle.f, Cible.f, P.f)
Declare GestionCamera()
; Initialisation des différents modules
If InitSprite() = 0 Or InitKeyboard() = 0 Or InitMouse() = 0
End
EndIf
; Initialise la fenêtre graphique
DM_Graphics3D(800, 600, 32, 0, 1)
; Charge une font
*font = DM_LoadFont("Arial",8 , 1)
DM_SetColorText (*font, 198,198,198,255)
DM_AmbiantLight(150, 150, 150)
SetCurrentDirectory("medias/")
;---------------------------------
; INIT DU MOTEUR PHYSIQUE
;---------------------------------
NX_Init(*DM_d3dDev9)
;--------------------------------------------------------------
; PETITE LIGHT HISTOIRE DE ...
;--------------------------------------------------------------
*light = DM_CreateLight(3, #Null)
;--------------------------------------------------------------
; set ground
;--------------------------------------------------------------
*ter = DM_LoadEntity("land3.x", #Null, #False)
DM_TranslateEntity(*ter, 0,20,0)
NX_CreateStaticElement(*ter,10, 2)
*ter = DM_CreateSphere(4)
DM_ScaleMesh(*ter, 30,30,30)
DM_TranslateEntity(*ter, 0,-35,100)
NX_CreateStaticElement(*ter,10, 2)
*brush = DM_GetBrush(*ter)
DM_LoadTexture( DM_GetTexture(*brush), "grille1.bmp")
*caisse = DM_CreateCube()
DM_ScaleMesh(*caisse, 4, 2,8)
DM_TranslateEntity(*caisse, 0,3,0)
NX_CreatePhysicBox(*caisse, 0,0,0, 5000.0,0)
For i = 0 To 3
*roue(i) = DM_CreateCylinder(16)
DM_ScaleMesh(*roue(i), 2,2,0.25)
DM_RotateMesh(*roue(i), 0,90,0)
Next
DM_TranslateEntity(*roue(0), 6,0,7) : NX_CreatePhysicMesh(*roue(0), 400,2) : NX_EntitySetContact(*roue(0), *caisse, 0)
DM_TranslateEntity(*roue(1), 6,0,-7): NX_CreatePhysicMesh(*roue(1), 400,2) : NX_EntitySetContact(*roue(1), *caisse, 0)
DM_TranslateEntity(*roue(2), -6,0,7): NX_CreatePhysicMesh(*roue(2), 400,2) : NX_EntitySetContact(*roue(2), *caisse, 0)
DM_TranslateEntity(*roue(3), -6,0,-7): NX_CreatePhysicMesh(*roue(3), 400,2) : NX_EntitySetContact(*roue(3), *caisse, 0)
For i = 0 To 3
*axis(i) = DM_CreateSphere(8)
DM_ScaleMesh(*axis(i), 0.45,0.45, 0.45)
Next
DM_TranslateEntity(*axis(0), 4,0,7) : NX_CreatePhysicSphere(*axis(0), 0,400,0)
NX_EntitySetContact(*axis(0), *caisse, 0)
NX_EntitySetContact(*axis(0), *roue(0), 0)
DM_TranslateEntity(*axis(1), 4,0,-7): NX_CreatePhysicSphere(*axis(1), 0,400,0)
NX_EntitySetContact(*axis(1), *caisse, 0)
NX_EntitySetContact(*axis(1), *roue(1), 0)
DM_TranslateEntity(*axis(2), -4,0,7): NX_CreatePhysicSphere(*axis(2), 0,400,0)
NX_EntitySetContact(*axis(2), *caisse, 0)
NX_EntitySetContact(*axis(2), *roue(2), 0)
DM_TranslateEntity(*axis(3), -4,0,-7): NX_CreatePhysicSphere(*axis(3), 0,400,0)
NX_EntitySetContact(*axis(3), *caisse, 0)
NX_EntitySetContact(*axis(3), *roue(3), 0)
;--------------------------------------------------------------
; create joints
;--------------------------------------------------------------
;
*joint = NX_CreateRevoluteJointM(*roue(0), *axis(0) , 6,0,7, 1,0,0)
*joint = NX_CreatePrismaticJoint(*axis(0), *caisse , 5,0,7, 0,1,0)
NX_CreateSpring(*axis(0), *caisse, 4,0,7 , 0.1)
*joint = NX_CreateRevoluteJointM(*roue(2), *axis(2) , -6,0,7, 1,0,0)
*joint = NX_CreatePrismaticJoint(*axis(2), *caisse , -5,0,7, 0,1,0)
NX_CreateSpring(*axis(2), *caisse, -4,0,7 , 0.1)
*joint = NX_CreateRevoluteJointM(*roue(1), *axis(1) , 6,0,-7, 1,0,0)
*joint = NX_CreatePrismaticJoint(*axis(1), *caisse , 5,0,-7, 0,1,0)
NX_CreateSpring(*axis(1), *caisse, 4,0,-7 , 0.1)
*joint = NX_CreateRevoluteJointM(*roue(3), *axis(3) , -6,0,-7, 1,0,0)
*joint = NX_CreatePrismaticJoint(*axis(3), *caisse , -5,0,-7, 0,1,0)
NX_CreateSpring(*axis(3), *caisse, -4,0,-7 , 0.1)
;---------------------------------------
; Gestion des caméras
; ---------------------------------------
*camera = DM_CreateCamera(#Null)
DM_MoveEntity(*camera, -9,25, 37)
DM_TurnEntity(*camera, 30,170, 0)
DM_CameraClsColor(*camera, 25, 25, 25)
; ---------------------------------------
; Boucle principale
; ---------------------------------------
Procedure.f atan2(y.d,x.d)
;atan2 procedure by Paul Dixon
;http://www.powerbasic.com/support/forums/Forum4/HTML/009180.html
;adapted to PureBasic by Jack
! fld qword [p.v_y] ;load y
! fld qword [p.v_x] ;load x
! fpatan ;get atan(y/x), put result in ST1. then pop stack to leave result in ST0
! ftst ;test ST0 (that's the top of stack) against zero
! fstsw ax ;put result of test into AX
! sahf ;get the FPU flags into the CPU flags
! jae @@skip ; if above or equal then skip the add 2*pi code
! fldpi ;get pi
! fadd st1,st0 ;add pi to result
! faddp st1,st0 ;and again, for 2pi, then pop the now unneeded pi from st0
! @@skip:
ProcedureReturn
EndProcedure
Procedure.f WrapValue(angle.f); <- wraps a value into [0,360) fringe
;Psychophanta : http://purebasic.fr/english/viewtopic.php?t=18635
!fild dword[@f] ; <- now i have 360 into st0
!fld dword[p.v_angle]
!fprem
!fadd st1,st0
!fldz
!fcomip st1
!fcmovnbe st0,st1
!fstp st1
ProcedureReturn
!@@:dd 360
EndProcedure
Procedure CalculAngle()
DM_EntityWorld(*Caisse, @mat)
; 0 1 2 3
; 4 5 6 7
; 8 9 10 11
;12 13 14 15
Define.f c, trx, try, RADIANS
RADIANS = 57.29578
angle_y.f = ASin(mat\_31); /* Calcul de l'Angle Y */
C = Cos(angle_y);
angle_y * RADIANS ; * RADIANS;
If (Abs(C) > 0.005) ; /* Gimbal lock ? */
trx = mat\_33 / C; /* Non, donc calcul de l'angle X */
try = -mat\_32 / C;
angle_x = atan2(try, trx) * RADIANS;
trx = mat\_11 / C; /* Calcul de l'angle Z */
try = -mat\_21 / C;
angle_z = atan2(try, trx) * RADIANS;
Else ; /* Gimbal lock */
angle_x = 0; /* Angle X à 0 */
trx = mat\_22; /* Calcul de l'angle Z */
try = mat\_12;
angle_z = atan2(try, trx) * RADIANS;
EndIf
angle_x = WrapValue(angle_x); /* Modulo ;) */
angle_y = WrapValue(angle_y);
angle_z = WrapValue(angle_z);
EndProcedure
Repeat
ExamineKeyboard()
ExamineMouse()
ShowCursor_(1)
If KeyboardReleased(#PB_Key_Escape) Or WindowEvent()=#PB_Event_CloseWindow
Quit=1
EndIf
If KeyboardPushed(#PB_Key_Up)
NX_EntitySetAngularMomentum(*roue(0), 5000,0,0)
NX_EntitySetAngularMomentum(*roue(2), 5000,0,0)
EndIf
If KeyboardPushed(#PB_Key_Down)
NX_EntityAddForceAtLocalPos(*caisse, 0,0,50000, 0,2,0)
EndIf
; ---------------
; Rendu
; ---------------
; mise a jour de la physique
NX_Update()
GestionCamera()
CalculAngle()
NX_RotateEntity(*Caisse,67,0,36)
DM_BeginScene()
DM_RenderWorld()
DM_DrawText(*font,0,0,StrF(angle_x,1) + " " + StrF(angle_y,1) + " " + StrF(angle_z,1))
DM_DrawText(*font,0,20,StrF(mat\_11,1) + " " + StrF(mat\_21,1) + " " + StrF(mat\_31,1) + " " + StrF(mat\_41,1))
DM_DrawText(*font,0,40,StrF(mat\_12,1) + " " + StrF(mat\_22,1) + " " + StrF(mat\_32,1) + " " + StrF(mat\_42,1))
DM_DrawText(*font,0,60,StrF(mat\_13,1) + " " + StrF(mat\_23,1) + " " + StrF(mat\_33,1) + " " + StrF(mat\_43,1))
DM_DrawText(*font,0,80,StrF(mat\_14,1) + " " + StrF(mat\_24,1) + " " + StrF(mat\_34,1) + " " + StrF(mat\_44,1))
DM_EndScene()
Until Quit=1
NX_Exit()
DM_ClearGraphics()
End
Procedure.f CurveValue(actuelle.f, Cible.f, P.f)
;Calcule une valeur progressive allant de la valeur actuelle à la valeur cible
Define.f Delta
Delta = Cible - actuelle
If P > 1000.0 : P = 1000.0 : EndIf
ProcedureReturn (actuelle + ( Delta * P / 1000.0))
EndProcedure
Procedure GestionCamera()
Define.f Px, Py, Pz, Pv
Define.f x,y,z
Static AngleCamera.f
Pv = 25
AngleCamera = CurveValue(AngleCamera, DM_EntityYaw(*Caisse) + 90, Pv)
Px = CurveValue(DM_EntityX(*Camera), NEW_X(DM_Entityx(*Caisse), AngleCamera, 50), Pv)
Py = CurveValue(DM_EntityY(*Camera), DM_EntityY(*Caisse) + 20, Pv)
Pz = CurveValue(DM_EntityZ(*Camera), NEW_Z(DM_EntityZ(*Caisse), AngleCamera, 50), Pv)
DM_PositionEntity(*Camera, Px, Py, Pz)
DM_PointEntity(*Camera, *Caisse)
EndProcedure