Camera movement - Walking on a plane
Posted: Sun Oct 24, 2021 3:46 pm
I posted this by accident in the 2D game sub forum initially.
I just need the camera to hover at a constant height (Y pos) and allow me to move it around without floating. Such as in an FPS game. But I don't need physics or anything complicated (no jumping). I tweaked some code for an hour and can't make it work. It either floats around or doesn't aim correctly. None of the examples do this from what I can tell.
Here's what I cobbled together from some examples. It's not fully working, but it has to be close.
I know there has to be an example of this or someone must have code. It would be needed in many applications/games.
I just need the camera to hover at a constant height (Y pos) and allow me to move it around without floating. Such as in an FPS game. But I don't need physics or anything complicated (no jumping). I tweaked some code for an hour and can't make it work. It either floats around or doesn't aim correctly. None of the examples do this from what I can tell.
Here's what I cobbled together from some examples. It's not fully working, but it has to be close.
Code: Select all
#MOVEMENT_SPEED = 1
Global MouseXRotation.f,MouseYRotation.f,KeyX.f,KeyZ.f
InitEngine3D()
InitSprite()
InitMouse()
InitKeyboard()
OpenWindow(0, 0, 0, 640, 480, "Plane example", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
OpenWindowedScreen(WindowID(0), 0, 0, 640, 480, 0, 0, 0)
; Light
CreateLight(#PB_Any, RGB(25, 25, 180), -5, 10, 5, #PB_Light_Point)
; Camera
CreateCamera(0, 0, 0, 100, 100)
MoveCamera(0, 2, 1, 3, #PB_Absolute | #PB_Local)
CameraLookAt(0, 0, 0, 0)
; Create the plane and attach it to the scene
CreatePlane(0, 32, 32, 1, 1, 0, 0)
CreateEntity(0, MeshID(0), #PB_Material_None)
Repeat
If ExamineMouse()
MouseYRotation = -MouseDeltaX() / 10
MouseXRotation = -MouseDeltaY() / 10
EndIf
RotateCamera(0, MouseXRotation, MouseYRotation, 0, #PB_Relative)
;Update Key Presses and position the Camera accordingly
If ExamineKeyboard()
If KeyboardPushed(#PB_Key_Left) : KeyX = -#MOVEMENT_SPEED : EndIf
If KeyboardPushed(#PB_Key_Right) : KeyX = #MOVEMENT_SPEED : EndIf
If KeyboardPushed(#PB_Key_Up) : KeyZ = -#MOVEMENT_SPEED : EndIf
If KeyboardPushed(#PB_Key_Down) : KeyZ = #MOVEMENT_SPEED : EndIf
If KeyboardPushed(#PB_Key_Escape) : ReleaseMouse(#True) : EndIf
MoveCamera(0, KeyX, 0, KeyZ)
KeyX = 0
KeyZ = 0
EndIf
RenderWorld()
FlipBuffers()
Until WaitWindowEvent(1) = #PB_Event_CloseWindow