Rotate camera around an object?
Posted: Sun Aug 25, 2013 12:41 am
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!
http://www.purebasic.com
https://www.purebasic.fr/english/
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

Code: Select all
Debug NodeYaw(Node,1)
Debug NodeRoll(Node,1)
Debug NodePitch(Node,1)
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()?
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)
EndIfCode: 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
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.
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!
Code: Select all
NodeFixedYawAxis(Node, #True, 0, 1, 0)