Totally lost at rotating an object
Posted: Fri Apr 05, 2024 2:49 pm
Hi,
I have a problem which I simply cannot solve (tried for two days now). I have a virtual pcb, which is shown from one side, so no rotation. Now the user can rotate the pcb with the mouse. I want to turn the pcb back into the starting rotation, when a key is released. This works without problems when setting the rotation via RotateEntity(ID,0,0,0,#PB_Absolute). So far so good. Now I want to make it more pleasing for the eye and rotate the pcb into position. Therefor I thought I just need to read the angles, divide them by 30 (for 30 steps) and substract the resulting values 30 times. But no, this does not work. I tried with EntityDirectionX/Y/Z(), EntityPitch/Roll/Yaw() and FetchOrientation(). This only resulted in the cognition that I have no idea what I'm doing. I googled too and found some info in the forum, but I simply don't understand it. So here's my question:
1. When do I need EntityDirectionX/Y/Z(), EntityPitch/Roll/Yaw(), FetchOrientation()?
2. How do I rotate my board smoothly into position?
The interesting part of the code is within the lines 41 to 66. I removed everything which is not necessary for this demonstration (including checks for successful initialization) to keep it short. Use the middle mouse key plus mouse movement to rotate the object and the end-key to reset orientation.
I have a problem which I simply cannot solve (tried for two days now). I have a virtual pcb, which is shown from one side, so no rotation. Now the user can rotate the pcb with the mouse. I want to turn the pcb back into the starting rotation, when a key is released. This works without problems when setting the rotation via RotateEntity(ID,0,0,0,#PB_Absolute). So far so good. Now I want to make it more pleasing for the eye and rotate the pcb into position. Therefor I thought I just need to read the angles, divide them by 30 (for 30 steps) and substract the resulting values 30 times. But no, this does not work. I tried with EntityDirectionX/Y/Z(), EntityPitch/Roll/Yaw() and FetchOrientation(). This only resulted in the cognition that I have no idea what I'm doing. I googled too and found some info in the forum, but I simply don't understand it. So here's my question:
1. When do I need EntityDirectionX/Y/Z(), EntityPitch/Roll/Yaw(), FetchOrientation()?
2. How do I rotate my board smoothly into position?
Code: Select all
ExamineDesktops()
InitEngine3D(#PB_Engine3D_NoLog)
InitKeyboard()
InitMouse()
InitSprite()
OpenWindow(0,0,0,800,600,"Test",#PB_Window_ScreenCentered)
OpenWindowedScreen(WindowID(MainWindow),0,0,WindowWidth(0),WindowHeight(0),0,0,0)
CreateCube(0,10)
CreateEntity(0,MeshID(0),#PB_Material_None)
ScaleEntity(0,1,1,0.05,#PB_Relative)
CreateLight(0, RGB(255, 0, 255), 0, 0, 200, #PB_Light_Point)
CreateCamera(0, 0, 0, 100, 100)
MoveCamera(0, 0, 0, 40, #PB_Absolute | #PB_Local)
CameraLookAt(0, 0, 0, 0)
CameraBackColor(0,#Gray)
Repeat
Repeat:Until WindowEvent()=0
KeyX = 0
KeyY = 0
MouseX = 0
MouseY = 0
;{ Maussteuerung
If ExamineMouse()
If MouseButton(#PB_MouseButton_Middle)
MouseX = -MouseDeltaX() * 0.2
MouseY = -MouseDeltaY() * 0.2
EndIf
EndIf
;}
If ExamineKeyboard()
; If KeyboardReleased(#PB_Key_End)
; RotateEntity(0,0,0,0,#PB_Absolute)
; EndIf
;{ Ausrichtung
If KeyboardReleased(#PB_Key_End)
Normalize=30
;Direction:
RotX=-1*EntityDirectionX(0)/30
RotY=-1*EntityDirectionY(0)/30
RotZ=-1*EntityDirectionZ(0)/30
;Pitch/Roll/Yaw:
;RotX=-1*EntityPitch(0,#PB_Engine3D_Adjusted)/30
;RotY=-1*EntityYaw(0,#PB_Engine3D_Adjusted)/30
;RotZ=-1*EntityRoll(0,#PB_Engine3D_Adjusted)/30
;Instantly:
;RotateEntity(0,0,0,0,#PB_Absolute)
EndIf
If Normalize
Normalize-1
If Normalize=0
RotateEntity(0,0,0,0,#PB_Absolute)
Else
RotateEntity(0,RotX,RotY,RotZ,#PB_Relative)
;RotateEntity(0,EntityDirectionX(0)-RotX,EntityDirectionY(0)-RotY,EntityDirectionZ(0)-RotZ,#PB_Absolute)
;RotateEntity(0,EntityPitch(0,#PB_Engine3D_Adjusted)-RotX,EntityYaw(0,#PB_Engine3D_Adjusted)-RotY,EntityRoll(0,#PB_Engine3D_Adjusted)-RotZ,#PB_Absolute)
EndIf
EndIf
;}
EndIf
RotateEntity(0, -MouseY, -MouseX, 0, #PB_Relative)
MoveCamera (0, KeyX, KeyY, 0)
RenderWorld()
FlipBuffers()
Until KeyboardPushed(#PB_Key_Escape)
ReleaseMouse(#True)
CloseScreen()