formula is (x_0, y_0) + (R1 * sin(t), R2 *cos(t)) for t= 0 to 2* PI
and R1 = R2 for circle
it seems like you are updating your initial points in the function foo, you probably meant "x = point.x + sin(a)" ?
edit: I tried something random... rather than fixing it
Code: Select all
--#region
Vector2 = {}
Vector2.__index = vector2
function vector2(x, y)
vec2 = {}
setmetatable(Vector2, vec2)
vec2.x = x
vec2.y = y
return vec2
end
--#endregion
-- sun
sunPos = vector2(0, 0)
sunGM = 100
--planet1
Planet1 = {R_min = 50, R_max = 125, tilt = math.pi/3, phase = 0, orbit = vector2(50, 0)}
Planet1.color = {0.2, 1, 0.2, 1}
Planet1.pos = vector2(0, 0)
Planet1.angle = Planet1.phase
--planet2
Planet2 = {R_min = 150, R_max = 100, tilt = math.pi/6, phase = 0, orbit = vector2(0, 40)}
Planet2.color = {1, 0.2, 0.2, 1}
Planet2.pos = vector2(0, 0)
Planet2.angle = Planet2.phase
function initPlanetPos(planet)
local p = planet.phase
local x = planet.orbit.x
local y = planet.orbit.y
local R = planet.R_max
local r = planet.R_min
planet.pos.x, planet.pos.y = x + R * math.cos(p), y + r * math.sin(p)
end
function updatePlanetPos(planet, dt)
local a = planet.angle
--local v = planet.velocity
local R = planet.R_max
local r = planet.R_min
local d1 = planet.pos.x - sunPos.x
local d2 = planet.pos.y - sunPos.y
local d = math.sqrt(d1 * d1 + d2 * d2)
local v = math.sqrt(sunGM * (2 / d - 1 / R))
a = (a + v * dt) % (2 * math.pi)
planet.angle = a
planet.pos.x, planet.pos.y = planet.orbit.x + R * math.cos(a), planet.orbit.y + r * math.sin(a)
end
function love.load(arg)
love.window.setTitle("Galaxy example 0.5")
love.window.setMode(512, 512, {msaa = 16, vsync = true}) --if disable the vsync the spiral decreases the curve
initPlanetPos(Planet1)
initPlanetPos(Planet2)
end
function love.update(dt)
if not paused then
updatePlanetPos(Planet1, dt)
updatePlanetPos(Planet2, dt)
else
--paused
end
end
function love.draw()
--if not paused then
love.graphics.translate(256, 256)
love.graphics.setColor(Planet1.color)
love.graphics.rotate(-Planet1.tilt)
love.graphics.ellipse( "line", Planet1.orbit.x, Planet1.orbit.y, Planet1.R_max, Planet1.R_min, 32 )
love.graphics.rotate(Planet1.tilt)
love.graphics.setColor(Planet2.color)
love.graphics.rotate(-Planet2.tilt)
love.graphics.ellipse( "line", Planet2.orbit.x, Planet2.orbit.y, Planet2.R_max, Planet2.R_min, 32 )
love.graphics.rotate(Planet2.tilt)
love.graphics.setColor(1, 0.7, 0, 1)
love.graphics.circle("fill", sunPos.x, sunPos.y, 32)
love.graphics.setColor(Planet1.color)
love.graphics.rotate(-Planet1.tilt)
love.graphics.circle("fill", Planet1.pos.x, Planet1.pos.y, 8)
love.graphics.rotate(Planet1.tilt)
love.graphics.setColor(Planet2.color)
love.graphics.rotate(-Planet2.tilt)
love.graphics.circle("fill", Planet2.pos.x, Planet2.pos.y, 8)
love.graphics.rotate(Planet2.tilt)
--else
--paused
--end
end
function love.keypressed(key, scancode, isrepeat)
if key == "escape" then
paused = not paused
end
end