k = 25 # spring constant m = 20 # block mass b = 0.5 # viscosity friction (proportional to velocity) dt = 0.1 # time step #initial conditions v = vector(0, 0, 0.2) x = vector(x0, 0, 0) xr = vector(l_rest, 0, 0) sx0 = vector(-0.8, 0, 0) offx = vector(0, 0.3, 0) vp.box(pos=(0, -0.1, 0), length=2.0, width=0.02, height=0.5) #surface vp.box(pos=(-.82, .15, 0), length=.04, width=0.50, height=0.3) #wall block = vp.cube(pos=x, length=0.2, c='t') spring = vp.helix(sx0, x, r=.06, thickness=.01, texture='metal1') pb = ProgressBar(0, 500, c='r') for i in pb.range(): F = -k * (x - xr) - b * v # Force and friction a = F / m # acceleration v = v + a * dt # velocity x = x + v * dt + 1 / 2 * a * dt**2 # position block.pos(x) # update block position spring.stretch(sx0, x) # stretch helix accordingly trace = vp.point(x + offx, c='r/0.5', r=3) # leave a red trace vp.camera.Azimuth(.1) vp.camera.Elevation(.1) vp.render(trace) # add trace to the list of actors and render
pts2 = vp.points(coords2, r=1, legend='#points = ' + str(len(coords2))) vp.show([a2, pts2], at=1, interactive=True) ######################################################################################## # Draw a bunch of simple objects on separate parts of the rendering window: # split window to best accomodate 9 renderers vp = Plotter(N=9, title='basic shapes') vp.sharecam = False # each object can be moved independently vp.show(at=0, actors=vp.arrow([0, 0, 0], [1, 1, 1]), legend='arrow') vp.show(at=1, actors=vp.line([0, 0, 0], [1, 1, 1]), legend='line') vp.show(at=2, actors=vp.points([[0, 0, 0], [1, 1, 1]]), legend='points') vp.show(at=3, actors=vp.text('Hello!')) vp.show(at=4, actors=vp.sphere()) vp.show(at=5, actors=vp.cube(), legend='cube') vp.show(at=6, actors=vp.ring(), legend='ring') vp.show(at=7, actors=vp.helix(), legend='helix') vp.show(at=8, actors=vp.cylinder(), legend='cylinder', interactive=1) ######################################################################################## # Draw a bunch of objects from various mesh formats. Loading is automatic. vp = Plotter(shape=(3, 3), title='mesh formats') # split window in 3 rows and 3 columns vp.sharecam = False # each object can be moved independently vp.show('data/beethoven.ply', at=0, c=0, axes=0, ruler=1) # dont show axes, add a ruler vp.show('data/cow.g', at=1, c=1, zoom=1.15) # make it 15% bigger vp.show('data/limb.pcd', at=2, c=2) vp.show('data/ring.gmsh', at=3, c=3, wire=1) vp.show('data/images/dog.jpg', at=4) # 2d images can be loaded the same way vp.show('data/shuttle.obj', at=5, c=5) vp.show('data/shapes/man.vtk', at=6, c=6,
Fgrav = vector(0, -M*9.81, 0) gaxis = vector(0, 0, 1) # initial orientation of gyroscope gaxis = norm(gaxis) I = 1/2*M*R**2 # moment of inertia of gyroscope Lrot = I*omega*gaxis # angular momentum cm = gpos + 0.5*Ls*gaxis # center of mass of shaft # ############################################################ the scene vp = Plotter(verbose=0, axes=0, interactive=0) shaft = vp.cylinder([[0,0,0], Ls*gaxis], r=0.03, c='dg') rotor = vp.cylinder([(Ls-0.55)*gaxis, (Ls-0.45)*gaxis], r=R, c='t') bar = vp.cylinder([Ls*gaxis/2-R*vector(0,1,0), Ls*gaxis/2+R*vector(0,1,0)], r=R/6, c='r') gyro = vp.Assembly([shaft, rotor, bar]) # group actors into a single one spring= vp.helix(top, gpos, r=0.06, thickness=0.01, c='gray') vp.box(top, length=0.2, width=0.02, height=0.2, c='gray') vp.box(pos=(0,.5,0), length=2.2, width=3, height=2.2, c='gray', wire=1, alpha=.2) # ############################################################ the physics pb = ProgressBar(0, 5, dt, c='b') for t in pb.range(): Fspring = -ks*norm(gpos-top)*(mag(gpos-top)-Lrest) torque = cross(-1/2*Ls*norm(Lrot), Fspring) # torque about center of mass Lrot += torque*dt precess += (Fgrav+Fspring)*dt # momentum of center of mass cm += (precess/M)*dt gpos = cm - 1/2*Ls*norm(Lrot) # set orientation along gaxis and rotate it around its axis by omega*t degrees gyro.orientation(Lrot, rotation=omega*t*57.3).pos(gpos)
vp.box(pos=(bob_x[0], bob_y[0], 0), length=12, width=12, height=.7, c='k', wire=1) bob = [vp.sphere(pos=(bob_x[0], bob_y[0], 0), r=R / 2, c='gray')] for k in range(1, N + 1): bob.append(vp.cylinder(pos=(bob_x[k], bob_y[k], 0), r=R, height=.3, c=k)) # Create the springs out of N links link = [0] * N for k in range(N): p0 = bob[k].pos() p1 = bob[k + 1].pos() link[k] = vp.helix(p0, p1, thickness=.015, r=R / 3, c='gray') # Create some auxiliary variables x_dot_m = [0] * (N + 1) y_dot_m = [0] * (N + 1) dij = [0] * (N + 1) # array with distances to previous bob dij_m = [0] * (N + 1) for k in range(1, N + 1): dij[k] = mag([bob_x[k] - bob_x[k - 1], bob_y[k] - bob_y[k - 1]]) fctr = (lambda x: (x - 1) / x) Dt *= np.sqrt(1 / g) Dt2 = Dt / 2 # Midpoint time step DiaSq = (2 * R)**2 # Diameter of bob squared