Advertisement
1m1m0

Simple Celestial Simulator

Mar 10th, 2024
42
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.48 KB | None | 0 0
  1. -- Initialize physics
  2. local physics = require("physics")
  3. physics.start()
  4. physics.setGravity(0, 0) -- Set gravity to 0
  5.  
  6. -- Core Variables
  7. local screenW = display.contentWidth
  8. local screenH = display.contentHeight
  9.  
  10. scale = 0.75 -- Factor to propotionally resize objects, size and mass included
  11.  
  12. -- Table to keep track of spheres
  13. local spheres = {}
  14.  
  15. -- Function to create a new sphere with a random color and trail
  16. local function createSphere(x, y, radius, mass)
  17.     -- Random color components
  18.     local r = math.random()
  19.     local g = math.random()
  20.     local b = math.random()
  21.  
  22.     local sphere = display.newCircle(x, y, radius)
  23.     sphere:setFillColor(r, g, b) -- Set the random color
  24.     physics.addBody(sphere, {radius=radius, density=mass/(math.pi*radius*radius), bounce=math.random(0.0, 0.5)})
  25.     sphere.mass = mass
  26.     table.insert(spheres, sphere)
  27.  
  28.     -- [[ Trail effect
  29.     local trailGroup = display.newGroup()
  30.  
  31.     function sphere:enterFrame(event)
  32.         local trail = display.newCircle(trailGroup, self.x, self.y, radius * 0.3)
  33.         trail:setFillColor(r, g, b, 0.5) -- Slightly transparent version of the sphere's color
  34.         transition.to(trail, {time=500, alpha=0, onComplete=function() display.remove(trail) end})
  35.     end
  36.     --]]
  37.     Runtime:addEventListener("enterFrame", sphere)
  38.  
  39.     -- Make spheres draggable
  40.     function sphere:touch(event)
  41.         if event.phase == "began" then
  42.             display.getCurrentStage():setFocus(self, event.id)
  43.             self.isFocus = true
  44.             self.markX = self.x
  45.             self.markY = self.y
  46.         elseif self.isFocus then
  47.             if event.phase == "moved" then
  48.                 self.x = event.x - event.xStart + self.markX
  49.                 self.y = event.y - event.yStart + self.markY
  50.             elseif event.phase == "ended" or event.phase == "cancelled" then
  51.                 display.getCurrentStage():setFocus(self, nil)
  52.                 self.isFocus = false
  53.             end
  54.         end
  55.         return true
  56.     end
  57.  
  58.     sphere:addEventListener("touch", sphere)
  59.     return sphere
  60. end
  61.  
  62. -- Function to update gravitational forces
  63. local function updateGravity()
  64.     for i = 1, #spheres do
  65.         for j = i+1, #spheres do
  66.             local sphere1 = spheres[i]
  67.             local sphere2 = spheres[j]
  68.             local dx = sphere2.x - sphere1.x
  69.             local dy = sphere2.y - sphere1.y
  70.             local distance = math.sqrt(dx*dx + dy*dy)
  71.             local force = (sphere1.mass * sphere2.mass) / (distance * distance)
  72.             local forceX = force * (dx / distance)
  73.             local forceY = force * (dy / distance)
  74.            
  75.             sphere1:applyForce(forceX, forceY, sphere1.x, sphere1.y)
  76.             sphere2:applyForce(-forceX, -forceY, sphere2.x, sphere2.y)
  77.         end
  78.     end
  79. end
  80.  
  81. -- Create screen boundaries that fit any screen size
  82. local function createWalls()
  83.     local leftWall = display.newRect(display.screenOriginX, display.contentCenterY, 1, display.actualContentHeight)
  84.     local rightWall = display.newRect(display.actualContentWidth + display.screenOriginX, display.contentCenterY, 1, display.actualContentHeight)
  85.     local topWall = display.newRect(display.contentCenterX, display.screenOriginY, display.actualContentWidth, 1)
  86.     local bottomWall = display.newRect(display.contentCenterX, display.actualContentHeight + display.screenOriginY, display.actualContentWidth, 1)
  87.    
  88.     physics.addBody(leftWall, "static")
  89.     physics.addBody(rightWall, "static")
  90.     physics.addBody(topWall, "static")
  91.     physics.addBody(bottomWall, "static")
  92. end
  93.  
  94. -- Function to generate a random number of circles with random properties
  95. local function generateRandomCircles()
  96.     local numCircles = math.random(2, 10) -- Random number of circles between 2 and 10
  97.     for i = 1, numCircles do
  98.         local radius = (math.random(math.random(8, 13), math.random(15, 25)))*scale -- Random radius between 5 and 20
  99.         local mass = (radius * math.random(1, 12))*scale -- Mass proportional to radius, adjust as needed
  100.         local x = math.random(radius, screenW - radius) -- Random x position, ensuring circle stays on screen
  101.         local y = math.random(radius, screenH - radius) -- Random y position, ensuring circle stays on screen
  102.         createSphere(x, y, radius, mass)
  103.     end
  104. end
  105.  
  106. -- Call the function to generate random circles
  107. generateRandomCircles()
  108.  
  109. -- Create walls
  110. createWalls()
  111.  
  112. -- Update gravity every frame
  113. Runtime:addEventListener("enterFrame", updateGravity)
  114.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement