My Solar System


function setup()
    displayMode(OVERLAY)
    displayMode(FULLSCREEN)
    
    TIME = 0
    FPS = 0
    fpsCounter = 1000
    
    parameter.integer("SPEED", -50, 50, 0)
    parameter.watch("TIME")
    parameter.watch("FPS")
    
    objects = {Sun(),
    Planet({moon = {Moon(10, 25), Moon(5, 40)}, color = color(255, 100, 0, 255)}),
    Planet({distance = 250, speed = .35, moon = {}, color = color(0, 75, 255, 255)}),
    Planet({speed = .265, distance = 375, moon = {Moon(7, 35, 10), Moon(4, 50, 2), Moon(10, 85, 7)}, color = color(255, 50, 250)})
    }
end

function draw()
    background(0, 0, 0, 255)
    
    totalSpeed = SPEED/10
    TIME = TIME + totalSpeed
    fpsCounter = fpsCounter + 1
    if fpsCounter > 100 then 
        FPS = math.floor(1/DeltaTime)
        fpsCounter = 0
    end
    
    for a, b in ipairs(objects) do
        b:draw()
    end
end

-- Objects

Sun = class()

function Sun:init()
    self.col = color(255, 150, 0, 255)
    self.rot = 0
end

function Sun:draw()
    fill(self.col)
    translate(WIDTH/2, HEIGHT/2)
    rotate(self.rot)
    ellipse(0, 0, 100, 90)
    rotate(-self.rot)
    translate(-WIDTH/2, -HEIGHT/2)
    self.rot = self.rot + .1 * totalSpeed
end

Planet = class()

function Planet:init(tab)
    tab = tab or {}
    self.speed = tab.speed or math.random(50, 2000)/2000
    self.distance = tab.distance or 175
    self.rot = tab.rot or 0
    self.size = tab.size or 25
    self.moon = tab.moon or {Moon(self.size/5, 50)}
    self.color = tab.color or color(math.random(255), math.random(255), math.random(255), 255)
end

function Planet:draw()
    translate(WIDTH/2, HEIGHT/2) -- Translate to Default Orientation
    rotate(self.rot) -- Rotate Around Sun
    translate(self.distance, 0) -- Translate to Distance from Sun
    
    -- Planet
    fill(self.color)
    ellipse(0, 0, self.size)
    
    -- Moons
    for a, b in ipairs(self.moon) do
        while b.rot > 360 do
            b.rot = b.rot - 360
        end
        local formula = (b.rot >= -b.size and b.rot <= b.size) and b.distance > self.distance / self.size
        if formula then
            b.red = true
        else
            b.red = false
        end
        b:draw()
    end
    
    -- To Normal
    translate(-self.distance, 0)
    rotate(-self.rot)
    translate(-WIDTH/2, -HEIGHT/2)
    
    -- Update Planets
    self.rot = self.rot + self.speed * totalSpeed
end

Moon = class()

function Moon:init(size, dst, speed)
    self.size = size or 5
    self.rot = 0
    self.distance = dst or 25
    self.speed = speed or math.random(10)
    self.red = true
end

function Moon:draw()
    rotate(self.rot)
    fill(75, 255)
    if self.red then
        fill(255, 0, 0, 255)
    end
    ellipse(self.distance, 0, self.size)
    rotate(-self.rot)
    self.rot = self.rot + self.speed * totalSpeed
end

So here’s my solar system. Change speed in Parametres.

Question: As you see I am rotating the planets around the sun by using rotate() and how to check the touch position, I mean rotate t.x and t.y around the sun… Same with moons

@TokOut looks real good, lots of ellipses and rotation but I think it would be easier to just pushMatrix() and popMatrix() rather than saying ~rotate(-self.rot)~

Oops, I meant to say rotate(-self.rot)

@CamelCoder If you make a mistake in a post, you can re-edit your original and correct it. Just tap the gear to the upper right of the post and select edit.

@TokOut Here’s another way to do a Solar system.

EDIT: Modified the table/code to make it easier to read.

displayMode(FULLSCREEN)

function setup()
    plTab={}
    table.insert(plTab,planet({
        distance=60,angle=70,speed=.8,size=10,color=color(255, 88, 0, 255)}))
        plTab[#plTab].moon={
            {distance=10,angle=50,speed=2},
            {distance=15,angle=180,speed=1},
            {distance=20,angle=90,speed=.5}}
    
    table.insert(plTab,planet({
        distance=120,angle=270,speed=.6,size=15,color=color(240, 239, 239, 255)}))
        plTab[#plTab].moon={
            {distance=12,angle=50,speed=2},
            {distance=17,angle=180,speed=1}}
    
    table.insert(plTab,planet({
        distance=180,angle=140,speed=.4,size=16,color=color(0, 255, 124, 255)}))
        plTab[#plTab].moon={
            {distance=13,angle=50,speed=2}}   
        
    table.insert(plTab,planet({
        distance=240,angle=140,speed=.1,size=11,color=color(255, 36, 0, 255)}))
        plTab[#plTab].moon={
            {distance=8,angle=150,speed=2},
            {distance=20,angle=0,speed=1}}  
  
    table.insert(plTab,planet({
        distance=300,angle=20,speed=.05,size=25,color=color(228, 201, 101, 255)}))
        plTab[#plTab].moon={
            {distance=20,angle=20,speed=2},
            {distance=24,angle=0,speed=1},
            {distance=30,angle=90,speed=.5},
            {distance=36,angle=240,speed=.25},
            {distance=44,angle=120,speed=.1}}

    table.insert(plTab,planet({
        distance=360,angle=270,speed=.03,size=21,color=color(225, 255, 0, 255)}))
        plTab[#plTab].moon={
            {distance=20,angle=120,speed=2},
            {distance=30,angle=0,speed=1}}
end

function draw()
    background(40, 40, 50)
    fill(255, 242, 0, 255)
    ellipse(WIDTH/2,HEIGHT/2,36)
    for a,b in pairs(plTab) do
        b:draw()
    end
end

planet=class()

function planet:init(p)
    self.planet=p
    self.moon={}
end

function planet:draw()
    pushMatrix()
    translate(WIDTH/2,HEIGHT/2)
    self.planet.angle=self.planet.angle+self.planet.speed
    rotate(self.planet.angle)
    fill(self.planet.color)
    ellipse(self.planet.distance,0,self.planet.size)
    for z=1,#self.moon do
        pushMatrix()
        translate(self.planet.distance,0)
        self.moon[z].angle=self.moon[z].angle+self.moon[z].speed
        rotate(self.moon[z].angle)
        fill(255)
        ellipse(self.moon[z].distance,0,3)
        popMatrix()
    end
    popMatrix()
end

Now let’s see you guys do elliptical orbits, like the real life planets >:)

@Ignatz That would be just a matter of adding a little and subtracting a little to the first value in the ellipse line as the angle value of rotate changes. The orbits are so small that the changes wouldn’t be noticeable unless they were over exaggerated. Not worth the trouble.

Modified my code above to make the tables and code easier to read just in case anyone is interested.

@Ignatz,
I cbs doing all the planets, but the idea is there.

--# Main
function setup()
    ellipseMode(CENTER)
    rectMode(CORNERS)
    backingMode(RETAINED)
    p = {Planet(WIDTH/2, HEIGHT/2, 200, 150, .01, 20, color(255))}
    noStroke()
    t = 0
    parameter.integer("tChange", -10, 10, 1)
end

function draw()
    fill(0,100)
    rect(0, 0, WIDTH, HEIGHT)
    fill(255)
    ellipse(WIDTH/2, HEIGHT/2, 50)
    for k,v in pairs(p) do
        v:draw()
    end
    t = t + tChange 
end

--# Planet
Planet = class()

function Planet:init(cenx, ceny, axisx, axisy, speed, size, c)
    self.cenx = cenx
    self.ceny = ceny
    self.axisx = axisx
    self.axisy = axisy
    self.speed = speed
    self.size = size
    self.c = c
end

function Planet:draw()
    self.x = self.cenx + self.axisx * math.cos(self.speed * t)
    self.y = self.ceny + self.axisy * math.sin(self.speed * t)
    fill(self.c)
    ellipse(self.x, self.y, 2 * self.size)
