1

我四处阅读以了解此错误的含义。我现在明白它在告诉我什么,但是当我向它传递一个列表时,它实际上没有任何意义。这些列表具有我试图索引它们的正确深度。如果我有更有经验的人告诉我出了什么问题,那真的很有帮助。

import direct.directbase.DirectStart
from direct.showbase import DirectObject
from panda3d.core import TextNode, Vec3, Vec4
from direct.interval.IntervalGlobal import *
from direct.gui.DirectGui import *
from direct.showbase.DirectObject import DirectObject
import sys
import math

global masses
global dt
global models
global xcel
global xcel2
xcel = []
xcel2 = []
models = []
masses = []
positions = []
velocities = []
dt = 0.1

class mass(DirectObject):
    def __init__(self):
        base.setBackgroundColor(0, 0, 0)
        camera.setPos ( 0, -100, 0 )         
        camera.setHpr ( 0, 0, 0 )

    def createMass(self, name, mass, pos, vel):
    self.name = name
    self.mass = mass
    self.pos = pos
    positions.append(pos)
    global positions
    self.vel = vel
    velocities.append(vel)
    global velocities

    def scaleRenderTexture(self, scale, tex):
    self.model = loader.loadModel("models/sphere")
    self.model.reparentTo(render)
    texture = loader.loadTexture("models/" + tex)
    self.model.setTexture(texture, 1)
    self.model.setPos(self.pos[0], self.pos[1], self.pos[2])
    self.model.setScale(scale)
    masses.append(self.mass)
    models.append(self.model)



class sun(mass):
def __init__(self):
        mass.__init__(self)
    self.createMass( 'sun', 1988920, [0, 0, 0], [0, 0, 0])
    self.scaleRenderTexture(1, 'sun.jpg')

class mercury(mass):
def __init__(self):
    mass.__init__(self)
    self.createMass( 'mercury', 0.3302, [3.241862599523176, -5.623459942709265, -0.7569237943021074], [3.246868365706273*0.000001, 2.673977456413460*0.000001, -7.943396297475591*0.00000001])
        self.scaleRenderTexture(0.5, 'mercury.jpg')

class venus(mass):
def __init__(self):
    mass.__init__(self)
        self.createMass('venus', 4.8685, [-1.034106071554218*10, -3.026887560710919, 5.553453628782571*0.1], [9.607057270995346*0.0000001, 9.607057270995346*0.000001, -1.017130175803783*0.0000001])
    self.scaleRenderTexture(1, 'venus.jpg')

class earth(mass):
def __init__(self):
    self.createMass('earth', 5.9736, [-1.156305861637755*10, -9.663398491949379, 3.476874978550299*0.0001], [ 1.862492028315679*0.000001, -2.296083935603351*0.000001, 9.235729792600083*0.00000000001])
    self.scaleRenderTexture(1, 'earth.jpg')

class moon(mass):
def __init__(self):
    mass.__init__(self)
    self.createMass('moon', 0.07349, [-1.159098389232509*10, -9.637310345944113, -2.989940894073912*0.001], [1.798178987142885*0.000001, -2.375339761912065*0.000001, -2.207441445869889*0.000000001])
    self.scaleRenderTexture(0.5, 'moon.jpg')

class mars(mass):
def __init__(self):
    self.createMass('mars', 0.64185, [-2.422905158679938*10, -3.744196041373893, 5.164753385142086*0.1], [ 4.606798644725879*0.0000001, -2.187755639279240*0.000001, -5.715066713235196*0.00000001])
    self.scaleRenderTexture(1, 'mars.jpg')

class jupiter(mass):
def __init__(self):
    self.createMass('jupiter', 1898.6, [4.622321051914394*10,  5.871875615908649*10, -1.278226639349561], [-1.043365341857297*0.000001, 8.709856358914491*0.0000001, 1.972500372665064*0.00000001])
    self.scaleRenderTexture(1, 'jupiter.jpg')

class saturn(mass):
def __init__(self):
    self.createMass('saturn', 568.46, [-1.301371599111853*100, -6.476615242349228*10, 6.307409695374766], [3.772024323255900*0.0000001, -8.663132035152103*0.0000001, 5.618951648515438*0.00000000001])
    self.scaleRenderTexture(1, 'saturn.jpg')

