## Simulate a pendulum

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: Klaus, FourthWorld, heatherlaine, kevinmiller

rumplestiltskin
Posts: 219
Joined: Wed Jun 21, 2006 7:33 pm
Location: West of the Pecos
Contact:

### Simulate a pendulum

I understand the "move" command. What do I have to do to make an object (a line, for example) behave like a pendulum? I'm looking for something that would work in a manner similar to that; for example:

Code: Select all

``move the endpoint of line "myLine" to 400,400 in 1 second``
...and that would cause a line to pivot around the start point of that line.

It's important to have programmatic control over the speed.

Maybe there's a command for that in the dictionary but I just haven't lucked into the proper search term.

Thanks in advance for any assistance.

dunbarx
VIP Livecode Opensource Backer
Posts: 6468
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

### Re: Simulate a pendulum

Hi.

Fun stuff.

How are you with simple trig functions? A pendulum swings in such a way that if you attached a pencil to the bottom of it and dragged a strip of paper underneath as it was swinging, it would draw a sine wave. The amplitude and period are determined by the function:

f(x) = Asin(kx-c) You can ignore "c".

This may be more than you wanted to know, but I am pointing you along a path, no pun intended. Your endpoint must look like a real pendulum, and that means that it must swing back and forth in a sine-like way, not in a linear way.

So if you map the values of a simple sine wave to a bunch of points along the arc of the circle that the line is traversing, you will be able to simulate a true swing. Will this be a problem in coding or math? Write back if you get stuck.

Craig Newman

sturgis
Livecode Opensource Backer
Posts: 1682
Joined: Sat Feb 28, 2009 11:49 pm

### Re: Simulate a pendulum

You can also cheat and use an oval. Set it to opaque, set the startangle to 200, the arcangle to 1. Set the fill color to black. This gives you a round graphic that is simply a center point and a line to the edge.

Then you can: set the angle of grc "mygraphic" to.. (a value between 200 and 340) Make sure to turn on the acceleratedrendering of the stack, and set the layermod of the grc to dynamic.

The reason for the numbers 200 and 340 is because the startangle of an oval starts with 0 facing right. So straight down is 270.

Simon
VIP Livecode Opensource Backer
Posts: 3901
Joined: Sat Mar 24, 2007 2:54 am
Location: Palo Alto

### Re: Simulate a pendulum

Cool sturgis.
I got me an upside down windshield washer.

Simon
I used to be a newbie but then I learned how to spell teh correctly and now I'm a noob!

rumplestiltskin
Posts: 219
Joined: Wed Jun 21, 2006 7:33 pm
Location: West of the Pecos
Contact:

### Re: Simulate a pendulum

"upside down windshield washer". Yep; that seems to be it but it's a bit harder to control the speed that through the "move" command. I am using a repeat loop with a wait command (wait .005 millisecond, for example) to do this. Am I wrong?

Thanks,
Barry

sturgis
Livecode Opensource Backer
Posts: 1682
Joined: Sat Feb 28, 2009 11:49 pm

### Re: Simulate a pendulum

For just a quicky test to see how it looked I used a send in time. Tracked the milliseconds, used the time to adjust how far to change the angle, and when to reverse direction.

Wasn't good enough, but wasn't bad. Would take more thinking on my part to make it smooth.

