Advertisement
1m1m0

Orbit: Lite

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