Debian Codegraph @

spawn/clouds.gravitspawn

raw
1 -- vim:syntax=lua tabstop=4
2
3 load("functions.lua")
4 load("constants.lua")
5 load("physics.lua")
6
7 function describe()
8 log("some clouds of ligher particles with random velocities")
9 end
10
11
12 -- this spawn script is "proper physics ready".
13
14
15 -- ----------------------------------------------------------------
16 -- ----------------------------------------------------------------
17
18
19 -- rotate vector: alpha around x axis, then beta around z axis
20 function rotvect_xz(pos, alpha, beta)
21 local res1 = v(0,0,0)
22 local res2 = v(0,0,0)
23 -- x-axis
24 --y' = y*cos q - z*sin q
25 --z' = y*sin q + z*cos q
26 res1.y = pos.y * math.cos(alpha) - pos.z * math.sin(alpha)
27 res1.z = pos.y * math.sin(alpha) + pos.z * math.cos(alpha)
28 res1.x = pos.x
29
30 -- z-axis
31 --x' = x*cos q - y*sin q
32 --y' = x*sin q + y*cos q
33 res2.x = res1.x * math.cos(beta) - res1.y * math.sin(beta)
34 res2.y = res1.x * math.sin(beta) + res1.y * math.cos(beta)
35 res2.z = res1.z
36
37 return res2
38 end
39
40
41 function rotvect_xz_inv(pos, alpha, beta)
42
43 local res1 = v(0,0,0)
44 local res2 = v(0,0,0)
45
46 -- z-axis
47 --x' = x*cos q - y*sin q
48 --y' = x*sin q + y*cos q
49 res1.x = pos.x * math.cos(beta) - pos.y * math.sin(beta)
50 res1.y = pos.x * math.sin(beta) + pos.y * math.cos(beta)
51 res1.z = pos.z
52
53 -- x-axis
54 --y' = y*cos q - z*sin q
55 --z' = y*sin q + z*cos q
56 res2.y = res1.y * math.cos(alpha) - res1.z * math.sin(alpha)
57 res2.z = res1.y * math.sin(alpha) + res1.z * math.cos(alpha)
58 res2.x = res1.x
59
60
61 return res2
62 end
63
64
65 -- make rotation around (y)-axis
66 function makerot(realpos, vel, strength, maxradius, avgspeed, alpha, beta)
67
68 -- constants
69 local angular_fraction = 24
70
71 -- rotate postional vector
72 --local pos = realpos
73 local pos = rotvect_xz( realpos, alpha, beta)
74
75 -- compute rotation vector around y-axis for pos
76 local len=vlength(vel)
77
78 local radius=vlength(v(pos.x, 0, pos.z))
79 local radspeed=radius * 2*math.pi / angular_fraction
80
81 local rotvector= v(-pos.z, 0, pos.x)
82 --local rotvector= pos * 0.2 + v(-pos.z, 0, pos.x)
83 rotvector = rotvector * (1.0/vlength(rotvector))
84 rotvector = rotvector * radspeed
85
86 -- to scale down rotvector: strength * agvspeed at maxradius
87 local maxspeed=2* math.pi * maxradius / angular_fraction
88 rotvector= rotvector * (strength * avgspeed / maxspeed)
89
90 -- anti-rotate velocity vector
91 rotvector=rotvect_xz_inv(rotvector, -alpha, -beta)
92
93
94 -- vector add, then restore length
95 -- local rotvel = rotvector
96 local rotvel = vel + rotvector
97 rotvel = rotvel * (1.0 / vlength(rotvel)) * len
98
99 return rotvel
100 end
101
102
103 -- make random cloud
104 function makecloud(org, orgvel, radius, innermass, massmin, massmax, firstparticle, particles, docenter)
105
106 local avg_mass = (massmin+massmax)/2.0
107 local total_mass = (avg_mass * particles)+innermass
108 local avg_vel = math.sqrt(gravit_g * total_mass / radius)
109 --local maxrad=radius * 0.75
110 local maxrad=radius * 0.99
111
112 local center_mode= docenter
113
114 local rotalpha=randomfloat(-math.pi, math.pi)
115 local rotbeta=randomfloat(-math.pi, math.pi)
116 local strength=2^(randomfloat(-1,5))
117 --local strength=2^3
118
119 local mass_mixed_sign = 0
120 -- if (randomint(0,7) > 6) then
121 -- mass_mixed_sign = 1
122 -- end
123
124 if (firstparticle+particles >= spawnparticles) then
125 rotalpha=-math.pi/2 + (randomint(0,1) * math.pi)
126 rotbeta=0
127 end
128
129 if gravit_physics < PH_PROPER then
130 avg_vel = orbit_velocity(total_mass /2 , radius / 2)
131 end
132
133 for i=firstparticle,firstparticle+particles-1 do
134 local len = randomfloat(0.005, maxrad)
135 local pos = randomshell(len)
136
137 --local mass = avg_mass
138 local mass = randomfloat(massmin, massmax)
139
140 local speed
141 local vel=v(0,0,0)
142
143 if (docenter>1) then
144 center_mode= randomint(0,1)
145 end
146
147
148 if ((center_mode < 1) and (len > (10.0 * radius / particles))) then
149 -- orbit velocity
150 speed = orbit_velocity((total_mass) * len / radius, len)
151 if gravit_physics == PH_CLASSIC then
152 speed = speed * math.sqrt(mass * 0.5)
153 end
154
155 -- velocity orthogonal to center
156 vel=randomortho(pos, speed)
157 else
158 -- average equlibrium speed
159 speed= avg_vel
160 if (center_mode < 1) then
161 vel=randomortho(pos, speed)
162 else
163 -- random velocity vector
164 vel = randomshell(speed)
165 end
166 end
167
168 vel=makerot(pos, vel, strength, radius, avg_vel, rotalpha, rotbeta)
169 if (mass_mixed_sign == 1) and (randomint(0,1) > 0) then
170 mass = mass * -1
171 end
172 particle(i, org+pos, orgvel+vel, mass)
173 end
174
175 end
176
177 --------- ==============================================
178 function do_the_cloud(center, mainvel, minrad, maxrad, fakminmass, fakmaxmass, centralorbit, onlyone, startp, parts)
179
180 -- local onlyone = randomint(0,1)
181 -- local onlyone = 1
182 -- local centralorbit = randomint(0,2)
183 -- local centralorbit=0
184 -- local center = v(0,0,0)
185 -- local mainvel =v(0,0,0)
186 -- missing : rotation vector !!
187
188
189 local mass_dev = randomfloat(0, 0.5)
190 --local mass_dev = 0
191
192 local massiveparticles
193 local bigmass
194 local bigradius
195
196 local radius=randomfloat(minrad, maxrad)
197 local target_mass = randomfloat(radius*fakminmass, radius*fakmaxmass)
198
199 if (onlyone > 0) then
200 massiveparticles = 0
201 bigmass = 0
202 else
203 -- massiveparticles = randomint(1, 6)
204 massiveparticles = 2^(randomint(0, 9))
205 bigmass = randomfloat(0.01, 0.1) * target_mass
206 bigradius = randomfloat(0.1, 0.4) * radius
207
208 local bigmassmin=(bigmass/massiveparticles) * (1-mass_dev)
209 local bigmassmax=(bigmass/massiveparticles) * (1+mass_dev)
210 makecloud(center, mainvel, bigradius, 0, bigmassmin, bigmassmax, startp, massiveparticles, 0)
211 end
212
213 local smallmass = (target_mass - bigmass) / spawnparticles
214 local massmin=smallmass * (1-mass_dev)
215 local massmax=smallmass * (1+mass_dev)
216 local innermass = bigmass
217
218 makecloud(center, mainvel, radius, innermass, massmin, massmax, startp + massiveparticles, parts-massiveparticles, centralorbit)
219 return target_mass
220 end
221
222 --------- ==============================================
223 function place_object_cloud(num, center, vel, ratio, start, particles)
224 -- local centralorbit=0
225 local onlyone = 1
226 --local onlyone = randomint(0,1)
227 local centralorbit = randomint(0,2)
228
229 local minrad=200
230 local maxrad=400
231 local fakminmass=7 * ratio
232 local fakmaxmass=14 * ratio
233
234 if gravit_physics < PH_PROPER then
235 fakminmass=fakminmass*12
236 fakmaxmass=fakmaxmass*16
237 end
238
239 if gravit_physics == PH_CLASSIC then
240 fakminmass=fakminmass*8
241 fakmaxmass=fakmaxmass*8
242 minrad=minrad * 2
243 maxrad=maxrad * 6
244 end
245
246 return do_the_cloud(center, vel, minrad, maxrad, fakminmass, fakmaxmass, centralorbit, onlyone, start, particles)
247
248 end
249
250 -- ----------------------------------------------------------------
251 -- ----------------------------------------------------------------
252
253
254 function spawn()
255 local num_objects=randomint(1,6)
256 local min_distance=1000
257
258 if (gravit_physics == PH_CLASSIC) then
259 min_distance = min_distance * 6
260 if (randomint(0,3) > 2) then num_objects=randomint(1,15) end
261 end
262
263 -- first cloud
264 local parts=math.floor(spawnparticles / num_objects)
265 local pos = randomrange(400)
266 local mainvel = v(0,0,0)
267 local mass = place_object_cloud(0, pos, mainvel, 1, 0, parts)
268 local center=pos
269
270 -- spawn some more clouds
271 local pstart=parts
272 for i=1, num_objects-1 do
273 if i==(num_objects-1) then
274 parts = spawnparticles - pstart
275 end
276
277 pos = center + randomshell(min_distance) + randomrange(min_distance/2.5)
278 local dist=distance(pos, center)
279
280 -- compute 2-body eliptic orbit velocity
281 local ratio=1.0/randomfloat(0.9,4)
282 local eccentricity = randomfloat(0.2, 0.8)
283 local smalldist = dist * math.sqrt(1- eccentricity*eccentricity)
284
285 local orbitVel=math.sqrt(gravit_g * mass *(1+ratio) * ((2/dist) - (1/smalldist)))
286
287 if (gravit_physics < PH_PROPER) or (randomint(0,1)>0) then
288 orbitVel = orbit_velocity(mass, dist)
289 end
290 if gravit_physics == PH_MODIFIED then
291 orbitVel = orbitVel * 0.5 * math.sqrt(mass/2);
292 end
293
294 mainvel=randomortho(pos - center, orbitVel * randomfloat(0.5, 0.94))
295
296 -- create cloud
297 mass = place_object_cloud(i, pos, mainvel, ratio, pstart, parts)
298 center=pos
299 pstart=pstart+parts
300 end
301 end
302