ball moving inside pipes
Posted: Mon Apr 29, 2013 10:52 am
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:

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.


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

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.


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
