forked from BruceSherwood/vpython-wx
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
1 changed file
with
72 additions
and
66 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,94 +1,100 @@ | ||
from visual import * | ||
|
||
print(""" | ||
#!/usr/bin/env python | ||
#coding:utf-8 | ||
""" Drape Demo | ||
Click to place spheres under falling string. | ||
Right button drag or Ctrl-drag to rotate view. | ||
Middle button drag or Alt-drag to zoom in or out. | ||
On a two-button mouse, middle is left + right. | ||
""") | ||
""" | ||
from __future__ import print_function, division | ||
#from visual import * | ||
import visual as vp | ||
|
||
print(__doc__) | ||
|
||
# David Scherer | ||
|
||
scene.title = "Drape" | ||
restlength = 0.02 | ||
m = 0.010 * restlength | ||
g = 9.8 | ||
dt = 0.002 | ||
k = 3 | ||
damp = (1-0)**dt | ||
nspheres = 3 | ||
floor = 0 | ||
vp.scene.title = "Drape" | ||
RESTLENGTH = 0.02 | ||
M = 0.010 * RESTLENGTH | ||
G = 9.8 | ||
DT = 0.002 | ||
K = 3 | ||
DAMP = (1-0)**DT | ||
NSPHERES = 3 | ||
FLOOR = 0 | ||
|
||
# Create the stringy thing: | ||
band = curve( x = arange(-1,1,restlength), | ||
y = 1, | ||
radius = 0.02 | ||
) | ||
BAND = vp.curve( | ||
x=vp.arange(-1, 1, RESTLENGTH), | ||
y=1, | ||
radius=0.02 | ||
) | ||
|
||
band.p = band.pos * 0 | ||
BAND.p = BAND.pos * 0 | ||
|
||
scene.range = 1.5 | ||
scene.autoscale = 0 | ||
vp.scene.range = 1.5 | ||
vp.scene.autoscale = 0 | ||
|
||
# Let the user position obstacles: | ||
spheres = [] | ||
for i in range(nspheres): | ||
s = sphere( pos = scene.mouse.getclick().pos, #(i*0.6 - 0.7,0.5 + i*0.1,0), | ||
radius = 0.25, | ||
color = (abs(sin(i)),cos(i)**2,(i%10)/10.0) ) | ||
spheres.append( s ) | ||
SPHERES = [] | ||
for i in range(NSPHERES): | ||
S = vp.sphere(pos=vp.scene.mouse.getclick().pos, #(i*0.6 - 0.7, 0.5 + i*0.1, 0), | ||
radius=0.25, | ||
color=(abs(vp.sin(i)), vp.cos(i)**2, (i%10)/10.0)) | ||
SPHERES.append(S) | ||
|
||
while True: | ||
rate(1.0 / dt) | ||
vp.rate(1.0 / DT) | ||
|
||
if scene.mouse.clicked: | ||
i = len(spheres) | ||
s = sphere( pos = scene.mouse.getclick().pos, | ||
radius = 0.25, | ||
color = (abs(sin(i)),cos(i)**2,(i%10)/10.0) ) | ||
spheres.append( s ) | ||
if vp.scene.mouse.clicked: | ||
i = len(SPHERES) | ||
S = vp.sphere(pos=vp.scene.mouse.getclick().pos, | ||
radius=0.25, | ||
color=(abs(vp.sin(i)), vp.cos(i)**2, (i%10)/10.0)) | ||
SPHERES.append(S) | ||
|
||
if floor: | ||
below = less(band.pos[:,1],-1) | ||
band.p[:,1] = where( below, 0, band.p[:,1] ) | ||
band.pos[:,1] = where( below, -1, band.pos[:,1] ) | ||
if FLOOR: | ||
BELOW = vp.less(BAND.pos[:, 1], -1) | ||
BAND.p[:, 1] = vp.where(BELOW, 0, BAND.p[:, 1]) | ||
BAND.pos[:, 1] = vp.where(BELOW, -1, BAND.pos[:, 1]) | ||
|
||
# need a more physical way to make 'damped springs' than this! | ||
band.p = band.p * damp | ||
# need a more physical way to make 'damped springs' than this! | ||
BAND.p = BAND.p * DAMP | ||
|
||
#band.p[0] = 0 # nail down left endpoint | ||
#band.p[-1] = 0 # nail down right endpoint | ||
#band.p[0] = 0 # nail down left endpoint | ||
#band.p[-1] = 0 # nail down right endpoint | ||
|
||
band.pos = band.pos + band.p/m*dt | ||
BAND.pos = BAND.pos + BAND.p/M*DT | ||
|
||
#gravity | ||
band.p[:,1] = band.p[:,1] - m * g * dt | ||
#gravity | ||
BAND.p[:, 1] = BAND.p[:, 1] - M * G * DT | ||
|
||
# force[n] is the force on point n from point n+1 (to the right): | ||
length = (band.pos[1:] - band.pos[:-1]) | ||
dist = sqrt(sum(length*length,-1)) | ||
force = k * ( dist - restlength ) | ||
force = length/dist[:,newaxis] * force[:,newaxis] | ||
# force[n] is the force on point n from point n+1 (to the right): | ||
LENGTH = (BAND.pos[1:] - BAND.pos[:-1]) | ||
DIST = vp.sqrt(vp.sum(LENGTH*LENGTH, -1)) | ||
FORCE = K * (DIST - RESTLENGTH) | ||
FORCE = LENGTH/DIST[:, vp.newaxis] * FORCE[:, vp.newaxis] | ||
|
||
band.p[:-1] = band.p[:-1] + force*dt | ||
band.p[1:] = band.p[1:] - force*dt | ||
BAND.p[:-1] = BAND.p[:-1] + FORCE*DT | ||
BAND.p[1:] = BAND.p[1:] - FORCE*DT | ||
|
||
# color based on "stretch": blue -> white -> red | ||
c = clip( dist/restlength * 0.5, 0, 2 ) | ||
# color based on "stretch": blue -> white -> red | ||
C = vp.clip(DIST/RESTLENGTH * 0.5, 0, 2) | ||
|
||
# blue (compressed) -> white (relaxed) -> red (tension) | ||
band.red[1:] = where( less(c,1), c, 1 ) | ||
band.green[1:] = where( less(c,1), c, 2-c ) | ||
band.blue[1:] = where( less(c,1), 1, 2-c ) | ||
# blue (compressed) -> white (relaxed) -> red (tension) | ||
BAND.red[1:] = vp.where(vp.less(C, 1), C, 1) | ||
BAND.green[1:] = vp.where(vp.less(C, 1), C, 2-C) | ||
BAND.blue[1:] = vp.where(vp.less(C, 1), 1, 2-C) | ||
|
||
for s in spheres: | ||
dist = mag( band.pos - s.pos )[:,newaxis] | ||
inside = less( dist, s.radius ) | ||
if sometrue(inside): | ||
R = ( band.pos - s.pos ) / dist | ||
surface = s.pos + (s.radius)*R | ||
for S in SPHERES: | ||
DIST = vp.mag(BAND.pos - S.pos)[:, vp.newaxis] | ||
inside = vp.less(DIST, S.radius) | ||
if vp.sometrue(inside): | ||
R = (BAND.pos - S.pos) / DIST | ||
surface = S.pos + (S.radius)*R | ||
|
||
band.pos = surface*inside + band.pos*(1-inside) | ||
BAND.pos = surface*inside + BAND.pos*(1-inside) | ||
|
||
pdotR = sum(asarray(band.p)*asarray(R),-1) | ||
band.p = band.p - R*pdotR[:,newaxis]*inside | ||
pdotR = vp.sum(vp.asarray(BAND.p)*vp.asarray(R), -1) | ||
BAND.p = BAND.p - R*pdotR[:, vp.newaxis]*inside |