Page 1 of 1

ball moving inside pipes

Posted: Mon Apr 29, 2013 10:52 am
by applePi
ball running inside pipes and when it exits triggers a music
this program make 1/4 torus ie a pipe then another 2 pipes united together to make a big one. now a ball hanging over the structure, press space key to disjoint the ball and letting it fall down through the pipes. and when it came out of the first pipe mouth it passes a laser beam (function RayCollide) so it triggers playing a music
you need the file http://www.mediafire.com/?ntwu877gwhgub0o for the music (startup.wav) (or any wav file you want)
and football texture:
Image
put it in the same folder of the code.
some notes:
1- the ball fall too slow when using the gravity default (-9.8 ) so i have increased gravity to -100 (WorldGravity(-100)) i don't know why
2- also i wonder if there are some specifics for the pipes material such as hardness or Solidity because if we choose gravity -500 the ball will penetrate the pipe violently and escape away.
Image

Image

it is my intention to separate pipes slightly instead of fitting it exactly.
PS: i will post an extension to pipes ie straight pipes in a few hours, it is in fact a donut stretched in the Z direction

Code: Select all

#CameraSpeed = 14
Global collision
Enumeration
   #tex = 100
   #mat
   #ball
   #camera
   #tubeQ ; 1/4 from a torus
   #axis
   #joint
         
 EndEnumeration
 
Declare tubeQ()
Define.f KeyX, KeyY, MouseX, MouseY

If InitEngine3D()
ExamineDesktops()
  
