Rotate camera around an object?

Everything related to 3D programming
Mythros
Enthusiast
Enthusiast
Posts: 306
Joined: Mon Aug 19, 2013 3:28 pm

Rotate camera around an object?

Post by Mythros »

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!
User avatar
Samuel
Enthusiast
Enthusiast
Posts: 756
Joined: Sun Jul 29, 2012 10:33 pm
Location: United States

Re: Rotate camera around an object?

Post by Samuel »

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.

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
Fred
Administrator
Administrator
Posts: 18384
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Re: Rotate camera around an object?

Post by Fred »

You miss the event loop, that's why it stops working after a while
Mythros
Enthusiast
Enthusiast
Posts: 306
Joined: Mon Aug 19, 2013 3:28 pm

Re: Rotate camera around an object?

Post by Mythros »

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:

Image

Thank you guys! :)
User avatar
DK_PETER
Addict
Addict
Posts: 904
Joined: Sat Feb 19, 2011 10:06 am
Location: Denmark
Contact:

Re: Rotate camera around an object?

Post by DK_PETER »

Hi Mythros.

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)
Best regards
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.
Mythros
Enthusiast
Enthusiast
Posts: 306
Joined: Mon Aug 19, 2013 3:28 pm

Re: Rotate camera around an object?

Post by Mythros »

Thanks, but how do I use these in conjunction with my MouseX() and MouseY()?
User avatar
DK_PETER
Addict
Addict
Posts: 904
Joined: Sat Feb 19, 2011 10:06 am
Location: Denmark
Contact:

Re: Rotate camera around an object?

Post by DK_PETER »

Mythros wrote:Thanks, but how do I use these in conjunction with my MouseX() and MouseY()?
1. You could simply check the values inside a conditional if: endif and 'lock' the specific values from moving
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.
Mythros
Enthusiast
Enthusiast
Posts: 306
Joined: Mon Aug 19, 2013 3:28 pm

Re: Rotate camera around an object?

Post by Mythros »

I still can't get this to work..

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)
      EndIf
User avatar
DK_PETER
Addict
Addict
Posts: 904
Joined: Sat Feb 19, 2011 10:06 am
Location: Denmark
Contact:

Re: Rotate camera around an object?

Post by DK_PETER »

[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).

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
Best regards
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.
Mythros
Enthusiast
Enthusiast
Posts: 306
Joined: Mon Aug 19, 2013 3:28 pm

Re: Rotate camera around an object?

Post by Mythros »

Pitch & Yaw is not a declaration, unfortunately. I'm using Purebasic 5.11. :(
User avatar
DK_PETER
Addict
Addict
Posts: 904
Joined: Sat Feb 19, 2011 10:06 am
Location: Denmark
Contact:

Re: Rotate camera around an object?

Post by DK_PETER »

Mythros wrote:Pitch & Yaw is not a declaration, unfortunately. I'm using Purebasic 5.11. :(
I'm not using PB 5.11 anymore, but try the example above now.
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.
Mythros
Enthusiast
Enthusiast
Posts: 306
Joined: Mon Aug 19, 2013 3:28 pm

Re: Rotate camera around an object?

Post by Mythros »

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! :)
User avatar
DK_PETER
Addict
Addict
Posts: 904
Joined: Sat Feb 19, 2011 10:06 am
Location: Denmark
Contact:

Re: Rotate camera around an object?

Post by DK_PETER »

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! :)
Try the above example one more time... :-)
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.
Mythros
Enthusiast
Enthusiast
Posts: 306
Joined: Mon Aug 19, 2013 3:28 pm

Re: Rotate camera around an object?

Post by Mythros »

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
User avatar
DK_PETER
Addict
Addict
Posts: 904
Joined: Sat Feb 19, 2011 10:06 am
Location: Denmark
Contact:

Re: Rotate camera around an object?

Post by DK_PETER »

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..

Code: Select all

NodeFixedYawAxis(Node, #True, 0, 1, 0)
Let me know how it behaves..

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.
Post Reply