Slider Joint + Hinge Joint

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

Slider Joint + Hinge Joint

Post by applePi »

this is the slider joint example from the Purebasic 3D Demos, but added a hinge joint to the left of the slider and attaching also a rotor block to the hinge joint. the hinge joint motor is enabled
operation instruction: PgUp/PgDown to give a pulse to the slider

Code: Select all

;
; ------------------------------------------------------------
;
;   PureBasic - sliderJoint
;
;    (c) Fantaisie Software
;
; ------------------------------------------------------------
;
IncludeFile #PB_Compiler_Home + "examples/3d/Screen3DRequester.pb"

Define.f KeyX, KeyY, MouseX, MouseY
#CameraSpeed = 0.3

If InitEngine3D()
  
  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()
  
  If Screen3DRequester()
    
    ; First create materials
    ;
    CreateMaterial(0, LoadTexture(0, "Dirt.jpg"))
    CreateMaterial(1, LoadTexture(1, "Wood.jpg"))
    GetScriptMaterial(2, "Color/Green")
    GetScriptMaterial(3, "Color/Red")  
    
    ; Meshes
    ;
    CreateCube(0, 1.0)
    CreateSphere(1, 0.5)
    
    ; Entities
    ;
    StaticBlock = CreateEntity(#PB_Any, MeshID(0), MaterialID(0),  1, 0, 0)
    ScaleEntity(StaticBlock, 2, 4, 0.5)
    slider = CreateEntity(#PB_Any, MeshID(0), MaterialID(1), -1, 0, 0)
    rotator = CreateEntity(#PB_Any, MeshID(0), MaterialID(1), -3, 0, 0)
    ScaleEntity(rotator, 2, 4, 0.5)
    Ground = CreateEntity(#PB_Any, MeshID(0), MaterialID(2), -1, -2.1, 0)
    ScaleEntity(Ground, 10, 0.1, 4)
    
    sphere = CreateEntity(#PB_Any, MeshID(1), MaterialID(3), -0.4, 6, 0.23)
        
    ; Bodies
    ;
    CreateEntityBody(StaticBlock, #PB_Entity_StaticBody)
    CreateEntityBody(slider, #PB_Entity_BoxBody, 1.0)
    CreateEntityBody(rotator, #PB_Entity_BoxBody, 1.0)
    CreateEntityBody(Ground, #PB_Entity_StaticBody)
    CreateEntityBody(sphere, #PB_Entity_SphereBody, 0.5)
    
    
    ; SliderJoint
    ;
    SliderJoint(0, EntityID(StaticBlock), -1, 0, 0, EntityID(slider), 1, 0, 0)
    SetJointAttribute(0, #PB_SliderJoint_LowerLimit, -2)  
    SetJointAttribute(0, #PB_SliderJoint_UpperLimit,  0)   
        
    ; Camera
    ;
    CreateCamera(0, 0, 0, 100, 100)
    MoveCamera(0, -1, 8, 5, #PB_Absolute)
    CameraLookAt(0, -1, 0, 0)
    
    ; Light
    CreateLight(0, $FFFFFF, 1560, 900, 500)
    AmbientColor($330000)
    
    ; Hinge Joint
    HingeJoint(1, EntityID(rotator), 1.5, 0, 0, 1, 0, 0, EntityID(slider), -0.5, 0, 0, 1, 0, 0)
    EnableHingeJointAngularMotor(1, 1, 3, 3)
    
    Repeat
      Screen3DEvents()
      
      If ExamineMouse()
        MouseX = -MouseDeltaX() * #CameraSpeed * 0.2
        MouseY = -MouseDeltaY() * #CameraSpeed * 0.2
      EndIf
      
      If ExamineKeyboard()
        
        If KeyboardPushed(#PB_Key_PageUp)
          ApplyEntityImpulse(slider, 5, 0, 0)
    
        ElseIf KeyboardPushed(#PB_Key_PageDown)
          
          ApplyEntityImpulse(slider, -5, 0, 0) 
          
        EndIf        
        
        If KeyboardPushed(#PB_Key_Left)
          KeyX = -#CameraSpeed 
        ElseIf KeyboardPushed(#PB_Key_Right)
          KeyX = #CameraSpeed 
        Else
          KeyX = 0
        EndIf
        
        If KeyboardPushed(#PB_Key_Up)
          KeyY = -#CameraSpeed 
        ElseIf KeyboardPushed(#PB_Key_Down)
          KeyY = #CameraSpeed 
        Else
          KeyY = 0
        EndIf
        
      EndIf
      
      SetWindowTitle(0, "PureBasic - 3D Demos... PgUp/ PgDn to push the slider")
      RotateCamera(0, MouseY, MouseX, 0, #PB_Relative)
      MoveCamera  (0, KeyX, 0, KeyY)
      
      RenderWorld()
      Screen3DStats()
      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
Kwai chang caine
Always Here
Always Here
Posts: 5524
Joined: Sun Nov 05, 2006 11:42 pm
Location: Lyon - France

Re: Slider Joint + Hinge Joint

Post by Kwai chang caine »

Works fine here, thanks for sharing 8)
ImageThe happiness is a road...
Not a destination
Post Reply