MoveEntity is not the answer, since you want collision. The physics system has the commands for forces etc.
If I'm not mistaken, you want some kind of basic character control. I played with your code, and come up with this:
Code: Select all
Enumeration
#Light
#Sphere
#BOXMESH
#BOXMESH2
#SphereEntity
#Camera
#Plane
#PlaneEntity
EndEnumeration
Structure camstruct
id.i
x.f
y.f
z.f
angle_x.f
angle_y.f
angle_z.f
targetangle_x.f
targetangle_y.f
targetangle_z.f
speed.f
height.f
physicsbody.i
EndStructure
#SPEED_MAXIMUM = 10
#CROUCH = 0.1
#STAND = 2
#MOUSE_SENSITIVITY = 0.05
Global maincamera.camstruct
maincamera\id = #Camera
maincamera\physicsbody = #SphereEntity
Procedure.f smoothmotion(x.f,targetx.f)
ProcedureReturn((0.9*x.f)+(0.1*targetx.f))
EndProcedure
Procedure handle_input()
If KeyboardPushed(#PB_Key_LeftControl) : maincamera\height = smoothmotion(maincamera\height,#CROUCH) :Else : maincamera\height = smoothmotion(maincamera\height,#STAND) : EndIf
maincamera\x = EntityX(maincamera\physicsbody)
maincamera\y = EntityY(maincamera\physicsbody)
maincamera\z = EntityZ(maincamera\physicsbody)
MoveCamera(maincamera\id,maincamera\x,(maincamera\y+maincamera\height),maincamera\z,#PB_Absolute)
RotateCamera(maincamera\id, maincamera\angle_x, maincamera\angle_y,(maincamera\targetangle_y-maincamera\angle_y)/4 , #PB_Absolute)
MouseX = -MouseDeltaX() * #MOUSE_SENSITIVITY
MouseY = -MouseDeltaY() * #MOUSE_SENSITIVITY
maincamera\targetangle_x = maincamera\targetangle_x + MouseY
maincamera\targetangle_y = maincamera\targetangle_y + Mousex
maincamera\angle_x = smoothmotion(maincamera\angle_x,maincamera\targetangle_x)
maincamera\angle_y = smoothmotion(maincamera\angle_y,maincamera\targetangle_y)
If MouseButton(#PB_MouseButton_Right)
maincamera\speed=smoothmotion(maincamera\speed,#speed_maximum)
Else
maincamera\speed=smoothmotion(maincamera\speed,0)
EndIf
EntityVelocity(maincamera\physicsbody,CameraDirectionX(maincamera\id)*maincamera\speed,0,CameraDirectionZ(maincamera\id)*maincamera\speed)
EntityAngularFactor(maincamera\physicsbody, 0, 0, 0)
EndProcedure
DPI.d = DesktopResolutionX()
InitEngine3D() : InitSprite() : InitKeyboard() : InitMouse()
OpenWindow(0, 0, 0, 1280, 720, "Plane example", #PB_Window_SystemMenu | #PB_Window_ScreenCentered | #PB_Window_MinimizeGadget | #PB_Window_MaximizeGadget)
OpenWindowedScreen(WindowID(0), 0, 0, 1280 * DPI, 720 * DPI, #True, 0, 0)
CreateLight(#Light, RGB(25, 25, 180), -5, 10, 5, #PB_Light_Point)
; Sphere
CreateSphere(#Sphere, 1)
CreateCube(#BOXMESH, 5)
CreateCube(#BOXMESH2, 2.5)
CreateEntity(#SphereEntity, MeshID(#Sphere), #PB_Material_None, 25, 1, 25)
CreateEntityBody(#SphereEntity, #PB_Entity_SphereBody)
; Camera
CreateCamera(#Camera, 0, 0, 100, 100)
; AttachEntityObject(#SphereEntity, "", CameraID(#Camera))
; Plane
CreatePlane(#Plane, 400, 400, 10, 10, 10, 10)
CreateEntity(#PlaneEntity, MeshID(#Plane), #PB_Material_None)
MoveEntity(#PlaneEntity, 5, 0, -5)
CreateEntityBody(#PlaneEntity, #PB_Entity_PlaneBody)
; some static and some dynamic box for collision test
For x = 1 To 50
entity = CreateEntity(#PB_Any,MeshID(#boxmesh),#PB_Material_None,Random(500)-250,2.5,Random(500)-250)
CreateEntityBody(entity, #PB_Entity_StaticBody)
entity = CreateEntity(#PB_Any,MeshID(#BOXMESH2),#PB_Material_None,Random(500)-250,25,Random(500)-250)
CreateEntityBody(entity, #PB_Entity_BoxBody)
Next x
EnableWorldPhysics(#True)
;main loop
Repeat
Repeat
current_event =WindowEvent()
If current_event = #PB_Event_CloseWindow : End : EndIf
Until current_event = 0
newtime = ElapsedMilliseconds()
If newtime-oldtime > 15
ExamineMouse()
ExamineKeyboard()
If KeyboardPushed(#PB_Key_Escape) : End : EndIf
handle_input()
RenderWorld(newtime-oldtime)
FlipBuffers()
oldtime = ElapsedMilliseconds()
EndIf
Delay (1)
ForEver
It's basic, and not so clean, but I hope you might adjust it to your needs. Turn around with mouse, right click=move forward, leftctrl=crouch.
Sliding collision with the big boxes, the small ones are dynamic. Also, you might consider capsule instead of a sphere for character.
Crouch only modifies the camera height, but you can change the phy object to a smaller one later. I did not implement that feature.
Happy coding