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
darkfrei
Party member
Posts: 1169
Joined: Sat Feb 08, 2020 11:09 pm

Re: Cricular velocity

Post by darkfrei »

It's very easy, just keep the table of parent in the child object:

Code: Select all

-- size of the screen
Width, Height = love.graphics.getDimensions( )

-- table with the middle
Sun = {x=Width/2, y=Height/2}

-- list of planets
Planets = {}

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

Mercury = addPlanet (Sun, 50, 2*math.pi*math.random(), 100/50)
Venus = addPlanet (Sun, 100, 2*math.pi*math.random(), 100/100)
Earth = addPlanet (Sun, 200, 2*math.pi*math.random(), 100/200)
Moon = addPlanet (Earth, 35, 2*math.pi*math.random(), 50/35)

 
function love.update(dt)
	local t = love.timer.getTime()
	for i, planet in ipairs (Planets) do
		planet.x = planet.middle.x + planet.radius*math.cos (t*planet.omega + planet.phi)
		planet.y = planet.middle.y + planet.radius*math.sin (t*planet.omega + planet.phi)
	end
end


function love.draw()
	love.graphics.setColor(1,1,0)
	love.graphics.circle ('fill', Sun.x, Sun.y, 5)
	love.graphics.setColor(1,1,1)
	for i, planet in ipairs (Planets) do
		love.graphics.circle ('line', planet.middle.x, planet.middle.y, planet.radius)
		love.graphics.circle ('fill', planet.x, planet.y, 5)
	end
end
2022-06-17T00_43_56-Untitled.png
2022-06-17T00_43_56-Untitled.png (23.65 KiB) Viewed 1627 times
orbits-01.love
(804 Bytes) Downloaded 78 times
:awesome: in Lua we Löve
:awesome: Platformer Guide
:awesome: freebies
User avatar
Gunroar:Cannon()
Party member
Posts: 1085
Joined: Thu Dec 10, 2020 1:57 am

Re: Cricular velocity

Post by Gunroar:Cannon() »

darkfrei wrote: Thu Jun 16, 2022 10:47 pm It's very easy, just keep the table of parent in the child object:

Code: Select all

-- size of the screen
Width, Height = love.graphics.getDimensions( )

-- table with the middle
Sun = {x=Width/2, y=Height/2}

-- list of planets
Planets = {}

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

Mercury = addPlanet (Sun, 50, 2*math.pi*math.random(), 100/50)
Venus = addPlanet (Sun, 100, 2*math.pi*math.random(), 100/100)
Earth = addPlanet (Sun, 200, 2*math.pi*math.random(), 100/200)
Moon = addPlanet (Earth, 35, 2*math.pi*math.random(), 50/35)

 
function love.update(dt)
	local t = love.timer.getTime()
	for i, planet in ipairs (Planets) do
		planet.x = planet.middle.x + planet.radius*math.cos (t*planet.omega + planet.phi)
		planet.y = planet.middle.y + planet.radius*math.sin (t*planet.omega + planet.phi)
	end
end


function love.draw()
	love.graphics.setColor(1,1,0)
	love.graphics.circle ('fill', Sun.x, Sun.y, 5)
	love.graphics.setColor(1,1,1)
	for i, planet in ipairs (Planets) do
		love.graphics.circle ('line', planet.middle.x, planet.middle.y, planet.radius)
		love.graphics.circle ('fill', planet.x, planet.y, 5)
	end
end
Oh, wow. So organized. I love it ^^
The risk I took was calculated,
but man, am I bad at math.

-How to be saved and born again :huh:
User avatar
knorke
Party member
Posts: 238
Joined: Wed Jul 14, 2010 7:06 pm
Contact:

Re: Cricular velocity

Post by knorke »

relevant reading: the unit circle
https://en.wikipedia.org/wiki/Unit_circle
https://commons.wikimedia.org/wiki/File ... os_sin.gif

also the orbits can become ellipses if you multiply sin() and cos() by different factors.
User avatar
darkfrei
Party member
Posts: 1169
Joined: Sat Feb 08, 2020 11:09 pm

Re: Cricular velocity

Post by darkfrei »

knorke wrote: Fri Jun 17, 2022 12:23 am also the orbits can become ellipses if you multiply sin() and cos() by different factors.
Also you are need to tilt the ellipse orbit to the random angle, move ellipse focus to the attraction point and adjust the orbital speed proportional to the triangle area by this dt. Every of this tasks is pretty complicated in math.
:awesome: in Lua we Löve
:awesome: Platformer Guide
:awesome: freebies
User avatar
Gunroar:Cannon()
Party member
Posts: 1085
Joined: Thu Dec 10, 2020 1:57 am

Re: Cricular velocity

Post by Gunroar:Cannon() »

Okay, I used a code like this to rotate an object around a point

Code: Select all


    rotator.getVx = function(self, vx)
        return math.cos(self.time_alive)*radius+vx
    and

   rotator.getVy = function (self, vy)
        return math.sin(self.time_alive)*radius+vy
    end

    rotator. update = function (self, dt)
        ...
       
       self.x = self.x + self:getVx(self.vx)*dt+ (self.parent.x-self.parent.old_x)
        ... the same thing for y ...
        ...
    end