class uranus(mass):
def __init__(self):
    self.createMass('uranus', 86.832, [2.992063464452280*100, 2.476994299596707*10, -3.783759032649831], [-6.199323493736431*0.000000001, 6.475569300476019*0.0000001, 3.210008048765792*0.000000001])
    self.scaleRenderTexture(1, 'uranus.jpg')

class neptune(mass):
def __init__(self):
    self.createMass('neptune', 102.43, [ 3.919464564967463*100, -2.185268039220477*100, -4.530872464442687], [2.601721469279150*0.0000001, 4.785231712545664*0.0000001, -1.579822877152521*0.000000001])
    self.scaleRenderTexture(1, 'neptune.jpg')

class pluto(mass):
def __init__(self):
    self.createMass('pluto', 0.0125, [ 6.480526105894912*10, -4.765510811963443*100, 3.226311175584855*10], [5.496347900202217*0.0000001, -3.260693806903501*0.00000001, -1.558804760367132*0.0000001])
    self.scaleRenderTexture(0.5, 'pluto.jpg')

def calcA(bodies):
s = [0,0,0]
me = [0,0,0]
v = [0,0,0]
e = [0,0,0]
mo = [0,0,0]
ma = [0,0,0]
ju = [0,0,0]
s = [0,0,0]
u = [0,0,0]
n = [0,0,0]
p = [0,0,0]
xcel = [s, me, v, e, mo, ma, ju, s, u, n, p]
xcel2 = xcel
aX = 0
aY = 0
aZ = 0
for i in range(len(masses)):
    xcel[i] = [aX, aY, aZ]
    aX = 0
    aY = 0
    aZ = 0
    for j in range(len(masses)):
    if i!=j:
        a = models[i].getPos()
        b = models[j].getPos()
        rx = b[0]-a[0]
        ry = b[1]-a[1]
        rz = b[2]-a[2]
        r = math.sqrt(rx*rx+ry*ry+rz*rz)
        xRatio = rx/(ry+rz)
        yRatio = ry/(rx+rz)
        zRatio = rz/(rx+ry)
        acceleration = (9.81*float(masses[i]))/(r*r)
        aX = aX + acceleration*xRatio
        aY = aY + acceleration*yRatio
            aZ = aZ + acceleration*zRatio

    return(xcel)

def calcNextPos(models, xcel, velocities, dt):
for i in range(len(models)):
    for j in range(3):
        a = xcel[i[j]]*0.5*dt*dt
        v = velocities[i[j]]*dt
    positions[i[j]] = positions[i[j]] + v + a
updatePos(positions)

def calcNextVel(velocities, xcel, xcel2, dt):
for i in range(len(models)):
    for j in range(3):
        velocities[i[j]] = velocities[i[j]] + 0.5*(xcel[i[j]] + xcel2[i[j]])*dt
xcel = xcel2
return vel


def updatePos(positions):
for i in range(models):
    for j in range(3):  
    models[i].setPos(positions[i[0]], positions[i[1]], positions[i[2]])

bodies = []     
sun = sun()
mercury = mercury() 
venus = venus()
earth = earth() 
moon = moon()
mars = mars()   
jupiter = jupiter()
saturn = saturn()
uranus = uranus()
neptune = neptune()
pluto = pluto() 
xcel = calcA(bodies)

print(xcel)
print(positions)
print(velocities)
def mainLoop(xcel, models, masses, velocities):
calcNextPos(models, xcel, velocities, dt)
xcel2 = calcA(bodies)
calcNextVel(velocities, xcel, xcel2, dt)

while True:
mainLoop(xcel, models, masses, velocities)
run()

这是控制台的图像:

截屏

我已经看了很多,我无法弄清楚。任何帮助,将不胜感激。我使用的api是panda3d。

谢谢

凯恩

4

2 回答 2

3

xcel是一个二维列表。访问第th 子列表j的第 th 元素的正确语法是i

xcel[i][j]   # not xcel[i[j]]!

目前,您正在尝试获取jinteger 的第 th 个元素i,这会导致您遇到错误。velocities和 也是如此positions

于 2012-04-30T08:08:27.763 回答
0

因为我是一个整数

for i in range(len(models)):

这使得 i 的值在 0 和 a 之间循环,小于models

我不确定您的预期目的是什么,但是您无法访问数组元素,就像在接下来的两行带有i[j]. 你也许是说

for i in range(len(models)):
    for j in range(3):
        a = xcel[models[j]]*0.5*dt*dt
        v = velocities[models[j]]*dt

注意i[j]现在models[j]

于 2012-04-30T07:46:46.790 回答