Page 1 of 1
Move Entity Help
Posted: Mon Oct 23, 2023 2:26 am
by J. Baker
Why does the sphere/camera not move?
Code: Select all
Enumeration
#Light
#Sphere
#SphereEntity
#Camera
#Plane
#PlaneEntity
EndEnumeration
DPI.d = DesktopResolutionX()
InitEngine3D() : InitSprite() : InitKeyboard()
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, 0.05)
CreateEntity(#SphereEntity, MeshID(#Sphere), #PB_Material_None, 5, 1, -5)
CreateEntityBody(#SphereEntity, #PB_Entity_SphereBody)
; Camera
CreateCamera(#Camera, 0, 0, 100, 100)
AttachEntityObject(#SphereEntity, "", CameraID(#Camera))
; Plane
CreatePlane(#Plane, 10, 10, 5, 5, 10, 10)
CreateEntity(#PlaneEntity, MeshID(#Plane), #PB_Material_None)
MoveEntity(#PlaneEntity, 5, 0, -5)
CreateEntityBody(#PlaneEntity, #PB_Entity_PlaneBody)
Repeat
If ExamineKeyboard()
If KeyboardPushed(#PB_Key_Up)
MoveEntity(#SphereEntity, 0, 0, -0.5, #PB_Relative | #PB_Local) ; Move Sphere/Camera
EndIf
EndIf
RenderWorld()
FlipBuffers()
Until WaitWindowEvent(1) = #PB_Event_CloseWindow
Re: Move Entity Help
Posted: Mon Oct 23, 2023 12:55 pm
by miso
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

Re: Move Entity Help
Posted: Mon Oct 23, 2023 1:35 pm
by J. Baker
Thanks for the source but doesn't that defeat the purpose for MoveEntity()?
Re: Move Entity Help
Posted: Mon Oct 23, 2023 1:50 pm
by miso
Not necessarily. You might want to "teleport" for example from one place to an other, usually, using absolute coordinates. Of course, there are other uses and solutions.
Re: Move Entity Help
Posted: Mon Oct 23, 2023 6:29 pm
by J. Baker
What's interesting is that ExamineWorldCollisions.pb example uses MoveEntity() just fine. I'm totally missing something.
Re: Move Entity Help
Posted: Mon Oct 23, 2023 7:04 pm
by miso
No, it was me, who did not know (or forgot), that works that way too. I'm happy with my solution though, I already upgraded it with gravity, jump and terrain. Use whatever you find better for your needs.

Re: Move Entity Help
Posted: Mon Oct 23, 2023 7:07 pm
by J. Baker
So do you know why it is not working in the code I posted? I've been stumped on this for a couple of days now. But yes, your method works great.

Re: Move Entity Help
Posted: Mon Oct 23, 2023 7:22 pm
by miso
Yes. With moveentity, use just #PB_Local. Rotate the sphere when you want to turn. Change the waitwindowevent() part. Instead, iterate windowevent() until it returns 0.
Re: Move Entity Help
Posted: Mon Oct 23, 2023 8:19 pm
by J. Baker
That makes sense. Oh my! Thank you very much.

Re: Move Entity Help
Posted: Mon Oct 23, 2023 10:46 pm
by J. Baker
This is interesting. Most of the time the MoveEntity works just fine but sometimes it doesn't. So I recompile and then it works again. Also, offset values for AttachEntityObject seem to do nothing. I tried extreme values and no difference.
I've been using PB for quite some time and have been happy with it but the 3D part drives me nuts.
Code: Select all
; Sphere
CreateSphere(#Sphere, 0.25) ;0.6
CreateEntity(#SphereEntity, MeshID(#Sphere), #PB_Material_None, 5, 0, -5)
CreateEntityBody(#SphereEntity, #PB_Entity_SphereBody)
; Camera
CreateCamera(#Camera, 0, 0, 100, 100)
AttachEntityObject(#SphereEntity, "", CameraID(#Camera), 0, 200, -150, 0, 0, 0) ;<--- No difference what so ever
Re: Move Entity Help
Posted: Tue Oct 24, 2023 12:03 am
by miso
I'm not familiar with that command, so I tested it. Seems fine, when you attach something to a bone. I can confirm, without bones, the offset values do not matter. (workaround: position your camera relative to the object, and then use the attach command. It will keep those values as offset.)
I think it's not a bug, but missing documentation.
Re: Move Entity Help
Posted: Tue Oct 24, 2023 12:45 am
by J. Baker
miso wrote: Tue Oct 24, 2023 12:03 am
(workaround: position your camera relative to the object, and then use the attach command. It will keep those values as offset.)
I was not aware of that. Thanks!
Re: Move Entity Help
Posted: Thu Oct 26, 2023 2:32 pm
by J. Baker
I found another issue I was having and it may be a PB bug. If you RotateEntity() before MoveEntity(), the entity will not move. As long as I move first before rotating, all is fine.
Code: Select all
; Bug fix
MoveEntity(#SphereEntity, 0, 0, -0.75, #PB_Local); Prevent RotateEntity() bug. Move first than all is fine.
Repeat
Event = WindowEvent()
If ExamineKeyboard()
If KeyboardPushed(#PB_Key_Up)
MoveEntity(#SphereEntity, 0, 0, -0.75, #PB_Local) ; Move Sphere / Attached Camera
EndIf
If KeyboardPushed(#PB_Key_Down)
MoveEntity(#SphereEntity, 0, 0, 0.75, #PB_Local)
EndIf
If KeyboardPushed(#PB_Key_Left)
RotateEntity(#SphereEntity, 0, 0.75, 0, #PB_Relative)
EndIf
If KeyboardPushed(#PB_Key_Right)
RotateEntity(#SphereEntity, 0, -0.75, 0, #PB_Relative)
EndIf
If KeyboardPushed(#PB_Key_Escape)
Quit = 1
EndIf
EndIf
RenderWorld()
FlipBuffers()
Until Quit = 1 Or Event = #PB_Event_CloseWindow
Re: Move Entity Help
Posted: Thu Oct 26, 2023 3:14 pm
by miso
Fine forum users posted a bug report about AttachEntityObject. It is confirmed, and under investigation. I think you should wait for the fix, if you do not want to position your camera manually.
Re: Move Entity Help
Posted: Thu Oct 26, 2023 5:14 pm
by J. Baker
Ok, thanks for the heads up.