Page 1 of 1

a play with Bridge.pb example part 1 (rolling a Chain)

Posted: Thu Mar 20, 2014 9:14 pm
by applePi
Examples\3D\Bridge.pb
about rolling and unrolling of the Bridge plates chain like the window blind woody plates.
by attaching a motor to plate number 1, and a hinge joint between some fixed sphere and plate 1.
i have replaced the plane by a scaled cube because sometimes the plate 50 sink inside the plane, and a scaled cube seems better.
to position the bridge chain higher change -40 in line 56 to -4, also change -40 line 106 to -4.
to run the motor Press 'Z' , to unroll the chain press 'X' , to stop the motor press 'S'

Image
Image

Code: Select all

;
; ------------------------------------------------------------
;
;   PureBasic - PointJoint (Bridge)
;
;    (c) 2011 - Fantaisie Software
;
; ------------------------------------------------------------
;

IncludeFile "Screen3DRequester.pb"

#CameraSpeed = 1
#NbPlanks = 50
#joint = 200
#axis = 205
Define.f KeyX, KeyY, MouseX, MouseY
Dim Plank(#NbPlanks)

If InitEngine3D()
  
  Add3DArchive("Data/Textures", #PB_3DArchive_FileSystem)
  Add3DArchive("Data/Scripts",#PB_3DArchive_FileSystem)
  Add3DArchive("Data/Packs/desert.zip", #PB_3DArchive_Zip)
  Parse3DScripts()
  
  InitSprite()
  InitKeyboard()
  InitMouse()
  
  If Screen3DRequester()
    
    WorldShadows(#PB_Shadow_Modulative, -1, RGB(127, 127, 127))
    
    ;Materials
    ;
    CreateMaterial(0, LoadTexture(0, "Wood.jpg"))
    GetScriptMaterial(1, "SphereMap/SphereMappedRustySteel")
    CreateMaterial(2, LoadTexture(2, "Dirt.jpg"))
    GetScriptMaterial(3, "Scene/GroundBlend")
    
    ; Ground
    ;
    ;CreatePlane(3, 500, 500, 5, 5, 5, 5)
    CreateCube(3,1)
    CreateEntity(3,MeshID(3),MaterialID(3), 0, -53, 0)
    ScaleEntity(3,500,10,500)
    EntityRenderMode(3, 0) 
    EntityPhysicBody(3, #PB_Entity_BoxBody, 0, 1, 1)
    
    ; Bridge
    CreateCube(1, 1.0)
    For i = 1 To #NbPlanks
      Plank(i)=CreateEntity(#PB_Any, MeshID(1), MaterialID(0))
      ScaleEntity(Plank(i), 2.8, 0.8, 20)
      MoveEntity(Plank(i), i * 3, -40, 0, #PB_Absolute)
      EntityPhysicBody(Plank(i), #PB_Entity_BoxBody, 1.0)
    Next i
    
    SetEntityMaterial(Plank(1), MaterialID(2)) ; mark plate 1 with Dirt material

    ;Debug EntityX(Plank(1)): Debug EntityY(Plank(1)): Debug EntityZ(Plank(1))
    Pas.f = 1.5
    ;;;;;PointJoint(#PB_Any, EntityID(Plank(1)), -Pas, 0, -5)
    For i= 1 To #NbPlanks-2
      Joint=PointJoint(#PB_Any, EntityID(Plank(i+1)), -Pas, 0, -5, EntityID(Plank(i)), Pas, 0, -5)
    Next i
    ;PointJoint(#PB_Any, EntityID(Plank(#NbPlanks)),  Pas, 0, -5)
    PointJoint(#PB_Any, EntityID(Plank(#NbPlanks-1)),  Pas, 0, -5, EntityID(Plank(#NbPlanks)), -Pas, 0, -5)
    
    ;;;;;PointJoint(#PB_Any, EntityID(Plank(1)), -Pas, 0, 5)
    For i= 1 To #NbPlanks-2
      Joint=PointJoint(#PB_Any, EntityID(Plank(i+1)),  -Pas, 0, 5, EntityID(Plank(i)), Pas, 0, 5)
    Next i
    ;PointJoint(#PB_Any, EntityID(Plank(#NbPlanks)),  Pas, 0, 5)
    toto=PointJoint(#PB_Any, EntityID(Plank(#NbPlanks-1)),  Pas, 0, 5, EntityID(Plank(#NbPlanks)), -Pas, 0, 5)
    
    
    ; Objects
    ;
    CreateSphere(2, 2, 30, 30)
    
    C = Plank(1)
    For i = 10 To #NbPlanks/2
      Perso = CreateEntity(#PB_Any, MeshID(2), MaterialID(1), EntityX(C) +i * 5, EntityY(C)+ i * 2, EntityZ(C))
      EntityPhysicBody(Perso, #PB_Entity_SphereBody, 1.0, 0.3, 0.5)
    Next i
    
        
    ; Camera
    ;
    CreateCamera(0, 0, 0, 100, 100)
    MoveCamera(0, -50, 10, -60, #PB_Absolute)
    CameraLookAt(0, EntityX(C) + 25, EntityY(C) - 15,  EntityZ(C))
    
    ; Skybox
    ;
    SkyBox("desert07.jpg")
    
    ; Light
    ;
    CreateLight(0, RGB(255, 255, 255), 100, 800, -500)
    AmbientColor(RGB(20, 20, 20))
    
    CreateSphere(#axis, 2) ; the axis
    CreateEntity(#axis, MeshID(#axis), MaterialID(2), 0, -40, 15)
    EntityPhysicBody(#axis, #PB_Entity_StaticBody)
    HingeJoint(#joint,EntityID(#axis),0,0,2, #False,#False,#True, EntityID(Plank(1)),1.4 ,0.4, 15, #False,#False,#True)
    EnableHingeJointAngularMotor(#joint, #True, 0, 100)
    
    
    Repeat
      Screen3DEvents()
      
      If ExamineMouse()
        MouseX = -MouseDeltaX() * #CameraSpeed * 0.05
        MouseY = -MouseDeltaY() * #CameraSpeed * 0.05
      EndIf
      
      If ExamineKeyboard()
        If KeyboardPushed(#PB_Key_Space)
          ApplyEntityImpulse(Plank(#NbPlanks/2), 0, 9, 0)
        EndIf
        If KeyboardPushed(#PB_Key_Z )
     ;ApplyEntityImpulse(Plank(1),  1, 0, 0,  2,0,0)
     EnableHingeJointAngularMotor(#joint, #True, 90, 100)
        ElseIf KeyboardPushed(#PB_Key_X )
          EnableHingeJointAngularMotor(#joint, #True, -90, 100)
        ElseIf KeyboardPushed(#PB_Key_S )
          EnableHingeJointAngularMotor(#joint, #False, -90, 100)  
        
   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
      
      MoveCamera  (0, KeyX, 0, KeyY)
      RotateCamera(0,  MouseY, MouseX, 0, #PB_Relative)
      
      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