Page 1 of 1

Spring simulation using the Joints

Posted: Fri Feb 27, 2015 5:08 pm
by applePi
one default hinge joint will allow the object to rotate 360 degree , 2 hinge joints separated by a noticeable space (as a percentage) will fix the object and can't rotate.
two hinge joints with a little space between them will enable the object to behave as a spring with a damping behavior, you test how much close by experiment.

look the official example HingJoint.pb about the hinge joint

the first code will position the hinge joint in the middle, drop a cube to test the spring feature. the second joint need not to be Hinge joint but a PointJoint.

the second code : the joints near the edge of the object. also there is a blocking wall to prevent the falling cubes from escaping

note 1: i have added a/d/s/w keys to MoveEntity the object, but beware MoveEntity can disturb the mechanics and i have used it here for convenience and testing only, you should use ApplyEntityImpulse and sisters in a serious project.
note 2: not sure of the absolute correctness of the directions of the joints but what i have used here show what i want.
note 3: here https://vimeo.com/106630420 a video show someone have used springs in his car wheels, he said it is made with purebasic 5.31, it is too realistic and good. he may have used the 2 joints idea to simulate the spring. may be !!.

any additional references or ideas will be great.

Code: Select all

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


Define.f KeyX, KeyY, MouseX, MouseY

Global Dim MeshData.PB_MeshVertex(0)
Global Dim MeshDataInd.PB_MeshFace(0)
Global Dim MeshData2.PB_MeshVertex(0)
Global Dim MeshDataInd2.PB_MeshFace(0)


ExamineDesktops()
If OpenWindow(0, 0, 0, DesktopWidth(0), DesktopHeight(0), "press Space...to drop a cube, a/d/w/s to move object.. use mouse + Arrows to rotate + move Camera", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)

Define.f KeyX, KeyY