My code is very ugly because it was a quick hack job but I can post it if you like. (Also didn't bother doing much of a job checking bounds so there is some weird behavior due to where 0 hits at 90 degrees to the right.)

rumplestiltskin
Posts: 219
Joined: Wed Jun 21, 2006 7:33 pm
Location: West of the Pecos
Contact:

### Re: Simulate a pendulum

Sturgis,

Yes, your code would be helpful if for no other reason that to know ways to do cool things. When I was simply moving a ball along a straight line, coding the controls to modify speed were quite simple. It may be that I am pursuing a skeuomorphic course when, in fact, it might be simpler to abandon the pendulum and think different about this. I am not far along in this project and switching away from the pendulum would just require a re-think of the skin I decide to put on the interface.

Regards,
Barry

EDIT: Fixed my last (incomplete) sentence in the first paragraph.
Last edited by rumplestiltskin on Mon Apr 29, 2013 4:19 am, edited 1 time in total.

Simon
VIP Livecode Opensource Backer
Posts: 3901
Joined: Sat Mar 24, 2007 2:54 am
Location: Palo Alto

### Re: Simulate a pendulum

Ahh sturgis,
Are you adding acceleration/deceleration to the movement?
cool.

Simon
I used to be a newbie but then I learned how to spell teh correctly and now I'm a noob!

sturgis
Livecode Opensource Backer
Posts: 1682
Joined: Sat Feb 28, 2009 11:49 pm

### Re: Simulate a pendulum

Nope, was just a straight adjustment to see how it looked. Would take a rework to put in acceleration/deceleration and I don't understand enough about the affects of gravity on an object with a fixed pivot point.

Did I mention the code is an ugly hack? Either way, here it is.
All code is in a button, only 1 grc on the screen so didn't bother to name it.

Code: Select all

``````local sRunning,sLastTime,sStartAngle,sEndAngle,sFactor,sDirection
on mouseUp
set the acceleratedrendering of this stack to true
if sRunning is empty then put false into sRunning
put 250 into sStartAngle
put 290 into sEndAngle
put (sEndAngle - sStartAngle) / 1000 into sFactor
put 1 into sDirection
put not sRunning into srunning
set the angle of grc 1 to 270
startPend
end mouseUp

command startPend
if sRunning then
if sLastTime is empty then put the milliseconds into sLastTime
put the milliseconds into tTime
set the angle of grc 1 to the angle of grc 1 + round((((tTime - sLastTime) * sFactor) * sDirection))
if the angle of grc 1 > sEndAngle then put -1 into sDirection
if the angle of grc 1 < sStartAngle then put 1 into sDirection
put tTime into sLastTime
if not lineoffset("startpend",the pendingmessages) then
send "startPend" to me in 15 milliseconds
end if
else
put empty into sLastTime
end if
end startPend
``````

EDITED to fix the behavior some (emptying sLastTime)

sturgis
Livecode Opensource Backer
Posts: 1682
Joined: Sat Feb 28, 2009 11:49 pm

### Re: Simulate a pendulum

The function provided by dunbar and a standard line would probably look better than what I did.

Also, somewhere around here I posted a simple library that deals with vectors/angles/vector lengths etc and it might have something useful in there.

RogGuay
VIP Livecode Opensource Backer
Posts: 65
Joined: Fri Apr 28, 2006 12:10 am

### Re: Simulate a pendulum

Have a look at my Simple Pendulum stack here:

https://www.dropbox.com/s/3cjrgwb7s7wdg ... ulumMS.rev
Cheers,
Roger

danielrr
Posts: 115
Joined: Mon Mar 04, 2013 4:03 pm

### Re: Simulate a pendulum

RogGuay wrote:Have a look at my Simple Pendulum stack here:

https://www.dropbox.com/s/3cjrgwb7s7wdg ... ulumMS.rev
Cheers,
Roger

Great! Thanks

sturgis
Livecode Opensource Backer
Posts: 1682
Joined: Sat Feb 28, 2009 11:49 pm

### Re: Simulate a pendulum

Nice pendulum!

rumplestiltskin
Posts: 219
Joined: Wed Jun 21, 2006 7:33 pm
Location: West of the Pecos
Contact:

### Re: Simulate a pendulum

Roger, that stack is beautiful. Thanks for the great example.

I'm...not...worthy.

dunbarx
VIP Livecode Opensource Backer
Posts: 6468
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

### Re: Simulate a pendulum

Roger.

Very nice indeed. I think this should be required reading.

There is a small bug somewhere. When the sine wave gets to the right edge of the screen, the gravity increases. I only mention it because I believe this stack will be needed by others in the future.

Craig Newman