Advertisement
1m1m0
Mar 14th, 2024
7
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. new version:
  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 = math.random(1.25, 1.50) -- Factor to propotionally resize objects, size and mass included
  13. throwAmplifier = 0.075 -- Factor in which how fast an object will be after throwing or flicking it
  14. dampingAmplifier = 0.25 -- Factor to simulate drag or air resistance (set to 0 for cosmic simulations)
  15.  
  16.  
  17. -- Table to keep track of spheres
  18. local spheres = {}
  19.  
  20. -- [[ Starting Circle with fixed size & mass for experimentations
  21. local function createCircle(x, y, radius, mass)
  22.     local circle = display.newCircle(x, y, radius)
  23.     physics.addBody(circle, {radius=radius, density=mass/(math.pi*radius*radius), bounce=0.5})
  24.     circle.mass = mass
  25.     table.insert(spheres, circle)
  26.  
  27.     -- Make spheres draggable
  28.     function sphere:touch(event)
  29.         if event.phase == "began" then
  30.             -- Initial touch
  31.             display.getCurrentStage():setFocus(self, event.id)
  32.             self.isFocus = true
  33.             self.markX = self.x
  34.             self.markY = self.y
  35.             self.startTime = system.getTimer()
  36.  
  37.             self.movementHistory = {} -- Array to store past positions and times
  38.  
  39.             -- Stop the sphere's movement
  40.             self:setLinearVelocity(0, 0)
  41.             self.angularVelocity = 0
  42.         elseif self.isFocus then
  43.             if event.phase == "moved" then
  44.                 -- Dragging
  45.                 self.x = event.x - event.xStart + self.markX
  46.                 self.y = event.y - event.yStart + self.markY
  47.  
  48.                 -- Record movement history
  49.                 table.insert(self.movementHistory, {time = system.getTimer(), x = event.x, y = event.y})
  50.  
  51.                 -- Clean up old movement history
  52.                 local currentTime = system.getTimer()
  53.                 local cutOffTime = currentTime - 1000
  54.                 for i, record in ipairs(self.movementHistory) do
  55.                     if record.time < cutOffTime then
  56.                         table.remove(self.movementHistory, i)
  57.                     end
  58.                 end
  59.  
  60.             elseif event.phase == "ended" or event.phase == "cancelled" then
  61.                 -- Released
  62.                 local endTime = system.getTimer()
  63.                 local distance = 0
  64.                 local throwForce = 0
  65.  
  66.                 -- Calculate distance based on movement history
  67.                 if #self.movementHistory >= 2 then
  68.                     local lastRecord = self.movementHistory[#self.movementHistory]
  69.                     local prevRecord = self.movementHistory[#self.movementHistory - 1]
  70.  
  71.                     distance = math.sqrt((lastRecord.x - prevRecord.x) ^ 2 + (lastRecord.y - prevRecord.y) ^ 2)
  72.                     throwForce = distance * throwAmplifier / (endTime - prevRecord.time)
  73.                 end
  74.  
  75.                 local vx = (event.x - self.markX) * throwForce * dampingAmplifier
  76.                 local vy = (event.y - self.markY) * throwForce * dampingAmplifier
  77.  
  78.                 -- Apply linear impulse
  79.                 self:applyLinearImpulse(vx, vy, self.x, self.y)
  80.  
  81.                 display.getCurrentStage():setFocus(self, nil)
  82.                 self.isFocus = false
  83.             end
  84.         end
  85.         return true
  86.     end
  87.  
  88.     circle:addEventListener("touch", circle)
  89.     return circle
  90. end
  91. --]]
  92. -- Function to create a new sphere with a random color and trail
  93. local function createSphere(x, y, radius, mass)
  94.     -- Random color components
  95.     local r = math.random()
  96.     local g = math.random()
  97.     local b = math.random()
  98.  
  99.     local sphere = display.newCircle(x, y, radius)
  100.     sphere:setFillColor(r, g, b) -- Set the random color
  101.     physics.addBody(sphere, {radius=radius, density=mass/(math.pi*radius*radius), bounce=math.random(0.0, 0.5)})
  102.     sphere.mass = mass
  103.     table.insert(spheres, sphere)
  104.  
  105.     -- [[ Trail effect
  106.     local trailGroup = display.newGroup()
  107.  
  108.     function sphere:enterFrame(event)
  109.         local trail = display.newCircle(trailGroup, self.x, self.y, radius * 0.3)
  110.         trail:setFillColor(r, g, b, 0.5) -- Slightly transparent version of the sphere's color
  111.         transition.to(trail, {time=500, alpha=0, onComplete=function() display.remove(trail) end})
  112.     end
  113.     --]]
  114.     Runtime:addEventListener("enterFrame", sphere)
  115.  
  116.     -- Make spheres draggable
  117.     function sphere:touch(event)
  118.         if event.phase == "began" then
  119.             -- Initial touch
  120.             display.getCurrentStage():setFocus(self, event.id)
  121.             self.isFocus = true
  122.             self.markX = self.x
  123.             self.markY = self.y
  124.             self.startTime = system.getTimer()
  125.  
  126.             self.movementHistory = {} -- Array to store past positions and times
  127.  
  128.             -- Stop the sphere's movement
  129.             self:setLinearVelocity(0, 0)
  130.             self.angularVelocity = 0
  131.         elseif self.isFocus then
  132.             if event.phase == "moved" then
  133.                 -- Dragging
  134.                 self.x = event.x - event.xStart + self.markX
  135.                 self.y = event.y - event.yStart + self.markY
  136.  
  137.                 -- Record movement history
  138.                 table.insert(self.movementHistory, {time = system.getTimer(), x = event.x, y = event.y})
  139.  
  140.                 -- Clean up old movement history
  141.                 local currentTime = system.getTimer()
  142.                 local cutOffTime = currentTime - 1000
  143.                 for i, record in ipairs(self.movementHistory) do
  144.                     if record.time < cutOffTime then
  145.                         table.remove(self.movementHistory, i)
  146.                     end
  147.                 end
  148.  
  149.             elseif event.phase == "ended" or event.phase == "cancelled" then
  150.                 -- Released
  151.                 local endTime = system.getTimer()
  152.                 local distance = 0
  153.                 local throwForce = 0
  154.  
  155.                 -- Calculate distance based on movement history
  156.                 if #self.movementHistory >= 2 then
  157.                     local lastRecord = self.movementHistory[#self.movementHistory]
  158.                     local prevRecord = self.movementHistory[#self.movementHistory - 1]
  159.  
  160.                     distance = math.sqrt((lastRecord.x - prevRecord.x) ^ 2 + (lastRecord.y - prevRecord.y) ^ 2)
  161.                     throwForce = distance * throwAmplifier / (endTime - prevRecord.time)
  162.                 end
  163.  
  164.                 local vx = (event.x - self.markX) * throwForce * dampingAmplifier
  165.                 local vy = (event.y - self.markY) * throwForce * dampingAmplifier
  166.  
  167.                 -- Apply linear impulse
  168.                 self:applyLinearImpulse(vx, vy, self.x, self.y)
  169.  
  170.                 display.getCurrentStage():setFocus(self, nil)
  171.                 self.isFocus = false
  172.             end
  173.         end
  174.         return true
  175.     end
  176.  
  177.     sphere:addEventListener("touch", sphere)
  178.     return sphere
  179. end
  180.  
  181. -- [[ Function to update gravitational forces
  182. local function updateGravity()
  183.     for i = 1, #spheres do
  184.         for j = i+1, #spheres do
  185.             local sphere1 = spheres[i]
  186.             local sphere2 = spheres[j]
  187.             local dx = sphere2.x - sphere1.x
  188.             local dy = sphere2.y - sphere1.y
  189.             local distance = math.sqrt(dx*dx + dy*dy)
  190.             local force = (sphere1.mass * sphere2.mass) / (distance * distance)
  191.             local forceX = force * (dx / distance)
  192.             local forceY = force * (dy / distance)
  193.  
  194.             sphere1:applyForce(forceX, forceY, sphere1.x, sphere1.y)
  195.             sphere2:applyForce(-forceX, -forceY, sphere2.x, sphere2.y)
  196.         end
  197.     end
  198. end
  199. --]]
  200. -- Create screen boundaries that fit any screen size
  201. local function createWalls()
  202.     local leftWall = display.newRect(display.screenOriginX, display.contentCenterY, 1, display.actualContentHeight)
  203.     local rightWall = display.newRect(display.actualContentWidth + display.screenOriginX, display.contentCenterY, 1, display.actualContentHeight)
  204.     local topWall = display.newRect(display.contentCenterX, display.screenOriginY, display.actualContentWidth, 1)
  205.     local bottomWall = display.newRect(display.contentCenterX, display.actualContentHeight + display.screenOriginY, display.actualContentWidth, 1)
  206.  
  207.     physics.addBody(leftWall, "static")
  208.     physics.addBody(rightWall, "static")
  209.     physics.addBody(topWall, "static")
  210.     physics.addBody(bottomWall, "static")
  211. end
  212.  
  213. -- Function to generate a random number of circles with random properties
  214. local function generateRandomCircles()
  215.     local numCircles = math.random(2, 12) -- Random number of circles between 2 and 10
  216.     for i = 1, numCircles do
  217.         local radius = (math.random(math.random(8, 13), math.random(15, 25)))*scale -- Random radius between 5 and 20
  218.         local mass = (radius * math.random(1, 12))*scale -- Mass proportional to radius, adjust as needed
  219.         local x = math.random(radius, screenW - radius) -- Random x position, ensuring circle stays on screen
  220.         local y = math.random(radius, screenH - radius) -- Random y position, ensuring circle stays on screen
  221.         createSphere(x, y, radius, mass)
  222.     end
  223. end
  224.  
  225. -- Call the function to generate random circles
  226. generateRandomCircles()
  227.  
  228. -- Create walls
  229. createWalls()
  230.  
  231. -- Create starting circle
  232. --createCircle(160, 240, 25, 25)
  233.  
  234. -- Update gravity every frame
  235. Runtime:addEventListener("enterFrame", updateGravity)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement