0

Here's my code:

from vpython import *
from math import *
import numpy as np
from __future__ import division

stick = cylinder()
stick.pos = vec(0,5,-15)
stick.axis = vec(0,1,0)
stick.length = 30
stick.radius = 0.75
stick.color = vec(.5,.5,.8)

scene.forward = vec(-1,-1,-2)
scene.range = 15

spheres = []
for i in range (0, 15):
    s = sphere()
    s.radius = 1.5
    s.theta_0 = -0.5
    s.period = 75/(50+i)
    s.color = color.hsv_to_rgb(vec(i/15, 0.75,0.9))
    s.wire = cylinder()
    s.wire.color = s.color
    s.wire.pos = stick.pos + vec(0,0,i+0.5)*stick.length/15
    s.wire.axis = vec(0,-1,0)
    s.wire.length = 2*np.pi*9.81*s.period*s.period
    s.wire.radius = 0.1
    spheres.append(s)

dt = 0.2
t = 0
while True:
    rate(1/dt)
    for i in range(0, len(spheres)):
        s = spheres[i]
        theta = s.theta_0 * cos( 2*pi*t/s.period )
        s.wire.axis = vec(np.sin(theta),-np.cos(theta),0)
        s.pos = s.wire.pos + s.wire.axis*s.wire.length 
        t = t + dt 

I want to make a dancing pendulum. However, the wire which should connect the spheres to the stick aren't the correct length. How do I make the wires the correct length?

Codor
  • 17,447
  • 9
  • 29
  • 56
bwest121
  • 1
  • 1

1 Answers1

0

You don't need from math import * (that's contained in from vpython import *), and in the Jupyter environment you can choose a VPython notebook that executes "from future import division, print_function" (in any case, such a statement must be the first statement in the file, so your program couldn't have run as is). No reason to import numpy and use it for pi and sin and cos. In fact, Python's own sin and cos functions are faster than those of numpy if the argument is a single number.

A more "Pythonic" way of handling the loop over spheres is to write it like this:

for s in spheres: theta = .....

I dont' know what your intent is, but I suggest you temporarily make the spheres invisible (visible=False), comment out the rate statement, and insert scene.pause() in the loop over spheres in order to see what's going on. You'll see that the wires are initially drawn downwards from the spheres and then apparently collapsed to very short lengths.

I'll advertise that a better place to pose VPython questions is in the VPython forum at

https://groups.google.com/forum/?fromgroups&hl=en#!forum/vpython-users

user1114907
  • 972
  • 8
  • 15