Addendum: Here is the complete program in this easy WebGL environment.
It's a numerical integration of two blocks (m1 and m2, each of mass m)
connected by a spring (or rubber band, represented by a long box); the
x component of momentum of the right-hand block is p.

Hopefully much of the program is self-explanatory, but maybe not the
rate(100) statement, which (taken from VPython) means "do no more than
100 iterations per second in this loop", to control the rate at which
the animation proceeds. In VPython this statement is optional, because
a rendering thread interrupts about 30 times per second to draw (using
OpenGL) the objects with their current attributes. I don't know how to
do that in JavaScript, so here the rate statement is obligatory,
because it not only limits the loop iteration rate but also
periodically drives the renderer.

d = 1
xi = 2

m1 = box({pos:vector(-xi,0,0), size:[d,d,d]})
m2 = box({pos:vector( xi,0,0), size:[d,d,d]})
L = (m2.pos.x-d/2) - (m1.pos.x+d/2)
w = 0.05*d
V = L*w*w
spring = box({pos:vector(0,0,0), size:[L,w,w]})

L0 = .9*L

ks = 10
m = 1
p = 0
t = 0
dt = .01
while (t < 15) {
        rate(100)
        F = -ks*(m2.pos.x-L0/2)
        p += F*dt
        m2.pos.x += (p/m)*dt
        m1.pos.x -= (p/m)*dt
        L = (m2.pos.x-d/2) - (m1.pos.x+d/2)
        w = sqrt(V/L)
        spring.size = [L,w,w]
        t += dt
}

On Wed, May 25, 2011 at 3:10 PM, Bruce Sherwood
<[email protected]> wrote:
> Here is an attempt to make it very easy to write WebGL programs:
>
> http://dl.dropbox.com/u/18859068/EasyWebGL20110525.zip
>
> Here is a summary found in the file oscillator.htm:
>
> // This is an attempt to emulate in JavaScript/WebGL
> // the ease of use found in Python/VPython/OpenGL (vpython.org).
> // In VPython, one writes simple programs that generate
> // navigable real-time 3D animations as a side-effect of
> // computations.
>
> // The following example uses vector operations only
> // sparingly compared to VPython, due to the absence of
> // operator overloading in JavaScript (e.g. vector1+vector2).
> // The example is intended as a simple proof of concept of
> // hiding all the extremely technical WebGL stuff, to make
> // it feasible for ordinary mortals to do 3D in a browser.
> // It is based on the well-known spinning box WebGL demo.
>
> // There's only one object (box) whose size is modifiable.
> // The color of everything is cyan, lighting is primitive.
> // There's some kind of timing glitch that is addressed by
> // the kludge of an alert box (in visual.js). You need to
> // acknowledge the alert, then on the next appearance you
> // need to check the box to show the alert no more. I would
> // appreciate advice on this problem: rendering is done but
> // doesn't appear until the end of the program, despite use
> // of gl.flush() and gl.finish().
>
> Bruce Sherwood
>

============================================================
FRIAM Applied Complexity Group listserv
Meets Fridays 9a-11:30 at cafe at St. John's College
lectures, archives, unsubscribe, maps at http://www.friam.org

Reply via email to