GSDEMO: Continuous Newton's Cradle

143 views
Skip to first unread message

Evan Biegel

unread,
May 3, 2016, 6:25:24 PM5/3/16
to Glowscript Users
The attached link consists of a simulation of Newton's Cradle. However, this version is purposefully continuous, in order for the user to view the motion of the spheres in the distinct x- and y-directions. The purpose is to illustrate to first year Physics students exactly how 2D motion operates; I learned that a solid foundation, in terms of intuition, goes a long way as a Physics student.

Please let me know if there are ways in which the program can be improved, and I hope it runs well.

Evan Biegel



Bruce Sherwood

unread,
May 3, 2016, 6:31:11 PM5/3/16
to Glowscript Users
A minor comment: There is no need to say rate(275,wait) in a VPython program. Preprocessing of a VPython program inserts the keyword "wait", which is required in GlowScript programs written in JavaScript or RapydScript.

Jim D.

unread,
May 4, 2016, 11:17:05 AM5/4/16
to Glowscript Users
I think it may be in the documentation that way; as I adapted programs and tutorials to Glowscript this year, I recall seeing the requirement for the (nnn,wait) in the rate statement somewhere.

Evan, you can also drop the "from visual import *" now.

Bruce Sherwood

unread,
May 4, 2016, 11:36:23 AM5/4/16
to Glowscript Users
I just did a search of the VPython documentation and found (and fixed) two obscure places where the keyword "wait" was mentioned. If you see one I've missed, please let me know.

I'll explain that in JavaScript, the browser programming language to which a VPython program is compiled (using the RapydScript-to-JavaScript compiler plus some preprocessing), if you write an infinite loop that is a common structure in VPython programs, the browser locks up -- no updates to the web page are possible while the loop is running. GlowScript uses the Streamline library to restructure such "synchronous" programs to make them "asynchronous", by moving the loop contents to a separate function that is then called repeatedly. A loop containing rate(200) is transformed into a function that is driven by a timer 200 times per second.

Streamline needs to be told that code needs to be restructured before being executed, and the hint is the keyword "wait". As a convenience to those writing GlowScript VPython programs, the preprocessing that is part of compiling a VPython program looks for statements such as rate and sleep and scene.waitfor and inserts the keyword "wait" if it is not present.

Note that this means that one should not name a variable "wait", which will confuse matters.
Reply all
Reply to author
Forward
0 new messages