Rotate camera around an object?
Rotate camera around an object?
Hi all, I'm new here and would like an example on how to rotate a camera around a 3D cube using both MouseX() & MouseY().
Thank You!
Thank You!
Re: Rotate camera around an object?
Hello and Welcome Mythros,
I have an example, but it uses MouseDeltaX() and MouseDeltaY() instead of MouseX() and MouseY(). A little bit of tweaking and you can fix that though.
The main idea of my example is to create a node at the center of the cube. Then you attach the camera to the node. You then rotate that node and the camera will follow. With a little work you could make it more fluid and give the user a lot more control.
There are also a some nice Camera related examples that come with Purebasic. So check them out too.
I have an example, but it uses MouseDeltaX() and MouseDeltaY() instead of MouseX() and MouseY(). A little bit of tweaking and you can fix that though.
The main idea of my example is to create a node at the center of the cube. Then you attach the camera to the node. You then rotate that node and the camera will follow. With a little work you could make it more fluid and give the user a lot more control.
There are also a some nice Camera related examples that come with Purebasic. So check them out too.
Code: Select all
;PRESS Escape to exit
#CameraSpeed = 1
Define.f KeyX, KeyY, MouseX, MouseY
If InitEngine3D()
InitSprite()
InitKeyboard()
InitMouse()
ExamineDesktops()
Width=DesktopWidth(0)
Height=DesktopHeight(0)
OpenWindow(0, 0, 0, Width, Height, "Camera")
OpenWindowedScreen(WindowID(0), 0, 0, Width, Height, 0, 0, 0)
BoxTexture=CreateTexture(#PB_Any,256,256)
StartDrawing(TextureOutput(BoxTexture))
Box(0,0,256,256,RGB(255,255,0))
StopDrawing()
BoxMaterial=CreateMaterial(#PB_Any, TextureID(BoxTexture))
SetMaterialColor(BoxMaterial, #PB_Material_SpecularColor, RGB(255,255,255))
MaterialShininess(BoxMaterial,32)
PlaneTexture=CreateTexture(#PB_Any,256,256)
StartDrawing(TextureOutput(PlaneTexture))
Box(0,0,256,256,RGB(0,255,255))
StopDrawing()
PlaneMaterial=CreateMaterial(#PB_Any, TextureID(PlaneTexture))
SetMaterialColor(PlaneMaterial, #PB_Material_SpecularColor, RGB(255,255,255))
MaterialShininess(PlaneMaterial,32)
Box=CreateCube(#PB_Any,0.5)
BoxEntity=CreateEntity(#PB_Any,MeshID(Box),MaterialID(BoxMaterial))
Plane=CreatePlane(#PB_Any,10,10,10,10,10,10)
PlaneEntity=CreateEntity(#PB_Any,MeshID(Plane),MaterialID(PlaneMaterial),0,-1,0)
Light=CreateLight(#PB_Any, RGB(130,130,130), 1, 4, 4)
SetLightColor(Light, #PB_Light_DiffuseColor, RGB(200,200,200))
SetLightColor(Light, #PB_Light_SpecularColor, RGB(255,255,255))
Camera=CreateCamera(#PB_Any, 0, 0, 100, 100)
MoveCamera(Camera, 0, 0, 5, #PB_Absolute)
CameraBackColor(Camera,RGB(50,50,50))
CameraFixedYawAxis(Camera, #False)
Node=CreateNode(#PB_Any,0,0,0)
AttachNodeObject(Node,CameraID(Camera))
XLine=CreateLine3D(#PB_Any,0,0,0,RGB(255,0,0),5,0,0,RGB(255,0,0))
YLine=CreateLine3D(#PB_Any,0,0,0,RGB(0,255,0),0,5,0,RGB(0,255,0))
ZLine=CreateLine3D(#PB_Any,0,0,0,RGB(0,0,255),0,0,5,RGB(0,0,255))
Repeat
If ExamineMouse()
MouseX = -MouseDeltaX() * #CameraSpeed * 0.05
MouseY = -MouseDeltaY() * #CameraSpeed * 0.05
EndIf
If ExamineKeyboard()
EndIf
RotateNode(Node,MouseY, MouseX, 0,#PB_Relative)
CameraLookAt(Camera,0,0,0)
RenderWorld()
FlipBuffers()
Until KeyboardPushed(#PB_Key_Escape)
Else
MessageRequester("Error", "The 3D Engine can't be initialized", 0)
EndIf
End
Re: Rotate camera around an object?
You miss the event loop, that's why it stops working after a while
Re: Rotate camera around an object?
Hi, thanks alot for welcoming me, guys! =)
Also, thank you so much for the help, I really appreciate it immensely! =)
I do have one question, though. On the X-rotation Axis (the "Node" is "looking" "up or down", how do I stop it at exactly 90 degrees and -90 degrees, so I can see the top and the bottom of my cube?
Also, for some reason, my node won't stay accurately on my Cube.
Here's an image of what it's doing:

Thank you guys!
Also, thank you so much for the help, I really appreciate it immensely! =)
I do have one question, though. On the X-rotation Axis (the "Node" is "looking" "up or down", how do I stop it at exactly 90 degrees and -90 degrees, so I can see the top and the bottom of my cube?
Also, for some reason, my node won't stay accurately on my Cube.
Here's an image of what it's doing:

Thank you guys!
Re: Rotate camera around an object?
Hi Mythros.
You can use these (return values) to limit/restrict the roll, pitch and yaw of the node.
Best regards
Peter
You can use these (return values) to limit/restrict the roll, pitch and yaw of the node.
Code: Select all
Debug NodeYaw(Node,1)
Debug NodeRoll(Node,1)
Debug NodePitch(Node,1)
Peter
Last edited by DK_PETER on Tue Aug 27, 2013 4:38 pm, edited 1 time in total.
Current configurations:
Ubuntu 20.04/64 bit - Window 10 64 bit
Intel 6800K, GeForce Gtx 1060, 32 gb ram.
Amd Ryzen 9 5950X, GeForce 3070, 128 gb ram.
Ubuntu 20.04/64 bit - Window 10 64 bit
Intel 6800K, GeForce Gtx 1060, 32 gb ram.
Amd Ryzen 9 5950X, GeForce 3070, 128 gb ram.
Re: Rotate camera around an object?
Thanks, but how do I use these in conjunction with my MouseX() and MouseY()?
Re: Rotate camera around an object?
1. You could simply check the values inside a conditional if: endif and 'lock' the specific values from movingMythros wrote:Thanks, but how do I use these in conjunction with my MouseX() and MouseY()?
past a threshold in Rotatenode(node, x, y, z). Replace the mouseX and mouseY, when a max/min values are reached and
stop it from continous node rotation.
2. Try this to keep the node from going all over the place:
Pitch(NodeID(Node) ,MouseY, #PB_Local)
Yaw(NodeID(Node), MouseX, #PB_World)
If that's what you were looking for...
Best regards
Peter
Current configurations:
Ubuntu 20.04/64 bit - Window 10 64 bit
Intel 6800K, GeForce Gtx 1060, 32 gb ram.
Amd Ryzen 9 5950X, GeForce 3070, 128 gb ram.
Ubuntu 20.04/64 bit - Window 10 64 bit
Intel 6800K, GeForce Gtx 1060, 32 gb ram.
Amd Ryzen 9 5950X, GeForce 3070, 128 gb ram.
Re: Rotate camera around an object?
I still can't get this to work..
Here's what I have so far:
Here's what I have so far:
Code: Select all
If ExamineMouse()
MouseX = -MouseDeltaX() * #CameraSpeed * 0.05
MouseY = -MouseDeltaY() * #CameraSpeed * 0.05
EndIf
If ExamineKeyboard()
EndIf
Node_X = NodePitch(Node, 1)
Node_Y = NodeYaw(Node, 1)
Node_Z = NodeRoll(Node, 1)
If Node_X >90: Node_Y=90: EndIf
If Node_X <-90: Node_Y=-90: EndIf
If Node_Y >90: Node_Y=90: EndIf
If Node_Y <-90: Node_Y=-90: EndIf
If MouseButton(#PB_MouseButton_Left)
RotateNode(Node, MouseY, MouseX, 0, #PB_Relative)
RotateEntity(BoxEntity, 0, Node_X, 0, #PB_Absolute)
EndIfRe: Rotate camera around an object?
[quote="Mythros"]I still can't get this to work..
Here's what I have so far:
Edit: Should work on PB 5.11 now.. (Edited again).
Best regards
Peter
Here's what I have so far:
Edit: Should work on PB 5.11 now.. (Edited again).
Code: Select all
;PRESS Escape to exit
#CameraSpeed = 1
Define.f KeyX, KeyY, MouseX, MouseY
If InitEngine3D()
InitSprite()
InitKeyboard()
InitMouse()
ExamineDesktops()
Width=DesktopWidth(0)
Height=DesktopHeight(0)
OpenWindow(0, 0, 0, Width, Height, "Camera")
OpenWindowedScreen(WindowID(0), 0, 0, Width, Height, 0, 0, 0)
BoxTexture=CreateTexture(#PB_Any,256,256)
StartDrawing(TextureOutput(BoxTexture))
Box(0,0,256,256,RGB(255,255,0))
StopDrawing()
BoxMaterial=CreateMaterial(#PB_Any, TextureID(BoxTexture))
SetMaterialColor(BoxMaterial, #PB_Material_SpecularColor, RGB(255,255,255))
MaterialShininess(BoxMaterial,32)
PlaneTexture=CreateTexture(#PB_Any,256,256)
StartDrawing(TextureOutput(PlaneTexture))
Box(0,0,256,256,RGB(0,255,255))
StopDrawing()
PlaneMaterial=CreateMaterial(#PB_Any, TextureID(PlaneTexture))
SetMaterialColor(PlaneMaterial, #PB_Material_SpecularColor, RGB(255,255,255))
MaterialShininess(PlaneMaterial,32)
Box=CreateCube(#PB_Any,0.5)
BoxEntity=CreateEntity(#PB_Any,MeshID(Box),MaterialID(BoxMaterial))
Plane=CreatePlane(#PB_Any,10,10,10,10,10,10)
PlaneEntity=CreateEntity(#PB_Any,MeshID(Plane),MaterialID(PlaneMaterial),0,-1,0)
Light=CreateLight(#PB_Any, RGB(130,130,130), 1, 4, 4)
SetLightColor(Light, #PB_Light_DiffuseColor, RGB(200,200,200))
SetLightColor(Light, #PB_Light_SpecularColor, RGB(255,255,255))
Camera=CreateCamera(#PB_Any, 0, 0, 100, 100)
MoveCamera(Camera, 0, 0, 5, #PB_Absolute)
CameraBackColor(Camera,RGB(50,50,50))
XLine=CreateLine3D(#PB_Any,0,0,0,RGB(255,0,0),5,0,0,RGB(255,0,0))
YLine=CreateLine3D(#PB_Any,0,0,0,RGB(0,255,0),0,5,0,RGB(0,255,0))
ZLine=CreateLine3D(#PB_Any,0,0,0,RGB(0,0,255),0,0,5,RGB(0,0,255))
Node=CreateNode(#PB_Any, 0, 0, 0)
node2 = CreateNode(#PB_Any, 0, 0, 0)
AttachNodeObject(Node,CameraID(Camera))
AttachNodeObject(node2, NodeID(Node))
Repeat
ev = WindowEvent()
If ExamineMouse()
MouseX = -MouseDeltaX() * #CameraSpeed * 0.05
MouseY = -MouseDeltaY() * #CameraSpeed * 0.05
EndIf
If ExamineKeyboard()
EndIf
If NodePitch(Node, 1) > -80 And NodePitch(Node,1) < 80
RotateNode(Node, MouseY, 0, 0,#PB_Relative)
Else
If NodePitch(Node,1) < -80
MouseY + 5
Else
MouseY - 5
EndIf
RotateNode(Node, MouseY, 0,0, #PB_Relative)
EndIf
RotateNode(node2, 0, MouseX, 0, #PB_Relative)
;CameraLookAt(Camera, 0, 0, 0)
RenderWorld()
FlipBuffers()
Until KeyboardPushed(#PB_Key_Escape)
Else
MessageRequester("Error", "The 3D Engine can't be initialized", 0)
EndIf
End
Peter
Last edited by DK_PETER on Mon Aug 26, 2013 9:08 pm, edited 3 times in total.
Current configurations:
Ubuntu 20.04/64 bit - Window 10 64 bit
Intel 6800K, GeForce Gtx 1060, 32 gb ram.
Amd Ryzen 9 5950X, GeForce 3070, 128 gb ram.
Ubuntu 20.04/64 bit - Window 10 64 bit
Intel 6800K, GeForce Gtx 1060, 32 gb ram.
Amd Ryzen 9 5950X, GeForce 3070, 128 gb ram.
Re: Rotate camera around an object?
Pitch & Yaw is not a declaration, unfortunately. I'm using Purebasic 5.11. 
Re: Rotate camera around an object?
I'm not using PB 5.11 anymore, but try the example above now.Mythros wrote:Pitch & Yaw is not a declaration, unfortunately. I'm using Purebasic 5.11.
Current configurations:
Ubuntu 20.04/64 bit - Window 10 64 bit
Intel 6800K, GeForce Gtx 1060, 32 gb ram.
Amd Ryzen 9 5950X, GeForce 3070, 128 gb ram.
Ubuntu 20.04/64 bit - Window 10 64 bit
Intel 6800K, GeForce Gtx 1060, 32 gb ram.
Amd Ryzen 9 5950X, GeForce 3070, 128 gb ram.
Re: Rotate camera around an object?
Ok, that works, but for some reason, the camera won't stay on the player when I try to rotate on the X or Y axis and when it reaches negative or positive 90 on the X Rotation, for some reason the camera shakes just a tad. Other than that, it is BEAUTIFUL! 
Re: Rotate camera around an object?
Try the above example one more time...Mythros wrote:Ok, that works, but for some reason, the camera won't stay on the player when I try to rotate on the X or Y axis and when it reaches negative or positive 90 on the X Rotation, for some reason the camera shakes just a tad. Other than that, it is BEAUTIFUL!
Current configurations:
Ubuntu 20.04/64 bit - Window 10 64 bit
Intel 6800K, GeForce Gtx 1060, 32 gb ram.
Amd Ryzen 9 5950X, GeForce 3070, 128 gb ram.
Ubuntu 20.04/64 bit - Window 10 64 bit
Intel 6800K, GeForce Gtx 1060, 32 gb ram.
Amd Ryzen 9 5950X, GeForce 3070, 128 gb ram.
Re: Rotate camera around an object?
Still not keeping it from camera gimbal lock a little bit, and it still snaps the camera at 90 & -90 on the X-axis. the rotation is still not correct. I have attached 2 images. 1 with the INCORRECT camera rotation, and 1 with the CORRECT camera rotation when using the mouse to rotate. =)
Incorrect Rotation:
http://oi41.tinypic.com/sgijgj.jpg
Correct Rotation:
http://oi41.tinypic.com/2gucm1c.jpg
Incorrect Rotation:
http://oi41.tinypic.com/sgijgj.jpg
Correct Rotation:
http://oi41.tinypic.com/2gucm1c.jpg
Re: Rotate camera around an object?
Yeah...I see, what you mean. Try changing CameraFixedYawAxis as below:
It may 'flip out', when reaching the threshold, but that should be easy to fix.
Edit: Duh...Not Camera....But node..
Let me know how it behaves..
Best regards
Peter
It may 'flip out', when reaching the threshold, but that should be easy to fix.
Edit: Duh...Not Camera....But node..
Code: Select all
NodeFixedYawAxis(Node, #True, 0, 1, 0)
Best regards
Peter
Last edited by DK_PETER on Tue Aug 27, 2013 4:39 pm, edited 1 time in total.
Current configurations:
Ubuntu 20.04/64 bit - Window 10 64 bit
Intel 6800K, GeForce Gtx 1060, 32 gb ram.
Amd Ryzen 9 5950X, GeForce 3070, 128 gb ram.
Ubuntu 20.04/64 bit - Window 10 64 bit
Intel 6800K, GeForce Gtx 1060, 32 gb ram.
Amd Ryzen 9 5950X, GeForce 3070, 128 gb ram.


