下面的VPython代码的目标是使用14个球模拟一个紧身衣来代表紧身裤的破碎部分。但是我的代码遇到了一些问题。例如,
R[i] = ball[i].pos - ball[i+1].pos
加注
'int'对象没有属性'pos'
上述错误的含义是什么?
这是我的计划:
from __future__ import print_function, division
from visual import *
ceiling = box(pos=(0,0,0), size=(0.15, 0.015, 0.15), color=color.green)
n = 14 #number of "coils" (ball objects)
m = 0.015 #mass of each ball; the mass of the slinky is therefore m*n
L = 0.1 #length of the entire slinky
d = L/n #a starting distance that is equal between each
k = 200 #spring constant of the bonds between the coils
t = 0
deltat = 0.002
g = -9.8
a = vector(0,g,0)
ball=[28]
F=[]
R=[]
#make 28 balls, each a distance "d" away from each other
for i in range(n):
ball = ball+[sphere(pos=vector(0,-i*d,0), radius = 0.005, color=color.white)]
#ball[i].p = vector(0,0,0)
for i in range(n-1):
F.append(vector(0,0,0))
R.append(vector(0,0,0))
#calculate the force on each ball and update its position as it falls
while t < 5:
rate(200)
for i in range (n-1):
R[i]=ball[i].pos-ball[i+1].pos
F[i]=200*(mag(R[i]) - d)*norm(R[i])
#r[i] = ball[i].pos - ball[i+1].pos
#F[i] = 200*((r[i].mag - d)*((r[i])/(r[i].mag)))
for i in range(n-1):
ball[i+1].p = ball[i+1].p + (F[i] - F[i+1] + (0.015*a))*deltat
ball[i+1].pos = ball[i+1].pos + ((ball[i+1].p)/0.015)*deltat
t = deltat + t
答案 0 :(得分:0)
您似乎已为int
对象分配了ball[i]
值。考虑到这一点,错误消息非常清楚:整数对象没有属性.pos
(属于sphere
类。
如果你希望ball是一个球体对象列表,你需要做一些不同的事情。目前,您已初始化ball = [28]
,这是一个带有单个元素的列表,整数值为28。
因此,在i
= 0的情况下,ball[i]
会返回28
,这显然没有球体的任何此类属性。
这可能会这样做:
ball=[] ### initialize ball as an empty list
F=[]
R=[]
#make 28 balls, each a distance "d" away from each other
"""
You said you want only 14 balls, but the comment above says 28...
"""
for i in range(n):
ball.append(sphere(pos=vector(0,-i*d,0), radius = 0.005, color=color.white))