July 25, 2021, 17:23:41

### Author Topic: [bb] simple ball physics by bradford6 [ 1+ years ago ]  (Read 860 times)

#### BlitzBot

• Jr. Member
• Posts: 1
##### [bb] simple ball physics by bradford6 [ 1+ years ago ]
« on: June 29, 2017, 00:28:41 »
Title : simple ball physics
Posted : 1+ years ago

Description : Types and simple ball physics

Code :
Code: BlitzBasic
1. ; this is an experiment in physics and types
3. ; from the outset I am attempting to attach physical properties to 3d objects
4. ; using Blitz's TYPE command set. I am new to this (about 1 week) so if you find a better
5. ; method of doing this or improve my code, please let me know b_radford@yahoo.com
6. ;
8.
9. ; set up the display
10. ; graphics
11.
12. If Windowed3D ()  ; see if the Graphics card supports 3D in  a window, if not go to fullscreen
13.         Graphics3D 640, 480, 0, 2
14. Else
15.         Graphics3D 640, 480, 0, 1
16. EndIf
17.
18. SetBuffer BackBuffer () ; point all drawing to the hidden back buffer which we will eventually FLIP
19.
20. light=CreateLight()
21. cam=CreateCamera()
22. MoveEntity cam,20,10,-20
23.
24. ; VARIABLE LIST
25. Gravity# = .01
26. Friction# = .99
27.
28.
29.
30. ; End OF VARIABLE LIST
31.
32.
33. Gosub create_objects
34. Gosub define_types
35. y#=20
36.
37. Repeat
38.
39.
40. For thing.ball = Each ball  ; cycle through all the "balls" and set the position
41.
42. thingposy#=thingposy#+thingyvel#  ;  yposition = yposition + Velocity
43. thingyvel#=thingyvel#-gravity#     ;  velocity = velocity - gravity
44.
45. ; if the ball hits the ground, convert the velocity value to a positive number with the ABS() function
46. ; and multiply it by the balls elasticity value to dissipate some of the energy. .1=flab 1.1=flubber
47. If thingposy#<1 Then thingyvel#=Abs(thingyvel#) * thingelasticity#
48.
49. PositionEntity thingentity,thingposx#,thingposy#,thingposz#
50.
51. Next
52.
53.
54.
55.
56.
57. RenderWorld
58. UpdateWorld
59. Text 0,0, y#
60. Flip
61.
62. Until KeyHit(1)=1 ; keep looping (repeating) until the Escape key is hit
63.
64.
65. ; *************************************************
66. .create_objects ; label for the gosub command to find this subroutine
67. ballmodel=CreateSphere(6)
68. ;HideEntity ballmodel
69. PositionEntity ballmodel,0,50,0
70. plane=CreatePlane() ; CreatePlane ( [sub_divs][,parent] )
71. EntityColor plane,240,5,5
72.
73. Return ;
74. ; **************************************************
75.
76. .define_types
77.
78. Type ball
79. Field posx#,posy#,posz#
80. Field entity
81. Field mass,weight,size
82. Field xvel#,yvel#,zvel#
83. Field elasticity#,alpha#
84.
85. End Type
86.
87.
88. For x=1 To 8                            ; number of balls (8x8=64)
89. For z=1 To 8
90. thing.ball = New ball
91. thingentity = CopyEntity(ballmodel)
92. thingposx# = x*4                       ; set the x position
93. thingposy# = 30                                 ;
94. thingposz# = z*4                                                ; set the z positon
95. thingelasticity#=Rnd(.7,.89)
96. EntityColor thingentity,Rnd(1,255),Rnd(1,255),Rnd(1,255)
97. PositionEntity thingentity,thingposx#,thingposy#,thingposz#
98. thingalpha# = .6
99. EntityAlpha thingentity,thingalpha#
100.
101. Next
102. Next
103.
104.
105.
106. Return