Page 1 of 3

### Simulate a pendulum

Posted: Sun Apr 28, 2013 4:09 pm
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.

### Re: Simulate a pendulum

Posted: Sun Apr 28, 2013 11:02 pm
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

### Re: Simulate a pendulum

Posted: Mon Apr 29, 2013 12:30 am
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.

### Re: Simulate a pendulum

Posted: Mon Apr 29, 2013 1:22 am
Cool sturgis.
I got me an upside down windshield washer.

Simon

### Re: Simulate a pendulum

Posted: Mon Apr 29, 2013 3:29 am
"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

### Re: Simulate a pendulum

Posted: Mon Apr 29, 2013 3:38 am
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.)

### Re: Simulate a pendulum

Posted: Mon Apr 29, 2013 4:00 am
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.

### Re: Simulate a pendulum

Posted: Mon Apr 29, 2013 4:03 am
Ahh sturgis,
Are you adding acceleration/deceleration to the movement?
cool.

Simon

### Re: Simulate a pendulum

Posted: Mon Apr 29, 2013 4:07 am
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)

### Re: Simulate a pendulum

Posted: Mon Apr 29, 2013 4:20 am
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.

### Re: Simulate a pendulum

Posted: Mon Apr 29, 2013 6:11 am
Have a look at my Simple Pendulum stack here:

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

### Re: Simulate a pendulum

Posted: Mon Apr 29, 2013 9:50 am
RogGuay wrote:Have a look at my Simple Pendulum stack here:

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

Great! Thanks

### Re: Simulate a pendulum

Posted: Mon Apr 29, 2013 1:35 pm
Nice pendulum!

### Re: Simulate a pendulum

Posted: Mon Apr 29, 2013 4:47 pm
Roger, that stack is beautiful. Thanks for the great example.

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

### Re: Simulate a pendulum

Posted: Mon Apr 29, 2013 6:36 pm
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