0
值的功能,我有問我使用一些給定的代碼編寫,它計算到擊中目標10米以外所需角度的功能分配。使用代碼編寫計算彈丸運動
這裏是給定的代碼:
from visual import *
from visual.graph import * # For the graphing functions
#Create a graph display window (gdisplay)
win = gdisplay(xtitle="Distance [m]", ytitle="Height [m]")
#And a curve on this display
poscurve = gcurve(gdisplay=win, color=color.cyan)
#Target position (10 meters away)
target_pos = vector(10,0,0)
#Set the starting angle (in degrees)
angle = 45
#Set the magnitude of the starting velocity (in m/s)
v0 = 12.0
#Gravity vector (m/s**2)
g = vector(0, -9.8, 0)
#Create a vector for the projectile's velocity
velocity = v0 * vector(cos(anglepi/180), sin(anglepi/180), 0)
#and the position
position = vector(0,0,0)
dt = 0.01 # Time step
#Start loop. Each time taking a small step in time
while (position.y > 0) or (velocity.y > 0): # Change in position # dx = (dx/dt) * dt dx = velocity * dt
# Change in velocity
# dv = (dv/dt) * dt
dv = g * dt
# Update the position and velocity
position = position + dx
velocity = velocity + dv
# Plot the current position
poscurve.plot(pos=position)
#When loop finishes, velocity.y must be < 0, and position.y < 0
print "Landed at X position: ", position.x print "X distance to target: ", position.x - target_pos.x
會怎樣我現在寫一個函數來計算所需要的價值?我不知道從哪裏開始,任何幫助將不勝感激!
感謝
,因此用戶可能不會寫你的代碼,但回答您制定明確的問題。如果你的問題沒有顯示出足夠的知識,那麼它將在這裏被視爲脫離主題。 – Alfe
看起來你正在嘗試一些數值積分/運動模擬。至少在2D中需要一個循環來進行這個和向量計算。在代碼中執行此操作,然後可以在此處詢問出現的明確問題。請確保向我們展示您的代碼。 – Alfe