Advertisement
This is comment for paste
Orbit 1.3.5 [Polygonal]
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- PREVIOUS VERSION OF ORBIT:
- -- Initialize physics
- local physics = require("physics")
- physics.start()
- physics.setGravity(0, 0) -- Set gravity to 0
- -- Core Variables
- local screenW = display.contentWidth
- local screenH = display.contentHeight
- scale = 0.75 -- Factor to propotionally resize objects, size and mass included
- -- Table to keep track of spheres
- local spheres = {}
- -- [[ Starting Circle with fixed size & mass for experimentations
- local function createCircle(x, y, radius, mass)
- local circle = display.newCircle(x, y, radius)
- physics.addBody(circle, {radius=radius, density=mass/(math.pi*radius*radius), bounce=0.5})
- circle.mass = mass
- table.insert(spheres, circle)
- -- Make spheres draggable and stop their movement while dragging
- function circle:touch(event)
- if event.phase == "began" then
- -- Initial touch
- display.getCurrentStage():setFocus(self, event.id)
- self.isFocus = true
- self.markX = self.x
- self.markY = self.y
- self.startTime = system.getTimer()
- -- Stop the sphere's movement
- self:setLinearVelocity(0, 0)
- self.angularVelocity = 0
- elseif self.isFocus then
- if event.phase == "moved" then
- -- Dragging
- 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
- -- Released
- local endTime = system.getTimer()
- local timePassed = endTime - self.startTime
- local vx = (event.x - self.markX) / timePassed
- local vy = (event.y - self.markY) / timePassed
- -- Apply the linear impulse based on the calculated velocity
- self:applyLinearImpulse(vx * 0.1, vy * 0.1, self.x, self.y)
- display.getCurrentStage():setFocus(self, nil)
- self.isFocus = false
- end
- end
- return true
- end
- circle:addEventListener("touch", circle)
- return circle
- end
- --]]
- -- Function to create a new sphere with a random color and trail
- local function createSphere(x, y, radius, mass)
- -- Random color components
- local r = math.random()
- local g = math.random()
- local b = math.random()
- local sphere = display.newCircle(x, y, radius)
- sphere:setFillColor(r, g, b) -- Set the random color
- physics.addBody(sphere, {radius=radius, density=mass/(math.pi*radius*radius), bounce=math.random(0.0, 0.65)})
- sphere.mass = mass
- table.insert(spheres, sphere)
- --[[ Trail effect
- local trailGroup = display.newGroup()
- function sphere:enterFrame(event)
- local trail = display.newCircle(trailGroup, self.x, self.y, radius * 0.3)
- trail:setFillColor(r, g, b, 0.5) -- Slightly transparent version of the sphere's color
- transition.to(trail, {time=500, alpha=0, onComplete=function() display.remove(trail) end})
- end
- --]]
- Runtime:addEventListener("enterFrame", sphere)
- -- Make spheres draggable and stop their movement while dragging
- function sphere:touch(event)
- if event.phase == "began" then
- -- Initial touch
- display.getCurrentStage():setFocus(self, event.id)
- self.isFocus = true
- self.markX = self.x
- self.markY = self.y
- self.startTime = system.getTimer()
- -- Stop the sphere's movement
- self:setLinearVelocity(0, 0)
- self.angularVelocity = 0
- elseif self.isFocus then
- if event.phase == "moved" then
- -- Dragging
- 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
- -- Released
- local endTime = system.getTimer()
- local timePassed = endTime - self.startTime
- local vx = (event.x - self.markX) / timePassed
- local vy = (event.y - self.markY) / timePassed
- -- Apply the linear impulse based on the calculated velocity
- self:applyLinearImpulse(vx * 0.1, vy * 0.1, self.x, self.y)
- display.getCurrentStage():setFocus(self, nil)
- self.isFocus = false
- end
- end
- return true
- end
- sphere:addEventListener("touch", 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 generate a random number of circles with random properties
- local function generateRandomCircles()
- local numCircles = math.random(2, 12) -- Random number of circles between 2 and 10
- for i = 1, numCircles do
- local radius = (math.random(math.random(8, 13), math.random(15, 25)))*scale -- Random radius between 5 and 20
- local mass = (radius * math.random(1, 12))*scale -- Mass proportional to radius, adjust as needed
- local x = math.random(radius, screenW - radius) -- Random x position, ensuring circle stays on screen
- local y = math.random(radius, screenH - radius) -- Random y position, ensuring circle stays on screen
- createSphere(x, y, radius, mass)
- end
- end
- -- Call the function to generate random circles
- generateRandomCircles()
- -- Create walls
- createWalls()
- -- Create starting circle
- --createCircle(160, 240, 25, 25)
- -- Update gravity every frame
- Runtime:addEventListener("enterFrame", updateGravity)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement