Page 1 of 1

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

Posted: Thu Apr 03, 2014 11:27 am
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 :)

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

Posted: Thu Apr 03, 2014 2:30 pm
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

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

Posted: Thu Apr 03, 2014 9:31 pm
by falsam
T4r4ntul4 wrote:its working as expected with: 0.1 (but not with 0.0)
It is not logical. ! I smell a bug.

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

Posted: Thu Apr 03, 2014 9:51 pm
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

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

Posted: Thu Apr 03, 2014 10:14 pm
by applePi
it is strange, the if...then are executed but the insider MoveEntity does not.

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

Posted: Thu Apr 03, 2014 10:19 pm
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.

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

Posted: Thu Apr 03, 2014 11:05 pm
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)

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

Posted: Fri Apr 04, 2014 12:49 am
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)

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

Posted: Fri Apr 04, 2014 7:59 am
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.

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

Posted: Fri Apr 04, 2014 12:03 pm
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. :)

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

Posted: Fri Apr 04, 2014 1:42 pm
by falsam