#copyright (c) Aaron Titus, 2004 #licensed under the gpl license #http://linus.highpoint.edu/~atitus/mandi/vpython from visual import * from __future__ import division print "Simulation of a rotating, translating ring." print "" print "Click to begin, click to stop, click to go to the next sequence." #Note that sequences are defined in the animate() function #0 do not show planet #1 show planet showPlanet=0 #0 do not show points #1 show points showPoints=0 #0 do not show ring #1 show ring showRing=1 R=1 #radius scene.autoscale=0 scene.range=3.2*R scene.height=400 scene.width=800 #define how many points around the ring by specifiying their angular separation dtheta = 45/180*pi theta0=0 #colors pointColor=color.red rotArrowColor=color.yellow transArrowColor=color.blue vArrowColor=color.green #lists of points and vectors points=[] vRotVectors=[] vTransVectors=[] vVectors=[] rTrans0=vector(-2,0,0) #initial position of the planet rTrans=rTrans0 vTrans0=vector(2,0,0) #initial velocity of the planet vTrans=vTrans0 omega0=1 #initial angular velocity omega=omega0 omegaHat=omega/abs(omega) t=0 dt=0.002 arrowScale=0.5 planet=sphere(radius=R, color=pointColor, pos=rTrans) planet.visible=showPlanet circum=ring(radius=R, color=pointColor, axis=(0,0,1), pos=rTrans,thickness=0.01*R) circum.visible=showRing #create points and arrows for theta in arange(theta0,2*pi,dtheta): pointPos=rTrans+vector(R*cos(theta),R*sin(theta),0) thisPoint=sphere(radius=0.05*R, pos=pointPos, color=pointColor) points.append(thisPoint) vRotVectors.append(arrow(color=rotArrowColor,pos=pointPos,axis=(0,0,0))) vTransVectors.append(arrow(color=transArrowColor,pos=pointPos,axis=(0,0,0))) vVectors.append(arrow(color=vArrowColor,pos=pointPos,axis=(0,0,0))) if showPoints==0: thisPoint.visible=0 def animate(seq): global rTrans, vTrans, omega #instructions sequence #0 custom #1 show translation, no rotation #2 show rotation, no translation #3 show translation and rotation #4 show resultant for all points, translation and rotation #seq=4 #0 no vectors #1 1 rot vector only #2 1 trans vector only #3 1 of each vector and resultant #4 all rot vectors only #5 all trans vectors only #6 all vectors #7 1 resultant #8 all resultants show=0 #0 do not show tip-to-tail #1 show tip-to-tail showTipToTail=1 if show==5 or show==1: showTipToTail=0 #otherwise, it looks funny #set variables for instructional sequence if seq==1: vTrans=vTrans0 omega=0 show=5 showTipToTail=0 if seq==2: vTrans=vector(0,0,0) omega=omega0 show=4 if seq==3: vTrans=vTrans0 omega=omega0 show=3 if seq==4: vTrans=vTrans0 omega=omega0 show=8 while 1: rate(100) dphi=omega*dt drTrans=vTrans*dt for i in range(len(points)): point=points[i] #find position relative to center rRot=point.pos-rTrans #rotate ring rRot=rotate(rRot,angle=dphi) #find new positive relative to origin point.pos=rTrans+drTrans+rRot #determine arrows vRotVectors[i].pos=point.pos vRotVectors[i].axis=rotate(rRot,angle=pi/2*omegaHat)*mag(rRot)*R*abs(omega)*arrowScale vTransVectors[i].pos=point.pos vTransVectors[i].axis=vTrans*arrowScale vVectors[i].pos=point.pos vVectors[i].axis=vTransVectors[i].axis+vRotVectors[i].axis #make arrows invisible by default vRotVectors[i].visible=0 vTransVectors[i].visible=0 vVectors[i].visible=0 if showTipToTail==1: vTransVectors[i].pos=vRotVectors[i].pos+vRotVectors[i].axis if i==0: if show==1 or show==3 or show==4 or show==6: vRotVectors[i].visible=1 if show==2 or show==3 or show==5 or show==6: vTransVectors[i].visible=1 if show==3 or show==6 or show==7 or show==8: vVectors[i].visible=1 else: if show==4 or show == 6: vRotVectors[i].visible=1 if show==5 or show ==6: vTransVectors[i].visible=1 if show==6 or show==8: vVectors[i].visible=1 rTrans=rTrans+drTrans planet.pos=rTrans circum.pos=rTrans if scene.mouse.events: m=scene.mouse.getevent() if m.click: break def reset(): global rTrans rTrans=rTrans0 planet.pos=rTrans circum.pos=rTrans for i in range(len(points)): theta=i*dtheta points[i].pos=rTrans+vector(R*cos(theta),R*sin(theta),0) iSeq=0 #sequence number while 1: if iSeq==4: iSeq=1 else: iSeq=iSeq+1 scene.mouse.getclick() reset() #resets positions of everything animate(iSeq)