Declare CreateMatrix()


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, 0, 50, 160, #PB_Absolute)
    ;CameraFOV(#camera, 70)
    CameraBackColor(#camera, RGB(255,200,200))
    CameraLookAt(#camera,0,0,0)
        
    CreateLight(0, RGB(255,255,255), 0, 20, 30)
    AmbientColor(RGB(200, 200, 200))
    
    CreateMaterial(3, LoadTexture(3, "snow_1024.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)
    
    CreateMaterial(5, LoadTexture(5, "Geebee2.bmp"))
    MaterialCullingMode(5, #PB_Material_NoCulling)
    
    CreatePlane(500, 200, 200, 5, 5, 2, 2) ; the ground
    CreateEntity(500, MeshID(500), MaterialID(4), 0,-15,0)
    CreateEntityBody(500, #PB_Entity_StaticBody, 1, 0.1, 1)
    
    CreateCube(0,10)
    CreateEntity(0, MeshID(0), MaterialID(1), 0, 0, 0)
    ScaleEntity(0, 3,3,2.5)
    CreateEntity(1, MeshID(0), MaterialID(3), 60,0, 0)
    ScaleEntity(1, 8,0.5,8)
    
    CreateEntityBody(0, #PB_Entity_ConvexHullBody, 90, 0.1,10)
    CreateEntityBody(1, #PB_Entity_BoxBody, 1, 0.1, 1)  
        
    
    SetEntityCollisionFilter(0       , 128, 103 )
    SetEntityCollisionFilter(1      , 128 , 103 )
    
    
    
    ;joints 0 and 1 just to fix ent 0 and ent 1 together
    HingeJoint(0, EntityID(0),
               0, 5, 0,
               0, 0, 1, 
               EntityID(1),
               0, 0, 0, 
               0, 0, 1)
     HingeJoint(1, EntityID(0),
               -2, 5, 0,
               0, 0, 1, 
               EntityID(1),
               -2, 0, 0, 
               0, 0, 1) 
  
    ;PointJoint(1, EntityID(0), -2,0,0, EntityID(1), -2,0,0)
  
 SetJointAttribute(0, #PB_HingeJoint_LowerLimit, 0)  
;--------------  not sleep  ---------------------------------------
  SetEntityAttribute(0, #PB_Entity_LinearSleeping, 0)
  SetEntityAttribute(1, #PB_Entity_LinearSleeping, 0)  

    WorldGravity(-9)
                   
    Repeat
    Repeat
      Event = WindowEvent()
    Until Event = 0
        
      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_Space)
          stone = CreateCube(#PB_Any , 3)
          stone = CreateEntity(#PB_Any, MeshID(stone),MaterialID(2))
          MoveEntity(stone, 20,40, 20)
          CreateEntityBody(stone,#PB_Entity_BoxBody,10)
      
       EndIf
  
    If KeyboardPushed(#PB_Key_D ) ; move right
      MoveEntity(0, 20,0,0)
      
    ElseIf KeyboardPushed(#PB_Key_A) ; move Left
      MoveEntity(0, -20,0,0)
    ElseIf KeyboardPushed(#PB_Key_S)  ; move south
      MoveEntity(0, 0,0,11)
    ElseIf KeyboardPushed(#PB_Key_W)  ; move north
      MoveEntity(0, 0,0,-11)  
    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
second code the joints are on the object edge

Code: Select all

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


Define.f KeyX, KeyY, MouseX, MouseY

Global Dim MeshData.PB_MeshVertex(0)
Global Dim MeshDataInd.PB_MeshFace(0)
Global Dim MeshData2.PB_MeshVertex(0)
Global Dim MeshDataInd2.PB_MeshFace(0)
Global Dim stone(1000)
Global stn

ExamineDesktops()
If OpenWindow(#mainwin, 0, 0, DesktopWidth(0), DesktopHeight(0), "press Space...to drop a cube, .. use mouse + Arrows to rotate + move Camera", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)

Define.f KeyX, KeyY

Declare CreateMatrix()


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(#mainwin), 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, 0, 50, 160, #PB_Absolute)
    ;CameraFOV(#camera, 70)
    CameraBackColor(#camera, RGB(255,200,200))
    CameraLookAt(#camera,0,0,0)
        
    CreateLight(0, RGB(255,255,255), 0, 20, 30)
    AmbientColor(RGB(200, 200, 200))
    
    CreateMaterial(3, LoadTexture(3, "snow_1024.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)
    
    CreateMaterial(5, LoadTexture(5, "Geebee2.bmp"))
    MaterialCullingMode(5, #PB_Material_NoCulling)
    
    CreatePlane(500, 200, 200, 5, 5, 2, 2)
    CreateEntity(500, MeshID(500), MaterialID(4), 0,-40,0)
    CreateEntityBody(500, #PB_Entity_StaticBody, 1, 0.1, 1)
    
    CreateCube(0,10)
    CreateEntity(0, MeshID(0), MaterialID(1), 0, 0, 0)
    ScaleEntity(0, 3,8,2.5)
    CreateEntity(1, MeshID(0), MaterialID(3), 60,0, 0)
    ScaleEntity(1, 8,0.5,8)
    
    CreateEntityBody(0, #PB_Entity_ConvexHullBody, 90, 0.1,10)
    CreateEntityBody(1, #PB_Entity_BoxBody, 1, 0.1, 1)  
        
    
    SetEntityCollisionFilter(0       , 128, 103 )
    SetEntityCollisionFilter(1      , 128 , 103 )
    
    
    
    CreateEntity(21, MeshID(0), MaterialID(5), 40, 15, 0)
    ScaleEntity(21, 8,2,0.2)
    RotateEntity(21, 0,90,0)
    CreateEntityBody(21, #PB_Entity_BoxBody, 1, 0.1, 1)  
    SetEntityCollisionFilter(21      , 128, 103 )
    
    ;joints 0 and 1 just to fix ent 0 and ent 1 together
    HingeJoint(0, EntityID(0),
               -35, 0, 0,
               0, 0, 1, 
               EntityID(1),
               -35, 0, 0, 
               0, 0, 1)
     HingeJoint(1, EntityID(0),
               -39, 0, 0,
               0, 0, 1, 
               EntityID(1),
               -39, 0, 0, 
               0, 0, 1) 
  
    
     HingeJoint(2, EntityID(1), ; joint 2+3 to fix the right red wall
                40, 0, 0,
                1, 0, 0, 
                EntityID(21),
                0, -6, 0, 
                0, 0, 1)
     HingeJoint(3, EntityID(1),
               40, 0, -40,
               1, 0, 0, 
               EntityID(21),
               40, -6, 0, 
               0, 0, 1)
     
  
 SetJointAttribute(0, #PB_HingeJoint_LowerLimit, 0) 
;- -------------  not sleep  ---------------------------------------
  SetEntityAttribute(0, #PB_Entity_LinearSleeping, 0)
  SetEntityAttribute(1, #PB_Entity_LinearSleeping, 0)  

    WorldGravity(-9)
                   
    Repeat
      Repeat
      Event = WindowEvent()
        Until Event = 0
      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_Space)
          stone = CreateCube(#PB_Any , 3)
          stone = CreateEntity(#PB_Any, MeshID(stone),MaterialID(2))
          SetEntityCollisionFilter(stone , 8 , 0 )
          MoveEntity(stone, 20,40, 20)
          CreateEntityBody(stone,#PB_Entity_BoxBody,10)
      
       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

Re: Spring simulation using the Joints

Posted: Mon Mar 02, 2015 9:41 pm
by Fred
Nice demo !