Advertisement
1m1m0
Mar 14th, 2024
9
0
Never
This is comment for paste Orbit 1.3.5 [Polygonal]
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. PREVIOUS VERSION OF ORBIT:
  2.  
  3. -- Initialize physics
  4. local physics = require("physics")
  5. physics.start()
  6. physics.setGravity(0, 0) -- Set gravity to 0
  7.  
  8. -- Core Variables
  9. local screenW = display.contentWidth
  10. local screenH = display.contentHeight
  11.  
  12. scale = 0.75 -- Factor to propotionally resize objects, size and mass included
  13.  
  14. -- Table to keep track of spheres
  15. local spheres = {}
  16.  
  17. -- [[ Starting Circle with fixed size & mass for experimentations
  18. local function createCircle(x, y, radius, mass)
  19.     local circle = display.newCircle(x, y, radius)
  20.     physics.addBody(circle, {radius=radius, density=mass/(math.pi*radius*radius), bounce=0.5})
  21.     circle.mass = mass
  22.     table.insert(spheres, circle)
  23.  
  24.     -- Make spheres draggable and stop their movement while dragging
  25.     function circle:touch(event)
  26.         if event.phase == "began" then
  27.             -- Initial touch
  28.             display.getCurrentStage():setFocus(self, event.id)
  29.             self.isFocus = true
  30.             self.markX = self.x
  31.             self.markY = self.y
  32.             self.startTime = system.getTimer()
  33.             -- Stop the sphere's movement
  34.             self:setLinearVelocity(0, 0)
  35.             self.angularVelocity = 0
  36.         elseif self.isFocus then
  37.             if event.phase == "moved" then
  38.                 -- Dragging
  39.                 self.x = event.x - event.xStart + self.markX
  40.                 self.y = event.y - event.yStart + self.markY
  41.             elseif event.phase == "ended" or event.phase == "cancelled" then
  42.                 -- Released
  43.                 local endTime = system.getTimer()
  44.                 local timePassed = endTime - self.startTime
  45.                 local vx = (event.x - self.markX) / timePassed
  46.                 local vy = (event.y - self.markY) / timePassed
  47.                 -- Apply the linear impulse based on the calculated velocity
  48.                 self:applyLinearImpulse(vx * 0.1, vy * 0.1, self.x, self.y)
  49.                
  50.                 display.getCurrentStage():setFocus(self, nil)
  51.                 self.isFocus = false
  52.             end
  53.         end
  54.         return true
  55.     end
  56.  
  57.  
  58.     circle:addEventListener("touch", circle)
  59.     return circle
  60. end
  61. --]]
  62. -- Function to create a new sphere with a random color and trail
  63. local function createSphere(x, y, radius, mass)
  64.     -- Random color components
  65.     local r = math.random()
  66.     local g = math.random()
  67.     local b = math.random()
  68.  
  69.     local sphere = display.newCircle(x, y, radius)
  70.     sphere:setFillColor(r, g, b) -- Set the random color
  71.     physics.addBody(sphere, {radius=radius, density=mass/(math.pi*radius*radius), bounce=math.random(0.0, 0.65)})
  72.     sphere.mass = mass
  73.     table.insert(spheres, sphere)
  74.  
  75.     --[[ Trail effect
  76.     local trailGroup = display.newGroup()
  77.  
  78.     function sphere:enterFrame(event)
  79.         local trail = display.newCircle(trailGroup, self.x, self.y, radius * 0.3)
  80.         trail:setFillColor(r, g, b, 0.5) -- Slightly transparent version of the sphere's color
  81.         transition.to(trail, {time=500, alpha=0, onComplete=function() display.remove(trail) end})
  82.     end
  83.     --]]
  84.     Runtime:addEventListener("enterFrame", sphere)
  85.  
  86.     -- Make spheres draggable and stop their movement while dragging
  87.     function sphere:touch(event)
  88.         if event.phase == "began" then
  89.             -- Initial touch
  90.             display.getCurrentStage():setFocus(self, event.id)
  91.             self.isFocus = true
  92.             self.markX = self.x
  93.             self.markY = self.y
  94.             self.startTime = system.getTimer()
  95.             -- Stop the sphere's movement
  96.             self:setLinearVelocity(0, 0)
  97.             self.angularVelocity = 0
  98.         elseif self.isFocus then
  99.             if event.phase == "moved" then
  100.                 -- Dragging
  101.                 self.x = event.x - event.xStart + self.markX
  102.                 self.y = event.y - event.yStart + self.markY
  103.             elseif event.phase == "ended" or event.phase == "cancelled" then
  104.                 -- Released
  105.                 local endTime = system.getTimer()
  106.                 local timePassed = endTime - self.startTime
  107.                 local vx = (event.x - self.markX) / timePassed
  108.                 local vy = (event.y - self.markY) / timePassed
  109.                 -- Apply the linear impulse based on the calculated velocity
  110.                 self:applyLinearImpulse(vx * 0.1, vy * 0.1, self.x, self.y)
  111.                
  112.                 display.getCurrentStage():setFocus(self, nil)
  113.                 self.isFocus = false
  114.             end
  115.         end
  116.         return true
  117.     end
  118.  
  119.     sphere:addEventListener("touch", sphere)
  120.     return sphere
  121. end
  122.  
  123. -- [[ Function to update gravitational forces
  124. local function updateGravity()
  125.     for i = 1, #spheres do
  126.         for j = i+1, #spheres do
  127.             local sphere1 = spheres[i]
  128.             local sphere2 = spheres[j]
  129.             local dx = sphere2.x - sphere1.x
  130.             local dy = sphere2.y - sphere1.y
  131.             local distance = math.sqrt(dx*dx + dy*dy)
  132.             local force = (sphere1.mass * sphere2.mass) / (distance * distance)
  133.             local forceX = force * (dx / distance)
  134.             local forceY = force * (dy / distance)
  135.            
  136.             sphere1:applyForce(forceX, forceY, sphere1.x, sphere1.y)
  137.             sphere2:applyForce(-forceX, -forceY, sphere2.x, sphere2.y)
  138.         end
  139.     end
  140. end
  141. --]]
  142. -- Create screen boundaries that fit any screen size
  143. local function createWalls()
  144.     local leftWall = display.newRect(display.screenOriginX, display.contentCenterY, 1, display.actualContentHeight)
  145.     local rightWall = display.newRect(display.actualContentWidth + display.screenOriginX, display.contentCenterY, 1, display.actualContentHeight)
  146.     local topWall = display.newRect(display.contentCenterX, display.screenOriginY, display.actualContentWidth, 1)
  147.     local bottomWall = display.newRect(display.contentCenterX, display.actualContentHeight + display.screenOriginY, display.actualContentWidth, 1)
  148.    
  149.     physics.addBody(leftWall, "static")
  150.     physics.addBody(rightWall, "static")
  151.     physics.addBody(topWall, "static")
  152.     physics.addBody(bottomWall, "static")
  153. end
  154.  
  155. -- Function to generate a random number of circles with random properties
  156. local function generateRandomCircles()
  157.     local numCircles = math.random(2, 12) -- Random number of circles between 2 and 10
  158.     for i = 1, numCircles do
  159.         local radius = (math.random(math.random(8, 13), math.random(15, 25)))*scale -- Random radius between 5 and 20
  160.         local mass = (radius * math.random(1, 12))*scale -- Mass proportional to radius, adjust as needed
  161.         local x = math.random(radius, screenW - radius) -- Random x position, ensuring circle stays on screen
  162.         local y = math.random(radius, screenH - radius) -- Random y position, ensuring circle stays on screen
  163.         createSphere(x, y, radius, mass)
  164.     end
  165. end
  166.  
  167. -- Call the function to generate random circles
  168. generateRandomCircles()
  169.  
  170. -- Create walls
  171. createWalls()
  172.  
  173. -- Create starting circle
  174. --createCircle(160, 240, 25, 25)
  175.  
  176. -- Update gravity every frame
  177. Runtime:addEventListener("enterFrame", updateGravity)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement