How do I make objects orbit another object

Hello, for my game I would like 3 circles to orbit around a button or another object. I want the button to make the 3 circles orbit further out and closer at a speed. If that is asking to much, the 3 circles orbiting the object would be fine. The whole point is to make the circles have 2 orbits with a button controlling what orbit it will be moving to. I plan on making the circles characters.

I attached an image that shows an example or click this screenshot link.

https://gyazo.com/8aabf4af7eeda29d2f5deebf9182030b

Key -

The black circles are the first orbiting circles.

The grey circles are orbiting the 2nd orbit.

The red lines are the lines of trajectory just to show what i mean.

Middle circle is the button that controls the circles and is the object being orbited.

I have been searching around for a long time on orbiting mechanics etc. All them seem to be outdated and I can’t seem to get any coding to work for it. Much love to whoever can help me with this. I have been trying for months and this has stopped me from moving on any further in my projects.

Will there be collision detection involved?

If not, this is essentially a simple vector math problem.

I have a 2d math library you can use to do the calculations in SSK2( Note: I treat up as 0-degrees; right as 90, and so on.  i.e. I don’t use a Cartesian system, but one that matches Corona’s coordinates and orientations.)

The basic equation you’ll need is (assume object in center is objA)

-- MAY CONTAIN TYPOS local objA = display.newCircle( 120, 120, 10 ) objA:setFillColor(1,0,0) local objB = display.newCircle( 10, 10, 10 ) objB:setFillColor(0,1,0) objB.lastTime = system.getTimer() objB.curAngle = 135 objB.orbitDist = 200 objB.rate = 15 -- degrees per section orbital rotation local objC = display.newCircle( 20, 20, 10 ) objC:setFillColor(0,0,1) objC.lastTime = system.getTimer() objC.curAngle = 0 objC.orbitDist = 250 objC.rate = -5 -- Counter rotates at 5-degrees per second local function updateOrbit( self ) local curTime = system.getTimer() if(curTime \<= obj.lastTime ) then return end local dt = curTime - obj.lastTime obj.lastTime = curTime local dr = obj.rate \* dt/1000 -- change in rotation local vec = ssk.math2d.angle2Vector( dr, true ) -- change in time vec = ssk.math2d.scale( vec, obj.orbitDist ) vec = ssk.math2d.add( objA, vec ) obj.x = vec.x obj.y = vec.y end objB.enterFrame = updateOrbit Runtime:addEventListener( "enterFrame", objB ) objC.enterFrame = updateOrbit Runtime:addEventListener( "enterFrame", objC )

Oh, and if you’re modelling REAL orbital rotations, this does become more complex as most orbits are not spherical, but ellipitical.  So (if memory serves) distance and orbital velocity change over time based on 2nd order equations that can be solved with calculus.

Will there be collision detection involved?

If not, this is essentially a simple vector math problem.

I have a 2d math library you can use to do the calculations in SSK2( Note: I treat up as 0-degrees; right as 90, and so on.  i.e. I don’t use a Cartesian system, but one that matches Corona’s coordinates and orientations.)

The basic equation you’ll need is (assume object in center is objA)

-- MAY CONTAIN TYPOS local objA = display.newCircle( 120, 120, 10 ) objA:setFillColor(1,0,0) local objB = display.newCircle( 10, 10, 10 ) objB:setFillColor(0,1,0) objB.lastTime = system.getTimer() objB.curAngle = 135 objB.orbitDist = 200 objB.rate = 15 -- degrees per section orbital rotation local objC = display.newCircle( 20, 20, 10 ) objC:setFillColor(0,0,1) objC.lastTime = system.getTimer() objC.curAngle = 0 objC.orbitDist = 250 objC.rate = -5 -- Counter rotates at 5-degrees per second local function updateOrbit( self ) local curTime = system.getTimer() if(curTime \<= obj.lastTime ) then return end local dt = curTime - obj.lastTime obj.lastTime = curTime local dr = obj.rate \* dt/1000 -- change in rotation local vec = ssk.math2d.angle2Vector( dr, true ) -- change in time vec = ssk.math2d.scale( vec, obj.orbitDist ) vec = ssk.math2d.add( objA, vec ) obj.x = vec.x obj.y = vec.y end objB.enterFrame = updateOrbit Runtime:addEventListener( "enterFrame", objB ) objC.enterFrame = updateOrbit Runtime:addEventListener( "enterFrame", objC )

Oh, and if you’re modelling REAL orbital rotations, this does become more complex as most orbits are not spherical, but ellipitical.  So (if memory serves) distance and orbital velocity change over time based on 2nd order equations that can be solved with calculus.