Problem to make an Orbit (circle with sin(x) and cos(x)) [SOLVED]

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
nickelodeon0077
Prole
Posts: 22
Joined: Mon Feb 04, 2019 2:38 pm
Location: Curitiba, PR, Brazil

Problem to make an Orbit (circle with sin(x) and cos(x)) [SOLVED]

Post by nickelodeon0077 »

i have an problem with the algorythm of make an circle with sin and cos
when i call this algorythm i have an spiral and not a circle
pls help

an example

Code: Select all

x = 100
y = 50
point = {x = 50, y = 50}

function foo()
	a = math.atan2(y-point.y, x-point.x)
	x = x + sin(a)
	y = y + cos(a)
end
:cry:
Attachments
space.love
(967 Bytes) Downloaded 168 times
main.lua
(2.41 KiB) Downloaded 170 times
Last edited by nickelodeon0077 on Fri Mar 08, 2019 11:56 pm, edited 3 times in total.
monkyyy
Citizen
Posts: 52
Joined: Fri Mar 16, 2012 5:29 pm

Re: Proble to make an Orbit (circle with sin(x) and cos(x))

Post by monkyyy »

Logic bug?

Floats don't follow indentity laws as the rounding errors stack up

If you want the distance to not change you can't really use addition and subtraction, try fixing the distance at the start of a step then scale the position after your logic
monolifed
Party member
Posts: 188
Joined: Sat Feb 06, 2016 9:42 pm

Re: Proble to make an Orbit (circle with sin(x) and cos(x))

Post by monolifed »

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
Last edited by monolifed on Wed Aug 26, 2020 9:06 am, edited 2 times in total.
nickelodeon0077
Prole
Posts: 22
Joined: Mon Feb 04, 2019 2:38 pm
Location: Curitiba, PR, Brazil

Re: Proble to make an Orbit (circle with sin(x) and cos(x))

Post by nickelodeon0077 »

ingsoc451 wrote: Thu Mar 07, 2019 3:41 am 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
man thnks with your code i understood better ;)
anyway, what is the github link in the code
monolifed
Party member
Posts: 188
Joined: Sat Feb 06, 2016 9:42 pm

Re: Problem to make an Orbit (circle with sin(x) and cos(x))

Post by monolifed »

Ignore it, it is a copy-paste error (I was searching through some github projects at the time)
I used the equation from the following page to calculate speed
https://en.wikipedia.org/wiki/Elliptic_orbit
nickelodeon0077
Prole
Posts: 22
Joined: Mon Feb 04, 2019 2:38 pm
Location: Curitiba, PR, Brazil

Re: Problem to make an Orbit (circle with sin(x) and cos(x))

Post by nickelodeon0077 »

ingsoc451 wrote: Fri Mar 08, 2019 3:38 am Ignore it, it is a copy-paste error (I was searching through some github projects at the time)
I used the equation from the following page to calculate speed
https://en.wikipedia.org/wiki/Elliptic_orbit
i'm imagineted ;)
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Google [Bot] and 46 guests