[Solved] ApplyEntityTorqueImpulse() & ApplyEntityTorque()

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

[Solved] ApplyEntityTorqueImpulse() & ApplyEntityTorque()

Post by falsam »

[PB 4.40 Beta 2)

Hello. I see no difference with these two functions. Only the values of x, y or z change.
- Press space bar for a pulse - Esc for exit.

Code: Select all

Enumeration
  #Mainform
EndEnumeration

InitEngine3D() : InitKeyboard() : InitSprite() : InitMouse()

OpenWindow(#Mainform,0,0, 1024, 768, "[PB 5.40 - ApplyEntityTorque", #PB_Window_SystemMenu)
OpenWindowedScreen(WindowID(#Mainform),0,0,1024,768,0, 0, 0)
Add3DArchive(#PB_Compiler_Home + "Examples/3D/Data/Textures", #PB_3DArchive_FileSystem)

;Light & Shadow
CreateLight(#PB_Any,RGB(255, 255, 255), -5000, 2000, 1500)
WorldShadows(#PB_Shadow_Additive)

;Camera
CreateCamera(0, 0, 0, 100,100)
CameraBackColor(0, RGB(145, 182, 201))
MoveCamera(0, 3, 5, 20, #PB_Absolute)  
CameraLookAt(0, 0,1,0)   

;Ground
Mesh = CreatePlane(#PB_Any, 40, 40, 10, 10, 2, 2)
Material = CreateMaterial(#PB_Any, TextureID(LoadTexture(#PB_Any, "MRAMOR6X6.jpg")))
Ground = CreateEntity(#PB_Any, MeshID(Mesh), MaterialID(material))
CreateEntityBody(Ground, #PB_Entity_StaticBody)

;Cube
Mesh = CreateCube(#PB_Any, 2)
Material = CreateMaterial(#PB_Any, TextureID(LoadTexture(#PB_Any, "soil_wall.jpg")))

CubeA = CreateEntity(#PB_Any, MeshID(Mesh), MaterialID(Material), -2, 1, 0)
CreateEntityBody(CubeA, #PB_Entity_BoxBody, 0.1, 0.1, 0.1)

CubeB = CreateEntity(#PB_Any, MeshID(Mesh), MaterialID(Material), 2, 1, 0)
CreateEntityBody(CubeB, #PB_Entity_BoxBody, 0.1, 0.1, 0.1)


Repeat
  Repeat : Until WindowEvent() = 0
          
  If ExamineKeyboard()
    If KeyboardPushed (#PB_Key_Escape)
      Break
    EndIf
    
    If KeyboardReleased (#PB_Key_Space)
      ;WorkAround : Wake up entities
      DisableEntityBody(CubeA, #False) 
      DisableEntityBody(CubeB, #False)
      
      ApplyEntityTorqueImpulse(CubeA, 0.8, 0, 0)
      ApplyEntityTorque(CubeB, 20, 0, 0)
    EndIf
    
  EndIf
    
  RenderWorld(40)
  FlipBuffers()  
ForEver
I am a beginner in 3D. If anyone can show me the differences (with codes). Thanks.
Last edited by falsam on Fri Sep 04, 2015 1:54 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 🤪
applePi
Addict
Addict
Posts: 1404
Joined: Sun Jun 25, 2006 7:28 pm

Re: [PB 4.40) ApplyEntityTorqueImpulse() & ApplyEntityTorque

Post by applePi »

falsam, it most likely the same as ApplyEntityImpulse(): Apply an impulse to the entity. The new impulse is added to the current force of the entity..... ApplyEntityForce(): Apply the specified force to the entity The new force value replace any previous force previously applied to the entity.
but with a difference that ApplyEntityTorqueImpulse() & ApplyEntityTorque are more tricky and mathematical look :what is Torque: https://www.physics.uoguelph.ca/tutoria ... intro.html
i don't alleged i understand the Torque deeply since it is a mathematical issue. but in practice look this example. when you apply ApplyEntityTorqueImpulse by pressing Z you see how rapidly the rotor rotate and draw the heavy big rectangular rod, while when apply ApplyEntityTorque by pressing 'Space' (rerun the example again for fair comparison) we see that it is very slowly rotate the rotor because the force here not accumulating
you may say okay remove the big weight and we will see the rotor accelerate in either cases, but i think the answer is that this is because there is no friction to stop the rotor and the newton law said that the continuous force accelerate the bodies and once it reaches a certain speed and we stop the force it will continue to move at the final speed unless there is a friction which is the usual case. so a better example than mine will be a heavy wheel rolling over a road, i choose my approach because it is easier to implement
surely there is much more to the subject, and it needs documentation. i just wish there is a friction to the Hinge Joint imagine it a rust on the joint axis.

Code: Select all

Enumeration
   
   #LIGHT
   #CAMERA
   #mainwin
   #Plane
   #Joint
   #cyl
     
EndEnumeration


Define.f KeyX, KeyY, MouseX, MouseY

ExamineDesktops()
If OpenWindow(0, 0, 0, DesktopWidth(0), DesktopHeight(0), "Space: ApplyEntityTorque .....'Z': ApplyEntityTorqueImpulse, .. use mouse + Arrows to rotate + move Camera", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)

If InitEngine3D()
  
  ;Add3DArchive(".", #PB_3DArchive_FileSystem)
  Add3DArchive(#PB_Compiler_Home + "Examples/3D/Data/Textures", #PB_3DArchive_FileSystem)
  Add3DArchive(#PB_Compiler_Home + "Examples/3D/Data/Models", #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)
    
  
    CreateMaterial(0, LoadTexture(0, "White.jpg"))
    DisableMaterialLighting(0, #True)
    MaterialShadingMode(0, #PB_Material_Wireframe     )
    MaterialCullingMode(0, #PB_Material_NoCulling)
            
    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(2, LoadTexture(2, "RustySteel.jpg"))
    MaterialCullingMode(2, #PB_Material_NoCulling)
    
    CreateMaterial(4, LoadTexture(4, "ground_diffuse.png"))
    MaterialCullingMode(4, #PB_Material_NoCulling)
        
    CreatePlane(500, 200, 200, 5, 5, 2, 2) ; the ground
    CreateEntity(500, MeshID(500), MaterialID(4), 0,-5,0)
    CreateEntityBody(500, #PB_Entity_StaticBody, 1, 0.0, 1)
    
    CreateCube(0,1)
    CreateEntity(0, MeshID(0), MaterialID(1), 0, 0, 0) ; the static cube
    ScaleEntity(0, 3,3,3)
    CreateCylinder(#cyl, 1, 5)
    CreateEntity(#cyl, MeshID(#cyl), MaterialID(3), 0,5, 0) 
    ScaleEntity(#cyl, 1,0.5,1)
    
    CreateEntityBody(0, #PB_Entity_StaticBody, 90, 0.1,10)
    CreateEntityBody(#cyl, #PB_Entity_CylinderBody, 1000, 0.1, 2) 

        
    ;hinge joint 0 
    HingeJoint(0, EntityID(0),
               0, 5, 0,
               0, 1, 0, 
               EntityID(#cyl),
               0, 0, 0, 
               0, 1, 0)
  
    RotateEntity(0, 0,0,90) 
    CreateEntity(2, MeshID(0), MaterialID(1), -5, 20, -50) 
    ScaleEntity(2, 3,1,100)
    CreateEntityBody(2, #PB_Entity_BoxBody, 100, 0.1, 2) 
    CreateEntity(3, MeshID(0), MaterialID(1), -8, 0, 0) ; the static cube
    ScaleEntity(3, 6,0.2,10)
    RotateEntity(3,0,0,90)
    CreateEntityBody(3, #PB_Entity_StaticBody, 1, 0.1, 2) 
    
;--------------  not sleep  ---------------------------------------
  ;SetEntityAttribute(0, #PB_Entity_LinearSleeping, 0)
  SetEntityAttribute(#cyl, #PB_Entity_LinearSleeping, 0)  

                   
    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 KeyboardPushed(#PB_Key_Space)
          
          ;ApplyEntityForce(1, 0, 100, 0 , 0, 0, -6)
           ApplyEntityTorque(#cyl, 500, 0, 0)
          
        ElseIf KeyboardPushed(#PB_Key_Z)
             ;ApplyEntityImpulse(1, 0,  20, 0 , 0, 0, -6)
             ApplyEntityTorqueImpulse(#cyl, 20, 0, 0)
          
       EndIf
  
       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
falsam
Enthusiast
Enthusiast
Posts: 632
Joined: Wed Sep 21, 2011 9:11 am
Location: France
Contact:

Re: [PB 4.40) ApplyEntityTorqueImpulse() & ApplyEntityTorque

Post by falsam »

Thank you applePi for this code that allowed me to fully understand the difference between ApplyEntityTorque() function and ApplyEntityTorqueImpulse(). I'am happy (And i'am not Droopy)


Image

➽ 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