some mechanical devices

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

some mechanical devices

Post by applePi »

first example have these ingredients:
1- wheel with a motor
2- the wheel have a hand attached to it by slider joint (with move freedom = zero so it is rigid and stable) and the hand allowed to go inside the wheel body with SetEntityCollisionFilter
3- another slider joint near the wheel
4- a spring near the the last slider joint
operations manual on the title bar
Image

Code: Select all

Enumeration
#material = 10
#Joint
#axisCube
#Hand
#Joint2
#Joint3
#Joint4
EndEnumeration
#CameraSpeed = 0.1

InitEngine3D()
InitSprite()
InitKeyboard()
InitMouse()
;WorldDebug(#PB_World_DebugEntity)
;WorldDebug(#PB_World_DebugBody)
ExamineDesktops()

OpenWindow(0,0,0,DesktopWidth(0), DesktopHeight(0)," T: Energize the the motor...V: slower motor speed.....Mouse + arrows: for the Camera",#PB_Window_ScreenCentered)
OpenWindowedScreen(WindowID(0),0,0,DesktopWidth(0), DesktopHeight(0),1,0,0,#PB_Screen_WaitSynchronization)

Add3DArchive(".", #PB_3DArchive_FileSystem)
Add3DArchive(#PB_Compiler_Home + "examples/3d/Data/Textures", #PB_3DArchive_FileSystem)


Add3DArchive(#PB_Compiler_Home + "examples/3d/Data/Packs/desert.zip", #PB_3DArchive_Zip)
Add3DArchive(#PB_Compiler_Home + "examples/3d/Data/Scripts", #PB_3DArchive_FileSystem)
Parse3DScripts()

 ;-------------  Materials  -----------------------------------------
  CreateMaterial(0, LoadTexture(0, "Dirt.jpg"))
  CreateMaterial(1, LoadTexture(1, "wood.jpg"))
  CreateMaterial(3, LoadTexture(3, "ground_diffuse.png"))
  CreateMaterial(2, LoadTexture(2, "Geebee2.bmp"))
  ;MaterialShadingMode(1, #PB_Material_Wireframe     )
 ;--------------  Camera  -----------------------------------------
  #camera =0
  CreateCamera(#Camera, 0, 0, 100, 100)
  MoveCamera(#camera, 0.0254, 3.518, -4.5366, #PB_Absolute)
  CameraLookAt(0, -1.5, 2, 0)


;- -------------  Light  ---------------------------------------------
  CreateLight(0, $FFFFFF, 1560, 900, 500)
  AmbientColor(RGB(255,255,255))
;- -------------  World  -------------------------------------------
  WorldGravity(-9)
  SkyBox("desert07.jpg")
  ;----------------- the big wheel-----------------------------------------
  
  CreateCylinder(0, 1, 0.2)
  CreateEntity(0, MeshID(0), MaterialID(1), 0, 2, 0)
    
  ;---------------- Hand on the wheel -----------------------------------
  CreateEntityBody(0, #PB_Entity_CylinderBody, 1, 0.1,1)
  CreateCylinder(#Hand,0.4, 0.2)
  ;TransformMesh(#Mesh, x, y, z, ScaleX, ScaleY, ScaleZ, RotateX, RotateY, RotateZ [, SubMesh])
  TransformMesh(#Hand,0,0,0,  0.2, 5, 0.2, 0,0,0)
  CreateEntity(#Hand, MeshID(#Hand), MaterialID(0), 0, 2, 0)
  ;CreateEntityBody(#Hand, #PB_Entity_CylinderBody, 1, 0.1,1)
  ;----------------- CollisionFilter between the wheel and its hand -----------------------------
  ;SetEntityCollisionFilter(#Entity, CollisionGroup, CollisionMask)
  SetEntityCollisionFilter(0      , 4 , 2 ) 
  SetEntityCollisionFilter(#Hand , 4 , 2 )
  
  CreateEntityBody(#Hand, #PB_Entity_ConvexHullBody, 1, 0.1,1)
  
  
  CreateCube(#axisCube, 0.5)
  CreateEntity(#axisCube, MeshID(#axisCube), MaterialID(0) , 0, 0, 0)
  CreateEntityBody(#axisCube, #PB_Entity_StaticBody)
 
  ;------------------joints  ---------------------------------------------
 ;HingeJoint(#Joint, EntityID, PivotX, PivotY, PivotZ, AxisX, AxisY, AxisZ, EntityID2, PivotX2, PivotY2, PivotZ2, AxisX2, AxisY2, AxisZ2)
 ;SetJointAttribute(0, #PB_HingeJoint_LowerLimit, 0) 
 ; the joint between the wheel EntityID(0) And the static cube on the ground EntityID(#axisCube)
  HingeJoint(#Joint, EntityID(0),
             0, 3, 0,
             0, 1, 0, 
             EntityID(#axisCube),
             -0.5, 0, 0, 
             1, 0, 0)
  
  HingeJoint(#Joint, EntityID(0),
             0, -1, 0,
             0, 1, 0, 
             EntityID(#axisCube),
             0, 1, 0, 
             0, 1, 0)
  ;;========================================================
  
    SliderJoint(#Joint3, EntityID(0), -0.3, 0, 0, EntityID(#Hand),0.5, 0, 0)
    SetJointAttribute(#Joint3, #PB_SliderJoint_LowerLimit, 0)  
    SetJointAttribute(#Joint3, #PB_SliderJoint_UpperLimit,  0) 
 ;----------------- not sleep  ---------------------------------------
  SetEntityAttribute(0, #PB_Entity_LinearSleeping, 0)

  ;-------------   ----------------------------------------------
  CreateCube(3, 1) ; for the body of the slider

  CreateEntity(6, MeshID(3), MaterialID(2) , -1.2, 2.4, 1.0) ; the moving slider
  ScaleEntity(6, 1, 0.2, 0.2)
  CreateEntityBody(6, #PB_Entity_BoxBody, 1)
    
  CreateEntity(7, MeshID(3), MaterialID(0) , -1.3, 2.4, 1.5) ; fixed 
  ScaleEntity(7, 1, 0.1, 0.1)
  CreateEntityBody(7, #PB_Entity_StaticBody, 1)
  
  SliderJoint(#Joint4, EntityID(7), 0, 0, -1, EntityID(6),0, 0, 1)
  SetJointAttribute(#Joint4, #PB_SliderJoint_LowerLimit, -1)  
  SetJointAttribute(#Joint4, #PB_SliderJoint_UpperLimit,  0) 
  
  
Define.f KeyX, KeyY, MouseX, MouseY
CreateMaterial(#material, LoadTexture(#material, "MRAMOR6X6.jpg"))
CreatePlane(500, 30, 30, 5, 5, 2, 2)
CreateEntity(500, MeshID(500), MaterialID(#material), 0,-2.5,0.0)
CreateEntityBody(500, #PB_Entity_StaticBody, 1, 0.1, 1)


EnableHingeJointAngularMotor(#Joint,0,0.0,10)

CreateCube(50,0.1) ; static body connected with Hinge joint with 51
    CreateEntity(50, MeshID(50), MaterialID(1), -2.35, 2.7, -0.5)
    ScaleEntity(50, 3,3,2.5)
    CreateEntity(51, MeshID(50), MaterialID(3), -5,0, 0) ; spring
    ScaleEntity(51, 8,0.5,8)
    
    RotateEntity(50, 0,0,-90)
    CreateEntityBody(50, #PB_Entity_StaticBody, 90, 0.1,10)
    CreateEntityBody(51, #PB_Entity_BoxBody, 1, 0.1, 1)  
        
    SetEntityCollisionFilter(6       , 5, 3 ) ; (the Hammer) ;slider
    SetEntityCollisionFilter(51      , 5 , 3 ) ; spring block
    SetEntityCollisionFilter(50       , 4, 3 ) ; static body connected with Hinge joint with 51 
    
        
    ;HingeJoint(#Joint, EntityID, PivotX, PivotY, PivotZ, AxisX, AxisY, AxisZ, EntityID2, PivotX2, PivotY2, PivotZ2, AxisX2, AxisY2, AxisZ2)
    ;joints 60 and 61 just to fix entity 50 and entity 51 together 
    HingeJoint(60, EntityID(50), ;entity 50 are Static cube
               0, 0.5, 0,
               0, 0, 1, 
               EntityID(51),     ; entity 51 is the spring
               0, 0, 0, 
               0, 0, 1)
     HingeJoint(61, EntityID(50),
               -0.05, 0.5, 0,
               0, 0, 1, 
               EntityID(51),
               0.05, 0, 0, 
               0, 0, 1) 

     
  
Repeat
 Repeat
      Event = WindowEvent()
     Until Event = 0
  
  If ExamineMouse()
        MouseX = -MouseDeltaX() * #CameraSpeed * 0.2
        MouseY = -MouseDeltaY() * #CameraSpeed * 0.2
      EndIf
  ShowCursor_(0)
    
  ExamineKeyboard()
  
        ; Use arrow keys and mouse to rotate camera and fly in/out
        If KeyboardPushed(#PB_Key_Left)
          KeyX = -#CameraSpeed/2
        ElseIf KeyboardPushed(#PB_Key_Right)
          KeyX = #CameraSpeed/2
        Else
          KeyX = 0
        EndIf
        
        If KeyboardPushed(#PB_Key_Up)
          KeyY = -#CameraSpeed/2
        ElseIf KeyboardPushed(#PB_Key_Down)
          KeyY = #CameraSpeed/2
        Else
          KeyY = 0
        EndIf

If KeyboardPushed(#PB_Key_T) ; motor rotation
  
  EnableHingeJointAngularMotor(#Joint,1,3,10)
  
EndIf 

  
  If KeyboardReleased(#PB_Key_V) ; slower motor rotation
    
    EnableHingeJointAngularMotor(#Joint,1,0.5,10)
    
  EndIf
    
  If KeyboardReleased(#PB_Key_C) ; check camera position
    Debug CameraX(0): Debug CameraY(0): Debug CameraZ(0)
  EndIf
      
  RotateCamera(0, MouseY, MouseX, 0, #PB_Relative)
  MoveCamera  (0, KeyX, 0, KeyY)
  
  RenderWorld()
  
  FlipBuffers()

Until KeyboardPushed(#PB_Key_Escape) Or WindowEvent() = #PB_Event_CloseWindow
this is a simple spring, less complex than the one i have posted before. i think it can be used in a car wheels simulation, the spring behavior is the result of attaching an object with two Hinge joints which are close enough .
the first joint is at the center of springy block, the second joint at the left by -2
do this experiment:
change the second joint positions (they are relative to the center of objects)

Code: Select all

HingeJoint(1, EntityID(0),
               -2, 5, 0,
               0, 0, 1, 
               EntityID(1),
               -2, 0, 0, 
               0, 0, 1) 
to this position

Code: Select all

HingeJoint(1, EntityID(0),
               -0.1, 5, 0,
               0, 0, 1, 
               EntityID(1),
               -0.1, 0, 0, 
               0, 0, 1) 
then the spring will be very soft spring and will swing back and forth for several seconds. this is because the second joint are too close to the first one. change -0.1 to -6 and the spring suitable to measure tons.
Image

Code: Select all

Enumeration
   
   #LIGHT
   #CAMERA
   #mainwin
   #Plane
   #Joint
     
EndEnumeration


Define.f KeyX, KeyY, MouseX, MouseY

ExamineDesktops()
If OpenWindow(0, 0, 0, DesktopWidth(0), DesktopHeight(0), "press Space...to drop a cube, .. use mouse + Arrows to rotate + move Camera", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)

If InitEngine3D()
  
  ;Add3DArchive(".", #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)
    
  Parse3DScripts()
  
  InitSprite()
  InitKeyboard()
  InitMouse()
  OpenWindowedScreen(WindowID(0), 0, 0, DesktopWidth(0), DesktopHeight(0), 0, 0, 0)
    
  
    CreateMaterial(0, LoadTexture(0, "White.jpg"))
    DisableMaterialLighting(0, #True)
    MaterialShadingMode(0, #PB_Material_Wireframe     )
    MaterialCullingMode(0, #PB_Material_NoCulling)
            
    CreateCamera(#camera, 0, 0, 100, 100)
    MoveCamera(#camera, 0, 10, 20, #PB_Absolute)
    ;CameraFOV(#camera, 70)
    CameraBackColor(#camera, RGB(255,200,200))
    CameraLookAt(#camera,0,3,0)
        
    CreateLight(0, RGB(255,255,255), 0, 20, 30)
    AmbientColor(RGB(200, 200, 200))
    
    CreateMaterial(3, LoadTexture(3, "snow_1024.jpg"))
    DisableMaterialLighting(3, #False)
    SetMaterialColor(3, #PB_Material_AmbientColor, RGB(250, 255, 0))
    SetMaterialColor(3, #PB_Material_SpecularColor, RGB(255, 255, 0))
    
    CreateMaterial(1, LoadTexture(1, "MRAMOR6X6.jpg"))
    MaterialCullingMode(1, #PB_Material_NoCulling)

    CreateMaterial(2, LoadTexture(2, "RustySteel.jpg"))
    MaterialCullingMode(2, #PB_Material_NoCulling)
    
    CreateMaterial(4, LoadTexture(4, "ground_diffuse.png"))
    MaterialCullingMode(4, #PB_Material_NoCulling)
        
    CreatePlane(500, 200, 200, 5, 5, 2, 2) ; the ground
    CreateEntity(500, MeshID(500), MaterialID(4), 0,-15,0)
    CreateEntityBody(500, #PB_Entity_StaticBody, 1, 0.1, 1)
    
    CreateCube(0,1)
    CreateEntity(0, MeshID(0), MaterialID(1), 0, 0, 0) ; the static cube
    ScaleEntity(0, 3,3,3)
    CreateEntity(1, MeshID(0), MaterialID(3), 0,5, 0) ; the spring
    ScaleEntity(1, 12,0.5,12)
    
    CreateEntityBody(0, #PB_Entity_StaticBody, 90, 0.1,10)
    CreateEntityBody(1, #PB_Entity_BoxBody, 1, 0.1, 1)  
        
    ;joints 0 and 1 just to fix ent 0 and ent 1 together ;if the 2 joints are close they will show spring behaviour
    HingeJoint(0, EntityID(0),
               0, 5, 0,
               0, 0, 1, 
               EntityID(1),
               0, 0, 0, 
               0, 0, 1)
     HingeJoint(1, EntityID(0),
               -2, 5, 0,
               0, 0, 1, 
               EntityID(1),
               -2, 0, 0, 
               0, 0, 1) 
  
  
;--------------  not sleep  ---------------------------------------
  SetEntityAttribute(0, #PB_Entity_LinearSleeping, 0)
  SetEntityAttribute(1, #PB_Entity_LinearSleeping, 0)  

  ;WorldGravity(-9)
                   
    Repeat
     Repeat
      Event = WindowEvent()
     Until Event = 0
        
      If ExamineMouse()
        MouseX = -MouseDeltaX()/20 
        MouseY = -MouseDeltaY()/20
      EndIf
      
          
      If ExamineKeyboard()
         
        If KeyboardPushed(#PB_Key_Left)
          KeyX = -1
        ElseIf KeyboardPushed(#PB_Key_Right)
          KeyX = 1
        Else
          KeyX = 0
        EndIf
        
        If KeyboardPushed(#PB_Key_Up)
          KeyY = -1
        ElseIf KeyboardPushed(#PB_Key_Down)
          KeyY = 1
        Else
          KeyY = 0
        EndIf
        
        If KeyboardReleased(#PB_Key_Space)
          stone = CreateCube(#PB_Any , 1)
          stone = CreateEntity(#PB_Any, MeshID(stone),MaterialID(2))
          ScaleEntity(stone,2,0.7,2)
          MoveEntity(stone, 4,8, 0)
          CreateEntityBody(stone,#PB_Entity_BoxBody,20)
      
       EndIf
  
       EndIf
       
        
      RotateCamera(#Camera, MouseY, MouseX, 0, #PB_Relative)
      MoveCamera(#Camera, KeyX, 0, KeyY)
      
      RenderWorld()
      FlipBuffers()
      
    Until KeyboardPushed(#PB_Key_Escape) Or Quit = 1
  EndIf
  
Else
  MessageRequester("Error", "The 3D Engine can't be initialized", 0)
EndIf

End


Last edited by applePi on Tue Apr 12, 2016 1:39 pm, edited 4 times in total.
IdeasVacuum
Always Here
Always Here
Posts: 6426
Joined: Fri Oct 23, 2009 2:33 am
Location: Wales, UK
Contact:

Re: some mechanical devices

Post by IdeasVacuum »

operations manual on the title bar
Brilliant! :mrgreen:
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
box_80
Enthusiast
Enthusiast
Posts: 117
Joined: Mon Sep 03, 2012 8:52 pm

Re: some mechanical devices

Post by box_80 »

Nice example with Entity Collision Filter. I was wondering on the different ways it could be used.
applePi
Addict
Addict
Posts: 1404
Joined: Sun Jun 25, 2006 7:28 pm

Re: some mechanical devices

Post by applePi »

Entity Collision Filter and collision Mask
Thank you IdeasVacuum and box_80
i have found a short description by drleviathan here http://bulletphysics.org/Bullet/phpBB3/ ... hp?t=10551 about this subject
in concise my interpretation:
we want to set SetEntityCollisionFilter for cube 1 and cube 2:
we call G: group, B: bit mask
SetEntityCollisionFilter( cube1, G1, B1)
SetEntityCollisionFilter( cube2, G2, B2)
if B1 AND G2 > 0 Then
there is collision
else
cube1 will pass through cube2
from a few tests it seems also the condition B2 AND G1 > 0 should be fulfilled


these numbers will cause collision between all
SetEntityCollisionFilter(#plane , 3 , 2 ) ; binary 11, 10
SetEntityCollisionFilter(#cube1 , 7, 1 ) ; binary 111, 1
SetEntityCollisionFilter(#cube2 , 7 , 2 ) ; binary 111, 10
SetEntityCollisionFilter(#cylinder , 3 , 2 ) ; binary 11, 10

since every (Bit Mask) AND (any Object group) > 0

exercise: allow cube2 to collide with all the objects but not with the ground (the plane) ?
answer: change plane filter to 5, 2 since:
#plane Group AND cube2 bitMask =
5 AND 2 = 0
it is something like a puzzle solution.
thanks in advance for any other contributions in this subject.
test these idea with this code (use z,x to push the cubes)

Code: Select all

Enumeration
#material
#camera 
#plane
#cube1
#cube2
#cylinder

EndEnumeration
#CameraSpeed = 0.1

InitEngine3D()
InitSprite()
InitKeyboard()
InitMouse()
;WorldDebug(#PB_World_DebugEntity)
;WorldDebug(#PB_World_DebugBody)
ExamineDesktops()

OpenWindow(0,0,0,DesktopWidth(0), DesktopHeight(0)," Z: move left box....X: move right Box.....Mouse + arrows: for the Camera",#PB_Window_ScreenCentered)
OpenWindowedScreen(WindowID(0),0,0,DesktopWidth(0), DesktopHeight(0),1,0,0,#PB_Screen_WaitSynchronization)

Add3DArchive(".", #PB_3DArchive_FileSystem)
Add3DArchive(#PB_Compiler_Home + "examples/3d/Data/Textures", #PB_3DArchive_FileSystem)


Add3DArchive(#PB_Compiler_Home + "examples/3d/Data/Packs/desert.zip", #PB_3DArchive_Zip)
Add3DArchive(#PB_Compiler_Home + "examples/3d/Data/Scripts", #PB_3DArchive_FileSystem)
Parse3DScripts()

 ;-------------  Materials  -----------------------------------------
  CreateMaterial(0, LoadTexture(0, "Dirt.jpg"))
  CreateMaterial(1, LoadTexture(1, "wood.jpg"))
  CreateMaterial(3, LoadTexture(3, "ground_diffuse.png"))
  CreateMaterial(2, LoadTexture(2, "Geebee2.bmp"))
  ;MaterialShadingMode(1, #PB_Material_Wireframe     )
 ;--------------  Camera  -----------------------------------------
  
  CreateCamera(#Camera, 0, 0, 100, 100)
  MoveCamera(#camera, 0, 1, -7, #PB_Absolute)
  CameraLookAt(#camera, 0, -2, 0)


;- -------------  Light  ---------------------------------------------
  CreateLight(0, $FFFFFF, 1560, 900, 500)
  AmbientColor(RGB(255,255,255))
;- -------------  World  -------------------------------------------
  WorldGravity(-9)
  SkyBox("desert07.jpg")
  ;----------------- the big wheel-----------------------------------------
    
Define.f KeyX, KeyY, MouseX, MouseY
CreateMaterial(#material, LoadTexture(#material, "MRAMOR6X6.jpg"))
CreatePlane(#plane, 30, 30, 5, 5, 2, 2)
CreateEntity(#plane, MeshID(#plane), MaterialID(#material), 0,-2.5,0.0)
CreateEntityBody(#plane, #PB_Entity_StaticBody, 1, 0.1, 1)

  
  CreateCube(#cube1, 1)
  CreateEntity(#cube1, MeshID(#cube1), MaterialID(0) , 0, 0, 0)
  CreateEntityBody(#cube1, #PB_Entity_BoxBody)
     
  CreateEntity(#cube2, MeshID(#cube1), MaterialID(2) , -0.4, 3.4, 0)
  ScaleEntity(#cube2, 0.5,0.5,0.5)
  CreateEntityBody(#cube2, #PB_Entity_BoxBody, 1, 0.1, 1) 
     
  CreateCylinder(#cylinder, 0.5, 0.2)
  CreateEntity(#cylinder, MeshID(#cylinder), MaterialID(1), 0, 2, 0)
  CreateEntityBody(#cylinder, #PB_Entity_CylinderBody, 1, 0.1, 1) 
     
  SetEntityCollisionFilter(#plane      , 3 , 2 ) ;  binary 11,  10
  SetEntityCollisionFilter(#cube1       , 7, 1 )  ; binary 111, 1
  SetEntityCollisionFilter(#cube2      , 7 , 2 )  ;  binary 111,  10
  SetEntityCollisionFilter(#cylinder   , 3 , 2 ) ;  binary 11,  10
  
  ;Debug GetEntityCollisionGroup(#cylinder)
  ;Debug GetEntityCollisionMask(#cylinder)

     
Repeat
Repeat
 ev =  WindowEvent()
Until ev = 0
  
  If ExamineMouse()
        MouseX = -MouseDeltaX() * #CameraSpeed * 0.2
        MouseY = -MouseDeltaY() * #CameraSpeed * 0.2
      EndIf
  ShowCursor_(0)
    
  ExamineKeyboard()
  
        ; Use arrow keys and mouse to rotate camera and fly in/out
        If KeyboardPushed(#PB_Key_Left)
          KeyX = -#CameraSpeed/2
        ElseIf KeyboardPushed(#PB_Key_Right)
          KeyX = #CameraSpeed/2
        Else
          KeyX = 0
        EndIf
        
        If KeyboardPushed(#PB_Key_Up)
          KeyY = -#CameraSpeed/2
        ElseIf KeyboardPushed(#PB_Key_Down)
          KeyY = #CameraSpeed/2
        Else
          KeyY = 0
        EndIf

If KeyboardPushed(#PB_Key_Z) ; motor rotation
 ApplyEntityImpulse(#cube1, 1,0,0) 
  
EndIf 

  
  If KeyboardPushed(#PB_Key_X) ; slower motor rotation
   ApplyEntityImpulse(#cube2, 1,0,0)  

    
  EndIf
    
  If KeyboardReleased(#PB_Key_C) ; check camera position
    Debug CameraX(#camera): Debug CameraY(#camera): Debug CameraZ(#camera)
  EndIf
      
  RotateCamera(#camera, MouseY, MouseX, 0, #PB_Relative)
  MoveCamera  (#camera, KeyX, 0, KeyY)
  
  RenderWorld()
  
  FlipBuffers()

Until KeyboardPushed(#PB_Key_Escape) Or WindowEvent() = #PB_Event_CloseWindow
exercise 2: add another cube to fall over the big wheel in the first example (top of this page )
without setting the filter the cube will pass through the wheel
look the cube and plane filters before the Repeat Loop.

Code: Select all

Enumeration
#material = 10
#plane
#Joint
#axisCube
#Hand
#Joint2
#Joint3
#Joint4
EndEnumeration
#CameraSpeed = 0.1

InitEngine3D()
InitSprite()
InitKeyboard()
InitMouse()
;WorldDebug(#PB_World_DebugEntity)
;WorldDebug(#PB_World_DebugBody)
ExamineDesktops()

OpenWindow(0,0,0,DesktopWidth(0), DesktopHeight(0)," T: Energize the the motor...V: slower motor speed.....Mouse + arrows: for the Camera",#PB_Window_ScreenCentered)
OpenWindowedScreen(WindowID(0),0,0,DesktopWidth(0), DesktopHeight(0),1,0,0,#PB_Screen_WaitSynchronization)

Add3DArchive(".", #PB_3DArchive_FileSystem)
Add3DArchive(#PB_Compiler_Home + "examples/3d/Data/Textures", #PB_3DArchive_FileSystem)


Add3DArchive(#PB_Compiler_Home + "examples/3d/Data/Packs/desert.zip", #PB_3DArchive_Zip)
Add3DArchive(#PB_Compiler_Home + "examples/3d/Data/Scripts", #PB_3DArchive_FileSystem)
Parse3DScripts()

 ;-------------  Materials  -----------------------------------------
  CreateMaterial(0, LoadTexture(0, "Dirt.jpg"))
  CreateMaterial(1, LoadTexture(1, "wood.jpg"))
  CreateMaterial(3, LoadTexture(3, "ground_diffuse.png"))
  CreateMaterial(2, LoadTexture(2, "Geebee2.bmp"))
  ;MaterialShadingMode(1, #PB_Material_Wireframe     )
 ;--------------  Camera  -----------------------------------------
  #camera =0
  CreateCamera(#Camera, 0, 0, 100, 100)
  MoveCamera(#camera, 0.0254, 3.518, -4.5366, #PB_Absolute)
  CameraLookAt(0, -1.5, 2, 0)


;- -------------  Light  ---------------------------------------------
  CreateLight(0, $FFFFFF, 1560, 900, 500)
  AmbientColor(RGB(255,255,255))
;- -------------  World  -------------------------------------------
  WorldGravity(-9)
  SkyBox("desert07.jpg")
  ;----------------- the big wheel-----------------------------------------
  
  CreateCylinder(0, 1, 0.2)
  CreateEntity(0, MeshID(0), MaterialID(1), 0, 2, 0)
    
  ;---------------- Hand on the wheel -----------------------------------
  CreateEntityBody(0, #PB_Entity_CylinderBody, 1, 0.1,1)
  CreateCylinder(#Hand,0.4, 0.2)
  ;TransformMesh(#Mesh, x, y, z, ScaleX, ScaleY, ScaleZ, RotateX, RotateY, RotateZ [, SubMesh])
  TransformMesh(#Hand,0,0,0,  0.2, 5, 0.2, 0,0,0)
  CreateEntity(#Hand, MeshID(#Hand), MaterialID(0), 0, 2, 0)
  ;CreateEntityBody(#Hand, #PB_Entity_CylinderBody, 1, 0.1,1)
  ;----------------- CollisionFilter between the wheel and its hand -----------------------------
  ;SetEntityCollisionFilter(#Entity, CollisionGroup, CollisionMask)
  SetEntityCollisionFilter(0      , 9 , 2 ) 
  SetEntityCollisionFilter(#Hand , 4 , 2 )
  
  CreateEntityBody(#Hand, #PB_Entity_ConvexHullBody, 1, 0.1,1)
  
  
  CreateCube(#axisCube, 0.5)
  CreateEntity(#axisCube, MeshID(#axisCube), MaterialID(0) , 0, 0, 0)
  CreateEntityBody(#axisCube, #PB_Entity_StaticBody)
 
  ;------------------joints  ---------------------------------------------
 ;HingeJoint(#Joint, EntityID, PivotX, PivotY, PivotZ, AxisX, AxisY, AxisZ, EntityID2, PivotX2, PivotY2, PivotZ2, AxisX2, AxisY2, AxisZ2)
 ;SetJointAttribute(0, #PB_HingeJoint_LowerLimit, 0) 
 ; the joint between the wheel EntityID(0) And the static cube on the ground EntityID(#axisCube)
  HingeJoint(#Joint, EntityID(0),
             0, 3, 0,
             0, 1, 0, 
             EntityID(#axisCube),
             -0.5, 0, 0, 
             1, 0, 0)
  
  HingeJoint(#Joint, EntityID(0),
             0, -1, 0,
             0, 1, 0, 
             EntityID(#axisCube),
             0, 1, 0, 
             0, 1, 0)
  ;;========================================================
  
    SliderJoint(#Joint3, EntityID(0), -0.3, 0, 0, EntityID(#Hand),0.5, 0, 0)
    SetJointAttribute(#Joint3, #PB_SliderJoint_LowerLimit, 0)  
    SetJointAttribute(#Joint3, #PB_SliderJoint_UpperLimit,  0) 
 ;----------------- not sleep  ---------------------------------------
  SetEntityAttribute(0, #PB_Entity_LinearSleeping, 0)

  ;-------------   ----------------------------------------------
  CreateCube(3, 1) ; for the body of the slider

  CreateEntity(6, MeshID(3), MaterialID(2) , -1.2, 2.4, 1.0) ; the moving slider
  ScaleEntity(6, 1, 0.2, 0.2)
  CreateEntityBody(6, #PB_Entity_BoxBody, 1)
    
  CreateEntity(7, MeshID(3), MaterialID(0) , -1.3, 2.4, 1.5) ; fixed 
  ScaleEntity(7, 1, 0.1, 0.1)
  CreateEntityBody(7, #PB_Entity_StaticBody, 1)
  
  SliderJoint(#Joint4, EntityID(7), 0, 0, -1, EntityID(6),0, 0, 1)
  SetJointAttribute(#Joint4, #PB_SliderJoint_LowerLimit, -1)  
  SetJointAttribute(#Joint4, #PB_SliderJoint_UpperLimit,  0) 
  
  
Define.f KeyX, KeyY, MouseX, MouseY
CreateMaterial(#material, LoadTexture(#material, "MRAMOR6X6.jpg"))
CreatePlane(#plane, 30, 30, 5, 5, 2, 2)
CreateEntity(#plane, MeshID(#plane), MaterialID(#material), 0,-2.5,0.0)
CreateEntityBody(#plane, #PB_Entity_StaticBody, 1, 0.1, 1)


EnableHingeJointAngularMotor(#Joint,0,0.0,10)

CreateCube(50,0.1) ; static body connected with Hinge joint with 51
    CreateEntity(50, MeshID(50), MaterialID(1), -2.35, 2.7, -0.5)
    ScaleEntity(50, 3,3,2.5)
    CreateEntity(51, MeshID(50), MaterialID(3), -5,0, 0) ; spring
    ScaleEntity(51, 8,0.5,8)
    
    RotateEntity(50, 0,0,-90)
    CreateEntityBody(50, #PB_Entity_StaticBody, 90, 0.1,10)
    CreateEntityBody(51, #PB_Entity_BoxBody, 1, 0.1, 1)  
        
    SetEntityCollisionFilter(6       , 5, 3 ) ; (the Hammer) ;slider
    SetEntityCollisionFilter(51      , 5 , 3 ) ; spring block
    SetEntityCollisionFilter(50       , 4, 3 ) ; static body connected with Hinge joint with 51 
    
        
    ;HingeJoint(#Joint, EntityID, PivotX, PivotY, PivotZ, AxisX, AxisY, AxisZ, EntityID2, PivotX2, PivotY2, PivotZ2, AxisX2, AxisY2, AxisZ2)
    ;joints 60 and 61 just to fix entity 50 and entity 51 together 
    HingeJoint(60, EntityID(50), ;entity 50 are Static cube
               0, 0.5, 0,
               0, 0, 1, 
               EntityID(51),     ; entity 51 is the spring
               0, 0, 0, 
               0, 0, 1)
     HingeJoint(61, EntityID(50),
               -0.05, 0.5, 0,
               0, 0, 1, 
               EntityID(51),
               0.05, 0, 0, 
               0, 0, 1) 

     
     CreateEntity(70, MeshID(3), MaterialID(2), -0.6, 3.7, -0.4) ; another cube
     ScaleEntity(70, 0.5, 0.5, 0.5)
     ;SetEntityCollisionFilter(70       , 2, 5 )
     CreateEntityBody(70, #PB_Entity_BoxBody, 1, 0.1, 1) 
     SetEntityCollisionFilter(70       , 2, 5 )
     SetEntityCollisionFilter(#plane      , 7, 7 )
     
     ;Debug GetEntityCollisionMask(#plane)
     ;Debug GetEntityCollisionGroup(#plane)


     
Repeat
repeat
ev =  WindowEvent()
Until ev=0
  
  If ExamineMouse()
        MouseX = -MouseDeltaX() * #CameraSpeed * 0.2
        MouseY = -MouseDeltaY() * #CameraSpeed * 0.2
      EndIf
  ShowCursor_(0)
    
  ExamineKeyboard()
  
        ; Use arrow keys and mouse to rotate camera and fly in/out
        If KeyboardPushed(#PB_Key_Left)
          KeyX = -#CameraSpeed/2
        ElseIf KeyboardPushed(#PB_Key_Right)
          KeyX = #CameraSpeed/2
        Else
          KeyX = 0
        EndIf
        
        If KeyboardPushed(#PB_Key_Up)
          KeyY = -#CameraSpeed/2
        ElseIf KeyboardPushed(#PB_Key_Down)
          KeyY = #CameraSpeed/2
        Else
          KeyY = 0
        EndIf

If KeyboardPushed(#PB_Key_T) ; motor rotation
  
  EnableHingeJointAngularMotor(#Joint,1,3,10)
  
EndIf 

  
  If KeyboardReleased(#PB_Key_V) ; slower motor rotation
    
    EnableHingeJointAngularMotor(#Joint,1,0.5,10)
    
  EndIf
    
  If KeyboardReleased(#PB_Key_C) ; check camera position
    Debug CameraX(0): Debug CameraY(0): Debug CameraZ(0)
  EndIf
      
  RotateCamera(0, MouseY, MouseX, 0, #PB_Relative)
  MoveCamera  (0, KeyX, 0, KeyY)
  
  RenderWorld()
  
  FlipBuffers()

Until KeyboardPushed(#PB_Key_Escape) Or WindowEvent() = #PB_Event_CloseWindow
Last edited by applePi on Tue Apr 12, 2016 1:45 pm, edited 2 times in total.
applePi
Addict
Addict
Posts: 1404
Joined: Sun Jun 25, 2006 7:28 pm

Re: some mechanical devices

Post by applePi »

linear motion to rotation and vice versa
the following program is an abstraction of this device:
Image

about the slider crank mechanism look velaccdiag.pdf file here
http://www.freestudy.co.uk/dynamics/

when you give a push to the slider (X key) the inertia in the frictionless wheel will force it to continue rotation. or press T to energize the persistent motor and so converting rotation to Linear
Image
change camera view by keys 2/1

Code: Select all

Enumeration
#material = 10
#plane
#Joint
#axisCube
#Hand
#Joint2
#Joint3
#Joint4
#slider
#Arm
#Wheel
EndEnumeration
#CameraSpeed = 0.5
Define.f KeyX, KeyY, MouseX, MouseY

InitEngine3D()
InitSprite()
InitKeyboard()
InitMouse()
;WorldDebug(#PB_World_DebugEntity)
;WorldDebug(#PB_World_DebugBody)
ExamineDesktops()

OpenWindow(0,0,0,DesktopWidth(0), DesktopHeight(0),"...  X: give initial push to the slider..F: drop a weight,... 2 / 1 change camera scene",#PB_Window_ScreenCentered)
OpenWindowedScreen(WindowID(0),0,0,DesktopWidth(0), DesktopHeight(0),1,0,0,#PB_Screen_WaitSynchronization)

Add3DArchive(".", #PB_3DArchive_FileSystem)
Add3DArchive(#PB_Compiler_Home + "examples/3d/Data/Textures", #PB_3DArchive_FileSystem)


Add3DArchive(#PB_Compiler_Home + "examples/3d/Data/Packs/desert.zip", #PB_3DArchive_Zip)
Add3DArchive(#PB_Compiler_Home + "examples/3d/Data/Scripts", #PB_3DArchive_FileSystem)
Parse3DScripts()

 ;-------------  Materials  -----------------------------------------
  CreateMaterial(0, LoadTexture(0, "Dirt.jpg"))
  CreateMaterial(1, LoadTexture(1, "wood.jpg"))
  CreateMaterial(3, LoadTexture(3, "ground_diffuse.png"))
  CreateMaterial(2, LoadTexture(2, "Geebee2.bmp"))
  ;MaterialShadingMode(1, #PB_Material_Wireframe     )
 ;--------------  Camera  -----------------------------------------
  #camera = 0
  CreateCamera(#Camera, 0, 0, 100, 100)
  MoveCamera(#camera, -0.01694414764643, 4.2273473739624, -3.91346645355225, #PB_Absolute)
  CameraLookAt(0, 0, 2.5, 0)
  RotateCamera(0, 0, -7,3, #PB_Relative)

;- -------------  Light  ---------------------------------------------
  CreateLight(0, $FFFFFF, 1560, 900, 500)
  AmbientColor(RGB(255,255,255))
;- -------------  World  -------------------------------------------
  WorldGravity(-9)
  SkyBox("desert07.jpg")
  ;----------------- the big wheel-----------------------------------------
  
  CreateCylinder(#Wheel, 1, 0.2)
  CreateEntity(#Wheel, MeshID(#Wheel), MaterialID(1), 0, 2, 0)
    
  ;---------------- Hand on the wheel -----------------------------------
  CreateEntityBody(#Wheel, #PB_Entity_CylinderBody, 1, 0.1,1)
  CreateCylinder(#Hand,0.4, 0.2)
  ;TransformMesh(#Mesh, x, y, z, ScaleX, ScaleY, ScaleZ, RotateX, RotateY, RotateZ [, SubMesh])
  TransformMesh(#Hand,0,0,0,  0.2, 5, 0.2, 0,0,0)
  CreateEntity(#Hand, MeshID(#Hand), MaterialID(0), 0, 2, 0)
  ;CreateEntityBody(#Hand, #PB_Entity_CylinderBody, 1, 0.1,1)
  ;----------------- CollisionFilter between the wheel and its hand -----------------------------
  ;SetEntityCollisionFilter(#Entity, CollisionGroup, CollisionMask)
  SetEntityCollisionFilter(#Wheel      , 9 , 2 ) 
  SetEntityCollisionFilter(#Hand , 4 , 2 )
  
  CreateEntityBody(#Hand, #PB_Entity_ConvexHullBody, 1, 0.1,1)
  
  
  CreateCube(#axisCube, 0.5)  ; the static cube at the ground connected to the big Wheel with Hinge
  CreateEntity(#axisCube, MeshID(#axisCube), MaterialID(0) , 0, 0, 0)
  CreateEntityBody(#axisCube, #PB_Entity_StaticBody)
 
  ;------------------joints  ---------------------------------------------
 ;HingeJoint(#Joint, EntityID, PivotX, PivotY, PivotZ, AxisX, AxisY, AxisZ, EntityID2, PivotX2, PivotY2, PivotZ2, AxisX2, AxisY2, AxisZ2)
 ; the joint between the wheel EntityID(#Wheel) And the static cube on the ground EntityID(#axisCube)
  HingeJoint(#Joint, EntityID(#Wheel),
             0, -1, 0,
             0, 1, 0, 
             EntityID(#axisCube),
             0, 1, 0, 
             0, 1, 0)
  ;;========================================================wwwwwwwwwwwwwwwwwwwwwwwwww
  
    SliderJoint(#Joint3, EntityID(#Wheel), 0.0, 0, 0, EntityID(#Hand),0.3, 0, 0)
    SetJointAttribute(#Joint3, #PB_SliderJoint_LowerLimit, 0)  
    SetJointAttribute(#Joint3, #PB_SliderJoint_UpperLimit,  0) 
 ;----------------- not sleep  ---------------------------------------
  SetEntityAttribute(#Wheel, #PB_Entity_LinearSleeping, 0)
  ;-------------   ---------------------------------------------- wwwwwwwwwwwwwwwwww
  CreateCube(3, 1) ; for the body of the slider

  CreateEntity(#slider, MeshID(3), MaterialID(2) , -1.2, 2.4, 1.0) ; the moving slider
  ScaleEntity(#slider, 1, 0.2, 0.2)
  CreateEntityBody(#slider, #PB_Entity_BoxBody, 1)
    
  CreateEntity(7, MeshID(3), MaterialID(0) , -1.1, 2.5, 2.0) ; fixed 
  ScaleEntity(7, 1, 0.1, 0.1)
  CreateEntityBody(7, #PB_Entity_StaticBody, 1)
  
  SliderJoint(#Joint4, EntityID(7), 0, 0, -1, EntityID(#slider),0, 0, 1)
  SetJointAttribute(#Joint4, #PB_SliderJoint_LowerLimit, -1.2)  
  SetJointAttribute(#Joint4, #PB_SliderJoint_UpperLimit,  0) 

CreateMaterial(#material, LoadTexture(#material, "MRAMOR6X6.jpg"))
CreatePlane(#plane, 30, 30, 5, 5, 2, 2)
CreateEntity(#plane, MeshID(#plane), MaterialID(#material), 0,-2.5,0.0)
CreateEntityBody(#plane, #PB_Entity_StaticBody, 1, 0.1, 1)

EnableHingeJointAngularMotor(#Joint,0,0.0,10)
SetEntityCollisionFilter(#slider       , 5, 3 ) ; (the Hammer) ;slider

CreateEntity(#Arm, MeshID(3), MaterialID(2) , 0, 2.7, 0)
ScaleEntity(#Arm, 1, 0.1, 0.1)
CreateEntityBody(#Arm, #PB_Entity_BoxBody, 1)
HingeJoint(70, EntityID(#Arm),
           0.4, 0, 0,
           0, 1, 0, 
           EntityID(#Hand),
           0, 0.5, 0, 
           0, 1, 0)

SetEntityCollisionFilter(#Arm, 4, 2 )

HingeJoint(90, EntityID(#Arm),
           -0.52, 0, 0,
           0, 1, 0, 
           EntityID(#slider),
           0.52, 0, 0, 
           0, 1, 0)

SetEntityCollisionFilter(#plane  , 7, 7 )

Repeat
  Repeat
    ev = WindowEvent()
    Until ev = 0
  
  If ExamineMouse()
        MouseX = -MouseDeltaX() * #CameraSpeed * 0.2
        MouseY = -MouseDeltaY() * #CameraSpeed * 0.2
      EndIf
  ShowCursor_(0)
    
  ExamineKeyboard()
  
        ; Use arrow keys and mouse to rotate camera and fly in/out
        If KeyboardPushed(#PB_Key_Left)
          KeyX = -#CameraSpeed/2
        ElseIf KeyboardPushed(#PB_Key_Right)
          KeyX = #CameraSpeed/2
        Else
          KeyX = 0
        EndIf
        
        If KeyboardPushed(#PB_Key_Up)
          KeyY = -#CameraSpeed/2
        ElseIf KeyboardPushed(#PB_Key_Down)
          KeyY = #CameraSpeed/2
        Else
          KeyY = 0
        EndIf

If KeyboardPushed(#PB_Key_T) ; motor rotation
  
  EnableHingeJointAngularMotor(#Joint,1,3,10)
  
EndIf 

  
  If KeyboardReleased(#PB_Key_V) ; slower motor rotation
    
    EnableHingeJointAngularMotor(#Joint,1,0.5,10)
    
  EndIf
    
  If KeyboardReleased(#PB_Key_C) ; check camera position
    Debug CameraX(0): Debug CameraY(0): Debug CameraZ(0)
  EndIf
  
  If KeyboardPushed(#PB_Key_X)
    ApplyEntityImpulse(#slider, 1,0,0)
  ElseIf KeyboardPushed(#PB_Key_Z)
    
    ApplyEntityImpulse(#slider, -1,0,0)
  EndIf

  If KeyboardPushed(#PB_Key_1)
    MoveCamera(#camera, -0.01694414764643, 4.2273473739624, -3.91346645355225, #PB_Absolute)
    CameraLookAt(0, 0, 2.5, 0)
    RotateCamera(0, 0, -7,3, #PB_Relative)
    ElseIf KeyboardPushed(#PB_Key_2)
  MoveCamera(#camera, -0.63755083084106, 9.09852600097656, 0.11698222160339, #PB_Absolute)
  CameraLookAt(0, 0, 2.5, 0)
  RotateCamera(0, 0, -10,0, #PB_Relative)  
EndIf

If KeyboardReleased(#PB_Key_F)
 
ent= CreateEntity(#PB_Any , MeshID(3), MaterialID(2), 0.5, 3.7, -0.4) ; another cube
     ScaleEntity(ent, 0.3, 0.2, 0.3)
     CreateEntityBody(ent, #PB_Entity_BoxBody, 2, 0.1, 1) 
     SetEntityCollisionFilter(ent       , 2, 5 )
     
   EndIf
   
     
  RotateCamera(0, MouseY, MouseX, 0, #PB_Relative)
  MoveCamera  (0, KeyX, 0, KeyY)
  
  RenderWorld()
  
  FlipBuffers()

Until KeyboardPushed(#PB_Key_Escape) Or WindowEvent() = #PB_Event_CloseWindow
Edit: i have found an interesting Hinge joint feature
add
SetJointAttribute(#Joint, #PB_HingeJoint_LowerLimit, 0)
SetJointAttribute(#Joint, #PB_HingeJoint_UpperLimit, 0)

And this will Lock the rotation completely even if we use a motor power.
add:
SetJointAttribute(#Joint, #PB_HingeJoint_LowerLimit, 0)
SetJointAttribute(#Joint, #PB_HingeJoint_UpperLimit, 90)

will Limit the rotation To 90 degree only

================================================================
Wheel with center axis, it seems as if it has a friction somehow when we add axis at its center, so it is your responsibility to keep it rotating manually with X/Z keys or press T/V to power a motor
Image

Code: Select all

;#PB_HingeJoint_LowerLimit

Enumeration
#material = 20
#plane
#Joint
#axisCube
#Hand
#Joint2
#Joint3
#Joint4
#slider
#Arm
#Wheel
#WheelAxis
#WheelAxisJoint
EndEnumeration
#CameraSpeed = 0.5
Define.f KeyX, KeyY, MouseX, MouseY

InitEngine3D()
InitSprite()
InitKeyboard()
InitMouse()
;WorldDebug(#PB_World_DebugEntity)
;WorldDebug(#PB_World_DebugBody)
ExamineDesktops()

OpenWindow(0,0,0,DesktopWidth(0), DesktopHeight(0),"...  X / Z: give initial push to the slider..F: drop a weight,...T/V motor force ...2 / 1 change camera scene",#PB_Window_ScreenCentered)
OpenWindowedScreen(WindowID(0),0,0,DesktopWidth(0), DesktopHeight(0),1,0,0,#PB_Screen_WaitSynchronization)

Add3DArchive(".", #PB_3DArchive_FileSystem)
Add3DArchive(#PB_Compiler_Home + "examples/3d/Data/Textures", #PB_3DArchive_FileSystem)
;Add3DArchive(#PB_Compiler_Home + "examples/3d/Data/Textures/nvidia", #PB_3DArchive_FileSystem)


Add3DArchive(#PB_Compiler_Home + "examples/3d/Data/Packs/desert.zip", #PB_3DArchive_Zip)
Add3DArchive(#PB_Compiler_Home + "examples/3d/Data/Scripts", #PB_3DArchive_FileSystem)
Parse3DScripts()

 ;-------------  Materials  -----------------------------------------
  CreateMaterial(0, LoadTexture(0, "Dirt.jpg"))
  CreateMaterial(1, LoadTexture(1, "wood.jpg"))
  CreateMaterial(3, LoadTexture(3, "ground_diffuse.png"))
  CreateMaterial(2, LoadTexture(2, "Geebee2.bmp"))
  CreateMaterial(4, LoadTexture(4, "nskinrd.jpg"))
  
  ;MaterialShadingMode(1, #PB_Material_Wireframe     )
 ;--------------  Camera  -----------------------------------------
  #camera = 0
  CreateCamera(#Camera, 0, 0, 100, 100)
  MoveCamera(#camera, -0.01694414764643, 4.2273473739624, -3.91346645355225, #PB_Absolute)
  CameraLookAt(0, 0, 2.5, 0)
  RotateCamera(0, 0, -7,3, #PB_Relative)

;- -------------  Light  ---------------------------------------------
  CreateLight(0, $FFFFFF, 1560, 900, 500)
  AmbientColor(RGB(255,255,255))
;- -------------  World  -------------------------------------------
  ;WorldGravity(-9)
  SkyBox("desert07.jpg")
  ;----------------- the big wheel-----------------------------------------
  
  CreateCylinder(#Wheel, 1, 0.2)
  CreateEntity(#Wheel, MeshID(#Wheel), MaterialID(1), 0, 2, 0)
    
  ;---------------- Hand on the wheel -----------------------------------
  CreateEntityBody(#Wheel, #PB_Entity_CylinderBody, 1, 0.1,1)
  CreateCylinder(#Hand,0.4, 0.2)
  ;TransformMesh(#Mesh, x, y, z, ScaleX, ScaleY, ScaleZ, RotateX, RotateY, RotateZ [, SubMesh])
  TransformMesh(#Hand,0,0,0,  0.2, 5, 0.2, 0,0,0)
  CreateEntity(#Hand, MeshID(#Hand), MaterialID(0), 0, 2, 0)
  ;CreateEntityBody(#Hand, #PB_Entity_CylinderBody, 1, 0.1,1)
  ;----------------- CollisionFilter between the wheel and its hand -----------------------------
  ;SetEntityCollisionFilter(#Entity, CollisionGroup, CollisionMask)
  SetEntityCollisionFilter(#Wheel      , 9 , 2 ) 
  SetEntityCollisionFilter(#Hand , 4 , 2 )
  
  CreateEntityBody(#Hand, #PB_Entity_ConvexHullBody, 1, 0.1,1)
  
  
  CreateCube(#axisCube, 0.5)
  CreateEntity(#axisCube, MeshID(#axisCube), MaterialID(0) , 0, 0, 0)
  CreateEntityBody(#axisCube, #PB_Entity_StaticBody)
  
  ;CreateEntityBody(#WheelAxis, #PB_Entity_CylinderBody, 1, 0.1,1)
  CreateCylinder(#WheelAxis,0.4, 0.2)
  ;TransformMesh(#Mesh, x, y, z, ScaleX, ScaleY, ScaleZ, RotateX, RotateY, RotateZ [, SubMesh])
  TransformMesh(#WheelAxis,0,-0.7,0,  0.2, 10, 0.2, 0,0,0)
 UpdateMeshBoundingBox(#WheelAxis)
  CreateEntity(#WheelAxis, MeshID(#WheelAxis), MaterialID(4), 0, 2, 0)
  SetEntityCollisionFilter(#WheelAxis , 4 , 2 )
  CreateEntityBody(#WheelAxis, #PB_Entity_ConvexHullBody, 1, 0.1,1)
  
 
  ;------------------joints  ---------------------------------------------
 ;HingeJoint(#Joint, EntityID, PivotX, PivotY, PivotZ, AxisX, AxisY, AxisZ, EntityID2, PivotX2, PivotY2, PivotZ2, AxisX2, AxisY2, AxisZ2)
 ; the joint between the wheel EntityID(#Wheel) And the static cube on the ground EntityID(#axisCube)
    
  HingeJoint(#Joint, EntityID(#Wheel),
             0, -1, 0,
             0, 1, 0, 
             EntityID(#axisCube),
             0, 1, 0, 
             0, 1, 0)
  
  ;SetJointAttribute(#Joint, #PB_HingeJoint_LowerLimit,  0) 
  ;SetJointAttribute(#Joint, #PB_HingeJoint_UpperLimit,  360) 
  ;;========================================================wwwwwwwwwwwwwwwwwwwwwwwwww
  
    SliderJoint(#Joint3, EntityID(#Wheel), 0.0, 0, 0, EntityID(#Hand),0.3, 0, 0)
    SetJointAttribute(#Joint3, #PB_SliderJoint_LowerLimit, 0)  
    SetJointAttribute(#Joint3, #PB_SliderJoint_UpperLimit,  0) 
    ;----------------- Wheel Axis Joint  ---------------------------------------
    SliderJoint(#WheelAxisJoint, EntityID(#Wheel), 0, 0, 0, EntityID(#WheelAxis),0, 0, 0)
    SetJointAttribute(#WheelAxisJoint, #PB_SliderJoint_LowerLimit, 0)  
    SetJointAttribute(#WheelAxisJoint, #PB_SliderJoint_UpperLimit,  0) 
    ;----------------------------------------------------------------------
  SetEntityAttribute(#Wheel, #PB_Entity_LinearSleeping, 0)
  ;-------------   ---------------------------------------------- wwwwwwwwwwwwwwwwww
  CreateCube(3, 1) ; for the body of the slider

  CreateEntity(#slider, MeshID(3), MaterialID(2) , -1.2, 2.4, 1.0) ; the moving slider
  ScaleEntity(#slider, 1, 0.2, 0.2)
  CreateEntityBody(#slider, #PB_Entity_BoxBody, 1)
    
  CreateEntity(7, MeshID(3), MaterialID(0) , -1.1, 2.5, 2.0) ; fixed 
  ScaleEntity(7, 1, 0.1, 0.1)
  CreateEntityBody(7, #PB_Entity_StaticBody, 1)
  
  SliderJoint(#Joint4, EntityID(7), 0, 0, -1, EntityID(#slider),0, 0, 1)
  SetJointAttribute(#Joint4, #PB_SliderJoint_LowerLimit, -1.2)  
  SetJointAttribute(#Joint4, #PB_SliderJoint_UpperLimit,  0) 

CreateMaterial(#material, LoadTexture(#material, "MRAMOR6X6.jpg"))
CreatePlane(#plane, 30, 30, 5, 5, 2, 2)
CreateEntity(#plane, MeshID(#plane), MaterialID(#material), 0,-2.5,0.0)
CreateEntityBody(#plane, #PB_Entity_StaticBody, 1, 0.1, 1)

EnableHingeJointAngularMotor(#Joint,0,0.0,10)
SetEntityCollisionFilter(#slider       , 5, 3 ) ; (the Hammer) ;slider

CreateEntity(#Arm, MeshID(3), MaterialID(2) , 0, 2.7, 0)
ScaleEntity(#Arm, 1, 0.1, 0.1)
CreateEntityBody(#Arm, #PB_Entity_BoxBody, 1)
HingeJoint(70, EntityID(#Arm),
           0.4, 0, 0,
           0, 1, 0, 
           EntityID(#Hand),
           0, 0.5, 0, 
           0, 1, 0)

SetEntityCollisionFilter(#Arm, 4, 2 )

HingeJoint(90, EntityID(#Arm),
           -0.52, 0, 0,
           0, 1, 0, 
           EntityID(#slider),
           0.52, 0, 0, 
           0, 1, 0)

SetEntityCollisionFilter(#plane  , 7, 7 )


Repeat
Repeat
    ev = WindowEvent()
    Until ev=0
  
  If ExamineMouse()
        MouseX = -MouseDeltaX() * #CameraSpeed * 0.2
        MouseY = -MouseDeltaY() * #CameraSpeed * 0.2
      EndIf
  ShowCursor_(0)
    
  ExamineKeyboard()
  
        ; Use arrow keys and mouse to rotate camera and fly in/out
        If KeyboardPushed(#PB_Key_Left)
          KeyX = -#CameraSpeed/2
        ElseIf KeyboardPushed(#PB_Key_Right)
          KeyX = #CameraSpeed/2
        Else
          KeyX = 0
        EndIf
        
        If KeyboardPushed(#PB_Key_Up)
          KeyY = -#CameraSpeed/2
        ElseIf KeyboardPushed(#PB_Key_Down)
          KeyY = #CameraSpeed/2
        Else
          KeyY = 0
        EndIf

If KeyboardPushed(#PB_Key_T) ; motor rotation
  
  EnableHingeJointAngularMotor(#Joint,1,3,10)
  ApplyEntityImpulse(#slider, 1,0,0)
  
EndIf 

  
  If KeyboardReleased(#PB_Key_V) ; slower motor rotation
    
    EnableHingeJointAngularMotor(#Joint,1,1,10)
    ApplyEntityImpulse(#slider, 1,0,0)
  EndIf
    
  If KeyboardReleased(#PB_Key_C) ; check camera position
    Debug CameraX(0): Debug CameraY(0): Debug CameraZ(0)
  EndIf
  
  If KeyboardPushed(#PB_Key_X)
    ApplyEntityImpulse(#slider, 1,0,0)
  ElseIf KeyboardPushed(#PB_Key_Z)
    
    ApplyEntityImpulse(#slider, -1,0,0)
  EndIf

  If KeyboardPushed(#PB_Key_1)
    MoveCamera(#camera, -0.01694414764643, 4.2273473739624, -3.91346645355225, #PB_Absolute)
    CameraLookAt(0, 0, 2.5, 0)
    RotateCamera(0, 0, -7,3, #PB_Relative)
    ElseIf KeyboardPushed(#PB_Key_2)
  MoveCamera(#camera, -0.63755083084106, 9.09852600097656, 0.11698222160339, #PB_Absolute)
  CameraLookAt(0, 0, 2.5, 0)
  RotateCamera(0, 0, -10,0, #PB_Relative)  
EndIf

If KeyboardReleased(#PB_Key_F)
 
ent= CreateEntity(#PB_Any , MeshID(3), MaterialID(2), 0.5, 3.7, -0.4) ; another cube
     ScaleEntity(ent, 0.3, 0.2, 0.3)
     CreateEntityBody(ent, #PB_Entity_BoxBody, 2, 0.1, 1) 
     SetEntityCollisionFilter(ent       , 2, 5 )
     
   EndIf
   
     
  RotateCamera(0, MouseY, MouseX, 0, #PB_Relative)
  MoveCamera  (0, KeyX, 0, KeyY)
  
  RenderWorld()
  
  FlipBuffers()

Until KeyboardPushed(#PB_Key_Escape) Or WindowEvent() = #PB_Event_CloseWindow
in the above version the wheel axis (#WheelAxis) have problems with camera position , since we have used TransformMesh to resize and position it. but when we use scaleEntity and MoveEntity it works normal with camera position. in the new version we can look from the bottom of the wheel without the axis disappearance
Edit: corrected: when we use UpdateMeshBoundingBox(#WheelAxis) after TransformMesh the object will not disappear, the code above corrected

Code: Select all

;#PB_HingeJoint_LowerLimit

Enumeration
#material = 20
#plane
#Joint
#axisCube
#Hand
#Joint2
#Joint3
#Joint4
#slider
#Arm
#Wheel
#WheelAxis
#WheelAxisJoint
EndEnumeration
#CameraSpeed = 0.5
Define.f KeyX, KeyY, MouseX, MouseY

InitEngine3D()
InitSprite()
InitKeyboard()
InitMouse()
;WorldDebug(#PB_World_DebugEntity)
;WorldDebug(#PB_World_DebugBody)
ExamineDesktops()

OpenWindow(0,0,0,DesktopWidth(0), DesktopHeight(0),"...  X / Z: give initial push to the slider..F: drop a weight,...T/V motor force ...2 / 1 change camera scene",#PB_Window_ScreenCentered)
OpenWindowedScreen(WindowID(0),0,0,DesktopWidth(0), DesktopHeight(0),1,0,0,#PB_Screen_WaitSynchronization)

Add3DArchive(".", #PB_3DArchive_FileSystem)
Add3DArchive(#PB_Compiler_Home + "examples/3d/Data/Textures", #PB_3DArchive_FileSystem)
;Add3DArchive(#PB_Compiler_Home + "examples/3d/Data/Textures/nvidia", #PB_3DArchive_FileSystem)


Add3DArchive(#PB_Compiler_Home + "examples/3d/Data/Packs/desert.zip", #PB_3DArchive_Zip)
Add3DArchive(#PB_Compiler_Home + "examples/3d/Data/Scripts", #PB_3DArchive_FileSystem)
Parse3DScripts()

 ;-------------  Materials  -----------------------------------------
  CreateMaterial(0, LoadTexture(0, "Dirt.jpg"))
  CreateMaterial(1, LoadTexture(1, "wood.jpg"))
  CreateMaterial(3, LoadTexture(3, "ground_diffuse.png"))
  CreateMaterial(2, LoadTexture(2, "Geebee2.bmp"))
  CreateMaterial(4, LoadTexture(4, "nskinrd.jpg"))
  
  ;MaterialShadingMode(1, #PB_Material_Wireframe     )
 ;--------------  Camera  -----------------------------------------
  #camera = 0
  CreateCamera(#Camera, 0, 0, 100, 100)
  MoveCamera(#camera, -0.01694414764643, 4.2273473739624, -3.91346645355225, #PB_Absolute)
  CameraLookAt(0, 0, 2.5, 0)
  RotateCamera(0, 0, -7,3, #PB_Relative)

;- -------------  Light  ---------------------------------------------
  CreateLight(0, $FFFFFF, 0, 90, 0)
  AmbientColor(RGB(255,255,255))
;- -------------  World  -------------------------------------------
  ;WorldGravity(-9)
  SkyBox("desert07.jpg")
  ;----------------- the big wheel-----------------------------------------
  
  CreateCylinder(#Wheel, 1, 0.2)
  CreateEntity(#Wheel, MeshID(#Wheel), MaterialID(1), 0, 2, 0)
    
  ;---------------- Hand on the wheel -----------------------------------
  CreateEntityBody(#Wheel, #PB_Entity_CylinderBody, 1, 0.1,1)
  CreateCylinder(#Hand,0.4, 0.2)
  ;TransformMesh(#Mesh, x, y, z, ScaleX, ScaleY, ScaleZ, RotateX, RotateY, RotateZ [, SubMesh])
  TransformMesh(#Hand,0,0,0,  0.2, 5, 0.2, 0,0,0)
  CreateEntity(#Hand, MeshID(#Hand), MaterialID(0), 0, 2, 0)
  ;CreateEntityBody(#Hand, #PB_Entity_CylinderBody, 1, 0.1,1)
  ;----------------- CollisionFilter between the wheel and its hand -----------------------------
  ;SetEntityCollisionFilter(#Entity, CollisionGroup, CollisionMask)
  SetEntityCollisionFilter(#Wheel      , 9 , 2 ) 
  SetEntityCollisionFilter(#Hand , 4 , 2 )
  
  CreateEntityBody(#Hand, #PB_Entity_ConvexHullBody, 1, 0.1,1)
  
  
  CreateCube(#axisCube, 0.5)
  CreateEntity(#axisCube, MeshID(#axisCube), MaterialID(0) , 0, 0, 0)
  CreateEntityBody(#axisCube, #PB_Entity_StaticBody)
  
  ;CreateEntityBody(#WheelAxis, #PB_Entity_CylinderBody, 1, 0.1,1)
  CreateCylinder(#WheelAxis,0.4, 0.2)
  ;TransformMesh(#Mesh, x, y, z, ScaleX, ScaleY, ScaleZ, RotateX, RotateY, RotateZ [, SubMesh])
  ;TransformMesh(#WheelAxis,0,-0.7,0,  0.2, 10, 0.2, 0,0,0)
  CreateEntity(#WheelAxis, MeshID(#WheelAxis), MaterialID(4))
  ScaleEntity(#WheelAxis, 0.2, 10, 0.2)
  MoveEntity(#WheelAxis, 0, 1,0, #PB_Absolute)
  SetEntityCollisionFilter(#WheelAxis , 4 , 2 )
  CreateEntityBody(#WheelAxis, #PB_Entity_ConvexHullBody, 1, 0.1,1)
  
 
  ;------------------joints  ---------------------------------------------
 ;HingeJoint(#Joint, EntityID, PivotX, PivotY, PivotZ, AxisX, AxisY, AxisZ, EntityID2, PivotX2, PivotY2, PivotZ2, AxisX2, AxisY2, AxisZ2)
 ; the joint between the wheel EntityID(#Wheel) And the static cube on the ground EntityID(#axisCube)
    
  HingeJoint(#Joint, EntityID(#Wheel),
             0, -1, 0,
             0, 1, 0, 
             EntityID(#axisCube),
             0, 1, 0, 
             0, 1, 0)
  
  ;SetJointAttribute(#Joint, #PB_HingeJoint_LowerLimit,  0) 
  ;SetJointAttribute(#Joint, #PB_HingeJoint_UpperLimit,  360) 
  ;;========================================================wwwwwwwwwwwwwwwwwwwwwwwwww
  
    SliderJoint(#Joint3, EntityID(#Wheel), 0.0, 0, 0, EntityID(#Hand),0.3, 0, 0)
    SetJointAttribute(#Joint3, #PB_SliderJoint_LowerLimit, 0)  
    SetJointAttribute(#Joint3, #PB_SliderJoint_UpperLimit,  0) 
    ;----------------- Wheel Axis Joint  ---------------------------------------
    SliderJoint(#WheelAxisJoint, EntityID(#Wheel), 0, 0, 0, EntityID(#WheelAxis),0, 0.7, 0)
    SetJointAttribute(#WheelAxisJoint, #PB_SliderJoint_LowerLimit, 0)  
    SetJointAttribute(#WheelAxisJoint, #PB_SliderJoint_UpperLimit,  0) 
    ;----------------------------------------------------------------------
  SetEntityAttribute(#Wheel, #PB_Entity_LinearSleeping, 0)
  ;-------------   ---------------------------------------------- wwwwwwwwwwwwwwwwww
  CreateCube(3, 1) ; for the body of the slider

  CreateEntity(#slider, MeshID(3), MaterialID(2) , -1.2, 2.4, 1.0) ; the moving slider
  ScaleEntity(#slider, 1, 0.2, 0.2)
  CreateEntityBody(#slider, #PB_Entity_BoxBody, 1)
    
  CreateEntity(7, MeshID(3), MaterialID(0) , -1.1, 2.5, 2.0) ; fixed 
  ScaleEntity(7, 1, 0.1, 0.1)
  CreateEntityBody(7, #PB_Entity_StaticBody, 1)
  
  SliderJoint(#Joint4, EntityID(7), 0, 0, -1, EntityID(#slider),0, 0, 1)
  SetJointAttribute(#Joint4, #PB_SliderJoint_LowerLimit, -1.2)  
  SetJointAttribute(#Joint4, #PB_SliderJoint_UpperLimit,  0) 

CreateMaterial(#material, LoadTexture(#material, "MRAMOR6X6.jpg"))
CreatePlane(#plane, 30, 30, 5, 5, 2, 2)
CreateEntity(#plane, MeshID(#plane), MaterialID(#material), 0,-2.5,0.0)
CreateEntityBody(#plane, #PB_Entity_StaticBody, 1, 0.1, 1)

EnableHingeJointAngularMotor(#Joint,0,0.0,10)
SetEntityCollisionFilter(#slider       , 5, 3 ) ; (the Hammer) ;slider

CreateEntity(#Arm, MeshID(3), MaterialID(2) , 0, 2.7, 0)
ScaleEntity(#Arm, 1, 0.1, 0.1)
CreateEntityBody(#Arm, #PB_Entity_BoxBody, 1)
HingeJoint(70, EntityID(#Arm),
           0.4, 0, 0,
           0, 1, 0, 
           EntityID(#Hand),
           0, 0.5, 0, 
           0, 1, 0)

SetEntityCollisionFilter(#Arm, 4, 2 )

HingeJoint(90, EntityID(#Arm),
           -0.52, 0, 0,
           0, 1, 0, 
           EntityID(#slider),
           0.52, 0, 0, 
           0, 1, 0)

SetEntityCollisionFilter(#plane  , 7, 7 )


Repeat
Repeat
    ev = WindowEvent()
    Until ev=0
  
  If ExamineMouse()
        MouseX = -MouseDeltaX() * #CameraSpeed * 0.2
        MouseY = -MouseDeltaY() * #CameraSpeed * 0.2
      EndIf
  ShowCursor_(0)
    
  ExamineKeyboard()
  
        ; Use arrow keys and mouse to rotate camera and fly in/out
        If KeyboardPushed(#PB_Key_Left)
          KeyX = -#CameraSpeed/2
        ElseIf KeyboardPushed(#PB_Key_Right)
          KeyX = #CameraSpeed/2
        Else
          KeyX = 0
        EndIf
        
        If KeyboardPushed(#PB_Key_Up)
          KeyY = -#CameraSpeed/2
        ElseIf KeyboardPushed(#PB_Key_Down)
          KeyY = #CameraSpeed/2
        Else
          KeyY = 0
        EndIf

If KeyboardPushed(#PB_Key_T) ; motor rotation
  
  EnableHingeJointAngularMotor(#Joint,1,3,10)
  ApplyEntityImpulse(#slider, 1,0,0)
  
EndIf 

  
  If KeyboardReleased(#PB_Key_V) ; slower motor rotation
    
    EnableHingeJointAngularMotor(#Joint,1,1,10)
    ApplyEntityImpulse(#slider, 1,0,0)
  EndIf
    
  If KeyboardReleased(#PB_Key_C) ; check camera position
    Debug CameraX(0): Debug CameraY(0): Debug CameraZ(0)
  EndIf
  
  If KeyboardPushed(#PB_Key_X)
    ApplyEntityImpulse(#slider, 1,0,0)
  ElseIf KeyboardPushed(#PB_Key_Z)
    
    ApplyEntityImpulse(#slider, -1,0,0)
  EndIf

  If KeyboardPushed(#PB_Key_1)
    MoveCamera(#camera, -0.01694414764643, 4.2273473739624, -3.91346645355225, #PB_Absolute)
    CameraLookAt(0, 0, 2.5, 0)
    RotateCamera(0, 0, -7,3, #PB_Relative)
    ElseIf KeyboardPushed(#PB_Key_2)
  MoveCamera(#camera, -0.63755083084106, 9.09852600097656, 0.11698222160339, #PB_Absolute)
  CameraLookAt(0, 0, 2.5, 0)
  RotateCamera(0, 0, -10,0, #PB_Relative)  
EndIf

If KeyboardReleased(#PB_Key_F)
 
ent= CreateEntity(#PB_Any , MeshID(3), MaterialID(2), 0.5, 3.7, -0.4) ; another cube
     ScaleEntity(ent, 0.3, 0.2, 0.3)
     CreateEntityBody(ent, #PB_Entity_BoxBody, 2, 0.1, 1) 
     SetEntityCollisionFilter(ent       , 2, 5 )
     
   EndIf
   
     
  RotateCamera(0, MouseY, MouseX, 0, #PB_Relative)
  MoveCamera  (0, KeyX, 0, KeyY)
  
  RenderWorld()
  
  FlipBuffers()

Until KeyboardPushed(#PB_Key_Escape) Or WindowEvent() = #PB_Event_CloseWindow
Post Reply