ApplyEntityTorque() 5.40 beta more tests

Everything related to 3D programming
applePi
Addict
Addict
Posts: 1404
Joined: Sun Jun 25, 2006 7:28 pm

ApplyEntityTorque() 5.40 beta more tests

Post by applePi »

have you noticed that x,y,z values for ApplyEntityTorque and sister (PB 5.4 beta) are universal and not local to the rotor, and this is suitable for the fixed rotors, wheels, and other fixed machines.
if we want to change the orientation of the rotor then the same x,y,z values will not work as we naturally wants for the rotor. ie we should then change the values as in the example below.
what if there is a local or global flag ??
of course for the rotors we can use EnableHingeJointAngularMotor as demonstrated by example carPhysics.pb in the PB examples\Demos
in the following the default torque is
ApplyEntityTorque(#cyl, 1000, 0, 0)
ApplyEntityTorque(#cyl2, 1000, 0, 0)
and due to imperfections in the wheel cylinder body the car will turn to the opposite direction and when it is almost stop press '2' to change torque to:
ApplyEntityTorque(#cyl, -1000, 0, 0)
ApplyEntityTorque(#cyl2, -1000, 0, 0)

Code: Select all

Enumeration
   
   #camera
   #Plane
   #cyl
   #cyl2
     
EndEnumeration


Define.f KeyX, KeyY, MouseX, MouseY

ExamineDesktops()
If OpenWindow(0, 0, 0, DesktopWidth(0), DesktopHeight(0), "change with 1, 2, ... use mouse + Arrows to rotate + move Camera", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)

If InitEngine3D()
  
  Add3DArchive(#PB_Compiler_Home + "Examples/3D/Data/Textures", #PB_3DArchive_FileSystem)
  Add3DArchive(#PB_Compiler_Home + "Examples/3D/Data/Scripts",#PB_3DArchive_FileSystem)
    
  Parse3DScripts()
  
  InitSprite()
  InitKeyboard()
  InitMouse()
  OpenWindowedScreen(WindowID(0), 0, 0, DesktopWidth(0), DesktopHeight(0), 0, 0, 0)
    
              
    CreateCamera(#camera, 0, 0, 100, 100)
    MoveCamera(#camera, 20, 10, 20, #PB_Absolute)
    CameraBackColor(#camera, RGB(255,200,200))
    CameraLookAt(#camera,0,3,0)
        
    CreateLight(0, RGB(255,255,255), 0, 20, 30)
    AmbientColor(RGB(200, 200, 200))
    
    CreateMaterial(3, LoadTexture(3, "ValetCoeur.jpg"))
    DisableMaterialLighting(3, #False)
    SetMaterialColor(3, #PB_Material_AmbientColor, RGB(250, 255, 0))
    SetMaterialColor(3, #PB_Material_SpecularColor, RGB(255, 255, 0))
    
    CreateMaterial(1, LoadTexture(1, "MRAMOR6X6.jpg"))
    MaterialCullingMode(1, #PB_Material_NoCulling)
        
    CreateMaterial(4, LoadTexture(4, "ground_diffuse.png"))
    MaterialCullingMode(4, #PB_Material_NoCulling)
        
    CreatePlane(500, 450, 400, 5, 5, 2, 2) ; the ground
    CreateEntity(500, MeshID(500), MaterialID(4), 0,-4,0)
    CreateEntityBody(500, #PB_Entity_StaticBody, 1, 0.1, 2)

    
    CreateCube(0,1) 
    CreateEntity(0, MeshID(0), MaterialID(1), 0, 0, 0) ; car body
    ScaleEntity(0, 3,3,6)
    CreateCylinder(#cyl, 1, 5)
    CreateEntity(#cyl, MeshID(#cyl), MaterialID(3), -5,0, 0) 
    RotateEntity(#cyl,0,0,90)
    ScaleEntity(#cyl, 3,0.5,3)
    CreateEntity(#cyl2, MeshID(#cyl), MaterialID(3), 5,0, 0) 
    RotateEntity(#cyl2,0,0,90)
    ScaleEntity(#cyl2, 3,0.5,3)
    
    CreateEntityBody(0, #PB_Entity_BoxBody, 90, 0.1,10)
    CreateEntityBody(#cyl, #PB_Entity_CylinderBody, 100, 0.1, 4) 
    CreateEntityBody(#cyl2,#PB_Entity_CylinderBody, 100, 0.1, 4) 

        
     HingeJoint(0, EntityID(0),
               -2, 0, 0,
               1, 0, 0, 
               EntityID(#cyl),
               0, -2, 0, 
               0, -1, 0)
     HingeJoint(1, EntityID(0),
               2, 0, 0,
               -1, 0, 0, 
               EntityID(#cyl2),
               0, 2, 0, 
               0, 1, 0)

    autoGear = 1               
    Repeat
      Event = WindowEvent()
        
      If ExamineMouse()
        MouseX = -MouseDeltaX()/20 
        MouseY = -MouseDeltaY()/20
      EndIf
      
          
      If ExamineKeyboard()
         
        If KeyboardPushed(#PB_Key_Left)
          KeyX = -1
        ElseIf KeyboardPushed(#PB_Key_Right)
          KeyX = 1
        Else
          KeyX = 0
        EndIf
        
        If KeyboardPushed(#PB_Key_Up)
          KeyY = -1
        ElseIf KeyboardPushed(#PB_Key_Down)
          KeyY = 1
        Else
          KeyY = 0
        EndIf
        
        If KeyboardReleased(#PB_Key_1)
          autoGear = 1
        ElseIf KeyboardReleased(#PB_Key_2)
          autoGear = 2
        EndIf
        
        Select autoGear
          Case 1
            ApplyEntityTorque(#cyl,  1000, 0, 0)
            ApplyEntityTorque(#cyl2, 1000, 0, 0)
          Case 2
            ApplyEntityTorque(#cyl, -1000, 0, 0)
            ApplyEntityTorque(#cyl2, -1000, 0, 0)
        EndSelect
                   
          
       EndIf
       
          
      RotateCamera(#camera, MouseY, MouseX, 0, #PB_Relative)
      MoveCamera(#camera, KeyX, 0, KeyY)
      
      RenderWorld()
      FlipBuffers()
      
    Until KeyboardPushed(#PB_Key_Escape) Or Quit = 1
  EndIf
  
Else
  MessageRequester("Error", "The 3D Engine can't be initialized", 0)
EndIf

End
User avatar
DK_PETER
Addict
Addict
Posts: 904
Joined: Sat Feb 19, 2011 10:06 am
Location: Denmark
Contact:

Re: ApplyEntityTorque() 5.40 beta more tests

Post by DK_PETER »

Nice example applePi.

The physics engine is much more happy, when you use extremely small values.
I do wish, that they would fix the artifact in CreateCylinder(), though. PureBasic's 3D is
becoming really awesome, but It looks amateurish and looks good with single color textures only.
Oh, well. They probably get around to it sooner or later. :)

Here I've changed your example to use small values. I've added a z-value to torque, which makes
your ' Mercedes S500" turn, when changing between keys 1 and 2. Quite fun to play with. :wink:

Code: Select all

Enumeration
   
   #camera
   #Plane
   #cyl
   #cyl2
     
EndEnumeration


Define.f KeyX, KeyY, MouseX, MouseY

ExamineDesktops()
If OpenWindow(0, 0, 0, DesktopWidth(0), DesktopHeight(0), "change with 1, 2, ... use mouse + Arrows to rotate + move Camera", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)

If InitEngine3D()
 
  Add3DArchive(#PB_Compiler_Home + "Examples/3D/Data/Textures", #PB_3DArchive_FileSystem)
  Add3DArchive(#PB_Compiler_Home + "Examples/3D/Data/Scripts",#PB_3DArchive_FileSystem)
   
  Parse3DScripts()
 
  InitSprite()
  InitKeyboard()
  InitMouse()
  OpenWindowedScreen(WindowID(0), 0, 0, DesktopWidth(0), DesktopHeight(0), 0, 0, 0)
   
             
    CreateCamera(#camera, 0, 0, 100, 100)
    MoveCamera(#camera, 20, 10, 20, #PB_Absolute)
    CameraBackColor(#camera, RGB(255,200,200))
    CameraLookAt(#camera,0,3,0)
       
    CreateLight(0, RGB(255,255,255), 0, 20, 30)
    AmbientColor(RGB(200, 200, 200))
   
    CreateMaterial(3, LoadTexture(3, "ValetCoeur.jpg"))
    DisableMaterialLighting(3, #False)
    SetMaterialColor(3, #PB_Material_AmbientColor, RGB(250, 255, 0))
    SetMaterialColor(3, #PB_Material_SpecularColor, RGB(255, 255, 0))
   
    CreateMaterial(1, LoadTexture(1, "MRAMOR6X6.jpg"))
    MaterialCullingMode(1, #PB_Material_NoCulling)
       
    CreateMaterial(4, LoadTexture(4, "ground_diffuse.png"))
    MaterialCullingMode(4, #PB_Material_NoCulling)
       
    CreatePlane(500, 450, 400, 5, 5, 2, 2) ; the ground
    CreateEntity(500, MeshID(500), MaterialID(4), 0,-4,0)
    CreateEntityBody(500, #PB_Entity_StaticBody, 1, 0.1, 2)

   
    CreateCube(0,1)
    CreateEntity(0, MeshID(0), MaterialID(1), 0, 0, 0) ; car body
    ScaleEntity(0, 3, 3, 6)
    CreateCylinder(#cyl, 1, 5, 20, 1, 0)
    CreateEntity(#cyl, MeshID(#cyl), MaterialID(3), -5,0, 0)
    RotateEntity(#cyl,0,0,90)
    ScaleEntity(#cyl, 3,0.5,3)
    CreateEntity(#cyl2, MeshID(#cyl), MaterialID(3), 5,0, 0)
    RotateEntity(#cyl2,0,0,90)
    ScaleEntity(#cyl2, 3,0.5,3)
   
    CreateEntityBody(0, #PB_Entity_BoxBody, 0.0001, 0.1,10)
    CreateEntityBody(#cyl, #PB_Entity_CylinderBody, 0.0002, 2.1, 40)
    CreateEntityBody(#cyl2,#PB_Entity_CylinderBody, 0.0002, 2.1, 40)

       
     HingeJoint(0, EntityID(0),
               -2, 0, 0,
               1, 0, 0,
               EntityID(#cyl),
               0, -2, 0,
               0, -1, 0)
     HingeJoint(1, EntityID(0),
               2, 0, 0,
               -1, 0, 0,
               EntityID(#cyl2),
               0, 2, 0,
               0, 1, 0)

    autoGear = 1               
    Repeat
      Event = WindowEvent()
       
      If ExamineMouse()
        MouseX = -MouseDeltaX()/20
        MouseY = -MouseDeltaY()/20
      EndIf
     
         
      If ExamineKeyboard()
         
        If KeyboardPushed(#PB_Key_Left)
          KeyX = -1
        ElseIf KeyboardPushed(#PB_Key_Right)
          KeyX = 1
        Else
          KeyX = 0
        EndIf
       
        If KeyboardPushed(#PB_Key_Up)
          KeyY = -1
        ElseIf KeyboardPushed(#PB_Key_Down)
          KeyY = 1
        Else
          KeyY = 0
        EndIf
       
        If KeyboardReleased(#PB_Key_1)
          autoGear = 1
        ElseIf KeyboardReleased(#PB_Key_2)
          autoGear = 2
        EndIf
       
        Select autoGear
          Case 1
            ApplyEntityTorque(#cyl,  0.001, 0, 0.002)
            ApplyEntityTorque(#cyl2, 0.001, 0, -0.002)
          Case 2
            ApplyEntityTorque(#cyl, -0.001, 0, -0.002)
            ApplyEntityTorque(#cyl2, -0.001, 0, 0.002)
        EndSelect
                   
         
       EndIf
       
         
      RotateCamera(#camera, MouseY, MouseX, 0, #PB_Relative)
      MoveCamera(#camera, KeyX, 0, KeyY)
     
      RenderWorld()
      FlipBuffers()
     
    Until KeyboardPushed(#PB_Key_Escape) Or Quit = 1
  EndIf
 
Else
  MessageRequester("Error", "The 3D Engine can't be initialized", 0)
EndIf

End
Current configurations:
Ubuntu 20.04/64 bit - Window 10 64 bit
Intel 6800K, GeForce Gtx 1060, 32 gb ram.
Amd Ryzen 9 5950X, GeForce 3070, 128 gb ram.
applePi
Addict
Addict
Posts: 1404
Joined: Sun Jun 25, 2006 7:28 pm

Re: ApplyEntityTorque() 5.40 beta more tests

Post by applePi »

Thank you DK_PETER very much for the interesting behavior of the car with such a changes in torque values & directions make the car behaves like a toy and i think the 2 wheels car (Mercedes $500 model) will interest the Physics teacher more than the 4 or 3 model, try your model with 0.1 wheels thickness instead of 0.5
i feel that if we want to simulate the #PB_Local parameter then we should do calculations between the torque values relative to the world (#PB_World ) and the current orientation of the car obtained by EntityRoll(), EntityYaw(), EntityPitch() and the result of these continuous calc should update the x,y,z torque values continuously, the effect should be when we change the orientation of the car the car will stay moving at that direction line.
but now here is a 3 wheels model which cost $50 more : model (Mercedes $550) :lol: :lol: , it seems to preserve the car better than the older model but less funny, it is usual in some countries.
Image

Code: Select all

Enumeration
   
   #camera
   #Plane
   #cyl
   #cyl2
   #cyl3 
   #cone
EndEnumeration


Define.f KeyX, KeyY, MouseX, MouseY

ExamineDesktops()
If OpenWindow(0, 0, 0, DesktopWidth(0), DesktopHeight(0), "change with 1, 2, ... use mouse + Arrows to rotate + move Camera", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)

If InitEngine3D()
  
  Add3DArchive(#PB_Compiler_Home + "Examples/3D/Data/Textures", #PB_3DArchive_FileSystem)
  Add3DArchive(#PB_Compiler_Home + "Examples/3D/Data/Scripts",#PB_3DArchive_FileSystem)
    
  Parse3DScripts()
  
  InitSprite()
  InitKeyboard()
  InitMouse()
  OpenWindowedScreen(WindowID(0), 0, 0, DesktopWidth(0), DesktopHeight(0), 0, 0, 0)
    
              
    CreateCamera(#camera, 0, 0, 100, 100)
    MoveCamera(#camera, 20, 10, 20, #PB_Absolute)
    CameraBackColor(#camera, RGB(255,200,200))
    CameraLookAt(#camera,0,3,0)
        
    CreateLight(0, RGB(255,255,255), 0, 20, 30)
    AmbientColor(RGB(200, 200, 200))
    
    CreateMaterial(3, LoadTexture(3, "ValetCoeur.jpg"))
    DisableMaterialLighting(3, #False)
    SetMaterialColor(3, #PB_Material_AmbientColor, RGB(250, 255, 0))
    SetMaterialColor(3, #PB_Material_SpecularColor, RGB(255, 255, 0))
    
    CreateMaterial(1, LoadTexture(1, "MRAMOR6X6.jpg"))
    MaterialCullingMode(1, #PB_Material_NoCulling)
        
    CreateMaterial(4, LoadTexture(4, "ground_diffuse.png"))
    MaterialCullingMode(4, #PB_Material_NoCulling)
        
    CreatePlane(#Plane, 1000, 1000, 5, 5, 2, 2) ; the ground
    CreateEntity(#Plane, MeshID(#Plane), MaterialID(4), 0,-4,0)
    CreateEntityBody(#Plane, #PB_Entity_StaticBody, 1, 0.1, 2)

    
    CreateCube(0,1) 
    CreateEntity(0, MeshID(0), MaterialID(1), 0, 0, 0) ; car body
    ScaleEntity(0, 3,3,6)
    CreateCylinder(#cyl, 1, 5)
    CreateEntity(#cyl, MeshID(#cyl), MaterialID(3), -5,0, 0) 
    RotateEntity(#cyl,0,0,90)
    ScaleEntity(#cyl, 3,0.5,3)
    CreateEntity(#cyl2, MeshID(#cyl), MaterialID(3), 5,0, 0) 
    RotateEntity(#cyl2,0,0,90)
    ScaleEntity(#cyl2, 3,0.5,3)
    CreateEntity(#cyl3, MeshID(#cyl), MaterialID(3), 0,0, 7)
    ;RotateEntity(#cyl3,0,0,90) ; uncomment this line to have a funny effect
    ScaleEntity(#cyl3, 3,0.1,3)
    
    CreateEntityBody(0, #PB_Entity_BoxBody, 90, 0.1,10)
    CreateEntityBody(#cyl, #PB_Entity_CylinderBody, 100, 0.1, 4) 
    CreateEntityBody(#cyl2,#PB_Entity_CylinderBody, 100, 0.1, 4) 
    CreateEntityBody(#cyl3,#PB_Entity_CylinderBody, 10, 1.1, 4)

        
     HingeJoint(0, EntityID(0),
               -2, 0, 0,
               1, 0, 0, 
               EntityID(#cyl),
               0, -2, 0, 
               0, -1, 0)
     HingeJoint(1, EntityID(0),
                2, 0, 0,
                -1, 0, 0, 
                EntityID(#cyl2),
                0, 2, 0, 
                0, 1, 0)
     HingeJoint(2, EntityID(0),
                0, 0, 7,
                1, 0, 0,
                EntityID(#cyl3),
                0, 0, 0,
                0, 1, 0)

    CreateCone(#cone, 4,5)
    CreateEntity(#cone, MeshID(#cone), MaterialID(4), 0,2,0)
    AttachEntityObject(0, "",EntityID(#cone))
     
    autoGear = 1               
    Repeat
      Event = WindowEvent()
        
      If ExamineMouse()
        MouseX = -MouseDeltaX()/20 
        MouseY = -MouseDeltaY()/20
      EndIf
      
          
      If ExamineKeyboard()
         
        If KeyboardPushed(#PB_Key_Left)
          KeyX = -1
        ElseIf KeyboardPushed(#PB_Key_Right)
          KeyX = 1
        Else
          KeyX = 0
        EndIf
        
        If KeyboardPushed(#PB_Key_Up)
          KeyY = -1
        ElseIf KeyboardPushed(#PB_Key_Down)
          KeyY = 1
        Else
          KeyY = 0
        EndIf
        
        If KeyboardReleased(#PB_Key_1)
          autoGear = 1
        ElseIf KeyboardReleased(#PB_Key_2)
          autoGear = 2
        EndIf
        
        Select autoGear
          Case 1
            ApplyEntityTorque(#cyl,  1000, 0, 0)
            ApplyEntityTorque(#cyl2, 1000, 0, 0)
          Case 2
            ApplyEntityTorque(#cyl, -1000, 0, 0)
            ApplyEntityTorque(#cyl2, -1000, 0, 0)
        EndSelect
                   
          
       EndIf
       
          
      RotateCamera(#camera, MouseY, MouseX, 0, #PB_Relative)
      MoveCamera(#camera, KeyX, 0, KeyY)
      
      RenderWorld()
      FlipBuffers()
      
    Until KeyboardPushed(#PB_Key_Escape) Or Quit = 1
  EndIf
  
Else
  MessageRequester("Error", "The 3D Engine can't be initialized", 0)
EndIf

End
davido
Addict
Addict
Posts: 1890
Joined: Fri Nov 09, 2012 11:04 pm
Location: Uttoxeter, UK

Re: ApplyEntityTorque() 5.40 beta more tests

Post by davido »

Very nice. Thank you.

Works well on the Mac.
DE AA EB
Post Reply