Advertisement
1m1m0

Basic Celestial Simulation

Mar 10th, 2024
31
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.34 KB | Source Code | 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. screenW = display.contentWidth
  8. screenH = display.contentHeight
  9.  
  10. -- Table to keep track of spheres
  11. local spheres = {}
  12.  
  13. -- Function to create a new sphere
  14. local function createSphere(x, y, radius, mass)
  15.     local sphere = display.newCircle(x, y, radius)
  16.     physics.addBody(sphere, {radius=radius, density=mass/(math.pi*radius*radius), bounce=0.25})
  17.     sphere.mass = mass
  18.     table.insert(spheres, sphere)
  19.     return sphere
  20. end
  21.  
  22. -- Function to update gravitational forces
  23. local function updateGravity()
  24.     for i = 1, #spheres do
  25.         for j = i+1, #spheres do
  26.             local sphere1 = spheres[i]
  27.             local sphere2 = spheres[j]
  28.             local dx = sphere2.x - sphere1.x
  29.             local dy = sphere2.y - sphere1.y
  30.             local distance = math.sqrt(dx*dx + dy*dy)
  31.             local force = (sphere1.mass * sphere2.mass) / (distance * distance)
  32.             local forceX = force * (dx / distance)
  33.             local forceY = force * (dy / distance)
  34.            
  35.             sphere1:applyForce(forceX, forceY, sphere1.x, sphere1.y)
  36.             sphere2:applyForce(-forceX, -forceY, sphere2.x, sphere2.y)
  37.         end
  38.     end
  39. end
  40.  
  41. -- Create screen boundaries that fit any screen size
  42. local function createWalls()
  43.     local leftWall = display.newRect(display.screenOriginX, display.contentCenterY, 1, display.actualContentHeight)
  44.     local rightWall = display.newRect(display.actualContentWidth + display.screenOriginX, display.contentCenterY, 1, display.actualContentHeight)
  45.     local topWall = display.newRect(display.contentCenterX, display.screenOriginY, display.actualContentWidth, 1)
  46.     local bottomWall = display.newRect(display.contentCenterX, display.actualContentHeight + display.screenOriginY, display.actualContentWidth, 1)
  47.    
  48.     physics.addBody(leftWall, "static")
  49.     physics.addBody(rightWall, "static")
  50.     physics.addBody(topWall, "static")
  51.     physics.addBody(bottomWall, "static")
  52. end
  53.  
  54. -- Function to create a new sphere
  55. local function createSphere(x, y, radius, mass)
  56.     local sphere = display.newCircle(x, y, radius)
  57.     physics.addBody(sphere, {radius=radius, density=mass/(math.pi*radius*radius), bounce=0.25})
  58.     sphere.mass = mass
  59.     table.insert(spheres, sphere)
  60.  
  61.     -- Make spheres draggable
  62.     function sphere:touch(event)
  63.         if event.phase == "began" then
  64.             display.getCurrentStage():setFocus(self, event.id)
  65.             self.isFocus = true
  66.             self.markX = self.x
  67.             self.markY = self.y
  68.         elseif self.isFocus then
  69.             if event.phase == "moved" then
  70.                 self.x = event.x - event.xStart + self.markX
  71.                 self.y = event.y - event.yStart + self.markY
  72.             elseif event.phase == "ended" or event.phase == "cancelled" then
  73.                 display.getCurrentStage():setFocus(self, nil)
  74.                 self.isFocus = false
  75.             end
  76.         end
  77.         return true
  78.     end
  79.  
  80.     sphere:addEventListener("touch", sphere)
  81.     return sphere
  82. end
  83.  
  84.  
  85. -- Create some spheres
  86. createSphere(160, 240, 20, 500) -- x, y, radius, mass
  87. createSphere(320, 240, 15, 1)
  88.  
  89. -- Create walls
  90. createWalls()
  91.  
  92. -- Update gravity every frame
  93. Runtime:addEventListener("enterFrame", updateGravity)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement