Cric-I mean CIRC-ular velocity

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.
User avatar
Gunroar:Cannon()
Party member
Posts: 1088
Joined: Thu Dec 10, 2020 1:57 am

Cric-I mean CIRC-ular velocity

Post by Gunroar:Cannon() »

I'm trying to do circular velocity. My implementation after some research:

Code: Select all

speed = 10
radius = 500
angle = 0
...
function update(dt)
    angle = angle+dt*speed
    local t = angle
    vx = radius*math.sin(t) -- I thought it was cos, but one source said sine :P
    vy = radius*math.cos(t)
end

But the speed doesn't seem constant. If it moves super fast at a radius of 500 it should move unreasonably fast at one of 5 but it seems like the speed is revolutions per second or something when I want it to be pixels per second (if that makes sense :P)
Last edited by Gunroar:Cannon() on Fri Jul 08, 2022 8:25 pm, edited 2 times in total.
The risk I took was calculated,
but man, am I bad at math.

-How to be saved and born again :huh:
User avatar
darkfrei
Party member
Posts: 1174
Joined: Sat Feb 08, 2020 11:09 pm

Re: Cricular velocity

Post by darkfrei »

Code: Select all

omega = (2*math.pi)/4 -- one rotation per 4 seconds
v = 400 -- velocity 400 pixels per second
x, y, angle = 0, 0, 0

function update(dt)
	-- do movement:
	x = x + v*dt*math.cos(angle)
	y = y + v*dt*math.sin(angle)
	-- do rotation:
	angle = angle + omega*dt
end
2022-06-15T18_38_26-Untitled.png
2022-06-15T18_38_26-Untitled.png (6.98 KiB) Viewed 1840 times
angular-velocity-01.love
(590 Bytes) Downloaded 80 times
velocities, angular velocities and radiuses:

Code: Select all

--	v	omega	r
--	400	1	400
--	300	1	300
--	200	1	200
--	200	0.5	800
--	100	0.5	200
--	50	0.5	100
--	50	0.25	200
--	50	0.125	800
--	25	0.25	100
:awesome: in Lua we Löve
:awesome: Platformer Guide
:awesome: freebies
User avatar
pgimeno
Party member
Posts: 3548
Joined: Sun Oct 18, 2015 2:58 pm

Re: Cricular velocity

Post by pgimeno »

Gunroar:Cannon() wrote: Wed Jun 15, 2022 9:23 am I'm trying to do circular velocity. My implementation after some research:

Code: Select all

speed = 10
radius = 500
angle = 0
...
function update(dt)
    angle = angle+dt*speed
If you want the speed in pixels per second and the radius is in pixels, divide the speed by the radius:

Code: Select all

    angle = angle+dt*speed/radius
User avatar
Gunroar:Cannon()
Party member
Posts: 1088
Joined: Thu Dec 10, 2020 1:57 am

Re: Cricular velocity

Post by Gunroar:Cannon() »

Oh, ...errr...wow. They both work? Thanks but now I'm not sure which is the "best" :ultrahappy:
pgimeno wrote: Wed Jun 15, 2022 7:28 pm If you want the speed in pixels per second and the radius is in pixels, divide the speed by the radius:

Code: Select all

    angle = angle+dt*speed/radius
Woah, How does that even work :? :rofl:
The risk I took was calculated,
but man, am I bad at math.

-How to be saved and born again :huh:
User avatar
pgimeno
Party member
Posts: 3548
Joined: Sun Oct 18, 2015 2:58 pm

Re: Cricular velocity

Post by pgimeno »

Gunroar:Cannon() wrote: Wed Jun 15, 2022 11:14 pm Woah, How does that even work :? :rofl:
Maths :)

1 radian, by definition, is the length of the arc of a circle that is equal to its radius (hence the name, "radian" comes from "radius").

Imagine you have a string of length R and a piece of chalk. You use your thumb to hold one end of the string, and use the other end to trace a circle using the chalk, which will have radius R.

Once you're done, you take the string and place it on the circle you have drawn, curving it as appropriate so it follows the circle exactly. Well, the angle between the centre of the circle and each of the ends of the string will be 1 radian.

So, 1 radian is the angle that makes the length of the arc be equal to the radius. If the radius is 50 pixels long, the arc will be 50 pixels long too. If the radius is 15 cm, the arc will be the 15 cm too, and so on.

You want to achieve a certain rotation speed in pixels per second, but the sine and cosine functions only understand radians. Dividing the speed in pixels per second by the radius in pixels, will give you the speed in radians per second, which is an actual angular speed that you can use to control the angle in radians, so that the trig functions can understand the units.

Maybe that gives you a first hint of why maths and physics people like radians better than degrees :)
User avatar
Gunroar:Cannon()
Party member
Posts: 1088
Joined: Thu Dec 10, 2020 1:57 am

Re: Cricular velocity

Post by Gunroar:Cannon() »

Ahhh, that makes sense. Thanks.
Maybe that gives you a first hint of why maths and
physics people like radians better than degrees
Maybe...just maybe...
The risk I took was calculated,
but man, am I bad at math.

-How to be saved and born again :huh:
User avatar
Gunroar:Cannon()
Party member
Posts: 1088
Joined: Thu Dec 10, 2020 1:57 am

Re: Cricular velocity

Post by Gunroar:Cannon() »

Oh, sorry for the double post, but I was fiddling around with it and it works well but then now I'm wondering how to make it so that it orbits around a point/other object at all times?
The risk I took was calculated,
but man, am I bad at math.

-How to be saved and born again :huh:
User avatar
darkfrei
Party member
Posts: 1174
Joined: Sat Feb 08, 2020 11:09 pm

Re: Cricular velocity

Post by darkfrei »

Gunroar:Cannon() wrote: Thu Jun 16, 2022 10:44 am Oh, sorry for the double post, but I was fiddling around with it and it works well but then now I'm wondering how to make it so that it orbits around a point/other object at all times?

Code: Select all

time = 0 -- time
cX, cY = 400, 300 -- center position
radius = 250 -- orbit position
omega = 0.1 -- angular velocity
phi = math.pi/2 -- starting angular position
length = 20

function love.update (dt)
	time = time + dt
end

function love.draw ()
	local posX = cX + radius*math.cos(time*omega + phi)
	local posY = cY + radius*math.sin(time*omega + phi)
	local direction = time*omega + phi + math.pi/2 -- perpendicular to radius
	local dx = length*math.cos(direction)
	local dy = length*math.sin(direction)
	love.graphics.line (posX, posY, posX+dx, posY+dy)
end

Solar system:

Code: Select all

Width, Height = love.graphics.getDimensions( )
cX, cY = Width/2, Height/2

Planets = {}

function addPlanet (radius, phi, omega)
	local t = love.timer.getTime()
	local planet = 
		{
			omega=omega,
			radius=radius,
			phi=phi,
			x = cX + radius*math.cos (t*omega + phi),
			y = cY + radius*math.sin (t*omega + phi),
		}
	
	table.insert (Planets, planet)
end


addPlanet (15, 2*math.pi*math.random(), 100/15)
addPlanet (30, 2*math.pi*math.random(), 100/30)
addPlanet (50, 2*math.pi*math.random(), 100/50)
addPlanet (100, 2*math.pi*math.random(), 100/100)
addPlanet (150, 2*math.pi*math.random(), 100/150)
addPlanet (200, 2*math.pi*math.random(), 100/200)
addPlanet (300, 2*math.pi*math.random(), 100/300)
 
function love.update(dt)
	local t = love.timer.getTime()
	for i, planet in ipairs (Planets) do
		planet.x = cX + planet.radius*math.cos (t*planet.omega + planet.phi)
		planet.y = cY + planet.radius*math.sin (t*planet.omega + planet.phi)
	end
end


function love.draw()
	love.graphics.setColor(1,1,0)
	love.graphics.circle ('fill', cX, cY, 5)
	love.graphics.setColor(1,1,1)
	for i, planet in ipairs (Planets) do
		love.graphics.circle ('line', cX, cY, planet.radius)
		love.graphics.circle ('fill', planet.x, planet.y, 5)
	end
end
Solar system
Solar system
2022-06-16T16_33_06-Untitled.png (44.26 KiB) Viewed 1697 times
:awesome: in Lua we Löve
:awesome: Platformer Guide
:awesome: freebies
User avatar
pgimeno
Party member
Posts: 3548
Joined: Sun Oct 18, 2015 2:58 pm

Re: Cricular velocity

Post by pgimeno »

Gunroar:Cannon() wrote: Thu Jun 16, 2022 10:44 am Oh, sorry for the double post, but I was fiddling around with it and it works well but then now I'm wondering how to make it so that it orbits around a point/other object at all times?
1. Engage brain.
2. Do it.

You have a circle with a centre; to orbit an object, just use the object's position as the centre. Duh.

You don't have a centre? You have a cosine and a sine times the radius; add the centre to that.
User avatar
Gunroar:Cannon()
Party member
Posts: 1088
Joined: Thu Dec 10, 2020 1:57 am

Re: Cricular velocity

Post by Gunroar:Cannon() »

pgimeno wrote: Thu Jun 16, 2022 3:07 pm 1. Engage brain.
2. Do it.

You have a circle with a centre; to orbit an object, just use the object's position as the centre. Duh.

You don't have a centre? You have a cosine and a sine times the radius; add the centre to that.
Oh, sorry. You see...errmm...the thing is...I don't have a brain I tried implementing it but it was off and not an exact orbit so I thought I might as well ask to see if I got an answer before I fixed it... buuuuuut then I put my debug on and realized the position of the collision rect was off (also the rect used for image) but the position of the actual size was correct. Heheh.

I made it move by the velocity of the object it's rotating then did some positioning for the object...so it works for now. Thanks for the help.
Attachments
Small object rotates around Knight thing
Small object rotates around Knight thing
Screenshot_2022-06-16-22-48-56.png (154.08 KiB) Viewed 1634 times
The risk I took was calculated,
but man, am I bad at math.

-How to be saved and born again :huh:
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot] and 85 guests