Page 1 of 2

[SOLVED] I would like Sinbad to stop sliding on the floor:)

Posted: Thu Jul 16, 2020 9:01 pm
by falsam
Hello. I was trying to solve a sound problem with CameraFollow() and SoundListenerLocate().

For the moment I would like Sinbad to stop sliding on the floor:)

Code: Select all

EnableExplicit

Global window, ww, wh, event, camera, Material, Mesh, Entity, Sound, Node
Global Player, PlayerSpeed, CurrentAnimation.s

CompilerIf Subsystem("OpenGL") = #False
  MessageRequester("Error", "Please set the Library subsystem to OpenGL from IDE : Compiler... Compiler Options")
  End
CompilerEndIf

InitEngine3D(#PB_Engine3D_DebugLog)
InitKeyboard()
InitSprite()

; Window & Screen 3D
window = OpenWindow(#PB_Any, 0, 0, 0, 0, "", #PB_Window_BorderLess | #PB_Window_Maximize)
ww = WindowWidth (window, #PB_Window_InnerCoordinate)
wh = WindowHeight(window, #PB_Window_InnerCoordinate)

OpenWindowedScreen(WindowID(window), 0, 0, ww, wh)
Add3DArchive(#PB_Compiler_Home+"Examples\3D\Data\", #PB_3DArchive_FileSystem)
Add3DArchive(#PB_Compiler_Home+"Examples\3D\Data\Textures", #PB_3DArchive_FileSystem)
Add3DArchive(#PB_Compiler_Home+"Examples\3D\Data\Packs\Sinbad.zip", #PB_3DArchive_Zip)
Parse3DScripts()

; Light
CreateLight(#PB_Any,RGB(255, 218, 185), 500, 1000, 1000)
WorldShadows(#PB_Shadow_Additive)

; Camera
camera = CreateCamera(#PB_Any, 0, 0, 100, 100)
MoveCamera(camera, 0, 400, 3000)
CameraLookAt(camera, 0, 0, 0)

; Entity
Mesh = CreatePlane(#PB_Any, 5000, 5000, 50, 50, 5, 5)
Material = CreateMaterial(#PB_Any, TextureID(LoadTexture(#PB_Any, "MRAMOR6X6.jpg")))
Entity = CreateEntity(#PB_Any, MeshID(Mesh), MaterialID(Material))
CreateEntityBody(Entity, #PB_Entity_StaticBody, 1, 0, 100)

Mesh = CreateCube(#PB_Any, 200)
Material = CreateMaterial(#PB_Any, TextureID(LoadTexture(#PB_Any, "Caisse.png")))
Entity = CreateEntity(#PB_Any, MeshID(Mesh), MaterialID(Material), -1000, 100, 0)
CreateEntityBody(Entity, #PB_Entity_StaticBody, 1, 0, 1)

Entity = CopyEntity(Entity, -1)
MoveEntity(Entity, 1000, 100, 0)

Mesh = LoadMesh(#PB_Any, "sinbad.mesh")
Player = CreateEntity(-1, MeshID(Mesh), #PB_Material_None, 0, 125, 2000)
ScaleEntity(Player, 25, 25, 25)
CreateEntityBody(Player, #PB_Entity_ConvexHullBody, 200, 1, 100)
EntityAngularFactor(Player, 0, 1, 0)
RotateEntity(Player, 0, 180, 0)

; Sound
Sound = LoadSound3D(#PB_Any, "Siren.ogg")
Node = CreateNode(#PB_Any, -1000, 100, 0)
AttachNodeObject(Node, SoundID3D(Sound))
SoundRange3D(Sound, 300, 1000)
PlaySound3D(Sound, #PB_Sound3D_Loop)

Sound = LoadSound3D(#PB_Any, "Roar.ogg")
Node = CreateNode(#PB_Any, 1000, 100, 0)
AttachNodeObject(Node, SoundID3D(Sound))
SoundRange3D(Sound, 300, 1000)
PlaySound3D(Sound, #PB_Sound3D_Loop)

; Render
While #True
  event = WindowEvent()
  ExamineKeyboard() 
  
  If event = #PB_Event_CloseWindow Or KeyboardPushed(#PB_Key_Escape)
    Break
  EndIf 
  
  If KeyboardPushed (#PB_Key_Up)
    PlayerSpeed = 300
    CurrentAnimation = "RunBase"
  ElseIf KeyboardPushed (#PB_Key_Down)
    PlayerSpeed = -300
    CurrentAnimation = "RunBase"
  Else
    CurrentAnimation = "IdleTop"
    PlayerSpeed = 0
  EndIf
  
  If KeyboardPushed (#PB_Key_Left)
    RotateEntity(Player, 0, 3, 0, #PB_Relative)
    CurrentAnimation = "RunBase"
  ElseIf KeyboardPushed (#PB_Key_Right)
    RotateEntity(Player, 0, -3, 0, #PB_Relative)
    CurrentAnimation = "RunBase"
  EndIf   
  
  If PlayerSpeed
    DisableEntityBody(Player, #False) ; Wake up entity (BugWare)
    MoveEntity(Player, 0, 0, PlayerSpeed, #PB_Absolute|#PB_Local)
  EndIf
  
  ;Play animation
  If EntityAnimationStatus(Player, CurrentAnimation) = #PB_EntityAnimation_Stopped
    StartEntityAnimation(Player, CurrentAnimation)
  EndIf
  
  CameraFollow(Camera, EntityID(Player), 180, EntityY(Player) + 100, 250, 0.5, 0.5, #True)
  SoundListenerLocate(CameraX(Camera), CameraY(Camera), CameraZ(Camera))
  
  RenderWorld()
  FlipBuffers()
Wend
Thank for your help.

Re: I would like Sinbad to stop sliding on the floor:)

Posted: Fri Jul 17, 2020 6:55 am
by Olli
I thank before a friction coefficient might be set, but it seems that you do not use native physic animation in this source code. Plus, you erase indirectly cube mesh identifier (by reuse "mesh" variable).

I imagine you should modify 300 and -300 values to adapt the mesh animation with its real translating move.

Re: I would like Sinbad to stop sliding on the floor:)

Posted: Fri Jul 17, 2020 7:42 am
by DK_PETER

Code: Select all

    CurrentAnimation = "IdleTop"
    PlayerSpeed = -0.5  ; Quick fix 

Re: I would like Sinbad to stop sliding on the floor:)

Posted: Fri Jul 17, 2020 10:11 am
by Olli
DK_PETER wrote:

Code: Select all

    CurrentAnimation = "IdleTop"
    PlayerSpeed = -0.5  ; Quick fix 
Ow... He has really slid !! :D

(*) << slid >> : thanks to the conjugator

Re: I would like Sinbad to stop sliding on the floor:)

Posted: Fri Jul 17, 2020 6:40 pm
by oreopa
As long as you remember that past tense of drag is NOT drug :D I *hate* that mistake :) (It's 100% "Dragged" :D )

Along with many others... such as using the completely made-up-word "pacifically" in the place of "specifically"... like d'oh!

I remember coding a (rubbish) text adventure in basic on the C16, of all things, with my friend... and I tried to credit him in a REM statement in the code. I wanted to use something along the lines of "With invaluable help from my super friend". Then a heated discussion arose on how "invaluable" was not a real word, becuase he'd just asked his mum. And even if it was a real word, then it meant "completely NOT valuable"...

Years later I got my own back playing scrabble with him. I played "aural". And he challenged it. And just before we opened the OFFICIAL SCRABBLE DICTIONARY we had to hand, I guessed the definition would be something like "Pertaining to the ear/sound". After locating in the dicitionary I was pleased to find it was EXACTLY THIS VERBATIM! :)

Sorry for derail. Language is fun/cool/annoying when ppl are too thick to understand it (native speakers).

Re: I would like Sinbad to stop sliding on the floor:)

Posted: Sat Jul 18, 2020 12:16 am
by Olli
As long as you remember that past tense of drag is NOT drug :D
Drag can however be over a *joint*, if Sinbad could just dance like Michael Jackson...

It stays good to learn in this subject that friction is usable. But, for this task, the user will loose less time if he makes himself physical calculating.

Excepted if there is hundreds Sinbad on a flat and pitching floor...

Re: I would like Sinbad to stop sliding on the floor:)

Posted: Sat Jul 18, 2020 9:40 am
by falsam
Hello. Thank you for your answers.
but it seems that you do not use native physic animation in this source code.
The physics engine will be enabled (default).

Code: Select all

EnableExplicit

Global window, event, camera, Entity

InitEngine3D(#PB_Engine3D_DebugLog)
InitKeyboard()
InitSprite()

window = OpenWindow(#PB_Any, 0, 0, 1024, 768, "Test Shadow")
OpenWindowedScreen(WindowID(window),0,0,1024,768)

; Light
CreateLight(#PB_Any,RGB(255, 218, 185), 500, 1000, -1000)
WorldShadows(#PB_Shadow_Additive)

; Camera
camera = CreateCamera(#PB_Any, 0, 0, 100, 100)
MoveCamera(camera, 0, 5, 20)
CameraLookAt(camera, 0, 0, 0)

Entity = CreateEntity(-1, MeshID(CreatePlane(-1, 10, 10, 1, 1, 1, 1)), #PB_Material_None)
CreateEntityBody(Entity, #PB_Entity_StaticBody, 0, 0.1, 0.1)

Entity = CreateEntity(-1, MeshID(CreateCube(-1, 2)), #PB_Material_None, 0, 5, 0)
CreateEntityBody(Entity, #PB_Entity_BoxBody, 1, 0.1, 0.1)

; Render
While #True
  event = WindowEvent()
  
  ExamineKeyboard() 
  
  If event = #PB_Event_CloseWindow Or KeyboardPushed(#PB_Key_Escape)
    Break
  EndIf 
  
  RenderWorld()
  FlipBuffers()
Wend
I don't use EnableWorldPhysics(). The cube falls :wink:

@DK_PETER thanks for your solution but it doesn't suit me if Sinbad executes a jump for example. The fall is too slow.

:idea: However, I think a solution is possible by changing the value of gravity. I am testing ...

Re: I would like Sinbad to stop sliding on the floor:)

Posted: Sat Jul 18, 2020 10:15 am
by DK_PETER
@falsam

True..If a jump is needed, it doesn't work with the entity stopping abruptly.
Sine you move (moveEntity()) the entity I would suggest you use a thread
to decrease the moving value... A small test using a thread gives the
appearance that the skidding and stopping is more natural.

Code: Select all

EnableExplicit

Global window, ww, wh, event, camera, Material, Mesh, Entity, Sound, Node
Global Player, PlayerSpeed.f, CurrentAnimation.s    ;PlayerSpeed changed to float..
Global thrSpeed.i, DecreaseNow.i = #False, Quit = #False   

CompilerIf Subsystem("OpenGL") = #False
  MessageRequester("Error", "Please set the Library subsystem to OpenGL from IDE : Compiler... Compiler Options")
  End
CompilerEndIf

Declare.i DecreaseSpeed(Value.i)

InitEngine3D(#PB_Engine3D_DebugLog)
InitKeyboard()
InitSprite()


; Window & Screen 3D
window = OpenWindow(#PB_Any, 0, 0, 0, 0, "", #PB_Window_BorderLess | #PB_Window_Maximize)
ww = WindowWidth (window, #PB_Window_InnerCoordinate)
wh = WindowHeight(window, #PB_Window_InnerCoordinate)

OpenWindowedScreen(WindowID(window), 0, 0, ww, wh)
Add3DArchive(#PB_Compiler_Home+"Examples\3D\Data\", #PB_3DArchive_FileSystem)
Add3DArchive(#PB_Compiler_Home+"Examples\3D\Data\Textures", #PB_3DArchive_FileSystem)
Add3DArchive(#PB_Compiler_Home+"Examples\3D\Data\Packs\Sinbad.zip", #PB_3DArchive_Zip)
Parse3DScripts()

; Light
CreateLight(#PB_Any,RGB(255, 218, 185), 500, 1000, 1000)
WorldShadows(#PB_Shadow_Additive)

; Camera
camera = CreateCamera(#PB_Any, 0, 0, 100, 100)
MoveCamera(camera, 0, 400, 3000)
CameraLookAt(camera, 0, 0, 0)

; Entity
Mesh = CreatePlane(#PB_Any, 5000, 5000, 50, 50, 5, 5)
Material = CreateMaterial(#PB_Any, TextureID(LoadTexture(#PB_Any, "MRAMOR6X6.jpg")))
Entity = CreateEntity(#PB_Any, MeshID(Mesh), MaterialID(Material))
CreateEntityBody(Entity, #PB_Entity_StaticBody, 1, 0, 100)

Mesh = CreateCube(#PB_Any, 200)
Material = CreateMaterial(#PB_Any, TextureID(LoadTexture(#PB_Any, "Caisse.png")))
Entity = CreateEntity(#PB_Any, MeshID(Mesh), MaterialID(Material), -1000, 100, 0)
CreateEntityBody(Entity, #PB_Entity_StaticBody, 1, 0, 1)

Entity = CopyEntity(Entity, -1)
MoveEntity(Entity, 1000, 100, 0)

Mesh = LoadMesh(#PB_Any, "sinbad.mesh")
Player = CreateEntity(-1, MeshID(Mesh), #PB_Material_None, 0, 125, 2000)
ScaleEntity(Player, 25, 25, 25)
CreateEntityBody(Player, #PB_Entity_ConvexHullBody, 0.002, 1, 100)
EntityAngularFactor(Player, 0, 1, 0)
RotateEntity(Player, 0, 180, 0)

; Sound
Sound = LoadSound3D(#PB_Any, "Siren.ogg")
Node = CreateNode(#PB_Any, -1000, 100, 0)
AttachNodeObject(Node, SoundID3D(Sound))
SoundRange3D(Sound, 300, 1000)
PlaySound3D(Sound, #PB_Sound3D_Loop)

Sound = LoadSound3D(#PB_Any, "Roar.ogg")
Node = CreateNode(#PB_Any, 1000, 100, 0)
AttachNodeObject(Node, SoundID3D(Sound))
SoundRange3D(Sound, 300, 1000)
PlaySound3D(Sound, #PB_Sound3D_Loop)

thrSpeed = CreateThread(@DecreaseSpeed(), 0)

; Render
While #True
  event = WindowEvent()
  ExamineKeyboard()
 
  If event = #PB_Event_CloseWindow Or KeyboardPushed(#PB_Key_Escape)
    Break
  EndIf
 
  If KeyboardPushed (#PB_Key_Up)
    PlayerSpeed = 300
    CurrentAnimation = "RunBase"
  ElseIf KeyboardPushed (#PB_Key_Down)
    PlayerSpeed = -300
    CurrentAnimation = "RunBase"
  Else
    CurrentAnimation = "IdleTop"
    ;PlayerSpeed = 0.0
    DecreaseNow = #True
  EndIf
 
  If KeyboardPushed (#PB_Key_Left)
    RotateEntity(Player, 0, 3, 0, #PB_Relative)
    CurrentAnimation = "RunBase"
  ElseIf KeyboardPushed (#PB_Key_Right)
    RotateEntity(Player, 0, -3, 0, #PB_Relative)
    CurrentAnimation = "RunBase"
  EndIf   
 
  If PlayerSpeed
    ;DisableEntityBody(Player, #False) ; Wake up entity (BugWare)
    MoveEntity(Player, 0, 0, PlayerSpeed, #PB_Absolute|#PB_Local)
  EndIf
 
  ;Play animation
  If EntityAnimationStatus(Player, CurrentAnimation) = #PB_EntityAnimation_Stopped
    StartEntityAnimation(Player, CurrentAnimation)
  EndIf
 
  CameraFollow(Camera, EntityID(Player), 180, EntityY(Player) + 100, 250, 0.5, 0.5, #True)
  SoundListenerLocate(CameraX(Camera), CameraY(Camera), CameraZ(Camera))
 
  RenderWorld()
  FlipBuffers()
Wend
Quit = #True

Procedure.i DecreaseSpeed(Value.i)  ;Example....
  Protected tval.i
  Repeat
    If DecreaseNow = #True
      If PlayerSpeed - 0.0002 > 0.0
        PlayerSpeed - 0.0002
      Else
        DecreaseNow = #False
      EndIf
    EndIf
  Until Quit = #True
EndProcedure

Re: I would like Sinbad to stop sliding on the floor:)

Posted: Sat Jul 18, 2020 10:27 am
by falsam
Thank DK_PETER. I'm testing ...

Don't comment.

Code: Select all

DisableEntityBody(Player, #False) ; Wake up entity (BugWare)
Run the code and wait five seconds before moving Sinbad. He's blocked ;)

Re: I would like Sinbad to stop sliding on the floor:)

Posted: Sat Jul 18, 2020 10:32 am
by falsam
I enabled threads in the compile options and tested your code. I removed the sounds and placed Sinbad on a crate. He doesn't fall off when I leave the crate.

Re: I would like Sinbad to stop sliding on the floor:)

Posted: Sat Jul 18, 2020 11:49 am
by DK_PETER
Damn!

Code: Select all

DisableEntityBody(Player, #False) ; Wake up entity (BugWare)
I thought it was fixed. :?

AFAIK, the problem is (floating Sinbad), that you manually move sinbad and thereby
overrides the existing physics settings. In this case you must use raycast to determine the height (position)
and move Sinbad accordingly or reset the physics settings. (maybe)..

If you want to maintain the primary physics you have to use other commands for movement, such as
ApplyEntityForce(), ApplyEntityImpulse(), EntityVelocity() e.t.c...
And getting current information using GetEntityAttribute().

I've generally moved entities manually myself, used raycast and raycollide to determine the position and
collision. Only objects, which doesn't require any special 'navigation' are allowed to 'splash' around without
my consent. :-)

Re: I would like Sinbad to stop sliding on the floor:)

Posted: Sat Jul 18, 2020 4:29 pm
by falsam
DK_PETER wrote:I've generally moved entities manually myself, used raycast and raycollide to determine the position and
collision. Only objects, which doesn't require any special 'navigation' are allowed to 'splash' around without
my consent.
Ok. Thank you for all this information. I will see what I can do with all these features (RayCollide, ...)

Re: I would like Sinbad to stop sliding on the floor:)

Posted: Sat Jul 18, 2020 5:05 pm
by Olli
falsam wrote:Hello. Thank you for your answers.
Olli wrote:but it seems that you do not use native physic animation in this source code.
The physics engine will be enabled (default).

[...]
You are right. Actually I have no computer, no test either since a long time : I imaginated, that a whole statement was required to increase all physical values.

What I do not understand is the utility of EntityAngularFactor() : what does it simulate ?


An remark I repeat : please use FreeNode(), FreeMesh(), Free etc...

1st reason : no memory problem.
2nd reason : the code is far more readable

Code: Select all

Sth = Create...()
...
Free...(Sth)
In any ways, 3D objects are freeing on the fly in the main loop, or subjacent procedures.

Re: I would like Sinbad to stop sliding on the floor:)

Posted: Sat Jul 18, 2020 8:19 pm
by Demivec
I wanted to add an observation I made about the problem. If Sinbad is moved of the edge of the grid (past the crate with the animal sound) and he walks there for a moment before being moved onto the grid in the opposite direction for a second or two, he will slide much farther and it is evident from his shadow that he is above the grid and slowly descending to the grid before he comes to rest. From this observation I would conclude that when he is being moved about on the grid he becomes raised of the grid's surface slightly and this is what causes the delay in his common to rest because he is actually falling slowly to the grid's surface before stopping.
I unfortunately have no advice on the solution. I think it relates to what DK_PETER was talking about.

Re: I would like Sinbad to stop sliding on the floor:)

Posted: Sun Jul 19, 2020 9:55 am
by DK_PETER
@Falsam
Apparently the physics is still valid for Sinbad in your first example...
Add this line to it.

Code: Select all

WorldGravity(-1500)
EXTREME MASSIVE VALUE, but it works... :shock: :)

Edit: Forgot the mention that manually moving Sinbad does
something/delays Sinbad's physics. Keep pressing forward and
you'll se that he's falling extremely slowly - until you release the key.