Moving point in a circle?

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
User avatar
Vimm
Party member
Posts: 113
Joined: Wed Mar 16, 2016 8:14 pm

Moving point in a circle?

Post by Vimm »

I'm using love.graphics.polygon to make a triangle, and I'm trying to move the last x and y coordinates change slowly, in a circular motion, so that the triangle kinda represents a shadow as the sun moves about its cycle.

However, I can't figure out how to do this, I found a thread for a different programming language that has someone describing how to do it:
x = cx + Math.sin(time)*rad;
y = cy + Math.cos(time)*rad;

cx,cy are the center of the circle and

rad is the radius of the circle.

As time goes from 0 to 2*PI (or multiples thereof), the x,y values will describe a complete circle.
but i wasnt able to apply this, if someone could give me a hand I'd appreciate it :D
User avatar
pgimeno
Party member
Posts: 3544
Joined: Sun Oct 18, 2015 2:58 pm

Re: Moving point in a circle?

Post by pgimeno »

That should work. Can you post what you have attempted?

Of course, in Lua, math should be lowercase but I guess you know that.
User avatar
Vimm
Party member
Posts: 113
Joined: Wed Mar 16, 2016 8:14 pm

Re: Moving point in a circle?

Post by Vimm »

pgimeno wrote:That should work. Can you post what you have attempted?

Of course, in Lua, math should be lowercase but I guess you know that.
I ended up getting some help from someone, but ill post the code that works just in case someone needs something similar

Code: Select all

function love.load()
	startX = love.graphics.getWidth()/2
	startY = love.graphics.getHeight()
	
	cx = 0
	cy = 0
	
	radius = 350
	angle = -math.pi
end

function love.update(dt)
	cx = startX + radius * math.cos(angle)
	cy = startY + radius * math.sin(angle)
	
	angle = angle + math.pi * dt
end

function love.draw()
	love.graphics.polygon("fill", (love.graphics.getWidth()/2) - 16,(love.graphics.getHeight()/2),
		(love.graphics.getWidth()/2) + 16,(love.graphics.getHeight()/2),
			cx,cy- love.graphics.getHeight()/2)
end
Post Reply

Who is online

Users browsing this forum: No registered users and 46 guests