Add3DArchive(".", #PB_3DArchive_FileSystem)
Add3DArchive(#PB_Compiler_Home + "Examples/3D/Data/Textures", #PB_3DArchive_FileSystem)
Add3DArchive(#PB_Compiler_Home + "Examples/Sources\Data", #PB_3DArchive_FileSystem)
Add3DArchive(#PB_Compiler_Home + "Examples/3D/Data/Scripts", #PB_3DArchive_FileSystem)
Parse3DScripts()
  
  InitSprite()
  InitKeyboard()
  InitMouse()
  InitSound()
  
DeskWidth=DesktopWidth(0)
DeskHeight=DesktopHeight(0)
    
  OpenWindow(0, 0, 0, DeskWidth, DeskHeight, "pipes _ press space to drop the ball _ use keys and mouse to move cam", #PB_Window_MinimizeGadget | #PB_Window_ScreenCentered)

  OpenWindowedScreen(WindowID(0), 0, 0, DeskWidth, DeskHeight, 0, 0, 0)
        
    LoadSound(collision,"startup.wav")
            
    CreateMaterial(3, LoadTexture(3, "MRAMOR6X6.jpg"))
    MaterialCullingMode(3, #PB_Material_NoCulling )
    CreatePlane(1,1000,1000,1,1,8,8)
    CreateEntity(1,MeshID(1),MaterialID(3),0,0,0)
    EntityPhysicBody(1, #PB_Entity_StaticBody,1,0.3,1)
       
    ; Camera 
    CreateCamera(#camera, 0, 0, 100, 100)
    CameraBackColor(#camera, RGB(177,191,245))
    MoveCamera(#camera,-600, 600, 300,#PB_Absolute)
    CameraLookAt(#camera, 0, 200, 0)
        
    ; Light
    CreateLight(0, RGB(230,230,230), 0, 1000, 0) ; general light
    SetLightColor(0, #PB_Light_SpecularColor, RGB(255,255,255))
    
    WorldShadows(#PB_Shadow_Additive , 6000, RGB(200,200,200))
    ;WorldDebug(#PB_World_DebugBody)
  
CreateMaterial(203, LoadTexture(203, "terrain_texture.jpg"))
;MaterialShadingMode(203, #PB_Material_Wireframe)
MaterialCullingMode(203, #PB_Material_NoCulling)
DisableMaterialLighting(203, #True)

;wwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwww
CreateMesh(#tubeQ, #PB_Mesh_TriangleList, #PB_Mesh_Static)
SetMeshMaterial(#tubeQ, MaterialID(203))
; the first pipe
tubeQ() ; call the pipe creation sub
CreateEntity(#tubeQ, MeshID(#tubeQ), MaterialID(203), -100, 150, 0)  
ScaleEntity(#tubeQ,50, 50, 50)
RotateEntity(#tubeQ,180,0,0)
EntityPhysicBody(#tubeQ, #PB_Entity_StaticBody   , 20,1,10)  

;make another 2 pipes
CopyEntity(#tubeQ, 100)
MoveEntity(100, 100, 160, 0)
RotateEntity(100,0,180,0)
CopyEntity(#tubeQ, 101)
MoveEntity(101, 100, 360, 0)
RotateEntity(101,0,180,180)
EntityPhysicBody(100, #PB_Entity_StaticBody   , 20,1,10)
EntityPhysicBody(101, #PB_Entity_StaticBody   , 20,1,10)

CreateSphere(#ball, 50)
CreateMaterial(34, LoadTexture(34, "football.jpg"))
CreateEntity(#ball,MeshID(#ball), MaterialID(34),200,450,0)
ScaleEntity(#ball,0.3,0.3,0.3)    
EntityPhysicBody(#ball, #PB_Entity_SphereBody  , 10,1,1)  

CopyMesh(#ball, #axis)
CreateEntity(#axis,MeshID(#axis), MaterialID(34),200, 500, 0)
ScaleEntity(#axis,0.1,0.1,0.1)
EntityPhysicBody(#axis, #PB_Entity_StaticBody)
PointJoint(#joint, EntityID(#ball), 0, 25, 0 ,   EntityID(#axis), 0, -25, 0)

CreateCylinder(110, 20, 30)
CreateEntity(110,MeshID(110), MaterialID(34),-200, 20, 100)
CopyEntity(110,111)
MoveEntity(111,-200,20,-100)

WorldGravity(-100)
Repeat

      If ExamineMouse()
        MouseX = -MouseDeltaX() * #CameraSpeed * 0.05
        MouseY = -MouseDeltaY() * #CameraSpeed * 0.05
      EndIf
      
     
      ExamineKeyboard()
        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 KeyboardReleased(#PB_Key_Space)
         FreeJoint(#joint)
         ApplyEntityImpulse(#ball,  0, -100, 0,  0,0,0) 
       EndIf   
        
        
      RotateCamera(#camera, MouseY, MouseX, 0, #PB_Relative)
      MoveCamera  (#camera, KeyX, 0, KeyY)
      
      re = RayCollide(-200, 10, 100, -200, 10, -100) ; detect the ball pass through the laser beam
      If re > 0 ; if it pass then play music 
        PlaySound(collision)
      EndIf  
      
      RenderWorld()
      FlipBuffers()
    Until KeyboardPushed(#PB_Key_Escape) Or Event = #PB_Event_CloseWindow

  
Else
  MessageRequester("Error", "The 3D Engine can't be initialized",0)
EndIf
FreeSound(collision)

End
  
  Procedure tubeQ()
    x.f: y.f :z.f :  u.f=0 :txu.f : txv.f

      rMajor.f = 2 : rMinor.f = 0.5

      majorRadius.f = 2: minorRadius.f = 0.5: numMajor.l = 50: numMinor.l = 25
      majorStep.f   = 0.5 * #PI / (numMajor+0); 0.5*pi to determine it is 1/4 of a circle
      minorStep.f   = 2 * #PI / (numMinor+0)
      i.l: j.l
 
      For i = 0 To numMajor
        t.f = i * majorStep;
        
        For j = 0 To numMinor
          u.f = j * minorStep;
          x = Cos(t) * (majorRadius + minorRadius * Cos(u))
          y = Sin(t) * (majorRadius + minorRadius * Cos(u))
          z.f = minorRadius * Sin(u)
                    
          MeshVertexPosition(x, y, z);
          MeshVertexTextureCoordinate(txu, txv)
          MeshVertexNormal(x, y, z)
          txv = txv + 1/numMinor ; texture coordinates
          
        Next
        txv = 0
        txu = txu + 1/numMajor ;texture coordinates
      Next
      ;Debug tt: Debug t
     v.l=0 :tt=0
     For i = 0 To numMajor
       For j = 0 To numMinor-1
         tt+1
          If tt>1273 :Break:EndIf
          MeshFace(v,v+1,v + numMinor+1)
          MeshFace(v + numMinor+1,v + numMinor+2,v+1 )
          If i=numMajor-1 And j=numMinor-1 ;bypass the last triangle
            ;numMinor-1
          EndIf 
          v + 1 
                    
     Next
     
   Next 
   NormalizeMesh(#tubeQ)
   FinishMesh(#True)
 EndProcedure
 
 

Re: ball moving inside pipes

Posted: Mon Apr 29, 2013 11:42 am
by Comtois
applePi wrote: some notes:
1- the ball fall too slow when using the gravity default (-9.8 ) so i have increased gravity to -100 (WorldGravity(-100)) i don't know why
Read this:)

Re: ball moving inside pipes

Posted: Mon Apr 29, 2013 2:50 pm
by applePi
thank you Comtois, thats make sense, i have changed many values to about 0.1 and now the ball falling with default gravity as with -100 in the above example.
i have added an extension pipe made from circles to extend the 1/4 torus near the ground, and another one made from torus by stretching it in Z direction, it is the lonely pipe on the ground . needs the same startup.wav file and football texture above
Image

Code: Select all

#CameraSpeed = 2
Global collision
Enumeration
   #tex = 100
   #mat
   #ball
   #camera
   #tubeQ ; 1/4 from a torus
   #pipe
   #axis
   #joint
   #torus
         
 EndEnumeration
 
 Declare tubeQ()
 Declare pipe()
 Declare torus()
Define.f KeyX, KeyY, MouseX, MouseY

If InitEngine3D()
ExamineDesktops()
  
Add3DArchive(".", #PB_3DArchive_FileSystem)
Add3DArchive(#PB_Compiler_Home + "Examples/3D/Data/Textures", #PB_3DArchive_FileSystem)
Add3DArchive(#PB_Compiler_Home + "Examples/Sources\Data", #PB_3DArchive_FileSystem)
Add3DArchive(#PB_Compiler_Home + "Examples/3D/Data/Scripts", #PB_3DArchive_FileSystem)
Parse3DScripts()
  
  InitSprite()
  InitKeyboard()
  InitMouse()
  InitSound()
  
DeskWidth=DesktopWidth(0)
DeskHeight=DesktopHeight(0)
    
  OpenWindow(0, 0, 0, DeskWidth, DeskHeight, "pipes _ press space to drop the ball _ use keys and mouse to move cam", #PB_Window_MinimizeGadget | #PB_Window_ScreenCentered)

  OpenWindowedScreen(WindowID(0), 0, 0, DeskWidth, DeskHeight, 0, 0, 0)
        
    LoadSound(collision,"startup.wav")
            
    CreateMaterial(3, LoadTexture(3, "MRAMOR6X6.jpg"))
    MaterialCullingMode(3, #PB_Material_NoCulling )
    CreatePlane(1,100,100,1,1,1,1)
    CreateEntity(1,MeshID(1),MaterialID(3),0,0,0)
    EntityPhysicBody(1, #PB_Entity_StaticBody,1,0.3,1)
       
    ; Camera 
    CreateCamera(#camera, 0, 0, 100, 100)
    CameraBackColor(#camera, RGB(177,191,245))
    MoveCamera(#camera,-60, 60, 30,#PB_Absolute)
    CameraLookAt(#camera, 0, 20, 0)
        
    ; Light
    CreateLight(0, RGB(230,230,230), 0, 100, 0) ; general light
    SetLightColor(0, #PB_Light_SpecularColor, RGB(255,255,255))
    
    WorldShadows(#PB_Shadow_Additive , 600, RGB(200,200,200))
    ;WorldDebug(#PB_World_DebugBody)
  
CreateMaterial(203, LoadTexture(203, "terrain_texture.jpg"))
;MaterialShadingMode(203, #PB_Material_Wireframe)
MaterialCullingMode(203, #PB_Material_NoCulling)
DisableMaterialLighting(203, #True)

;wwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwww
CreateMesh(#tubeQ, #PB_Mesh_TriangleList, #PB_Mesh_Static)
SetMeshMaterial(#tubeQ, MaterialID(203))
; the first pipe which have 1/4 torus shape
tubeQ() ; call the pipe creation sub
CreateEntity(#tubeQ, MeshID(#tubeQ), MaterialID(203), -10, 15, 0)  
ScaleEntity(#tubeQ,5, 5, 5)
RotateEntity(#tubeQ,180,0,0)
EntityPhysicBody(#tubeQ, #PB_Entity_StaticBody   , 20,1,10)  

;make another 2 pipes like the above
CopyEntity(#tubeQ, 100)
MoveEntity(100, 10, 14, 0)
RotateEntity(100,0,180,0)
CopyEntity(#tubeQ, 101)
MoveEntity(101, 9, 34, 0)
RotateEntity(101,0,180,180)
EntityPhysicBody(100, #PB_Entity_StaticBody   , 20,1,10)
EntityPhysicBody(101, #PB_Entity_StaticBody   , 20,1,10)

CreateSphere(#ball, 5)
CreateMaterial(34, LoadTexture(34, "football.jpg"))
CreateEntity(#ball,MeshID(#ball), MaterialID(34),20,45,0)
ScaleEntity(#ball,0.3,0.3,0.3)    
EntityPhysicBody(#ball, #PB_Entity_SphereBody  , 20,1,1)  

CopyMesh(#ball, #axis)
CreateEntity(#axis,MeshID(#axis), MaterialID(34),20, 50, 0)
ScaleEntity(#axis,0.1,0.1,0.1)
EntityPhysicBody(#axis, #PB_Entity_StaticBody)
PointJoint(#joint, EntityID(#ball), 0, 2.5, 0 ,   EntityID(#axis), 0, -2.5, 0)

;just places to show the laser beam positions
CreateCylinder(110, 2, 3)
CreateEntity(110,MeshID(110), MaterialID(34),-30, 2, 10)
CopyEntity(110,111)
MoveEntity(111,-30,2,-10)

;straight pipe, the one near the ground
CreateMesh(#pipe, #PB_Mesh_TriangleList, #PB_Mesh_Static)
SetMeshMaterial(#pipe, MaterialID(203))
pipe() ; call the pipe creation sub
CreateEntity(#pipe, MeshID(#pipe), MaterialID(203), -25, 5, 0)  
ScaleEntity(#pipe,0.25,0.25,0.4)
RotateEntity(#pipe,0,90,0)
EntityPhysicBody(#pipe, #PB_Entity_StaticBody   , 20,1,10)  

; pipe from torus ; the one on the ground alone
CreateMesh(#torus, #PB_Mesh_TriangleList, #PB_Mesh_Static)
SetMeshMaterial(#torus, MaterialID(203))
torus() ; in line 284: z.f = 20 * Sin(u) change 20 to 5 to get shorter pipe and eventually a torus
CreateEntity(#torus, MeshID(#torus), MaterialID(203), -20, 5, -20)  
;ScaleEntity(#torus,0.7,0.7,0.7)
RotateEntity(#torus,0,90,0)
EntityPhysicBody(#torus, #PB_Entity_ConvexHullBody    , 20,1,10)  

;WorldGravity(-100)
Repeat

      If ExamineMouse()
        MouseX = -MouseDeltaX() * #CameraSpeed * 0.05
        MouseY = -MouseDeltaY() * #CameraSpeed * 0.05
      EndIf
      
     
      ExamineKeyboard()
        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 KeyboardReleased(#PB_Key_Space)
         FreeJoint(#joint)
         ApplyEntityImpulse(#ball,  0, -10, 0,  0,0,0) 
       EndIf   
        
        
      RotateCamera(#camera, MouseY, MouseX, 0, #PB_Relative)
      MoveCamera  (#camera, KeyX, 0, KeyY)
      
      re = RayCollide(-30, 2, 10, -30, 2, -10) ; detect the ball pass through the laser beam
      If re > 0 ; if it pass then play music 
        PlaySound(collision)
      EndIf  
      
      RenderWorld()
      FlipBuffers()
    Until KeyboardPushed(#PB_Key_Escape) Or Event = #PB_Event_CloseWindow

  
Else
  MessageRequester("Error", "The 3D Engine can't be initialized",0)
EndIf
FreeSound(collision)

End

Procedure tubeQ()
    x.f: y.f :z.f :  u.f=0 :txu.f : txv.f

      rMajor.f = 2 : rMinor.f = 0.5

      majorRadius.f = 2: minorRadius.f = 0.5: numMajor.l = 50: numMinor.l = 25
      majorStep.f   = 0.5 * #PI / (numMajor+0); 0.5*pi to determine it is 1/4 of a circle
      minorStep.f   = 2 * #PI / (numMinor+0)
      i.l: j.l
 
      For i = 0 To numMajor
        t.f = i * majorStep;
        
        For j = 0 To numMinor
          u.f = j * minorStep;
          x = Cos(t) * (majorRadius + minorRadius * Cos(u))
          y = Sin(t) * (majorRadius + minorRadius * Cos(u))
          z.f = minorRadius * Sin(u)
                    
          MeshVertexPosition(x, y, z);
          MeshVertexTextureCoordinate(txu, txv)
          MeshVertexNormal(x, y, z)
          txv = txv + 1/numMinor ; texture coordinates
          
        Next
        txv = 0
        txu = txu + 1/numMajor ;texture coordinates
      Next
      ;Debug tt: Debug t
     v.l=0 :tt=0
     For i = 0 To numMajor
       For j = 0 To numMinor-1
         tt+1
          If tt>1273 :Break:EndIf
          MeshFace(v,v+1,v + numMinor+1)
          MeshFace(v + numMinor+1,v + numMinor+2,v+1 )
          If i=numMajor-1 And j=numMinor-1 ;bypass the last triangle
            ;numMinor-1
          EndIf 
          v + 1 
                    
     Next
     
   Next 
   NormalizeMesh(#tubeQ)
   FinishMesh(#True)
 EndProcedure
 
 Procedure pipe()
   x.f: y.f :z.f : u.f: v.f: r.f
      majorOrbit.l = 100 : minorOrbit.l = 20
      majorStep.f   = 2 * #PI / majorOrbit
      minorStep.f   = 2 * #PI / minorOrbit
      i.l: j.l: txu.f : txv.f
 
      For i = 0 To majorOrbit
        v = i * majorStep
        For j = 0 To minorOrbit
          u = j * minorStep
          r = 4 * (1 - (Cos(u)/2))
        
          x = Cos(u)*10
          y = Sin(u)*10
          ;z = Sin(v)
          z+0.02
          
          MeshVertexPosition(x, y, z);
          MeshVertexTextureCoordinate(txu, txv)
          MeshVertexNormal(x, y, z)
          ; texture the whole tube with one picture stretched
          txv = txv + 1/minorOrbit ; texture coordinates
          
        Next
        txv = 0
        txu = txu + 1/majorOrbit 
      Next
      For i = 0 To majorOrbit-1
      For j = 0 To minorOrbit
          MeshFace(t,t+1,t + minorOrbit+1)
          MeshFace(t + minorOrbit+1,t + minorOrbit+2,t+1 )
          If i=majorOrbit-1 And j=minorOrbit-1 ;bypass the last triangle
            minorOrbit-1
          EndIf 
          t + 1   
          
     Next
     
   Next  
      
    NormalizeMesh(1)
    FinishMesh(#True)
    
  EndProcedure
  
  Procedure torus()
x.f: y.f :z.f :  u.f=0 :txu.f : txv.f
rMajor.f = 2 : rMinor.f = 0.5
      majorRadius.f = 2: minorRadius.f = 0.5: numMajor.l = 50: numMinor.l = 25
      majorStep.f   = 2 * #PI / (numMajor+0);
      minorStep.f   = 2 * #PI / (numMinor+0)
      i.l: j.l
 
      For i = 0 To numMajor
        t.f = i * majorStep;
        
        For j = 0 To numMinor
          u.f = j * minorStep;
          x = Cos(t) * (majorRadius + minorRadius * Cos(u))
          y = Sin(t) * (majorRadius + minorRadius * Cos(u))
          ;z.f = minorRadius * Sin(u) , here we get original torus
          z.f = 20 * Sin(u) ; change 20 to get shorter or longer pipe
          tt+1
          MeshVertexPosition(x, y, z);
          MeshVertexTextureCoordinate(txu, txv)
          MeshVertexNormal(x, y, z)
          txv = txv + 1/numMinor ; texture coordinates
          
        Next
        txv = 0
        txu = txu + 1/numMajor ;texture coordinates
      Next
      
     v.l=0
     For i = 0 To numMajor
      For j = 0 To numMinor-1
          MeshFace(v,v+1,v + numMinor+1)
          MeshFace(v + numMinor+1,v + numMinor+2,v+1 )
          If i=numMajor-1 And j=numMinor-1 ;bypass the last triangle
            
          EndIf 
          v + 1 
          
     Next
     
   Next 
   
   ;last vertex to connect:
   lastV = (numMinor+1)*(numMajor+1)-1-(numMinor+1)
   s = lastv - numMinor
   For v = 0 To numMinor
          MeshFace(s,s+1, v)
          MeshFace(v,v+1,s+1 )
          s+1
                    
   Next
   NormalizeMesh(#torus)
   FinishMesh(#True)     
   
 EndProcedure
  
  
  

Re: ball moving inside pipes

Posted: Mon Apr 29, 2013 4:16 pm
by IdeasVacuum
thats make sense
Hmm, I agree it works that way but it doesn't make sense!

Re: ball moving inside pipes

Posted: Mon Apr 29, 2013 8:05 pm
by applePi
here is how i visualize the situation:
1- first consider acceleration of falling objects in the earth gravity is 9.81 m/s2 and it is constant whatever the size of the scene.
2- draw the above scene on a balloon surface and inflate it so it will become of the size of france, and also scale up the camera (ie yourself) the same . now drop the ball , remember the acceleration are constant 9.8 m/s2. certainly the ball will pass the france size balloon in a much more time so we will see it very slow relatively.
3- deflate the balloon to a room size, also scale the camera down , and drop the ball, the ball will pass the whole scene very rapidly, and we will see it a speedy ball even it is not speedier than previously when the room was as the size of france.
i am sure this is a correct visual description of the phenomena.
if all the universe size decreased in size (relative to unknown meta something) to the size of a tennis ball including us and the physics laws, no one will notice the difference. but if the acceleration stay constant we will notice a difference .

Re: ball moving inside pipes

Posted: Mon Apr 29, 2013 11:58 pm
by IdeasVacuum
Hi ApplePi:
certainly the ball will pass the france size balloon in a much more time so we will see it very slow relatively.
That's true, but it would be correct of course. Imagine you are making a CAD System instead of a game - you want to keep the models @ scale 1:1 normally - even in the aircraft industry and other large-scale industries, the 3D models are normally full-size. Now unless it takes too much memory to hold the description of a huge sphere than it does a small sphere, if your model scale is correct, the physics should be correct. I'm not saying that's the way it is, but the way it ought to be. In the real world, we do what we have to do to make the thing work - if that means typing the code in with our toes, or fudging gravity, so be it!

Re: ball moving inside pipes

Posted: Tue Apr 30, 2013 7:11 am
by applePi
Hi IdeasVacuum
i was worrying if the engine time reflects the reality or not so today i have googled and made some calculation using the formula for the falling bodies in yahoo here http://answers.yahoo.com/question/index ... 532AAfAKkB
distance = 0.5 * accelaeration * time^2
in the first example above the height of the ball is 450 meters so:
450 = 0.5 * 9.806 * t^2
t^2 = 2 * 450 / 9.806 = 91.78
t = sqrt( 91.78) = 9.58 seconds
in the code below (in default gravity 9,806) the time is 9,328 seconds approx. for distance 450 m , so we can say that the computer result reflects the reality approx.

Code: Select all

#CameraSpeed = 14
Global collision
Enumeration
   #tex = 100
   #mat
   #ball
   #camera
   #axis
   #joint
         
 EndEnumeration
 
Declare tubeQ()
Define.f KeyX, KeyY, MouseX, MouseY

If InitEngine3D()
ExamineDesktops()
  
Add3DArchive(".", #PB_3DArchive_FileSystem)
Add3DArchive(#PB_Compiler_Home + "Examples/3D/Data/Textures", #PB_3DArchive_FileSystem)
Add3DArchive(#PB_Compiler_Home + "Examples/Sources\Data", #PB_3DArchive_FileSystem)
Add3DArchive(#PB_Compiler_Home + "Examples/3D/Data/Scripts", #PB_3DArchive_FileSystem)
Parse3DScripts()
  
  InitSprite()
  InitKeyboard()
  InitMouse()
  InitSound()
  
DeskWidth=DesktopWidth(0)
DeskHeight=DesktopHeight(0)
    
  OpenWindow(0, 0, 0, DeskWidth, DeskHeight, "pipes _ press space to drop the ball _ use keys and mouse to move cam", #PB_Window_MinimizeGadget | #PB_Window_ScreenCentered)

  OpenWindowedScreen(WindowID(0), 0, 0, DeskWidth, DeskHeight, 0, 0, 0)
            
    CreateMaterial(3, LoadTexture(3, "MRAMOR6X6.jpg"))
    MaterialCullingMode(3, #PB_Material_NoCulling )
    CreatePlane(1,1000,1000,1,1,8,8)
    CreateEntity(1,MeshID(1),MaterialID(3),0,0,0)
    EntityPhysicBody(1, #PB_Entity_StaticBody,1,0.3,1)
       
    ; Camera 
    CreateCamera(#camera, 0, 0, 100, 100)
    CameraBackColor(#camera, RGB(177,191,245))
    MoveCamera(#camera,-600, 600, 300,#PB_Absolute)
    CameraLookAt(#camera, 0, 200, 0)
        
    ; Light
    CreateLight(0, RGB(230,230,230), 0, 1000, 0) ; general light
    SetLightColor(0, #PB_Light_SpecularColor, RGB(255,255,255))
    
    WorldShadows(#PB_Shadow_Additive , 6000, RGB(200,200,200))
    ;WorldDebug(#PB_World_DebugBody)
  
CreateMaterial(203, LoadTexture(203, "terrain_texture.jpg"))
;MaterialShadingMode(203, #PB_Material_Wireframe)
MaterialCullingMode(203, #PB_Material_NoCulling)
DisableMaterialLighting(203, #True)

;wwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwww

CreateSphere(#ball, 50)
;CreateMaterial(34, LoadTexture(34, "football.jpg"))
CreateEntity(#ball,MeshID(#ball), MaterialID(3),200,450,0)
ScaleEntity(#ball,0.3,0.3,0.3)    
EntityPhysicBody(#ball, #PB_Entity_SphereBody  , 10,1,1)  

CopyMesh(#ball, #axis)
CreateEntity(#axis,MeshID(#axis), MaterialID(3),200, 500, 0)
ScaleEntity(#axis,0.1,0.1,0.1)
EntityPhysicBody(#axis, #PB_Entity_StaticBody)
PointJoint(#joint, EntityID(#ball), 0, 25, 0 ,   EntityID(#axis), 0, -25, 0)


;WorldGravity(-9.806) ; default
Repeat

      If ExamineMouse()
        MouseX = -MouseDeltaX() * #CameraSpeed * 0.05
        MouseY = -MouseDeltaY() * #CameraSpeed * 0.05
      EndIf
      
     
      ExamineKeyboard()
        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 KeyboardReleased(#PB_Key_Space)
         FreeJoint(#joint)
         ApplyEntityImpulse(#ball,  0, -10, 0,  0,0,0) 
         StartTime.f = ElapsedMilliseconds()
        EndIf   
       If EntityCollide(#ball,1) ; if collide between plane and ball
        ElapsedTime.f = ElapsedMilliseconds()-StartTime 
        Debug ElapsedTime.f / 1000
       EndIf 
        
      RotateCamera(#camera, MouseY, MouseX, 0, #PB_Relative)
      MoveCamera  (#camera, KeyX, 0, KeyY)
      
      RenderWorld()
      FlipBuffers()
    Until KeyboardPushed(#PB_Key_Escape) Or Event = #PB_Event_CloseWindow

  
Else
  MessageRequester("Error", "The 3D Engine can't be initialized",0)
EndIf

End
  
in the second example above the ball height is 45 meters
45 = 0.5 * 9.806 * t^2
t^2 = 2 * 45 / 9.806 = 9.178
t = sqrt(9.178 ) = 3 seconds
in computer code (in default gravity 9,806) the time is 2.937 seconds approximately for 45 m distance, so also here we can say that the computer result reflects the reality approximately

Code: Select all

#CameraSpeed = 2
Global collision
Enumeration
   #tex = 100
   #mat
   #ball
   #camera
   #axis
   #joint
            
 EndEnumeration
  
Define.f KeyX, KeyY, MouseX, MouseY

If InitEngine3D()
ExamineDesktops()
  
Add3DArchive(".", #PB_3DArchive_FileSystem)
Add3DArchive(#PB_Compiler_Home + "Examples/3D/Data/Textures", #PB_3DArchive_FileSystem)
Add3DArchive(#PB_Compiler_Home + "Examples/Sources\Data", #PB_3DArchive_FileSystem)
Add3DArchive(#PB_Compiler_Home + "Examples/3D/Data/Scripts", #PB_3DArchive_FileSystem)
Parse3DScripts()
  
  InitSprite()
  InitKeyboard()
  InitMouse()
  InitSound()
  
DeskWidth=DesktopWidth(0)
DeskHeight=DesktopHeight(0)
    
  OpenWindow(0, 0, 0, DeskWidth, DeskHeight, "pipes _ press space to drop the ball _ use keys and mouse to move cam", #PB_Window_MinimizeGadget | #PB_Window_ScreenCentered)

  OpenWindowedScreen(WindowID(0), 0, 0, DeskWidth, DeskHeight, 0, 0, 0)
        
    ;LoadSound(collision,"startup.wav")
            
    CreateMaterial(3, LoadTexture(3, "MRAMOR6X6.jpg"))
    MaterialCullingMode(3, #PB_Material_NoCulling )
    CreatePlane(1,100,100,1,1,1,1)
    CreateEntity(1,MeshID(1),MaterialID(3),0,0,0)
    EntityPhysicBody(1, #PB_Entity_StaticBody,1,0.3,1)
       
    ; Camera 
    CreateCamera(#camera, 0, 0, 100, 100)
    CameraBackColor(#camera, RGB(177,191,245))
    MoveCamera(#camera,-60, 60, 30,#PB_Absolute)
    CameraLookAt(#camera, 0, 20, 0)
        
    ; Light
    CreateLight(0, RGB(230,230,230), 0, 100, 0) ; general light
    SetLightColor(0, #PB_Light_SpecularColor, RGB(255,255,255))
    
    WorldShadows(#PB_Shadow_Additive , 600, RGB(200,200,200))
    ;WorldDebug(#PB_World_DebugBody)
  
CreateMaterial(203, LoadTexture(203, "terrain_texture.jpg"))
;MaterialShadingMode(203, #PB_Material_Wireframe)
MaterialCullingMode(203, #PB_Material_NoCulling)
DisableMaterialLighting(203, #True)

;wwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwww

CreateSphere(#ball, 5)
;CreateMaterial(34, LoadTexture(34, "football.jpg"))
CreateEntity(#ball,MeshID(#ball), MaterialID(3),20,45,0)
ScaleEntity(#ball,0.3,0.3,0.3)    
EntityPhysicBody(#ball, #PB_Entity_SphereBody  , 20,1,1)  

CopyMesh(#ball, #axis)
CreateEntity(#axis,MeshID(#axis), MaterialID(3),20, 50, 0)
ScaleEntity(#axis,0.1,0.1,0.1)
EntityPhysicBody(#axis, #PB_Entity_StaticBody)
PointJoint(#joint, EntityID(#ball), 0, 2.5, 0 ,   EntityID(#axis), 0, -2.5, 0)

;WorldGravity(-9.806) ; default
Repeat

      If ExamineMouse()
        MouseX = -MouseDeltaX() * #CameraSpeed * 0.05
        MouseY = -MouseDeltaY() * #CameraSpeed * 0.05
      EndIf
      
     
      ExamineKeyboard()
        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 KeyboardReleased(#PB_Key_Space)
         FreeJoint(#joint)
         ApplyEntityImpulse(#ball,  0, -10, 0,  0,0,0) 
         StartTime.f = ElapsedMilliseconds()
        EndIf   
       If EntityCollide(#ball,1) ; if collide between plane and ball
        ElapsedTime.f = ElapsedMilliseconds()-StartTime 
        Debug ElapsedTime.f / 1000
       EndIf 
        
        
      RotateCamera(#camera, MouseY, MouseX, 0, #PB_Relative)
      MoveCamera  (#camera, KeyX, 0, KeyY)
      
      re = RayCollide(-30, 2, 10, -30, 2, -10) ; detect the ball pass through the laser beam
      If re > 0 ; if it pass then play music 
        ;PlaySound(collision)
      EndIf  
      
      RenderWorld()
      FlipBuffers()
    Until KeyboardPushed(#PB_Key_Escape) Or Event = #PB_Event_CloseWindow

  
Else
  MessageRequester("Error", "The 3D Engine can't be initialized",0)
EndIf
;FreeSound(collision)

End
the same results can be obtained by entering the distance values in the third box in this site: http://www.gravitycalc.com/

Re: ball moving inside pipes

Posted: Tue Apr 30, 2013 11:56 am
by IdeasVacuum
Excellent stuff ApplePi. :mrgreen:

Next: Friction of materials?

Re: ball moving inside pipes

Posted: Tue Apr 30, 2013 5:46 pm
by applePi
friction of materials: i think this is 2 parts, for rotating objects, and for flat objects. for the first case look in the last example above make the plane tilted slightly: add this after line 41
RotateEntity(1, 0, 0, 10)
change the ball friction to 0
EntityPhysicBody(#ball, #PB_Entity_SphereBody , 20,1,0)
and drop the ball it will move over the plane as if it is on ice without rotation (in ideal world )
the same phenomena when you change the ball friction to 1 or more and the plane friction to 0 , the ball will move as on the ice.

Code: Select all

#CameraSpeed = 2
Global collision
Enumeration
   #tex = 100
   #mat
   #ball
   #camera
   #axis
   #joint
            
 EndEnumeration
  
Define.f KeyX, KeyY, MouseX, MouseY

If InitEngine3D()
ExamineDesktops()
  
Add3DArchive(".", #PB_3DArchive_FileSystem)
Add3DArchive(#PB_Compiler_Home + "Examples/3D/Data/Textures", #PB_3DArchive_FileSystem)
Add3DArchive(#PB_Compiler_Home + "Examples/Sources\Data", #PB_3DArchive_FileSystem)
Add3DArchive(#PB_Compiler_Home + "Examples/3D/Data/Scripts", #PB_3DArchive_FileSystem)
Parse3DScripts()
  
  InitSprite()
  InitKeyboard()
  InitMouse()
  InitSound()
  
DeskWidth=DesktopWidth(0)
DeskHeight=DesktopHeight(0)
    
  OpenWindow(0, 0, 0, DeskWidth, DeskHeight, "friction _ press space to drop the ball _ use keys and mouse to move cam", #PB_Window_MinimizeGadget | #PB_Window_ScreenCentered)

  OpenWindowedScreen(WindowID(0), 0, 0, DeskWidth, DeskHeight, 0, 0, 0)
            
    CreateMaterial(3, LoadTexture(3, "MRAMOR6X6.jpg"))
    MaterialCullingMode(3, #PB_Material_NoCulling )
    CreatePlane(1,400,100,1,1,1,1)
    CreateEntity(1,MeshID(1),MaterialID(3),0,0,0)
    RotateEntity(1, 0,0,10)
    EntityPhysicBody(1, #PB_Entity_StaticBody,1,0.3,1)
       
    ; Camera 
    CreateCamera(#camera, 0, 0, 100, 100)
    CameraBackColor(#camera, RGB(177,191,245))
    MoveCamera(#camera,-60, 60, 30,#PB_Absolute)
    CameraLookAt(#camera, 0, 20, 0)
        
    ; Light
    CreateLight(0, RGB(230,230,230), 0, 100, 0) ; general light
    SetLightColor(0, #PB_Light_SpecularColor, RGB(255,255,255))
    
    WorldShadows(#PB_Shadow_Additive , 600, RGB(200,200,200))
    ;WorldDebug(#PB_World_DebugBody)
  
;wwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwww

CreateSphere(#ball, 5)
;CreateMaterial(34, LoadTexture(34, "football.jpg"))
CreateEntity(#ball,MeshID(#ball), MaterialID(3),20,45,0)
ScaleEntity(#ball,0.3,0.3,0.3)    
EntityPhysicBody(#ball, #PB_Entity_SphereBody  , 20, 1, 0)  

CopyMesh(#ball, #axis)
CreateEntity(#axis,MeshID(#axis), MaterialID(3),20, 50, 0)
ScaleEntity(#axis,0.1,0.1,0.1)
EntityPhysicBody(#axis, #PB_Entity_StaticBody)
PointJoint(#joint, EntityID(#ball), 0, 2.5, 0 ,   EntityID(#axis), 0, -2.5, 0)

;WorldGravity(-9.806) ; default
Repeat

      If ExamineMouse()
        MouseX = -MouseDeltaX() * #CameraSpeed * 0.05
        MouseY = -MouseDeltaY() * #CameraSpeed * 0.05
      EndIf
      
     
      ExamineKeyboard()
        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 KeyboardReleased(#PB_Key_Space)
         FreeJoint(#joint)
         ApplyEntityImpulse(#ball,  0, -10, 0,  0,0,0) 
         
       EndIf 
        
        
      RotateCamera(#camera, MouseY, MouseX, 0, #PB_Relative)
      MoveCamera  (#camera, KeyX, 0, KeyY)
      
            
      RenderWorld()
      FlipBuffers()
    Until KeyboardPushed(#PB_Key_Escape) Or Event = #PB_Event_CloseWindow

  
Else
  MessageRequester("Error", "The 3D Engine can't be initialized",0)
EndIf

End
but look for the dilemma in the second example of tubes above (which have more tubes) change the dimension of the plane (line 46) to:
CreatePlane(1,400,100,1,1,1,1) ; ie longer plane
and change the friction of ball in line 92 to 0
EntityPhysicBody(#ball, #PB_Entity_SphereBody , 20, 1, 0)
now when you drop the ball it will move over the plane as if it is on ice with a slight rotation because it is affected by bent tubes many times. but if making the ball friction 1 or more and the plane friction 0 the ball will still rotate over the plane , here i am about to feel bad as i have expected the behaviour as the first case, but i have remembered that the ball have friction and it rotates while falling in the tubes so it exits while rotating and continue rotation over the frictionless plane. so it is solved.
the second issue the behaviour of the flat bodies friction depends also on the the Inclined plane angle and its own friction coefficient, i need to read about it, http://en.wikipedia.org/wiki/Inclined_plane the behaviour under gravity or under applied force or together.
in the following example change the friction for cube and for plane.
in the example you press R to rotate the plane step by step until the cube fall down.
but here is a problem, when the cube dropped down wait 5 seconds and press the R key once rapidly, you will see the cube sink inside the plane, but will not sink if you continue pressing R key, this is why i said to try on previously adjusted inclined planes.
a great topic needs more reading and experiments.
don't forget the friction in help file are on EntityPhysicBody(), GetEntityAttribute(), SetEntityAttribute(), TerrainPhysicBody(#Terrain, Restitution, Friction)
: the friction on the terrain i haven't tried yet, it seems a fun subject . you can use the concise terrain version i have posted here:http://www.purebasic.fr/english/viewtop ... ng#p407718 to do testing.

the example for inclined plane: Edit: look a better way to rotate the plane slowly here http://www.purebasic.fr/english/viewtop ... 36&t=54556 this is by using force like reality and not rotateEntity

Code: Select all

#CameraSpeed = 2

  Global rot.f
  
Enumeration
   #tex = 100
   #mat
   #cube
   #camera
   #axis
   #joint
            
 EndEnumeration
  
Define.f KeyX, KeyY, MouseX, MouseY

If InitEngine3D()
ExamineDesktops()
  
Add3DArchive(".", #PB_3DArchive_FileSystem)
Add3DArchive(#PB_Compiler_Home + "Examples/3D/Data/Textures", #PB_3DArchive_FileSystem)
Add3DArchive(#PB_Compiler_Home + "Examples/Sources\Data", #PB_3DArchive_FileSystem)
Add3DArchive(#PB_Compiler_Home + "Examples/3D/Data/Scripts", #PB_3DArchive_FileSystem)
  
DeskWidth=DesktopWidth(0)
DeskHeight=DesktopHeight(0)
 InitSprite()
  InitKeyboard()
  InitMouse()
    
  OpenWindow(0, 0, 0, DeskWidth, DeskHeight, "press R to rotate the plane and space to apply force_ use keys and mouse to move cam", #PB_Window_MinimizeGadget | #PB_Window_ScreenCentered)
  
  OpenWindowedScreen(WindowID(0), 0, 0, DeskWidth, DeskHeight-70, 0, 0, 0)
  TextGadget(3, 100, DeskHeight-70, 100, 25, "0")

    CreateMaterial(3, LoadTexture(3, "MRAMOR6X6.jpg"))
    MaterialCullingMode(3, #PB_Material_NoCulling )
    
    ;plane 
    CreatePlane(1,400,100,1,1,1,1)
    CreateEntity(1,MeshID(1),MaterialID(3),0,0,0)
    RotateEntity(1, 0, 0, 0)
    EntityPhysicBody(1, #PB_Entity_StaticBody,1,1,1)
    ;cube
    CreateCube( #cube, 1)
    CreateEntity(#cube,MeshID(#cube), MaterialID(3),20,4,0)
    ScaleEntity(#cube,7,3,7) 
    EntityPhysicBody(#cube, #PB_Entity_BoxBody , 1,0.3,1)  
       
    ; Camera 
    CreateCamera(#camera, 0, 0, 100, 100)
    CameraBackColor(#camera, RGB(177,191,245))
    MoveCamera(#camera,-10, 20, 100,#PB_Absolute)
    CameraLookAt(#camera, 0, 20, 0)
        
    ; Light
    CreateLight(0, RGB(230,230,230), 0, 100, 0) ; general light
    SetLightColor(0, #PB_Light_SpecularColor, RGB(255,255,255))
    
    WorldShadows(#PB_Shadow_Additive , 600, RGB(200,200,200))
    ;WorldDebug(#PB_World_DebugBody)
  

;wwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwww

;WorldGravity(-9.806) ; default
;WorldDebug(#PB_World_DebugBody)
Repeat

      If ExamineMouse()
        MouseX = -MouseDeltaX() * #CameraSpeed * 0.05
        MouseY = -MouseDeltaY() * #CameraSpeed * 0.05
      EndIf
      
     
      ExamineKeyboard()
        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 KeyboardReleased(#PB_Key_Space)
         ApplyEntityImpulse(#cube,  -50, 0, 0,  0,0,0) 
         
       EndIf
       
       If KeyboardPushed(#PB_Key_R)
         rot + 0.05
         RotateEntity(1, 0, 0, rot)
         SetGadgetText(3, StrF(rot))
        
        EndIf   
               
      RotateCamera(#camera, MouseY, MouseX, 0, #PB_Relative)
      MoveCamera  (#camera, KeyX, 0, KeyY)
            
      RenderWorld()
      FlipBuffers()
    Until KeyboardPushed(#PB_Key_Escape) Or Event = #PB_Event_CloseWindow

  
Else
  MessageRequester("Error", "The 3D Engine can't be initialized",0)
EndIf

End