[Solved] Can not move an entity when the speed < > 0

Everything related to 3D programming
User avatar
falsam
Enthusiast
Enthusiast
Posts: 635
Joined: Wed Sep 21, 2011 9:11 am
Location: France
Contact:

[Solved] Can not move an entity when the speed < > 0

Post by falsam »

Hi.

I do not understand what is the problem : I try to move an entity if its speed is different from 0.

Code: Select all

  If PlayerSpeed <> 0
    MoveEntity(Player, 0, 0, PlayerSpeed, #PB_Absolute|#PB_Local)
  EndIf
I tested this value (PlayerSpeed) with a debug inside the condition. The value is greater than 0 but the player does not move.

If I comment the condition, the player moves. strange !!

Snippet code

Code: Select all

EnableExplicit

Enumeration
  #Mainform
EndEnumeration

Global WindowStyle.i=#PB_Window_MinimizeGadget|#PB_Window_MaximizeGadget|#PB_Window_ScreenCentered
Global Event.l

Global Camera, Player, PlayerSpeed.f

Procedure Load_Scene()
  Protected Texture, Material, Mesh, Entity
  
  ; Material 
  Texture = CreateTexture(#PB_Any,512,512)
  StartDrawing(TextureOutput(Texture))
  Box(0,0,512,512,RGB(0, 0, 0))
  Box(1,1,510,510,RGB(255, 216, 0))
  StopDrawing()
  Material = CreateMaterial(#PB_Any,TextureID(texture))
  
  ;
  ; Ground
  Mesh = CreatePlane(#PB_Any, 100, 100, 3, 6, 6, 6)
  Entity = CreateEntity(#PB_Any, MeshID(Mesh), MaterialID(Material), 0, 0, 0)
  EntityPhysicBody(Entity, #PB_Entity_StaticBody, 2, 0, 1)

  ;
  ; Player
  Mesh = CreateCube (#PB_Any, 1)
  Player = CreateEntity(#PB_Any, MeshID(Mesh), #PB_Material_None, 0, 1, 0)
  EntityPhysicBody(Player, #PB_Entity_BoxBody, 1, 1, 0)
  
  ;
  ; Ambience
  AmbientColor(RGB(127, 127, 127))
  CreateLight(#PB_Any,RGB(151, 251, 151), -1.8, 10, 5)
  WorldShadows(#PB_Shadow_Additive)

  ;
  ; camera 
  Camera = CreateCamera(#PB_Any,0,0,100,100)
  CameraBackColor(Camera, RGB(145, 182, 201))

  MoveCamera(Camera, 0, 500, 0, #PB_Absolute)  

EndProcedure

Procedure Start()
  InitEngine3D()
  InitKeyboard()
  InitSprite()
  InitMouse()
    
  OpenWindow(#Mainform,0,0,1024,768, "", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
  OpenWindowedScreen(WindowID(#Mainform),0,0,1024,768,0, 0, 0)

  KeyboardMode(#PB_Keyboard_International)

  Load_Scene()
EndProcedure

start()

Repeat
  Repeat
    Event  = WindowEvent()
    Select Event
      Case #PB_Event_CloseWindow
        End
        
    EndSelect
  Until Event = 0
  
  If ExamineKeyboard()
    
    If KeyboardPushed (#PB_Key_Escape)
      Break
    EndIf
  EndIf
  
  If KeyboardPushed (#PB_Key_Up)
    PlayerSpeed = -  1
  ElseIf KeyboardPushed (#PB_Key_Down)
    PlayerSpeed  =  1
  Else
    PlayerSpeed = 0
  EndIf
    
  If KeyboardPushed (#PB_Key_Left)
    RotateEntity(Player, 0, 1.5, 0, #PB_Relative)
  ElseIf KeyboardPushed (#PB_Key_Right)
    RotateEntity(Player, 0, -1.5, 0, #PB_Relative)
  EndIf
  
  ;If i Comment "If PlayerSpeed <> 0", the cube move
  If PlayerSpeed <> 0
    MoveEntity(Player, 0, 0, PlayerSpeed, #PB_Absolute|#PB_Local)
  EndIf
  
  CameraFollow(Camera, EntityID(Player), 0, EntityY(Player) + 2, 6, 0.08, 0.08, #True)
  
  ; Render
  ClearScreen(RGB(0, 0, 0))
  RenderWorld(80)
  FlipBuffers()  
  
ForEver
Thank you for your help :)
Last edited by falsam on Fri Apr 04, 2014 1:43 pm, edited 1 time in total.

➽ Windows 11 64-bit - PB 6.21 x64 - AMD Ryzen 7 - NVIDIA GeForce GTX 1650 Ti

Sorry for my bad english and the Dunning–Kruger effect 🤪
T4r4ntul4
Enthusiast
Enthusiast
Posts: 119
Joined: Tue Mar 04, 2014 4:15 pm
Location: Netherlands

Re: Can not move an entity when the speed < > 0

Post by T4r4ntul4 »

i think since its a float, you need to check it against a float.

its working as expected with: 0.1 (but not with 0.0)

Code: Select all

  If PlayerSpeed <> 0.1
    MoveEntity(Player, 0, 0, PlayerSpeed, #PB_Absolute|#PB_Local)
  EndIf
User avatar
falsam
Enthusiast
Enthusiast
Posts: 635
Joined: Wed Sep 21, 2011 9:11 am
Location: France
Contact:

Re: Can not move an entity when the speed < > 0

Post by falsam »

T4r4ntul4 wrote:its working as expected with: 0.1 (but not with 0.0)
It is not logical. ! I smell a bug.

➽ Windows 11 64-bit - PB 6.21 x64 - AMD Ryzen 7 - NVIDIA GeForce GTX 1650 Ti

Sorry for my bad english and the Dunning–Kruger effect 🤪
User avatar
falsam
Enthusiast
Enthusiast
Posts: 635
Joined: Wed Sep 21, 2011 9:11 am
Location: France
Contact:

Re: Can not move an entity when the speed < > 0

Post by falsam »

T4r4ntul4 wrote:its working as expected with: 0.1 (but not with 0.0)
If you place a debug, we see that the test <> 0 works. But the entity does not move.

Code: Select all

  If PlayerSpeed <> 0
    Debug PlayerSpeed
    MoveEntity(Player, 0, 0, PlayerSpeed, #PB_Absolute|#PB_Local)
  EndIf

➽ Windows 11 64-bit - PB 6.21 x64 - AMD Ryzen 7 - NVIDIA GeForce GTX 1650 Ti

Sorry for my bad english and the Dunning–Kruger effect 🤪
applePi
Addict
Addict
Posts: 1404
Joined: Sun Jun 25, 2006 7:28 pm

Re: Can not move an entity when the speed < > 0

Post by applePi »

it is strange, the if...then are executed but the insider MoveEntity does not.
User avatar
falsam
Enthusiast
Enthusiast
Posts: 635
Joined: Wed Sep 21, 2011 9:11 am
Location: France
Contact:

Re: Can not move an entity when the speed < > 0

Post by falsam »

Insert PlayerSpeed = -1 before if .... then

Code: Select all

  PlayerSpeed = -1
  If PlayerSpeed <> 0
    Debug PlayerSpeed
    MoveEntity(Player, 0, 0, PlayerSpeed, #PB_Absolute|#PB_Local)
  EndIf
MoveEntity works fine. strange behavior.

➽ Windows 11 64-bit - PB 6.21 x64 - AMD Ryzen 7 - NVIDIA GeForce GTX 1650 Ti

Sorry for my bad english and the Dunning–Kruger effect 🤪
User avatar
falsam
Enthusiast
Enthusiast
Posts: 635
Joined: Wed Sep 21, 2011 9:11 am
Location: France
Contact:

Re: Can not move an entity when the speed < > 0

Post by falsam »

Solved by Comtois : Wake entity :)

Code: Select all

  If PlayerSpeed <> 0
    DisableEntityBody(Player, 0) ;<==
    MoveEntity(Player, 0, 0, PlayerSpeed, #PB_Absolute|#PB_Local)
  EndIf
And increase the friction of the cube.

Code: Select all

EntityPhysicBody(Player, #PB_Entity_BoxBody, 1, 1, 0.5)

➽ Windows 11 64-bit - PB 6.21 x64 - AMD Ryzen 7 - NVIDIA GeForce GTX 1650 Ti

Sorry for my bad english and the Dunning–Kruger effect 🤪
T4r4ntul4
Enthusiast
Enthusiast
Posts: 119
Joined: Tue Mar 04, 2014 4:15 pm
Location: Netherlands

Re: Can not move an entity when the speed < > 0

Post by T4r4ntul4 »

falsam wrote:
T4r4ntul4 wrote:its working as expected with: 0.1 (but not with 0.0)
It is not logical. ! I smell a bug.
it didnt seemed logical indeed, but it solved the problem.

@falsam, that also could not to seem be right, if debug is giving output but not moving, i will call it indeed a bug... i dont know what the others thinking about that? (iam the least person to shout out for calling bugs imho)
applePi
Addict
Addict
Posts: 1404
Joined: Sun Jun 25, 2006 7:28 pm

Re: Can not move an entity when the speed < > 0

Post by applePi »

it works also if we use
ApplyEntityImpulse(Player, 0, 0, 0, 0, 0, 0)
ie force = 0
inside the If PlayerSpeed <> 0 ... EndIf
it is possible that this works as if it is DoEvents for the MoveEntity + multi (If...EndIf)
can't decide.
User avatar
falsam
Enthusiast
Enthusiast
Posts: 635
Joined: Wed Sep 21, 2011 9:11 am
Location: France
Contact:

Re: Can not move an entity when the speed < > 0

Post by falsam »

T4r4ntul4 wrote:i will call it indeed a bug... i dont know what the others thinking about that?
I agree with you, for me it's also a bug.

Thank you for your suggestion applePi. The result is the same: Stimulate an entity. :)

➽ Windows 11 64-bit - PB 6.21 x64 - AMD Ryzen 7 - NVIDIA GeForce GTX 1650 Ti

Sorry for my bad english and the Dunning–Kruger effect 🤪
User avatar
falsam
Enthusiast
Enthusiast
Posts: 635
Joined: Wed Sep 21, 2011 9:11 am
Location: France
Contact:

Re: Can not move an entity when the speed < > 0

Post by falsam »


➽ Windows 11 64-bit - PB 6.21 x64 - AMD Ryzen 7 - NVIDIA GeForce GTX 1650 Ti

Sorry for my bad english and the Dunning–Kruger effect 🤪
Post Reply