fill the tank with Balls
Posted: Wed Jul 16, 2014 10:44 am
then rotate the tank to let the balls flow at once to the ground
notes:1- it is too important to tilt the tank slightly so the balls will not accumulate vertically at the beginning: RotateEntity(#cyl, 0,0,1)
2- we have done illegal slight move for a static object (the tank), this will not move the tank but its purpose is to give the balls some action (that is cheating the engine and utilizing illegal action for our benefit, the tank can be considered as vibrating internally when we do this)
3- i have found that the balls with radius 0.3 is the best for this example. radius 0.2 works. but radius 0.1 will penetrate the tank walls. so we can check different tank meshes with different thickness to see what is more suitable for a specific spheres.


tested using PureBasic 5.30 beta 7
notes:1- it is too important to tilt the tank slightly so the balls will not accumulate vertically at the beginning: RotateEntity(#cyl, 0,0,1)
2- we have done illegal slight move for a static object (the tank), this will not move the tank but its purpose is to give the balls some action (that is cheating the engine and utilizing illegal action for our benefit, the tank can be considered as vibrating internally when we do this)
3- i have found that the balls with radius 0.3 is the best for this example. radius 0.2 works. but radius 0.1 will penetrate the tank walls. so we can check different tank meshes with different thickness to see what is more suitable for a specific spheres.


tested using PureBasic 5.30 beta 7
Code: Select all
#CameraSpeed = 1
#cyl = 200
#ball = 210
#mat = 220
#ballsReflector = 230
Global Dim ball(1000)
Global ballNum
Define.f KeyX, KeyY, MouseX, MouseY
ExamineDesktops()
OpenWindow(0, 0, 0, DesktopWidth(0), DesktopHeight(0), "....Space to throw balls,...... Z to rotate the Tank, ............ mouse and arrow keys to move the cam ", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
;Initialize environment
InitEngine3D()
InitSprite()
InitKeyboard()
InitMouse()
OpenWindowedScreen(WindowID(0), 0, 0, DesktopWidth(0), DesktopHeight(0)-4, 0, 0, 0)
Add3DArchive(#PB_Compiler_Home + "Examples/3D/Data/Textures/",#PB_3DArchive_FileSystem)
Add3DArchive(#PB_Compiler_Home + "Examples/3D/Data/Scripts",#PB_3DArchive_FileSystem)
Add3DArchive(#PB_Compiler_Home + "Examples/3D/Data/Packs/desert.zip", #PB_3DArchive_Zip)
Parse3DScripts()
; transparent Texture from example CreateTextureAlpha.pb in Purebasic 3D folder
CreateTexture(#mat, 256, 256)
StartDrawing(TextureOutput(#mat))
DrawingMode(#PB_2DDrawing_AllChannels | #PB_2DDrawing_AlphaBlend)
Box(0, 0, 256, 256, RGBA(0, 0, 0, 255))
Box(0, 0, 256, 256, RGBA(100, 255, 0, 70))
;Circle(127, 127, 50, RGBA(0, 0, 0, 0))
StopDrawing()
CreateMaterial(#mat, TextureID(#mat))
MaterialBlendingMode(#mat, #PB_Material_AlphaBlend)
MaterialCullingMode(#mat, #PB_Material_NoCulling)
DisableMaterialLighting(#mat, 1)
WorldShadows(#PB_Shadow_Modulative, -1, RGB(127, 127, 127))
;Materials
CreateMaterial(0, LoadTexture(0, "Wood.jpg"))
GetScriptMaterial(1, "SphereMap/SphereMappedRustySteel")
CreateMaterial(2, LoadTexture(2, "Dirt.jpg"))
GetScriptMaterial(3, "Scene/GroundBlend")
MaterialBlendingMode(0, #PB_Material_AlphaBlend)
MaterialCullingMode(0, #PB_Material_NoCulling )
DisableMaterialLighting(0, 1)
; create the Tank
CreateCylinder(#cyl,3,10,2,0,0) ; "2" specify a square tank look the Docs.
CreateEntity(#cyl, MeshID(#cyl), MaterialID(#mat), 0,9.5,0)
RotateEntity(#cyl, 0,0,1)
EntityPhysicBody(#cyl, #PB_Entity_StaticBody, 1, 0.1, 1)
; create the Balls
CreateSphere(#ball, 0.3 )
For i = 0 To 1000
ball(i) = CreateEntity(#PB_Any, MeshID(#ball),#PB_Material_None)
MoveEntity(ball(i), 0,14,0, #PB_Absolute)
Next
; Camera
;
CreateCamera(0, 0, 0, 100, 100)
MoveCamera(0, -30, 20, -20, #PB_Absolute)
CameraLookAt(0, 0 , 0 , 0)
; Skybox
;
SkyBox("desert07.jpg")
; Light
;
CreateLight(0, RGB(255, 255, 255), 100, 800, -500)
AmbientColor(RGB(20, 20, 20))
CreateCube(#ballsReflector,1)
CreateEntity(#ballsReflector, MeshID(#ballsReflector),MaterialID(0),0,15,0)
ScaleEntity(#ballsReflector, 3,1,3)
EntityPhysicBody(#ballsReflector, #PB_Entity_StaticBody, 1, 0.1, 1)
;ground
CreateEntity(3,MeshID(#ballsReflector),MaterialID(3), 0, 0, 0)
ScaleEntity(3,40,2,40)
EntityRenderMode(3, 0)
EntityPhysicBody(3, #PB_Entity_StaticBody, 1, 1, 1)
MoveEntity(#cyl,0,0.5,0) ; just a cheat to make the tank vibrating internaly to keep the balls busy and active a little
Repeat
If ExamineMouse()
MouseX = -MouseDeltaX() * #CameraSpeed * 0.05
MouseY = -MouseDeltaY() * #CameraSpeed * 0.05
EndIf
ExamineKeyboard()
If KeyboardPushed(#PB_Key_Space) ; throwing balls by giving it physical properties
If done = 0
ballNum+1
If ballNum = 1000: done = 1: EndIf
EntityPhysicBody(ball(ballNum),#PB_Entity_SphereBody,1,0.5,1)
;DisableEntityBody(ball(ballNum), 0)
EndIf
EndIf
If KeyboardPushed(#PB_Key_Z)
RotateEntity(#cyl, 0,0,2,#PB_Relative)
For i=1 To 1000
DisableEntityBody(ball(i),0)
Next
MoveEntity(#cyl,0,3,0) ; a cheat, look line 90
EndIf
If KeyboardPushed(#PB_Key_Left)
KeyX = -#CameraSpeed
ElseIf KeyboardPushed(#PB_Key_Right)
KeyX = #CameraSpeed
Else
KeyX = 0
EndIf
If KeyboardPushed(#PB_Key_Up)
KeyY = -#CameraSpeed
ElseIf KeyboardPushed(#PB_Key_Down)
KeyY = #CameraSpeed
Else
KeyY = 0
EndIf
MoveCamera (0, KeyX, 0, KeyY)
RotateCamera(0, MouseY, MouseX, 0, #PB_Relative)
RenderWorld()
FlipBuffers()
Repeat
event = WindowEvent()
If event = #PB_Event_CloseWindow
quit = #True
EndIf
Until event = 0 Or quit = #True
Until KeyboardPushed(#PB_Key_Escape) Or Quit = 1
End