76 lines
		
	
	
		
			1.6 KiB
		
	
	
	
		
			Lua
		
	
	
	
	
	
			
		
		
	
	
			76 lines
		
	
	
		
			1.6 KiB
		
	
	
	
		
			Lua
		
	
	
	
	
	
| BodyList = {}
 | |
| Body = class(nil,{type=0})
 | |
| 
 | |
| function newBody(data)
 | |
| 	local o = {}
 | |
| 	-- coordinates
 | |
| 	o.x = data.pos[1] or 0
 | |
| 	o.y = data.pos[2] or 0
 | |
| 	o.z = data.pos[3] or 0
 | |
| 	-- velocities
 | |
| 	o.vx = data.vel[1] or 0
 | |
| 	o.vy = data.vel[2] or 0
 | |
| 	o.vz = data.vel[3] or 0
 | |
| 	-- accceleration
 | |
| 	o.ax = 0
 | |
| 	o.ay = 0
 | |
| 	o.az = 0
 | |
| 	-- mass
 | |
| 	o.mass = data.mass or 1
 | |
| 	-- size
 | |
| 	o.size = data.size or VIEW.planet_size*math.sqrt(data.mass)
 | |
| 	-- color
 | |
| 	o.color = data.color or {1,1,1}
 | |
| 	o.max_distance = 0
 | |
| 	-- origin
 | |
| 	o.origin = data.origin or nil
 | |
| 	table.insert(BodyList,o)
 | |
| end
 | |
| 
 | |
| function UpdateBodiesAcceleration()
 | |
|   for n, body in pairs(BodyList) do
 | |
|     local ax = 0
 | |
|     local ay = 0
 | |
|     local az = 0
 | |
|     for o_n, other_body in pairs(BodyList) do
 | |
|       if n ~= o_n then
 | |
| 
 | |
|         local dx = other_body.x - body.x
 | |
|         local dy = other_body.y - body.y
 | |
|         local dz = other_body.z - body.z
 | |
| 
 | |
|         local d3 = dx*dx + dy*dy + dz*dz
 | |
|         if math.sqrt(d3)/3 > body.max_distance then body.max_distance = math.sqrt(d3)/3 end
 | |
| 
 | |
|         local f =
 | |
|           (CONST_G * other_body.mass) /
 | |
|           d3 * math.sqrt(d3 + CONST_S)
 | |
| 
 | |
|         ax = ax + dx * f
 | |
|         ay = ay + dy * f
 | |
|         az = az + dz * f
 | |
|       end
 | |
|     end
 | |
| 
 | |
|     body.ax = ax
 | |
|     body.ay = ay
 | |
|     body.az = az
 | |
|   end
 | |
| end
 | |
| 
 | |
| function UpdateBodiesVelocity(dt)
 | |
|   for _, body in pairs(BodyList) do
 | |
|     body.vx = body.vx + body.ax * dt
 | |
|     body.vy = body.vy + body.ay * dt
 | |
|     body.vz = body.vz + body.az * dt
 | |
|   end
 | |
| end
 | |
| 
 | |
| function UpdateBodiesPosition(dt)
 | |
|   for _, body in pairs(BodyList) do
 | |
|     body.x = body.x + body.vx * dt
 | |
|     body.y = body.y + body.vy * dt
 | |
|     body.z = body.z + body.vz * dt
 | |
|   end
 | |
| end
 |