end
-- LOL Buggy but funny.
function setup()
    displayMode(OVERLAY)
    displayMode(FULLSCREEN)
    
    TIME = 0
    FPS = 0
    fpsCounter = 100
    totalSpeed = 0
    totalSize = 0
    moved = vec2(0, 0)
    
    parameter.integer("SPEED", -50, 50, 0)
    parameter.integer("SIZE", 1, 100, 50)
    parameter.watch("TIME")
    parameter.watch("FPS")
    
    objects = {Sun(),
    Planet({moon = {Moon(10, 25), Moon(5, 40)}, color = color(255, 100, 0, 255), speed = .5}),
    Planet({distance = 250, speed = .35, moon = {}, color = color(0, 75, 255, 255)}),
    Planet({speed = .265, distance = 375, moon = {Moon(7, 35, 10), Moon(4, 50, 2), Moon(10, 85, 7)}, color = color(255, 50, 250)}),
    Planet({distance = 520, color = color(150, 200, 175), speed = .4, moon = {Moon(15, 35, .5)}, size = 35}),
    Planet({distance = 720, color = color(0, 150, 255), speed = 1, moon = {Moon(5, 40, 3)}, size = 45}),
    Planet({distance = 890, color = color(50, 200, 255), speed = .1, size = 75, moon = {Moon(5, 45, 2), Moon(6, 60, 1.8), Moon(4, 75, 1.7), Moon(9, 90, 1.4), Moon(7, 105, 1.2), Moon(10, 145, 1)}})
    }
end

function draw()
    background(0, 0, 0, 255)
    
    totalSpeed = SPEED/10
    totalSize = SIZE/50
    TIME = TIME + totalSpeed
    fpsCounter = fpsCounter + 1
    if fpsCounter > 50 then 
        FPS = math.floor(1/DeltaTime)
        fpsCounter = 0
    end
    translate(moved.x, moved.y)
    for a, b in ipairs(objects) do
        b:draw()
    end
    translate(-moved.x, -moved.y)
end

function touched(t)
    moved.x = moved.x + t.deltaX
    moved.y = moved.y + t.deltaY
end

-- Objects

Sun = class()

function Sun:init()
    self.col = color(255, 150, 0, 255)
    self.rot = 0
end

function Sun:draw()
    fill(self.col)
    translate(WIDTH/2, HEIGHT/2)
    rotate(self.rot)
    ellipse(0, 0, 100 * totalSize, 90 * totalSize)
    rotate(-self.rot)
    translate(-WIDTH/2, -HEIGHT/2)
    self.rot = self.rot + .1 * totalSpeed
end

Planet = class()

function Planet:init(tab)
    tab = tab or {}
    self.speed = tab.speed or math.random(50, 2000)/2000
    self.distance = tab.distance or 175
    self.rot = tab.rot or 0
    self.size = tab.size or 25
    self.moon = tab.moon or {Moon(self.size/5, 50)}
    self.color = tab.color or color(math.random(255), math.random(255), math.random(255), 255)
    self.longestOval = tab.ovalSize or 100
    self.currentOval = 0
    self.ovaling = 1
end

function Planet:draw()
    translate(WIDTH/2, HEIGHT/2) -- Translate to Default Orientation
    rotate(self.rot) -- Rotate Around Sun
    translate((self.distance + self.currentOval) * totalSize, 0) -- Translate to Distance from Sun
    
    -- Planet
    fill(self.color)
    ellipse(0, 0, self.size * totalSize)
    
    -- Moons
    for a, b in ipairs(self.moon) do
        while b.rot > 360 do
            b.rot = b.rot - 360
        end
        local formula = (b.rot >= -b.size and b.rot <= b.size) and b.distance > self.distance / self.size
        if formula then
            b.red = true
        else
            b.red = false
        end
        b:draw()
    end
    
    -- To Normal
    translate(-(self.distance + self.currentOval) * totalSize, 0)
    rotate(-self.rot)
    translate(-WIDTH/2, -HEIGHT/2)
    
    -- Update Planets
    self.rot = self.rot + self.speed * totalSpeed
    
    while self.rot > 360 do
        self.rot = self.rot - 360
    end
    
    -- Update Ovals
    if self.ovaling == 1 then
        self.currentOval = self.currentOval + totalSpeed
        if self.currentOval > self.longestOval then
            self.ovaling = 2
        end
    elseif self.ovaling == 2 then
        self.currentOval = self.currentOval - totalSpeed
        if self.currentOval < -self.longestOval then
            self.ovaling = 1
        end
    end
end

Moon = class()

function Moon:init(size, dst, speed)
    self.size = size or 5
    self.rot = 0
    self.distance = dst or 25
    self.speed = speed or math.random(10)
    self.red = true
end

function Moon:draw()
    rotate(self.rot)
    fill(75, 255)
    if self.red then
        fill(255, 0, 0, 255)
    end
    ellipse(self.distance * totalSize, 0, self.size * totalSize)
    rotate(-self.rot)
    self.rot = self.rot + self.speed * totalSpeed
end


@MattthewLXXIII See this link. Also, try a search for orbit.

https://codea.io/talk/discussion/2018/using-physics-to-simulate-moon-in-orbit-around-planet