Lotto Machine simulation

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

Lotto Machine simulation

Post by applePi »

Lotto machine simulation
make a transparent big sphere as a container with a hole in it, put inside it many small balls which have numbers from 0-9 . press space key to start the jumping balls,
the big spherical containder made using the Comtois procedure CreateSphere2 , because i want to make a hole in it http://purebasic.fr/english/viewtopic.p ... 15#p418885

now when you press space we move the bigSphere container up slightly. and this will push the balls giving them the power to jump, this depends on Restitution which are here 0.2 line 113 : EntityPhysicBody(100+i, #PB_Entity_SphereBody , 1, 0.2, 1)
now why these balls free energy continue, in my artistic Analysis the reason is this:
the bigSphere have a static physics (#PB_Entity_StaticBody) and it should not move, but when we press space key we moved it using MoveEntity up, so the bullet engine don't know what to do so it considers the bigSphere as vibrating between the original position and the current position after the push. change line 169 MoveEntity(#bigsphere,0,4,0) to MoveEntity(#bigsphere,0,7,0) and press space and you will see very powerful balls filling the bigSphere and it will penetrate the sphere walls rapidly, this is because the vibrating are more powerfull between the previous bigSphere position and the current one.
this is my theory
this is version 0.001, i have more ideas but for a future versions.
Image

Code: Select all

Declare CreateSphere2(No, mRadius, mNumRings, mNumSegments) ; ; the new CreateSphere2 procedure by Comtois changed in some parts, the Goto function added by me.
Enumeration
   #Tex = 50
   #MAT
   #MAT_plane
   #plane
   #LIGHT
   #CAMERA
   #mainwin
   #ball
   #cube
   #cube2
   #cube3
   #cube4
   #bigSphere
EndEnumeration
Global Quit.b = #False
Global x.f = 0
Global y.f = 10
Global z.f = -30
Global zoom.f = 13
Global.f ro = -35, rotVal=1
Global ballNum.l
Speed = 0.5
ExamineDesktops()
If OpenWindow(#mainwin, 0, 0, DesktopWidth(0), DesktopHeight(0), "space to start the machine, UP/Down to zoom _ version 0.001 , Z/A rotate camera  ", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)

;Initialize environment
InitEngine3D()
InitSprite()
InitKeyboard()
OpenWindowedScreen(WindowID(#mainwin), 0, 0, DesktopWidth(0), DesktopHeight(0)-4, 0, 0, 0)
    
  
EnableWorldPhysics(#True)
EnableWorldCollisions(#True)
SetFrameRate(60)

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/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, 20)) 
      ;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)
      

CreateMaterial(#MAT_plane, LoadTexture(#MAT_plane, "Geebee2.bmp"))
;CreatePlane(#Mesh, TileSizeX, TileSizeZ, TileCountX, TileCountZ, TextureRepeatCountX, TextureRepeatCountZ)
CreatePlane(#plane, 10, 10, 1, 1, 1, 1)
CreateEntity (#plane, MeshID(#plane), MaterialID(#MAT_plane),  0, -1.5,-0.7)
RotateEntity(#plane, 6,0,0)

EntityPhysicBody(#plane, #PB_Entity_StaticBody )  

CreateLight(0,RGB(255,255,255),-100,40,30)
AmbientColor(RGB(255,255,255))

CreateCamera(#CAMERA, 0, 0, 400, 400)
MoveCamera(#CAMERA, 0, 4, 13)
RotateCamera(#CAMERA, -15, 0, 0)
;the mother of the jumping balls witch we will make 500 copies
CreateSphere(#ball, 1)
CreateEntity(#ball, MeshID(#ball),#PB_Material_None )
ScaleEntity(#ball, 0.1, 0.1, 0.1)
MoveEntity(#ball, 0,2,3) 
EntityPhysicBody(#ball, #PB_Entity_SphereBody  , 1, 0.3, 1)

;material for the jumping balls
LoadTexture(0, "white.jpg")
CreateMaterial(0, TextureID(0))

DisableMaterialLighting(0, 1)

; make 9 textures
For i=1 To 9
  CopyTexture(0,i)
  CopyMaterial(0,i)
  AddMaterialLayer(i, TextureID(i), #PB_Material_AlphaBlend        )
  
Next

MaterialCullingMode(0, #PB_Material_NoCulling)
Font = LoadFont(#PB_Any, "Arial"  ,  72, #PB_Font_Bold)
ballNum.l ; assign numbers from 0 to 9 to the balls
;make 501 small balls and assign numbers from 0 to 9 to the balls
For i=0 To 500
  CopyEntity(#ball , 100+i) ; entities numbering begins from 100
      x.f = Random(1, 0)
      y.f= Random(3, 0)
      z.f = Random(2, 0)
    StartDrawing(TextureOutput(ballNum))
      DrawingFont(FontID(Font)) 
      ballNum = ballNum+1
      DrawText(0, 0, Str(ballNum), RGB(0, 0, 0),RGB(255, 255, 255))
      If ballNum = 10: ballNum=0:EndIf
      
      SetEntityMaterial(100+i, MaterialID(ballNum))

   StopDrawing()

  MoveEntity(100+i, x,y,z)
  EntityPhysicBody(100+i, #PB_Entity_SphereBody  , 1, 0.2, 1)
Next

CreateSphere2(#bigSphere, 3, 20, 20)
CreateEntity(#bigSphere, MeshID(#bigSphere), MaterialID(#MAT), 0,3.5,1 )
EntityPhysicBody(#bigSphere, #PB_Entity_StaticBody  , 1, 2, 2)
CreateCube(#cube, 1)
CreateEntity(#cube, MeshID(#cube),#PB_Material_None, 0.7,-1.5,2) 
ScaleEntity(#cube, 10,1,0.2)
RotateEntity(#cube,0,90,0)
CopyEntity(#cube, #cube2)
CopyEntity(#cube, #cube3)
MoveEntity(#cube2, -0.3,-1.5,2)
RotateEntity(#cube2, 0,90,0)
ScaleEntity(#cube3, 0.1,0.4,0.1)
MoveEntity(#cube3, 0,-1.7,4)
SetEntityMaterial(#cube3, MaterialID(#mat))

EntityPhysicBody(#cube, #PB_Entity_StaticBody ) 
EntityPhysicBody(#cube2, #PB_Entity_StaticBody ) 
EntityPhysicBody(#cube3, #PB_Entity_StaticBody ) 

EndIf
WorldGravity(-9.8)
;WorldGravity(0)

SkyBox("desert07.jpg")
;Main loop
Repeat
  
  Event = WindowEvent()
  rot.f+rotVal
  RotateEntity(#bigSphere, rot,0,0)   
  RenderWorld()
   
  FlipBuffers()

  ExamineKeyboard()
  If KeyboardPushed(#PB_Key_Up)
    zoom-0.5
    MoveCamera(#camera, 0,4, zoom, #PB_Absolute)
  ElseIf KeyboardPushed(#PB_Key_Down)
    zoom+0.5
    MoveCamera(#camera, 0,4, zoom,#PB_Absolute)
  ElseIf KeyboardPushed(#PB_Key_Z) 
    ro-0.5
    RotateCamera(#camera,ro,0,0)
  ElseIf KeyboardPushed(#PB_Key_A)  
    ro+0.5
    RotateCamera(#camera,ro,0,0)
  EndIf  
        
   If KeyboardReleased(#PB_Key_Escape)
      Quit = #True
    EndIf
    If KeyboardReleased(#PB_Key_Space)
      MoveEntity(#bigsphere,0,4,0)
    
      EndIf

    Until Quit = #True Or Event = #PB_Event_CloseWindow
    
    
  ; CreateSphere2 routines  
  Procedure Build(Radius, NumRings, NumSegments)
  DeltaRingAngle.f = #PI / NumRings ; for full sphere
  ;DeltaRingAngle.f = #PI / NumRings/2  ; for half sphere
  DeltaSegAngle.f = #PI*2 / NumSegments
  offset = 0
  
  For ring = 0 To NumRings
    r0.f = Radius * Sin(ring * DeltaRingAngle)
    y0.f = Radius * Cos(ring * DeltaRingAngle)
    
    For seg = 0 To NumSegments;
      x0.f = r0 * Sin(seg * DeltaSegAngle)
      z0.f = r0 * Cos(seg * DeltaSegAngle)
      
      MeshVertexPosition(x0, y0, z0)
      MeshVertexNormal(x0, y0, z0); should be normalized if Radius <> 1
      MeshVertexTextureCoordinate(seg / NumSegments, ring / NumRings)
      
      If ring <> NumRings 
        ;bypass the segments 0,1,20 to make a hole in the sphere
        If (seg = 0 Or seg = 1 Or seg = 20 ) And ring = 1:Goto jmp:EndIf
                       
        If seg <> NumSegments
          
          ;If ring <> NumRings-1
          If ring <> NumRings
            MeshFace(offset + NumSegments + 2, offset, offset + NumSegments + 1)
          EndIf  
          If ring <> 0
            MeshFace(offset + NumSegments + 2, offset + 1, offset)   
          EndIf  
        EndIf
        jmp:
        offset + 1
      EndIf
    Next
  Next  
EndProcedure

Procedure CreateSphere2(No, Radius, NumRings, NumSegments)
  
  CreateMesh(No)
  
  Build(Radius, NumRings, NumSegments)
  
  ;AddSubMesh()
  
  ;Build(Radius, NumRings, NumSegments)
  
  FinishMesh(#True)
  UpdateMeshBoundingBox(0)
  
EndProcedure

DataSection
  Data.i 255, 0, 0, 127
  Data.f -20, 0, 5
  Data.i 0, 0, 255, 127
  Data.f 20, 0, 5 
  Data.i 0, 255, 0, 127
  Data.f 0, 40, -5
EndDataSection  

   
    
applePi
Addict
Addict
Posts: 1404
Joined: Sun Jun 25, 2006 7:28 pm

Re: Lotto Machine simulation

Post by applePi »

another demo of this vibrations using together an object with a static physics and MoveEntity is this hammer and ball, the hammer are borrowed from Kelebrindae example , there is a sphere near the hammer, and when you press 1 or 2 or 3 or 4 keys you will run this:

Code: Select all

If KeyboardReleased(#PB_Key_1)
      ApplyEntityImpulse(hammerhead,  1, 0, 0)
      ; -1.5, 1.5, 0 ;original sphere position
      MoveEntity(300, 2,1.5,0)
    ElseIf KeyboardReleased(#PB_Key_2)
      ApplyEntityImpulse(hammerhead,  1, 0, 0)
      MoveEntity(300, 4,1.5,0)
    ElseIf KeyboardReleased(#PB_Key_3)
      ApplyEntityImpulse(hammerhead,  1, 0, 0)
        MoveEntity(300, 6,1.5,0)
    ElseIf KeyboardReleased(#PB_Key_4)
        ApplyEntityImpulse(hammerhead,  1, 0, 0)
        MoveEntity(300, 10,1.5,0) ; most energy
EndIf
note the last line gives the most power to the sphere, note ApplyEntityImpulse have the same value and using it alone the hammer will stop by gravity but with MoveEntity the sphere will be Energized for ever (and not moved visually because it is static i compare this with elasticity of solid objects).
the effect after you press 1/2/3/4 will be after the hammer touch the sphere. you don't need ApplyEntityImpulse and its only usage is when the hammer are completely stationary
Image

Code: Select all

;the hammer machine are by Kelebrindae
;the experiments are by applepi
;- initialization
InitEngine3D()
InitSprite()
InitKeyboard()

;- Window
ExamineDesktops()
OpenWindow(0, 0, 0, DesktopWidth(0), DesktopHeight(0), "press 1/2/3/4 to energize the ball,..... 1 . is the smallest power", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
OpenWindowedScreen(WindowID(0), 0, 0, DesktopWidth(0), DesktopHeight(0)-40, 0, 0, 0)

;-Texture
Add3DArchive(".",#PB_3DArchive_FileSystem)

CreateTexture(0, 128, 128)
StartDrawing(TextureOutput(0))
;DrawingMode(#PB_2DDrawing_AllChannels | #PB_2DDrawing_AlphaBlend)
Box(0, 0, 128, 128, $FFFFFF)
StopDrawing()
;-Material
CreateMaterial(1,TextureID(0))
;MaterialAmbientColor(1,#PB_Material_AmbientColors)
SetMaterialColor(1, #PB_Material_AmbientColor, RGB(100,100,100))
CreateMaterial(2,TextureID(0))
;MaterialAmbientColor(2,$FFFF00)
SetMaterialColor(2, #PB_Material_AmbientColor, $FFFF00)
CreateMaterial(3,TextureID(0))
;MaterialAmbientColor(3,$0077FF)
SetMaterialColor(3, #PB_Material_AmbientColor, $0077FF)
CreateMaterial(4,TextureID(0))
;MaterialAmbientColor(4,$FF00FF)
SetMaterialColor(4, #PB_Material_AmbientColor, $FF00FF)
CreateMaterial(5,TextureID(0))
;MaterialAmbientColor(5,$0055BB)
SetMaterialColor(5, #PB_Material_AmbientColor, $FF00FF)

;- Entities
CreatePlane(1,30,30,20,20,1,1)
plane  = CreateEntity(#PB_Any, MeshID(1), MaterialID(1))
EntityPhysicBody(plane, #PB_Entity_StaticBody)   

CreateCube(2, 1.0)

; Mobile
Global hammerX.f = 0,hammerY.f = 8.5,hammerZ.f=0
CreateSphere(4,0.4)
axe = CreateEntity(#PB_Any, MeshID(4), MaterialID(2))
MoveEntity(axe,hammerX,hammerY,hammerZ)
EntityPhysicBody(axe, #PB_Entity_StaticBody)

shaft = CreateEntity(#PB_Any, MeshID(2), MaterialID(5))
ScaleEntity(shaft,0.3,6,0.3)
MoveEntity(shaft,hammerX,hammerY - 3.5,hammerZ)
EntityPhysicBody(shaft, #PB_Entity_BoxBody,0.1)

hammerhead = CreateEntity(#PB_Any,MeshID(2), MaterialID(2))
ScaleEntity(hammerhead,2,1,1)
MoveEntity(hammerhead,hammerX,hammerY - 7,hammerZ)
EntityPhysicBody(hammerhead,#PB_Entity_BoxBody,20)

PointJoint(0,EntityID(shaft),0,-3.1 ,0, EntityID(hammerhead),0,0.5,0) ; Attach the hammerhead to the shaft
; Attach the hammer to axis.
HingeJoint(1,EntityID(axe),0,0,0, #False,#False,#True, EntityID(shaft),0,3.5,0, #False,#False,#True)
HingeJoint(2,EntityID(axe),0,0,0, #False,#False,#True, EntityID(hammerhead),0,7,0, #False,#False,#True)


;- Camera
CreateCamera(0, 0, 0, 100, 100)
MoveCamera(0,-2,8,18)
CameraLookAt(0,-2,5,0)

;-Light
AmbientColor($666666)
CreateLight(0,RGB(160,160,255),200,400,200)
WorldShadows(#PB_Shadow_Additive)

KeyboardMode(#PB_Keyboard_International)
;ApplyEntityImpulse(hammerhead,  20, 0, 0) ; not necessary anymore since 4.60 RC2
CreateSphere(300,1)
CreateEntity(300,MeshID(300),#PB_Material_None ,-1.7,1.5,0)
EntityPhysicBody(300, #PB_Entity_StaticBody , 1, 0.5, 1)
;RotateEntity(300,0,45,0)
Global gravity.f = 9.8
WorldGravity(-gravity)    
;- Main loop
Repeat
  Delay(1)
  While WindowEvent() : Wend

  ;- F1, F2, F3 : Change view
  If ExamineKeyboard()
    If KeyboardReleased(#PB_Key_F1)
      MoveCamera(0,-18,2,0)
      CameraLookAt(0,0,3,0)
    EndIf
    If KeyboardReleased(#PB_Key_F2)
      MoveCamera(0,0,12,20)
      CameraLookAt(0,0,5,0)
    EndIf
    If KeyboardReleased(#PB_Key_F3)
      MoveCamera(0,1,5,10)
      CameraLookAt(0,-3,3,0)
    EndIf
   
    ;- [Space] : Throw hammer
    If KeyboardPushed(#PB_Key_Space)
      ApplyEntityImpulse(hammerhead,  5, 0, 0)
    EndIf
    ;- R : Reset ball's position (doesn't work)
    If KeyboardReleased(#PB_Key_R)
      FreeEntity(ball)
    EndIf
    
    If KeyboardReleased(#PB_Key_1)
      ApplyEntityImpulse(hammerhead,  1, 0, 0)
      ; -1.5,1.5,0
      MoveEntity(300, 2,1.5,0)
    ElseIf KeyboardReleased(#PB_Key_2)
      ApplyEntityImpulse(hammerhead,  1, 0, 0)
      MoveEntity(300, 4,1.5,0)
    ElseIf KeyboardReleased(#PB_Key_3)
      ApplyEntityImpulse(hammerhead,  1, 0, 0)
        MoveEntity(300, 6,1.5,0)
      ElseIf KeyboardReleased(#PB_Key_4)
        ApplyEntityImpulse(hammerhead,  1, 0, 0)
        MoveEntity(300, 10,1.5,0)
      
    EndIf  
   
    ;- Return : Display FPS
    If KeyboardReleased(#PB_Key_Return)
      MessageRequester("Infos","FPS = " + Str(Engine3DFrameRate(#PB_Engine3D_Average)))
    EndIf

  EndIf

  ; Render
  RenderWorld()
  FlipBuffers()

Until KeyboardPushed(#PB_Key_Escape)

End
User avatar
DK_PETER
Addict
Addict
Posts: 904
Joined: Sat Feb 19, 2011 10:06 am
Location: Denmark
Contact:

Re: Lotto Machine simulation

Post by DK_PETER »

Thanks Applepi.
Always nice to study new examples :-)
Current configurations:
Ubuntu 20.04/64 bit - Window 10 64 bit
Intel 6800K, GeForce Gtx 1060, 32 gb ram.
Amd Ryzen 9 5950X, GeForce 3070, 128 gb ram.
IdeasVacuum
Always Here
Always Here
Posts: 6426
Joined: Fri Oct 23, 2009 2:33 am
Location: Wales, UK
Contact:

Re: Lotto Machine simulation

Post by IdeasVacuum »

I think you have got a quad-core brain applePi, the amount of code examples you produce is prolific! 8)
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
applePi
Addict
Addict
Posts: 1404
Joined: Sun Jun 25, 2006 7:28 pm

Re: Lotto Machine simulation

Post by applePi »

thanks DK_PETER and IdeasVacuum
I think you have got a quad-core brain applePi
i wish i have a good working single core and up, i have a tremendous low neurotransmitters between the brain neurons due to an old and severe Influenza so i can't focus more than 30 minutes then i need to rest for another 30 minutes and so on. my strategy depends on guessing and on using others code and fitting it so not to write the code again, i never remembers the functions syntax so i copy it from the available examples.
i'm always using the examples by other users and i just filling a few lines in suitable places such as the above code are all from Kelebrindae and i have added just a few lines. and still there are too much areas in purebasic i have not read yet, it is a very huge and vast language even it has a small size, it is like a black hole very dense and too heavy even it is too small , not to forget the huge number of libraries available.
Post Reply