Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Initialize physics
- local physics = require("physics")
- physics.start()
- physics.setGravity(0, 0) -- Set gravity to 0
- -- Core Variables
- screenW = display.contentWidth
- screenH = display.contentHeight
- -- Table to keep track of spheres
- local spheres = {}
- -- Function to create a new sphere
- local function createSphere(x, y, radius, mass)
- local sphere = display.newCircle(x, y, radius)
- physics.addBody(sphere, {radius=radius, density=mass/(math.pi*radius*radius), bounce=0.25})
- sphere.mass = mass
- table.insert(spheres, sphere)
- return sphere
- end
- -- Function to update gravitational forces
- local function updateGravity()
- for i = 1, #spheres do
- for j = i+1, #spheres do
- local sphere1 = spheres[i]
- local sphere2 = spheres[j]
- local dx = sphere2.x - sphere1.x
- local dy = sphere2.y - sphere1.y
- local distance = math.sqrt(dx*dx + dy*dy)
- local force = (sphere1.mass * sphere2.mass) / (distance * distance)
- local forceX = force * (dx / distance)
- local forceY = force * (dy / distance)
- sphere1:applyForce(forceX, forceY, sphere1.x, sphere1.y)
- sphere2:applyForce(-forceX, -forceY, sphere2.x, sphere2.y)
- end
- end
- end
- -- Create screen boundaries that fit any screen size
- local function createWalls()
- local leftWall = display.newRect(display.screenOriginX, display.contentCenterY, 1, display.actualContentHeight)
- local rightWall = display.newRect(display.actualContentWidth + display.screenOriginX, display.contentCenterY, 1, display.actualContentHeight)
- local topWall = display.newRect(display.contentCenterX, display.screenOriginY, display.actualContentWidth, 1)
- local bottomWall = display.newRect(display.contentCenterX, display.actualContentHeight + display.screenOriginY, display.actualContentWidth, 1)
- physics.addBody(leftWall, "static")
- physics.addBody(rightWall, "static")
- physics.addBody(topWall, "static")
- physics.addBody(bottomWall, "static")
- end
- -- Function to create a new sphere
- local function createSphere(x, y, radius, mass)
- local sphere = display.newCircle(x, y, radius)
- physics.addBody(sphere, {radius=radius, density=mass/(math.pi*radius*radius), bounce=0.25})
- sphere.mass = mass
- table.insert(spheres, sphere)
- -- Make spheres draggable
- function sphere:touch(event)
- if event.phase == "began" then
- display.getCurrentStage():setFocus(self, event.id)
- self.isFocus = true
- self.markX = self.x
- self.markY = self.y
- elseif self.isFocus then
- if event.phase == "moved" then
- self.x = event.x - event.xStart + self.markX
- self.y = event.y - event.yStart + self.markY
- elseif event.phase == "ended" or event.phase == "cancelled" then
- display.getCurrentStage():setFocus(self, nil)
- self.isFocus = false
- end
- end
- return true
- end
- sphere:addEventListener("touch", sphere)
- return sphere
- end
- -- Create some spheres
- createSphere(160, 240, 20, 500) -- x, y, radius, mass
- createSphere(320, 240, 15, 1)
- -- Create walls
- createWalls()
- -- Update gravity every frame
- Runtime:addEventListener("enterFrame", updateGravity)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement