Page 1 of 1

Rotational to Linear movement

Posted: Fri Aug 26, 2016 4:23 pm
by applePi
i have posted before such example with Hinge joints , but the new Generic Joints are much more easier.
Scotch Yoke Mechanism approximation
approximate this machine
https://www.youtube.com/watch?v=hsaoTo1vuY4
http://cadandme.blogspot.com/2009/02/sc ... on_12.html
the sliding is due to the rotating wheel and the spring force.
all the joints a genericJoints including the sliding joint
press Space to operate/stop the machine

Image

needs PB v5.50 . press Space to operate/stop the machine

Code: Select all

IncludeFile #PB_Compiler_Home + "examples/3d/Screen3DRequester.pb"

#CameraSpeed = 0.4
#sphere = 20

Define.f KeyX, KeyY, MouseX, MouseY

If InitEngine3D()
  
  InitSprite()
  InitKeyboard()
  InitMouse()
  
  If Screen3DRequester()
    
    Add3DArchive(#PB_Compiler_Home + "examples/3d/Data/"              , #PB_3DArchive_FileSystem)
    Add3DArchive(#PB_Compiler_Home + "examples/3d/Data/Textures"  , #PB_3DArchive_FileSystem)
    Add3DArchive(#PB_Compiler_Home + "examples/3d/Data/Scripts"    , #PB_3DArchive_FileSystem)
    Add3DArchive(#PB_Compiler_Home + "examples/3d/Data/GUI"        , #PB_3DArchive_FileSystem)
    Parse3DScripts()
    
    ;WorldDebug(#PB_World_DebugBody)
        
    ;-------------------------------
    ; create  material
    CreateMaterial(1, LoadTexture(1, "Tire_tex.png"))
    SetMaterialColor(1, #PB_Material_AmbientColor, #PB_Material_AmbientColors)
    ; 
    CreateMaterial(2, LoadTexture(2, "snow_1024.jpg"))
    SetMaterialColor(2, #PB_Material_AmbientColor, #PB_Material_AmbientColors)
    
    CreateMaterial(3, LoadTexture(3, "ground_diffuse.png"))
    SetMaterialColor(3, #PB_Material_AmbientColor, #PB_Material_AmbientColors)
    
    CreateMaterial(4, LoadTexture(4, "Wood.jpg"))
    MaterialBlendingMode(4, #PB_Material_AlphaBlend)
    SetMaterialColor(4, #PB_Material_DiffuseColor, RGBA(255, 255, 0, 150))
    
    CreateMaterial(6, LoadTexture(6, "Wood.jpg"))
    
    CreateMaterial(5, LoadTexture(5, "Geebee2.bmp"))
    
    ;-------------------------------

CreateCylinder(50, 1, 0.5)
CreateEntity(50, MeshID(50),MaterialID(1),  0, 2, 0) ; the wheel
axis = CreateEntity(#PB_Any, MeshID(50),MaterialID(6),  0.5, 2, 0) ; the rod on the wheel
ScaleEntity(axis, 0.15, 3, 0.15)

CreateCube(1, 1) ; for the the ground, and the rod attached to the movable ring

Global Compound = CreateEntity(#PB_Any,0,0)

AddSubEntity(Compound, 50, #PB_Entity_CylinderBody) ;; the wheel 
AddSubEntity(Compound, axis, #PB_Entity_CylinderBody) ; the rod on the wheel 
CreateEntityBody(Compound, #PB_Entity_CompoundBody, 1, 0.4, 0.5) ;the compound from the two

Global Comp2 = CreateEntity(#PB_Any,0,0) ; for the movable ring
CreateTube(5, 0.5, 0.4, 1)
CreateEntity(5, MeshID(5), MaterialID(3),0,2.5,0) ; the movable Ring
ScaleEntity(5, 1.5,0.5,1)
CreateEntity(6, MeshID(1),MaterialID(3),  0, 2.5, -1.5) ;the rod attached to movable ring
ScaleEntity(6,1.5,0.2,0.2) :RotateEntity(6, 0,90,0)
AddSubEntity(Comp2, 5, #PB_Entity_StaticBody) ; in this way we make the Ring as having movable concave geometry
AddSubEntity(Comp2, 6, #PB_Entity_BoxBody) ; add the rod
CreateEntityBody(Comp2, #PB_Entity_CompoundBody, 1, 0.5, 0.5) ;the compound made from the ring and the rod
  ;-----------------------------------------
; static ring 
CreateEntity(7, MeshID(5), MaterialID(4),0,2.5,-1.9)
RotateEntity(7,90,0,0)
CreateEntityBody(7, #PB_Entity_StaticBody, 1, 0.5, 0.5)
ScaleEntity(7,0.7,1,0.7)

;tube with sphere inside
CreateEntity(8, MeshID(5), MaterialID(4),0,3.0,-3.5)
RotateEntity(8,110,0,0)
CreateEntityBody(8, #PB_Entity_StaticBody, 1, 0.5, 0.5)
ScaleEntity(8,1.5, 2 ,1.5)


CreateSphere(#sphere, 0.5)
CreateEntity(#sphere, MeshID(#sphere),MaterialID(5), 0,3.0,-3.5) 
CreateEntityBody(#sphere, #PB_Entity_ConvexHullBody, 1, 0.5, 0.5)

  
;-----------------------------------------
    ;Ground
    ;
    Ground = CreateEntity(#PB_Any, MeshID(1), MaterialID(2), 0, 0, 0)
    ScaleEntity(Ground, 40, 0.4, 40)
    CreateEntityBody(Ground, #PB_Entity_StaticBody)
    
    ;sliding joint for the movable ring
    GenericJoint(2,EntityID(Ground), 0, 2, 0,EntityID(Comp2), 0, 2, 0)
    SetJointAttribute(2, #PB_Joint_EnableSpring, #True, 2)
    SetJointAttribute(2, #PB_Joint_Stiffness, 5, 2)
    SetJointAttribute(2, #PB_Joint_Damping, 0.5, 2)
    
    ; joint for the wheel
    GenericJoint(4,EntityID(Ground), 0, 2, 0,EntityID(Compound), 0, 2, 0)
    
    ;the following will make the wheel stop if we stop the angular force
    SetJointAttribute(4, #PB_Joint_EnableSpring, #True, 4)
    SetJointAttribute(4, #PB_Joint_Stiffness, 5, 4)
    SetJointAttribute(4, #PB_Joint_Damping, 0.0, 4)
    
    ;this is essenyial to stabilize the wheel
    EntityAngularFactor(Compound, 0, 1, 0)
        
     ; camera
    CreateCamera(0, 0, 0, 100, 100, #True)
    MoveCamera(0,4,7,3, #PB_Absolute)
    CameraLookAt(0,0,2,0)
    
    ; GUI
    OpenWindow3D(0, 0, 0, 50 , 10 , "")
    HideWindow3D(0,1)
    ShowGUI(128, 1) ; Display the GUI, semi-transparent and display the mouse cursor
    ;SetWindowTitle(0, ".'space':to operate/stop the machine. .... arrows and mouse for the Camera")
  
    Repeat
      Screen3DEvents()
      
      ApplyEntityTorque(Compound, 0, force*10, 0)
      
      If ExamineMouse()
        MouseX = -MouseDeltaX() * #CameraSpeed * 0.1
        MouseY = -MouseDeltaY() * #CameraSpeed * 0.1
        InputEvent3D(MouseX(), MouseY(),0)
        BodyPick(CameraID(0), MouseButton(#PB_MouseButton_Left), MouseX(), MouseY(), 1)
      EndIf
    
      If ExamineKeyboard()
        
                 
    If KeyboardReleased(#PB_Key_Space); throw cubes over the gear
      force ! 1
    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()
      
      FlipBuffers()
      
      
    Until KeyboardPushed(#PB_Key_Escape) Or Quit = 1
    
  EndIf
  
Else
  MessageRequester("Error", "The 3D Engine can't be initialized",0)
EndIf

End
more stiffness in the movable ring generic joint (slider), and add damping for the wheel : SetEntityAttribute(Compound, #PB_Entity_AngularDamping, 0.5) and a few changes:

Code: Select all

IncludeFile #PB_Compiler_Home + "examples/3d/Screen3DRequester.pb"

#CameraSpeed = 0.4
#sphere = 20

Define.f KeyX, KeyY, MouseX, MouseY

If InitEngine3D()
  
  InitSprite()
  InitKeyboard()
  InitMouse()
  
  If Screen3DRequester()
    
    Add3DArchive(#PB_Compiler_Home + "examples/3d/Data/"              , #PB_3DArchive_FileSystem)
    Add3DArchive(#PB_Compiler_Home + "examples/3d/Data/Textures"  , #PB_3DArchive_FileSystem)
    Add3DArchive(#PB_Compiler_Home + "examples/3d/Data/Scripts"    , #PB_3DArchive_FileSystem)
    Add3DArchive(#PB_Compiler_Home + "examples/3d/Data/GUI"        , #PB_3DArchive_FileSystem)
    Parse3DScripts()
    
    ;WorldDebug(#PB_World_DebugBody)
        
    ;-------------------------------
    ; create  material
    CreateMaterial(1, LoadTexture(1, "Tire_tex.png"))
    SetMaterialColor(1, #PB_Material_AmbientColor, #PB_Material_AmbientColors)
    ; 
    CreateMaterial(2, LoadTexture(2, "snow_1024.jpg"))
    SetMaterialColor(2, #PB_Material_AmbientColor, #PB_Material_AmbientColors)
    
    CreateMaterial(3, LoadTexture(3, "ground_diffuse.png"))
    SetMaterialColor(3, #PB_Material_AmbientColor, #PB_Material_AmbientColors)
    
    CreateMaterial(4, LoadTexture(4, "Wood.jpg"))
    MaterialBlendingMode(4, #PB_Material_AlphaBlend)
    SetMaterialColor(4, #PB_Material_DiffuseColor, RGBA(255, 255, 0, 150))
    
    CreateMaterial(6, LoadTexture(6, "Wood.jpg"))
    
    CreateMaterial(5, LoadTexture(5, "Geebee2.bmp"))
    
    ;-------------------------------

CreateCylinder(50, 1, 0.5)
CreateEntity(50, MeshID(50),MaterialID(1),  0, 2, 0) ; the wheel
axis = CreateEntity(#PB_Any, MeshID(50),MaterialID(6),  0.5, 2, 0) ; the rod on the wheel
ScaleEntity(axis, 0.15, 3, 0.15)

CreateCube(1, 1) ; for the the ground, and the rod attached to the movable ring

Global Compound = CreateEntity(#PB_Any,0,0)

AddSubEntity(Compound, 50, #PB_Entity_CylinderBody) ;; the wheel 
AddSubEntity(Compound, axis, #PB_Entity_CylinderBody) ; the rod on the wheel 
CreateEntityBody(Compound, #PB_Entity_CompoundBody, 1, 0.4, 0.5) ;the compound from the two

Global Comp2 = CreateEntity(#PB_Any,0,0) ; for the movable ring
CreateTube(5, 0.5, 0.4, 1)
CreateEntity(5, MeshID(5), MaterialID(3),0,2.5,0) ; the movable Ring
ScaleEntity(5, 1.5,0.5,1)
CreateEntity(6, MeshID(1),MaterialID(3),  0, 2.5, -1.5) ;the rod attached to movable ring
ScaleEntity(6,1.5,0.2,0.2) :RotateEntity(6, 0,90,0)
AddSubEntity(Comp2, 5, #PB_Entity_StaticBody) ; in this way we make the Ring as having movable concave geometry
AddSubEntity(Comp2, 6, #PB_Entity_BoxBody) ; add the rod
CreateEntityBody(Comp2, #PB_Entity_CompoundBody, 1, 0.5, 0.5) ;the compound made from the ring and the rod
  ;-----------------------------------------
; static ring 
CreateEntity(7, MeshID(5), MaterialID(4),0,2.5,-1.9)
RotateEntity(7,90,0,0)
CreateEntityBody(7, #PB_Entity_StaticBody, 1, 0.5, 0.5)
ScaleEntity(7,0.7,0.7,0.7)

;tube with sphere inside
CreateEntity(8, MeshID(5), MaterialID(4),0,3.0,-3.5)
RotateEntity(8,110,0,0)
CreateEntityBody(8, #PB_Entity_StaticBody, 1, 0.5, 0.5)
ScaleEntity(8,1.5, 2.5 ,1.5)


CreateSphere(#sphere, 0.5)
CreateEntity(#sphere, MeshID(#sphere),MaterialID(5), 0,3.0,-3.5) 
CreateEntityBody(#sphere, #PB_Entity_ConvexHullBody, 1, 0.5, 0.5)

  
;-----------------------------------------
    ;Ground
    ;
    Ground = CreateEntity(#PB_Any, MeshID(1), MaterialID(2), 0, 0, 0)
    ScaleEntity(Ground, 40, 0.4, 40)
    CreateEntityBody(Ground, #PB_Entity_StaticBody)
    
    ;sliding joint for the movable ring
    GenericJoint(2,EntityID(Ground), 0, 2, 0,EntityID(Comp2), 0, 2, 0)
    SetJointAttribute(2, #PB_Joint_EnableSpring, #True, 2)
    SetJointAttribute(2, #PB_Joint_Stiffness, 160, 2)
    SetJointAttribute(2, #PB_Joint_Damping, 0.0, 2)
    
    ; joint for the wheel
    GenericJoint(4,EntityID(Ground), 0, 2, 0,EntityID(Compound), 0, 2, 0)
    
    ;attrib of the wheel 
    SetJointAttribute(4, #PB_Joint_NoLimit, 0, 4)
    DisableDebugger
    SetEntityAttribute(Compound, #PB_Entity_AngularDamping, 0.5)
    EnableDebugger
    
    ;this is essenyial to stabilize the wheel
    EntityAngularFactor(Compound, 0, 1, 0)
        
     ; camera
    CreateCamera(0, 0, 0, 100, 100, #True)
    MoveCamera(0,6,6,-2, #PB_Absolute)
    CameraLookAt(0,0,2,-2)
    
    ; GUI
    OpenWindow3D(0, 0, 0, 50 , 10 , "")
    HideWindow3D(0,1)
    ShowGUI(128, 1) ; Display the GUI, semi-transparent and display the mouse cursor
    ;SetWindowTitle(0, ".'space':to operate/stop the machine. .... arrows and mouse for the Camera")
  
    Repeat
      Screen3DEvents()
      
      ApplyEntityTorque(Compound, 0, force*20, 0)
      ;ApplyEntityTorqueImpulse(Compound, 0, force*0.2, 0)
      
      If ExamineMouse()
        MouseX = -MouseDeltaX() * #CameraSpeed * 0.1
        MouseY = -MouseDeltaY() * #CameraSpeed * 0.1
        InputEvent3D(MouseX(), MouseY(),0)
        BodyPick(CameraID(0), MouseButton(#PB_MouseButton_Left), MouseX(), MouseY(), 1)
      EndIf
    
      If ExamineKeyboard()
        
                 
    If KeyboardReleased(#PB_Key_Space); throw cubes over the gear
      force ! 1
    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()
      
      FlipBuffers()
      
      
    Until KeyboardPushed(#PB_Key_Escape) Or Quit = 1
    
  EndIf
  
Else
  MessageRequester("Error", "The 3D Engine can't be initialized",0)
EndIf

End

Re: Rotational to Linear movement

Posted: Sat Aug 27, 2016 9:38 am
by applePi
now it is better simulation of the Scotch Yoke Mechanism as explained in https://www.youtube.com/watch?v=hsaoTo1vuY4
i have used hinge joint for the wheel to attach a motor to it (EnableHingeJointAngularMotor)
this makes it possible to rotate the wheel with slow rotational speed, note it is a motor which run and stop immediately. the ring replaced by 2 rods so it resembles more the ring in the youtube video above
Image

also have used for the (ring) :
SetEntityAttribute(Comp2, #PB_Entity_LinearSleeping, 0)
else it will not respond after idle for some seconds
to make it run speedier change 3 to 10 in line 147
EnableHingeJointAngularMotor(#Joint,#True,force*3,10)
10 is the highest speed
operation: press space to operate/stop the machine

Code: Select all

IncludeFile #PB_Compiler_Home + "examples/3d/Screen3DRequester.pb"

#CameraSpeed = 0.4
#sphere = 20
#Joint = 7
#baseCube = 200 ; for the hingejoint of the wheel

Define.f KeyX, KeyY, MouseX, MouseY

If InitEngine3D()
  
  InitSprite()
  InitKeyboard()
  InitMouse()
  
  If Screen3DRequester()
    
    Add3DArchive(#PB_Compiler_Home + "examples/3d/Data/"              , #PB_3DArchive_FileSystem)
    Add3DArchive(#PB_Compiler_Home + "examples/3d/Data/Textures"  , #PB_3DArchive_FileSystem)
    Add3DArchive(#PB_Compiler_Home + "examples/3d/Data/Scripts"    , #PB_3DArchive_FileSystem)
    Add3DArchive(#PB_Compiler_Home + "examples/3d/Data/GUI"        , #PB_3DArchive_FileSystem)
    Parse3DScripts()
    
    ;WorldDebug(#PB_World_DebugBody)
        
    ;-------------------------------
    ; create  material
    CreateMaterial(1, LoadTexture(1, "Tire_tex.png"))
    SetMaterialColor(1, #PB_Material_AmbientColor, #PB_Material_AmbientColors)
    ; 
    CreateMaterial(2, LoadTexture(2, "snow_1024.jpg"))
    SetMaterialColor(2, #PB_Material_AmbientColor, #PB_Material_AmbientColors)
    
    CreateMaterial(3, LoadTexture(3, "ground_diffuse.png"))
    SetMaterialColor(3, #PB_Material_AmbientColor, #PB_Material_AmbientColors)
    
    CreateMaterial(4, LoadTexture(4, "Wood.jpg"))
    MaterialBlendingMode(4, #PB_Material_AlphaBlend)
    SetMaterialColor(4, #PB_Material_DiffuseColor, RGBA(255, 255, 0, 150))
    
    CreateMaterial(6, LoadTexture(6, "Wood.jpg"))
    
    CreateMaterial(5, LoadTexture(5, "Geebee2.bmp"))
    
    ;-------------------------------

CreateCylinder(50, 1, 0.5)
CreateEntity(50, MeshID(50),MaterialID(1),  0, 2, 0) ; the wheel
wheelHand = CreateEntity(#PB_Any, MeshID(50),MaterialID(6),  0.5, 2, 0) ; the rod on the wheel
ScaleEntity(wheelHand, 0.15, 3, 0.15)

CreateCube(1, 1) ; for the the ground, and the rod attached to the movable ring

Global Compound = CreateEntity(#PB_Any,0,0)

AddSubEntity(Compound, 50, #PB_Entity_CylinderBody) ;; the wheel 
AddSubEntity(Compound, wheelHand, #PB_Entity_CylinderBody) ; the Handle on the wheel 
CreateEntityBody(Compound, #PB_Entity_CompoundBody, 1, 0.4, 0.5) ;the compound from the two

Global Comp2 = CreateEntity(#PB_Any,0,0) ; for the movable ring which are made from 2+1 rods
CreateEntity(5, MeshID(1),MaterialID(3),  0, 2.5, 0.5) ;the movable ring left rod
ScaleEntity(5,1.5,0.2,0.2)                           
CreateEntity(6, MeshID(1),MaterialID(3),  0, 2.5, 0) ;the movable ring right rod
ScaleEntity(6,1.5,0.2,0.2)

CreateEntity(7, MeshID(1),MaterialID(3),  0, 2.5, -1.0) ;the rod attached to movable ring
ScaleEntity(7, 2, 0.2, 0.2) :RotateEntity(7, 0,90,0)
AddSubEntity(Comp2, 5, #PB_Entity_BoxBody) ; in this way we make the Ring as having movable concave geometry
AddSubEntity(Comp2, 6, #PB_Entity_BoxBody)
AddSubEntity(Comp2, 7, #PB_Entity_BoxBody) ; add the rod
CreateEntityBody(Comp2, #PB_Entity_CompoundBody, 1, 0.5, 0.5) ;the compound made from the ring and the rod
  ;-----------------------------------------
  ; static ring 
CreateTube(30, 0.5, 0.4, 1)
CreateEntity(30, MeshID(30), MaterialID(4),0,2.5,-1.9)
RotateEntity(30,90,0,0)
CreateEntityBody(30, #PB_Entity_StaticBody, 1, 0.5, 0.5)
ScaleEntity(30,0.7,0.7,0.7)

;tube with sphere inside
CreateEntity(40, MeshID(30), MaterialID(4),0,3.0,-3.5)
RotateEntity(40,110,0,0)
CreateEntityBody(40, #PB_Entity_StaticBody, 1, 0.5, 0.5)
ScaleEntity(40,1.5, 2.5 ,1.5)


CreateSphere(#sphere, 0.5)
CreateEntity(#sphere, MeshID(#sphere),MaterialID(5), 0,3.0,-3.5) 
CreateEntityBody(#sphere, #PB_Entity_ConvexHullBody, 1, 0.5, 0.5)

  
;-----------------------------------------
    ;Ground
    ;
    Ground = CreateEntity(#PB_Any, MeshID(1), MaterialID(2), 0, 0, 0)
    ScaleEntity(Ground, 40, 0.4, 40)
    CreateEntityBody(Ground, #PB_Entity_StaticBody)
    
    ;sliding joint for the movable ring
    GenericJoint(2,EntityID(Ground), 0, 2, 0,EntityID(Comp2), 0, 2, 0)
    SetJointAttribute(2, #PB_Joint_EnableSpring, #True, 2)
    SetJointAttribute(2, #PB_Joint_Stiffness, 150, 2)
    SetJointAttribute(2, #PB_Joint_Damping, 0.0, 2)
    
    CreateCube(#baseCube, 1)
    CreateEntity(#baseCube, MeshID(#baseCube),  MaterialID(4), 0, 0, 0)
    CreateEntityBody(#baseCube, #PB_Entity_StaticBody)
    ; joint for the wheel
       
    HingeJoint(#Joint, EntityID(Compound),
             0, 1, 0,
             0, 1, 0, 
             EntityID(#baseCube),
             0, 1, 0, 
             0, 1, 0)
    
     
    EnableHingeJointAngularMotor(#Joint,0,0.0,10) ; speed zero
    SetEntityAttribute(Comp2, #PB_Entity_LinearSleeping, 0)
        
     ; camera
    CreateCamera(0, 0, 0, 100, 100, #True)
    MoveCamera(0,6,6,-2, #PB_Absolute)
    CameraLookAt(0,0,2,-2)
    
    ; GUI
    OpenWindow3D(0, 0, 0, 50 , 10 , "")
    HideWindow3D(0,1)
    ShowGUI(128, 0) ; Display the GUI, semi-transparent and don't display the mouse cursor
    ;SetWindowTitle(0, ".'space':to operate/stop the machine. .... arrows and mouse for the Camera")
 
    Repeat
      Screen3DEvents()
     
      If ExamineMouse()
        MouseX = -MouseDeltaX() * #CameraSpeed * 0.1
        MouseY = -MouseDeltaY() * #CameraSpeed * 0.1
        InputEvent3D(MouseX(), MouseY(),0)
        BodyPick(CameraID(0), MouseButton(#PB_MouseButton_Left), MouseX(), MouseY(), 1)
      EndIf
    
      If ExamineKeyboard()
        
                 
    If KeyboardReleased(#PB_Key_Space); slow motor rotation
      force ! 1
      EnableHingeJointAngularMotor(#Joint,#True,force*3,10)
    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()
      
      FlipBuffers()
      
      
    Until KeyboardPushed(#PB_Key_Escape) Or Quit = 1
    
  EndIf
  
Else
  MessageRequester("Error", "The 3D Engine can't be initialized",0)
EndIf

End

Re: Rotational to Linear movement

Posted: Sat Aug 27, 2016 10:14 am
by IdeasVacuum
Wow. This is your best work applePi, really interesting stuff. It is extremely important to have your work on the forum because you stimulate ideas and the usefulness of PB. I have learnt a lot from your code, been inspired by it.

Re: Rotational to Linear movement

Posted: Sat Aug 27, 2016 2:13 pm
by applePi
Thank you IdeasVacuum . i am happy you like the third example, i also surprised of the result of the third example and its simple construction, in the beginning i haven't recognized that we can use 2 parallel rods to simulate the elongated ring as in the youtube video. note that the amount of linear displacement equal the 2 times the radius from the woody handle to the wheel center.
here is the handle is on the edge of the wheel, and more speed. more and more speed can damage the machine or need a redesign of the thickness of the 2 rods (the ring) and the handle.
the new mechanics in the new purebasic v5.50 is something very good and very stable. AddSubEntity allow us to construct too many shapes with strong solidity. in the future i imagine we can remove the constituents of the Compound (the individual entities) and the Compound stay alive. but what we already have is very good.

Code: Select all

IncludeFile #PB_Compiler_Home + "examples/3d/Screen3DRequester.pb"

#CameraSpeed = 0.4
#sphere = 20
#Joint = 7
#baseCube = 200 ; for the hingejoint of the wheel

Define.f KeyX, KeyY, MouseX, MouseY

If InitEngine3D()
  
  InitSprite()
  InitKeyboard()
  InitMouse()
  
  If Screen3DRequester()
    
    Add3DArchive(#PB_Compiler_Home + "examples/3d/Data/"              , #PB_3DArchive_FileSystem)
    Add3DArchive(#PB_Compiler_Home + "examples/3d/Data/Textures"  , #PB_3DArchive_FileSystem)
    Add3DArchive(#PB_Compiler_Home + "examples/3d/Data/Scripts"    , #PB_3DArchive_FileSystem)
    Add3DArchive(#PB_Compiler_Home + "examples/3d/Data/GUI"        , #PB_3DArchive_FileSystem)
    Parse3DScripts()
    
    ;WorldDebug(#PB_World_DebugBody)
        
    ;-------------------------------
    ; create  material
    CreateMaterial(1, LoadTexture(1, "Tire_tex.png"))
    SetMaterialColor(1, #PB_Material_AmbientColor, #PB_Material_AmbientColors)
    ; 
    CreateMaterial(2, LoadTexture(2, "snow_1024.jpg"))
    SetMaterialColor(2, #PB_Material_AmbientColor, #PB_Material_AmbientColors)
    
    CreateMaterial(3, LoadTexture(3, "ground_diffuse.png"))
    SetMaterialColor(3, #PB_Material_AmbientColor, #PB_Material_AmbientColors)
    
    CreateMaterial(4, LoadTexture(4, "Wood.jpg"))
    MaterialBlendingMode(4, #PB_Material_AlphaBlend)
    SetMaterialColor(4, #PB_Material_DiffuseColor, RGBA(255, 255, 0, 100))
    
    CreateMaterial(6, LoadTexture(6, "Wood.jpg"))
    
    CreateMaterial(5, LoadTexture(5, "Geebee2.bmp"))
    
    ;-------------------------------

CreateCylinder(50, 1, 0.5)
CreateEntity(50, MeshID(50),MaterialID(1),  0, 2, 0) ; the wheel
wheelHand = CreateEntity(#PB_Any, MeshID(50),MaterialID(6),  0.8, 2, 0) ; the rod on the wheel
ScaleEntity(wheelHand, 0.15, 3, 0.15)

CreateCube(1, 1) ; for the the ground, and the rod attached to the movable ring

Global Compound = CreateEntity(#PB_Any,0,0)

AddSubEntity(Compound, 50, #PB_Entity_CylinderBody) ;; the wheel 
AddSubEntity(Compound, wheelHand, #PB_Entity_CylinderBody) ; the Handle on the wheel 
CreateEntityBody(Compound, #PB_Entity_CompoundBody, 1, 0.4, 0.5) ;the compound from the two

Global Comp2 = CreateEntity(#PB_Any,0,0) ; for the movable ring which are made from 2+1 rods
CreateEntity(5, MeshID(1),MaterialID(3),  0, 2.5, 0.5) ;the movable ring left rod
ScaleEntity(5,3,0.2,0.2)                           
CreateEntity(6, MeshID(1),MaterialID(3),  0, 2.5, 0) ;the movable ring right rod
ScaleEntity(6,3,0.2,0.2)

CreateEntity(7, MeshID(1),MaterialID(3),  0, 2.5, -1.0) ;the rod attached to movable ring
ScaleEntity(7, 2, 0.2, 0.2) :RotateEntity(7, 0,90,0)
AddSubEntity(Comp2, 5, #PB_Entity_BoxBody) ; in this way we make the Ring as having movable concave geometry
AddSubEntity(Comp2, 6, #PB_Entity_BoxBody)
AddSubEntity(Comp2, 7, #PB_Entity_BoxBody) ; add the rod
CreateEntityBody(Comp2, #PB_Entity_CompoundBody, 1, 0.5, 0.5) ;the compound made from the ring and the rod
  ;-----------------------------------------
  ; static ring 
CreateTube(30, 0.5, 0.4, 1)
CreateEntity(30, MeshID(30), MaterialID(4),0,2.5,-1.9)
RotateEntity(30,90,0,0)
CreateEntityBody(30, #PB_Entity_StaticBody, 1, 0.5, 0.5)
ScaleEntity(30,0.7,0.7,0.7)

;tube with sphere inside
CreateEntity(40, MeshID(30), MaterialID(4),0,4.0,-7.5)
RotateEntity(40,110,0,0)
CreateEntityBody(40, #PB_Entity_StaticBody, 1, 0.5, 0.5)
ScaleEntity(40,1.5, 10.0 ,1.5)


CreateSphere(#sphere, 0.5)
CreateEntity(#sphere, MeshID(#sphere),MaterialID(5), 0,3.0,-3.5) 
CreateEntityBody(#sphere, #PB_Entity_ConvexHullBody, 1, 0.5, 0.5)

  
;-----------------------------------------
    ;Ground
    ;
    Ground = CreateEntity(#PB_Any, MeshID(1), MaterialID(2), 0, 0, 0)
    ScaleEntity(Ground, 40, 0.4, 40)
    CreateEntityBody(Ground, #PB_Entity_StaticBody)
    
    ;sliding joint for the movable ring
    GenericJoint(2,EntityID(Ground), 0, 2, 0,EntityID(Comp2), 0, 2, 0)
    SetJointAttribute(2, #PB_Joint_EnableSpring, #True, 2)
    SetJointAttribute(2, #PB_Joint_Stiffness, 150, 2)
    SetJointAttribute(2, #PB_Joint_Damping, 0.0, 2)
    
    CreateCube(#baseCube, 1)
    CreateEntity(#baseCube, MeshID(#baseCube),  MaterialID(4), 0, 0, 0)
    CreateEntityBody(#baseCube, #PB_Entity_StaticBody)
    ; joint for the wheel
       
    HingeJoint(#Joint, EntityID(Compound),
             0, 1, 0,
             0, 1, 0, 
             EntityID(#baseCube),
             0, 1, 0, 
             0, 1, 0)
    
     
    EnableHingeJointAngularMotor(#Joint,0,0.0,10) ; speed zero
    SetEntityAttribute(Comp2, #PB_Entity_LinearSleeping, 0)
        
     ; camera
    CreateCamera(0, 0, 0, 100, 100, #True)
    MoveCamera(0,6,6,-2, #PB_Absolute)
    CameraLookAt(0,0,2,-2)
    
    ; GUI
    OpenWindow3D(0, 0, 0, 50 , 10 , "")
    HideWindow3D(0,1)
    ShowGUI(128, 0) ; Display the GUI, semi-transparent and don't display the mouse cursor
    ;SetWindowTitle(0, ".'space':to operate/stop the machine. .... arrows and mouse for the Camera")
 
    Repeat
      Screen3DEvents()
     
      If ExamineMouse()
        MouseX = -MouseDeltaX() * #CameraSpeed * 0.1
        MouseY = -MouseDeltaY() * #CameraSpeed * 0.1
        InputEvent3D(MouseX(), MouseY(),0)
        BodyPick(CameraID(0), MouseButton(#PB_MouseButton_Left), MouseX(), MouseY(), 1)
      EndIf
    
      If ExamineKeyboard()
        
                 
    If KeyboardReleased(#PB_Key_Space); slow motor rotation
      force ! 1
      EnableHingeJointAngularMotor(#Joint,#True,force*13,30)
    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()
      
      FlipBuffers()
      
      
    Until KeyboardPushed(#PB_Key_Escape) Or Quit = 1
    
  EndIf
  
Else
  MessageRequester("Error", "The 3D Engine can't be initialized",0)
EndIf

End

Re: Rotational to Linear movement

Posted: Mon Aug 29, 2016 3:40 pm
by box_80
Good show of how the physics engine can be used. :D

Re: Rotational to Linear movement

Posted: Mon Aug 29, 2016 8:24 pm
by applePi
Thank you box_80, the following is using CRANK AND SLIDER MECHANISM
http://www.technologystudent.com/cams/crkslid1.htm
it is harder than the above examples and more prone to errors
there is 2 hidden spheres to fix the rings in its place else it will fly, the machine serve as rotary to linear and as linear to rotary like in car combustion engine
Image
in fact i want vertical machine so when we drop heavy balls over the piston (just in time) to simulate combustion engine cylinder half cycle, in the second half cycle a heavy flywheel will do the job, but find it needs another design different than the horizontal machine, so hope for another time.
EDIT:sometimes the engine destroyed, in this case reduce the speed to 6 in line 152 ApplyEntityTorque(WheelCompound, 0, force*7, 0)
or make balances between the speed and the Restitution, Friction in line 65
CreateEntityBody(WheelCompound, #PB_Entity_CompoundBody, 1, 2, 0.1)

i have found that if we speed = 10 (line 152 listed above) and full screen = 800X600X32@60 with debugger On and we move the camera more with keys and mouse the engine is prone more to damage (ie the Ring fly away), but with debugger OFF until now i have not get damage, so i guess a powerfull cpu is better than poor cpu in this case.
press Space to operate/stop the engine. needs PB v5.50

Code: Select all

IncludeFile #PB_Compiler_Home + "examples/3d/Screen3DRequester.pb"

#CameraSpeed = 0.3
Enumeration
  #sphere = 200
  #sphere2
EndEnumeration


Define.f KeyX, KeyY, MouseX, MouseY

If InitEngine3D()
  
  InitSprite()
  InitKeyboard()
  InitMouse()
  
  If Screen3DRequester()
    
    Add3DArchive(#PB_Compiler_Home + "examples/3d/Data/"              , #PB_3DArchive_FileSystem)
    Add3DArchive(#PB_Compiler_Home + "examples/3d/Data/Textures"  , #PB_3DArchive_FileSystem)
    Add3DArchive(#PB_Compiler_Home + "examples/3d/Data/Scripts"    , #PB_3DArchive_FileSystem)
    Add3DArchive(#PB_Compiler_Home + "examples/3d/Data/GUI"        , #PB_3DArchive_FileSystem)
    Parse3DScripts()
    
    
    ;WorldDebug(#PB_World_DebugBody)
        
    ;-------------------------------
    ; create  material
    CreateMaterial(1, LoadTexture(1, "Tire_tex.png"))
    SetMaterialColor(1, #PB_Material_AmbientColor, #PB_Material_AmbientColors)
    ; 
    CreateMaterial(2, LoadTexture(2, "snow_1024.jpg"))
    SetMaterialColor(2, #PB_Material_AmbientColor, #PB_Material_AmbientColors)
    
    CreateMaterial(3, LoadTexture(3, "ground_diffuse.png"))
    SetMaterialColor(3, #PB_Material_AmbientColor, #PB_Material_AmbientColors)
    
    CreateMaterial(4, LoadTexture(4, "Wood.jpg"))
    MaterialBlendingMode(4, #PB_Material_AlphaBlend)
    SetMaterialColor(4, #PB_Material_DiffuseColor, RGBA(255, 255, 0, 150))
    
    CreateMaterial(6, LoadTexture(6, "Wood.jpg"))
    
    CreateMaterial(5, LoadTexture(5, "Geebee2.bmp"))
    
    ;-------------------------------

wheel = CreateCylinder(#PB_Any, 1, 0.5)
crankWheel = CreateEntity(#PB_Any, MeshID(wheel),MaterialID(1),  0, 2, 0) ; the wheel
Handle = CreateEntity(#PB_Any, MeshID(wheel),MaterialID(6),  0, 2.5, 0.5) ; the rod on the wheel
ScaleEntity(Handle, 0.15, 3, 0.15)

CreateCube(1, 1) ; for the the ground, and the rod attached to the movable ring

Global WheelCompound = CreateEntity(#PB_Any,0,0)
CreateSphere(#sphere,0.5)
sphere = CreateEntity(#PB_Any, MeshID(#sphere),MaterialID(1), 0, 3.5, 0.5) 
ScaleEntity(sphere,1,0.5,1): HideEntity(sphere,1)

AddSubEntity(WheelCompound, crankWheel, #PB_Entity_CylinderBody) ;; the wheel 
AddSubEntity(WheelCompound, Handle, #PB_Entity_CylinderBody) ; the rod on the wheel 
AddSubEntity(WheelCompound, sphere, #PB_Entity_SphereBody)
CreateEntityBody(WheelCompound, #PB_Entity_CompoundBody, 1, 2, 0.1) ;the compound from the two


Global crankArm = CreateEntity(#PB_Any,0,0) ; for the crank compound
CreateTube(5, 1, 0.4, 1.5)
CreateEntity(5, MeshID(5), MaterialID(3),0,2.5,0.5) ; the big Ring
ScaleEntity(5, 0.5,0.5,0.5)

CreateEntity(6, MeshID(1),MaterialID(3),  0, 2.5, -1.0) ;the rod attached to  ring
ScaleEntity(6,2.5,0.2,0.2) :RotateEntity(6, 0,90,0)
;from entity 10 to 13 : the 4 rods make the secomd swuare joint
CreateEntity(10, MeshID(1),MaterialID(3),  0.05, 2.5, -2.3) ;the movable ring left rod
ScaleEntity(10,0.7,0.2,0.2)                           
CreateEntity(11, MeshID(1),MaterialID(3),  0.05, 2.5, -2.8) ;the movable ring right rod
ScaleEntity(11,0.7,0.2,0.2)
CreateEntity(12, MeshID(1),MaterialID(3),  0.3, 2.5, -2.6) ;
ScaleEntity(12,0.5,0.2,0.2): RotateEntity(12,0,90,0)
CreateEntity(13, MeshID(1),MaterialID(3),  -0.2, 2.5, -2.6) ;
ScaleEntity(13,0.5,0.2,0.2): RotateEntity(13,0,90,0)

AddSubEntity(crankArm, 5, #PB_Entity_StaticBody) ; in this way we make the Ring as having movable concave geometry
AddSubEntity(crankArm, 6, #PB_Entity_BoxBody) ; add the rod
AddSubEntity(crankArm, 10, #PB_Entity_BoxBody)
AddSubEntity(crankArm, 11, #PB_Entity_BoxBody)
AddSubEntity(crankArm, 12, #PB_Entity_BoxBody)
AddSubEntity(crankArm, 13, #PB_Entity_BoxBody)
CreateEntityBody(crankArm, #PB_Entity_CompoundBody, 1, 0.5, 0.5) ;the compound made from the ring and the rod
  ;-----------------------------------------
;-----------------------------------------
    ;Ground
    ;
    Ground = CreateEntity(#PB_Any, MeshID(1), MaterialID(2), 0, 0, 0)
    ScaleEntity(Ground, 40, 0.4, 40)
    ;RotateEntity(Ground, 0,0,0)
    CreateEntityBody(Ground, #PB_Entity_StaticBody)
    ;---------------------------------------- 
    
piston = CreateCylinder(#PB_Any, 3, 0.3)
piston = CreateEntity(#PB_Any, MeshID(piston),MaterialID(1),  0, 2.5, -4) ; piston
RotateEntity(piston,90,0,0)
ScaleEntity(piston, 0.15, 5, 0.15) 
CreateEntity(8, MeshID(1),MaterialID(3),  0, 2.7, -3.2) ;the arm
ScaleEntity(8,1.5,0.2,0.2) :RotateEntity(8, 0,90,0)
CopyEntity(Handle, 9) ; 9 -> Handle2
MoveEntity(9,0,2.5,-2.5,#PB_Absolute)
CopyEntity(sphere,#sphere2)
MoveEntity(#sphere2,0,2.8,-2.5,#PB_Absolute) 
HideEntity(#sphere2,1)
Global slider = CreateEntity(#PB_Any,0,0)    

AddSubEntity(slider, 8, #PB_Entity_BoxBody)
AddSubEntity(slider, 9, #PB_Entity_CylinderBody)
AddSubEntity(slider, #sphere2, #PB_Entity_CylinderBody)
AddSubEntity(slider, piston, #PB_Entity_CylinderBody)
CreateEntityBody(slider, #PB_Entity_CompoundBody, 1, 0.5, 0.5)

;sliding joint for the slider (including the piston)
GenericJoint(3,EntityID(Ground), 0, 2, 3,EntityID(slider), 0, 2, 3)
SetJointAttribute(3, #PB_Joint_EnableSpring, #True, 2)
SetJointAttribute(3, #PB_Joint_Stiffness, 5, 2)
SetJointAttribute(3, #PB_Joint_Damping, 1, 2)  
;    
; joint for the wheel
GenericJoint(4,EntityID(Ground), 0, 2, 0,EntityID(WheelCompound), 0, 2, 0)
    
;the following will make the wheel stop if we stop the angular force
SetJointAttribute(4, #PB_Joint_EnableSpring, #True, 4)
SetJointAttribute(4, #PB_Joint_Stiffness, 5, 4)
SetJointAttribute(4, #PB_Joint_Damping, 0.2, 4)
    
 ;this is essenyial to stabilize the wheel
EntityAngularFactor(WheelCompound, 0, 1, 0)
        
    
    ; camera
    CreateCamera(0, 0, 0, 100, 100, #True)
    MoveCamera(0,4,7,3, #PB_Absolute)
    CameraLookAt(0,0,2,0)
    
    ; GUI
    OpenWindow3D(0, 0, 0, 50 , 10 , "")
    HideWindow3D(0,1)
    ShowGUI(128, 0) ; Display the GUI, semi-transparent and display the mouse cursor
    ;SetWindowTitle(0, ".'space':to operate/stop the machine. .... arrows and mouse for the Camera")
   Repeat
      Screen3DEvents()
      
      ApplyEntityTorque(WheelCompound, 0, force*7, 0)
      
      If ExamineMouse()
        MouseX = -MouseDeltaX() * #CameraSpeed * 0.1
        MouseY = -MouseDeltaY() * #CameraSpeed * 0.1
        InputEvent3D(MouseX(), MouseY(),0)
        BodyPick(CameraID(0), MouseButton(#PB_MouseButton_Left), MouseX(), MouseY(), 1)
      EndIf
    
      If ExamineKeyboard()
        
                 
    If KeyboardReleased(#PB_Key_Space); throw cubes over the gear
      force ! 1
    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
        If KeyboardPushed(#PB_Key_V)
          ApplyEntityImpulse(slider, 0, 0, 1)

          
        EndIf
        
        
      EndIf
      
      MoveCamera  (0, KeyX, 0, KeyY)
      RotateCamera(0,  MouseY, MouseX, 0, #PB_Relative)
      
      RenderWorld()
      
      FlipBuffers()
      
      
    Until KeyboardPushed(#PB_Key_Escape) Or Quit = 1
    
  EndIf
  
Else
  MessageRequester("Error", "The 3D Engine can't be initialized",0)
EndIf

End