2014-03-25 43 views
2

我正在做一個項目,涉及到使用SciPy集成器來模擬這個射擊遊戲中的子彈運動。我從來沒有必要在Python中編寫任何程序,而我的微積分有點生疏,但幾個小時後,我也很難確定究竟是什麼原因導致某些錯誤。經過幾個小時的反覆試驗,我已經將它與集成方法中的註釋代碼一起工作(我認爲這是一個歐拉集成,但我確實不是100%肯定的)。不過,我也應該使用SciPy庫中的兩個集成商,這些集成商尚未開展工作。無論在這同一個地方拋出同樣的錯誤信息:SciPy子彈ODE集成

" File "**file extension**", line 17, in eq 
    projX = x[0]; 
    TypeError: 'float' object has no attribute '__getitem__'" 

好像它是期待一個不同的對象類型,但老實說,我不知道這是怎麼回事。我不知道我是否使用了集成商,或者我在設置ODE時犯了錯誤。我真的很難過。任何幫助都將不勝感激。 下面的代碼:

from numpy import * 
from scipy.integrate import ode 
from scipy import * 

# setup wind (it will be replaced with the current value of the wind at every 
# instant in time) 
wd = array([0, 0]) 

# computes the ODE 
def eq(x, t): 

    # setup gravitational acceleration 
    #this is Acceleration for Y 
    GRAVITY = 9.81 

    # get out the positions and velocities 
    projX = x[0]; 
    projY = x[1]; 
    projVX = x[2]; 
    projVY = x[3]; 

    # TODO: setup the acceleration 
    #acceleration is the derivitive of velocity (VX..VY acceleration is GRAVITY) in this case  it's 0 (no wind)? 
    ACCELERATION = 0 #will be effected by wind 


    #TODO ground force 

    # TODO: return appropriate things 
    #needs to return: (x,y Time derivatives and x,y acceleration) 
    #acceleration is derivative of velocity. Velocity derivative of position. 
#is essentially dx/dt 
    return array([x[2], x[3], ACCELERATION, -GRAVITY]) 


# integrates a projectile forward in time using simple Euler integration 
# t: the current time of the system 
# dt: the time to step the system forward 
# x: the current state of the system ([x position, y position, x velocity, y 
#  velocity) 
# returns the new state of the system 
def integrate(t, dt, x): 
    #Euler Method 
    #x[0] = x[0] + x[2]*dt 
    #x[2] = x[2] 
    #x[1] = x[1]+x[3]*dt 
    #x[3] = x[3]-(9.81*dt) 

    #SciPy Dopri5 Integration 

    #vode Integrator 
    method = ode(eq).set_integrator('vode', method = 'bdf') 
    method.set_initial_value(x, t) 
    method.integrate(method.t+dt) 
    return x 
    #write one function that represents one step of the diff equation 

它從projectile.py調用。多數認爲只是圖紙和設置的pygame的,但在那裏這部分是有關的片段是:

 # integrate the projectile forward in time 
    x = dynamics.integrate(fireT, dt, array([projX, projY, projVX, projVY])) 
    projX = x[0] 
    projY = x[1] 
    projVX = x[2] 
    projVY = x[3] 

    # update the time 
    fireT = fireT + dt 

回答

0

的問題是,eq與順序錯誤的參數定義。

f : callable f(t, y, *f_args) 

,而在你的情況交換ty,通過定義它

def eq(x, t): 

那麼你的代碼嘗試:從odedocs,您傳遞的功能應該被定義訪問t[0],募集TypeError

該行簡單地改變

def eq(t, x): 
+0

非常感謝您的回覆。它修正了TypeError,現在遊戲至少是繪製彈丸。然而,拋射物只是坐在那裏,並不像它應該的那樣向前移動。我不確定積分器是不是更新數組x中的值,或者它是否甚至成功調用積分器。我認爲這是後者,因爲在調用「method.integrate(method.t + dt)」之前添加「while method.successful():」會阻止PyGame中的射彈發射。有沒有人有任何想法,爲什麼這可能是? – user3458256

+0

在這裏和那裏添加一些'print'語句來查看值是否更新。 – gg349

+0

感謝您的建議。我實現了一個打印語句,x [0] -x [3]的值在每個時間步都是一樣的。不知道爲什麼它不更新值,但肯定會縮小它的值。感謝你的幫助。 'integration()'中的 – user3458256