Gears
Posted: Tue Jun 07, 2016 1:46 pm
Gears, using the latest PB 5.50 beta 1. with the new type #PB_Entity_CompoundBody for the CreateEntityBody function which make us able to design and simulate mechanical machines. this is just a show case , a more precise movements is possible
press Z/X to apply torque force to the left gear. if gears teeth stuck give some push to the right gear by pressing Space
some notes after the code
Note: it seems the Damping (#PB_Joint_Damping) is active if we use it with #PB_Joint_EnableSpring for the Generic Joint, and not to the rotational objects with generic Joints. disable the body and joint of the right gear and uncomment line 85:
SetJointAttribute(4, #PB_Joint_Damping, 0.001, 5)
it does not have any effect on damping the left Gear rotation, it will keep running for ever
press Z/X to apply torque force to the left gear. if gears teeth stuck give some push to the right gear by pressing Space
some notes after the code
Code: Select all
;
; ------------------------------------------------------------
;
; PureBasic - CompoundBody
;
; (c) Fantaisie Software
;
; ------------------------------------------------------------
;
IncludeFile #PB_Compiler_Home + "examples/3d/Screen3DRequester.pb"
#CameraSpeed = 1
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/Models" , #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, "wood.jpg"))
SetMaterialColor(1, #PB_Material_AmbientColor, #PB_Material_AmbientColors)
;
CreateMaterial(2, LoadTexture(2, "dirt.jpg"))
SetMaterialColor(2, #PB_Material_AmbientColor, #PB_Material_AmbientColors)
CreateMaterial(3, LoadTexture(3, "clouds.jpg"))
SetMaterialColor(3, #PB_Material_AmbientColor, #PB_Material_AmbientColors)
CreateMaterial(4, LoadTexture(4, "r2skin.jpg"))
;-------------------------------
CreateCube(1, 2)
CreateSphere(3, 1)
CreateEntity(1, MeshID(1),MaterialID(1), 0, 0, 0)
CreateEntity(2, MeshID(1),MaterialID(1), 0, 0, 0.1)
RotateEntity(2, 0,0,45, #PB_Absolute)
Global Compound = CreateEntity(#PB_Any,0,0)
AddSubEntity(Compound, 1, #PB_Entity_BoxBody)
AddSubEntity(Compound, 2, #PB_Entity_BoxBody)
DisableDebugger
CreateEntityBody(Compound, #PB_Entity_CompoundBody, 1, 0.4, 0.5)
EnableDebugger
;-------------------------------
CreateEntity(3, MeshID(1),MaterialID(3), 0, 0, 0)
CreateEntity(4, MeshID(1),MaterialID(3), 0, 0, 0)
CreateEntity(5, MeshID(1),MaterialID(3), 0, 0, 0.1)
RotateEntity(4, 0,0,30, #PB_Absolute)
RotateEntity(5, 0,0,60, #PB_Absolute)
Comp2 = CreateEntity(#PB_Any,0,0)
AddSubEntity(Comp2, 3, #PB_Entity_BoxBody)
AddSubEntity(Comp2, 4, #PB_Entity_BoxBody)
AddSubEntity(Comp2, 5, #PB_Entity_BoxBody)
DisableDebugger
CreateEntityBody(Comp2, #PB_Entity_CompoundBody, 1, 0.4, 0.4)
EnableDebugger
;Ground
;
Ground = CreateEntity(#PB_Any, MeshID(1), MaterialID(2), 0, -7, 0)
ScaleEntity(Ground, 40, 0.4, 40)
CreateEntityBody(Ground, #PB_Entity_StaticBody)
GenericJoint(4,EntityID(Ground), 0, 4, 0,EntityID(Compound), 0, 0, 0)
SetJointAttribute(4, #PB_Joint_NoLimit, 0, 5)
;SetJointAttribute(4, #PB_Joint_LowerLimit, 1, 5)
;SetJointAttribute(4, #PB_Joint_UpperLimit, 200, 5)
;SetJointAttribute(4, #PB_Joint_Damping, 0.001, 5)
GenericJoint(5,EntityID(Ground), 2.6, 4, 0,EntityID(Comp2), 0, 0, 0)
SetJointAttribute(5, #PB_Joint_NoLimit, 0, 5)
; camera
CreateCamera(0, 0, 0, 100, 100, #True)
MoveCamera(0,1.5,-2,10, #PB_Absolute)
; 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, "PureBasic-3D Demos .... Press Z/X to apply torque to the left Gear")
Repeat
Screen3DEvents()
If ExamineMouse()
MouseX = -MouseDeltaX() * #CameraSpeed * 0.05
MouseY = -MouseDeltaY() * #CameraSpeed * 0.05
InputEvent3D(MouseX(), MouseY(),0)
BodyPick(CameraID(0), MouseButton(#PB_MouseButton_Left), MouseX(), MouseY(), 1)
EndIf
If ExamineKeyboard()
If KeyboardPushed(#PB_Key_Z)
ApplyEntityTorque(Compound, 0, 0, 25)
ElseIf KeyboardPushed(#PB_Key_X)
ApplyEntityTorque(Compound, 0, 0, -25)
ElseIf KeyboardPushed(#PB_Key_Space) ; in case gears teeth stuck
ApplyEntityImpulse(comp2, -0.2, 0, 0, 0, 10,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
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
SetJointAttribute(4, #PB_Joint_Damping, 0.001, 5)
it does not have any effect on damping the left Gear rotation, it will keep running for ever
