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

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
EndCode: 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



