Double Pendulum spinning like crazy

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

Double Pendulum spinning like crazy

Post by Gunroar:Cannon() »

Heyyyyyy,
I have a double pendulum simulation but sometimes it starts to spins too fast and then even disappears into space and time :halloween: . I would want it to spin and then maybe go back to a random motion and not just keep going in circles. I've checked and it seems one of the causes is angular velocity (w1/ w2) being to high(9 maybe?).

I've tried to include a check that when it goes too fast it slows down but that causes it to slow down too rapidly all of a sudden. It's weird :brows: :rofl:?

Anyone know how to fix this problem, or at least implement a check to stop it from happening?
Attachments
pending.love
(2.38 KiB) Downloaded 199 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: 1172
Joined: Sat Feb 08, 2020 11:09 pm

Re: Double Pendulum spinning like crazy

Post by darkfrei »

Gunroar:Cannon() wrote: Tue Jun 08, 2021 9:51 pm Anyone know how to fix this problem, or at least implement a check to stop it from happening?
Go away from Euler's method to Runge-Kutta method. It makes more carefully interpolation and the energy of system will be much closer to the constant.
:awesome: in Lua we Löve
:awesome: Platformer Guide
:awesome: freebies
User avatar
Gunroar:Cannon()
Party member
Posts: 1088
Joined: Thu Dec 10, 2020 1:57 am

Re: Double Pendulum spinning like crazy

Post by Gunroar:Cannon() »

But I'm not familiar with that method. It might break my whole system. Isn't there any shortcut or method to check this and if not what are thr benefits of the other method and implementations of it. Does it still use all the variables and only the formula changes?
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: 1172
Joined: Sat Feb 08, 2020 11:09 pm

Re: Double Pendulum spinning like crazy

Post by darkfrei »

I am not familiar too :)

It's just a function, that needs the acceleration formula (function) as one of input parameters.
On my first try I've made an error, but now I am much closer to make it in the right way.
:awesome: in Lua we Löve
:awesome: Platformer Guide
:awesome: freebies
User avatar
Gunroar:Cannon()
Party member
Posts: 1088
Joined: Thu Dec 10, 2020 1:57 am

Re: Double Pendulum spinning like crazy

Post by Gunroar:Cannon() »

darkfrei wrote: Wed Jun 09, 2021 8:14 pm I am not familiar too :)

It's just a function, that needs the acceleration formula (function) as one of input parameters.
On my first try I've made an error, but now I am much closer to make it in the right way.
Could you show me what you've done so far? I've been looking for the other method's formula but I can't see the exact function for a double pendulum I want any where.
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: 1172
Joined: Sat Feb 08, 2020 11:09 pm

Re: Double Pendulum spinning like crazy

Post by darkfrei »

Gunroar:Cannon() wrote: Fri Jun 11, 2021 11:02 am Could you show me what you've done so far? I've been looking for the other method's formula but I can't see the exact function for a double pendulum I want any where.
I've used this source:
https://gafferongames.com/post/integration_basics/

Update:
First google solution: https://github.com/DavidColson/Double-P ... r/main.lua
:awesome: in Lua we Löve
:awesome: Platformer Guide
:awesome: freebies
User avatar
Gunroar:Cannon()
Party member
Posts: 1088
Joined: Thu Dec 10, 2020 1:57 am

Re: Double Pendulum spinning like crazy

Post by Gunroar:Cannon() »

Hmmm, will that really make it better? Does it stop the infinite spinning but still allow it to spin fast? That other tab said euler method is good for games, but I'll try.
That first link you posted lead me to another link https://gafferongames.com/post/fix_your_timestep/(the last solution) which I used to fix the update so that the pendulum will always update correctly, I did:

Code: Select all

        love.load()
            accumulator = 0
            ...

        love.update(dt)
            ac = 1/100--fixed delta at 0.01
            accumulator = accumulator+dt
        ...
        --still in update
		    while (accumulator>=ac) do
		        for _,pendulum in ipairs(allpendulums) do
		            pendulum.update(ac)
		            boolean collsion = circleVsRect(pendulum.x2, pendulum.y2,
		            pendulum.radius,
		            player.x, player.y, player.w, player.h)
		
	               	if collsion then
		                   killPlayer()
                   end
	       end 	
		    
            accumulator = accumulator-ac
		    end
But now the pendulums always start slow, then move normally, why do they start slow? Is it because dt is really small in first frame so it doesn't update(dt is at 0.007)?
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: 1172
Joined: Sat Feb 08, 2020 11:09 pm

Re: Double Pendulum spinning like crazy

Post by darkfrei »

You can set the same dt for all cycles in the same update:

Code: Select all

love.update(dt)
  local dt_max = 0.005 -- (5 milliseconds)
  local n = math.ceil(dt/dt_max)
  for i = 1, n do
    update (dt/n)
  end
end
Last edited by darkfrei on Sat Jun 12, 2021 2:04 pm, edited 2 times in total.
:awesome: in Lua we Löve
:awesome: Platformer Guide
:awesome: freebies
User avatar
Gunroar:Cannon()
Party member
Posts: 1088
Joined: Thu Dec 10, 2020 1:57 am

Re: Double Pendulum spinning like crazy

Post by Gunroar:Cannon() »

Wait, what? Can you explain a little bit how this works. Doesn't math.ceil return a whole integer so won't it always return zero?
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: 1172
Joined: Sat Feb 08, 2020 11:09 pm

Re: Double Pendulum spinning like crazy

Post by darkfrei »

Gunroar:Cannon() wrote: Sat Jun 12, 2021 1:43 pm Wait, what? Can you explain a little bit how this works. Doesn't math.ceil return a whole integer so won't it always return zero?
Sorry, forgot that dt is in seconds, not in milliseconds.

16.6 ms / 5 ms gives the 3.32, it will be ceil-rounded to 4.

So the dt by the simulation will be 4 times with new dt = 16.6/4 = 4.15 ms.
Attachments
pending-02.love
(2.4 KiB) Downloaded 150 times
Last edited by darkfrei on Sun Jun 13, 2021 9:32 pm, edited 1 time in total.
:awesome: in Lua we Löve
:awesome: Platformer Guide
:awesome: freebies
Post Reply

Who is online

Users browsing this forum: No registered users and 26 guests