end

...but when the object moves the orbiter moves slower or faster if the object moves in the opposite direction.

These fixes don't work + radius also seems to not be working like intended (multiplying radius by 1.2 makes it way bigger then before when it was, let's say multiplied by 1.1)

Code: Select all


    rotator.getVx = function(self, vx)
        return math.cos(self.time_alive)*radius+vx-parent.vx
    and

   rotator.getVy = function (self, vy)
        return math.sin(self.time_alive)*radius+vy-parent.vy
    end
 
    --- and  other variants of this don't work too
    --- like only applying -/+ if the obj vx and parent vx are 
    --- in different directions

These fixes don't work.
Last edited by Gunroar:Cannon() on Sat Jul 09, 2022 10:06 am, 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: 1169
Joined: Sat Feb 08, 2020 11:09 pm

Re: Cricular velocity

Post by darkfrei »

Gunroar:Cannon() wrote: Fri Jul 08, 2022 6:53 pm Okay, I used a code like this to rotate an object around a point

Code: Select all

    rotator.getVx = function(self, vx)
        return math.cos(vx)*radius+vx
    and
1) Is the radius global?
2) Why "+vx"?
3) Why self.x will be used as vx? vx is horizontal speed, but x is horizontal position.

Both must be:

Code: Select all

function rotator:getVxVy ()
	local angle = self.angle
	return self.radius * math.cos(angle), self.radius * math.sin(angle) -- maybe [sin, cos] or [cos, -sin]
end

-- update
local vx, vy = rotator:getVxVy ()
rotator.x = rotator.x = dt*vx
rotator.y = rotator.y = dt*vy
rotator.angle = rotator.angle + dt*rotator.angularVelocity
OR
just

Code: Select all

function rotator:update (dt, x, y) -- x and y here are position of the rotation's center
	self.angle = self.angle + dt*self.angularVelocity
	local radius, angle = self.radius, self.angle
	self.x = x + radius * math.cos (angle)
	self.y = y + radius * math.sin (angle)
end

-- update
if player and player.rotators then
	for i, rotator in ipairs (player.rotators) do
		rotator:update (dt, player.x, player.y)
	end
end

-- draw
for i, rotator in ipairs (player.rotators) do
	love.graphics.circle ("fill", rotator.x, rotator.y, 3)
end
Last edited by darkfrei on Fri Jul 08, 2022 8:28 pm, edited 1 time in total.
:awesome: in Lua we Löve
:awesome: Platformer Guide
:awesome: freebies
User avatar
Gunroar:Cannon()
Party member
Posts: 1085
Joined: Thu Dec 10, 2020 1:57 am

Re: Cricular velocity

Post by Gunroar:Cannon() »

Just realized Ibspelt Circular incorrectly. :rofl: Sorry
darkfrei wrote: Fri Jul 08, 2022 7:29 pm
Gunroar:Cannon() wrote: Fri Jul 08, 2022 6:53 pm Okay, I used a code like this to rotate an object around a point

Code: Select all

    rotator.getVx = function(self, vx)
        return math.cos(vx)*radius+vx
    and
1) Is the radius global?
It's pseudocode.
2) Why "+vx"?
getVx doesn't change vx, only modifies it...that should work right?

3) Why self.x will be used as vx? vx is vertical speed, but x is vertical position.
Sorry, mistake. I fixed it. (Should be .vx)
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: 1169
Joined: Sat Feb 08, 2020 11:09 pm

Re: Cricular velocity

Post by darkfrei »

Gunroar:Cannon() wrote: Fri Jul 08, 2022 8:23 pm Sorry, mistake. I fixed it. (Should be .vx)
Ok, and why the math.cos(vx) has the horizontal velocity under the cos, not angle?
:awesome: in Lua we Löve
:awesome: Platformer Guide
:awesome: freebies
User avatar
Gunroar:Cannon()
Party member
Posts: 1085
Joined: Thu Dec 10, 2020 1:57 am

Re: Cric-I mean CIRC-ular velocity

Post by Gunroar:Cannon() »

Haha, dang I'm stupid. I didn't copypaste, and I was distracted while typing it out, that's why there's so many mistakes. In cos and sin should be time_alive (time which entity has lived).

And the problem of the rotator appearing to orbit slower when the parent is moving is still there with your code.
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: 1169
Joined: Sat Feb 08, 2020 11:09 pm

Re: Cric-I mean CIRC-ular velocity

Post by darkfrei »

Gunroar:Cannon() wrote: Sat Jul 09, 2022 10:05 am And the problem of the rotator appearing to orbit slower when the parent is moving is still there with your code.
The omega is an angular velocity, set it higher or use the parent's angular velocity for child's too.

Use the screen screen2gif and show what you have and write how it must be.
:awesome: in Lua we Löve
:awesome: Platformer Guide
:awesome: freebies
Post Reply

Who is online

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