Rotating point around another

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
vinegar-tom
Prole
Posts: 1
Joined: Tue Mar 31, 2020 3:33 pm

Rotating point around another

Post by vinegar-tom »

I wrote a simple code to rotate one point around another, based on this formula.

For some reason, each time this formula is run, the point rotates as expected, but also gradually converges on the center point. I initially had it running in love.update, but I changed it to rotate 30 degrees every time the space bar is pressed, so that I could see more clearly what was happening.

Code: Select all

function love.load()
  origin = {}
  origin.x = 200
  origin.y = 200

  point = {}
  point.x = 100
  point.y = 100
end

function love.draw()
  love.graphics.circle("fill", origin.x, origin.y, 4)
  love.graphics.circle("fill", point.x, point.y, 2)
end

function love.keypressed(key)
  if key == "space" then
    rotatePoint()
  end
end

function rotatePoint()
  angle = math.rad(30)
  cos = math.cos(angle)
  sin = math.sin(angle)

  point.x = cos * (point.x - origin.x) - sin * (point.y - origin.y) + origin.x
  point.y = sin * (point.x - origin.x) + cos * (point.y - origin.y) + origin.y
end
Any thoughts?
monolifed
Party member
Posts: 188
Joined: Sat Feb 06, 2016 9:42 pm

Re: Rotating point around another

Post by monolifed »

https://love2d.org/forums/viewtopic.php ... 04#p226204

Edit: Sorry, this is probably a solution for a different problem
What you are doing wrong is that you modify point.x and point.y.

Edit2:

Code: Select all

local p1, p2
function love.load()
	p1 = {
		ox = 100, oy = 100, -- origin
		rx = 100, ry = 50, -- radii
		tilt = math.rad(30), -- axial tilt (no effect if rx == ry)
		angle = 0, -- start angle in degrees
		color = {1,0,0}
	}
	p1.ct = math.cos(p1.tilt) -- these won't change so we compute here
	p1.st = math.sin(p1.tilt)
	p1.x = 0 -- we defer computation to love.update
	p1.y = 0 -- (or you can call rotatePoint on love.load)

	p2 = {
		ox = 250, oy = 100,
		rx = 100, ry = 100,
		tilt = 0,
		angle = 0,
		color = {0,1,0}
	}
	p2.ct = math.cos(p2.tilt) -- 1.0
	p2.st = math.sin(p2.tilt) -- 0.0
	p2.x = 0
	p2.y = 0
end

function love.draw()
	love.graphics.setColor(p1.color)
	love.graphics.circle("fill", p1.ox, p1.oy, 4)
	love.graphics.circle("fill", p1.x, p1.y, 2)

	love.graphics.setColor(p2.color)
	love.graphics.circle("fill", p2.ox, p2.oy, 4)
	love.graphics.circle("fill", p2.x, p2.y, 2)
end

function rotatePoint(p)
  local a = math.rad(p.angle)
  local x = math.cos(a) * p.rx
  local y = math.sin(a) * p.ry
  local ct = p.ct
  local st = p.st

  p.x =  ct * x + st * y + p.ox
  p.y = -st * x + ct * y + p.oy
end

-- ignore tilt, ct, st, and assume ry == rx
function rotatePointCircular(p)
  local a = math.rad(p.angle)

  p.x = math.cos(a) * p.rx + p.ox
  p.y = math.sin(a) * p.rx + p.oy
end

function love.update(dt)
	p1.angle = (p1.angle + 60 * dt) % 360
	rotatePoint(p1)

	p2.angle = (p2.angle + 60 * dt) % 360
	rotatePoint(p2)
	-- or
	-- rotatePointCircular(p2)
end
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Bing [Bot], Nikki and 6 guests