Advertisement
This is comment for paste
Orbit 1.3.5 [Polygonal]
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- new version:
- -- 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 = math.random(1.25, 1.50) -- Factor to propotionally resize objects, size and mass included
- throwAmplifier = 0.075 -- Factor in which how fast an object will be after throwing or flicking it
- dampingAmplifier = 0.25 -- Factor to simulate drag or air resistance (set to 0 for cosmic simulations)
- -- 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
- 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()
- self.movementHistory = {} -- Array to store past positions and times
- -- 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
- -- Record movement history
- table.insert(self.movementHistory, {time = system.getTimer(), x = event.x, y = event.y})
- -- Clean up old movement history
- local currentTime = system.getTimer()
- local cutOffTime = currentTime - 1000
- for i, record in ipairs(self.movementHistory) do
- if record.time < cutOffTime then
- table.remove(self.movementHistory, i)
- end
- end
- elseif event.phase == "ended" or event.phase == "cancelled" then
- -- Released
- local endTime = system.getTimer()
- local distance = 0
- local throwForce = 0
- -- Calculate distance based on movement history
- if #self.movementHistory >= 2 then
- local lastRecord = self.movementHistory[#self.movementHistory]
- local prevRecord = self.movementHistory[#self.movementHistory - 1]
- distance = math.sqrt((lastRecord.x - prevRecord.x) ^ 2 + (lastRecord.y - prevRecord.y) ^ 2)
- throwForce = distance * throwAmplifier / (endTime - prevRecord.time)
- end
- local vx = (event.x - self.markX) * throwForce * dampingAmplifier
- local vy = (event.y - self.markY) * throwForce * dampingAmplifier
- -- Apply linear impulse
- self:applyLinearImpulse(vx, vy, 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.5)})
- 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
- 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()
- self.movementHistory = {} -- Array to store past positions and times
- -- 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
- -- Record movement history
- table.insert(self.movementHistory, {time = system.getTimer(), x = event.x, y = event.y})
- -- Clean up old movement history
- local currentTime = system.getTimer()
- local cutOffTime = currentTime - 1000
- for i, record in ipairs(self.movementHistory) do
- if record.time < cutOffTime then
- table.remove(self.movementHistory, i)
- end
- end
- elseif event.phase == "ended" or event.phase == "cancelled" then
- -- Released
- local endTime = system.getTimer()
- local distance = 0
- local throwForce = 0
- -- Calculate distance based on movement history
- if #self.movementHistory >= 2 then
- local lastRecord = self.movementHistory[#self.movementHistory]
- local prevRecord = self.movementHistory[#self.movementHistory - 1]
- distance = math.sqrt((lastRecord.x - prevRecord.x) ^ 2 + (lastRecord.y - prevRecord.y) ^ 2)
- throwForce = distance * throwAmplifier / (endTime - prevRecord.time)
- end
- local vx = (event.x - self.markX) * throwForce * dampingAmplifier
- local vy = (event.y - self.markY) * throwForce * dampingAmplifier
- -- Apply linear impulse
- self:applyLinearImpulse(vx, vy, 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