2014-12-04 110 views
0

問題是什麼多個球與碰撞檢測

我具有由1 模擬球以1:1的框我必須給球的隨機速度,它需要一個隨機角度但是當我將他們的球直接繪製並且在0.25處有一個漸近線時。 我雙重cheked我的數學,但我找不到問題?

我的代碼

import numpy 
    import matplotlib.pyplot as plt 
    import math 
    import random 



def rand_tuples(aantalballen): # here i make a function that randomly makes a list with lists in # 

    list_balls = []    # the list, every index is a ball with x_pos , y_pos , speed , rotation # 

    for i in range(aantalballen): 

     list_balls.append([0.25, 0.75,0.1* random.random(), 2* math.pi*random.random()]) 


    return list_balls 


def ball_step(list_balls, dt): # this function calculates the steps the balls takes # 


    x = list_balls[0] 
    y = list_balls[1] 
    vx = math.cos(list_balls[3]) * list_balls[2] # speed in the x direction # 
    vy = math.sin(list_balls[3]) * list_balls[2] # speed in the y direction # 
    dt = dt 

    x_pos = x + (vx * dt)   # calculates the actual positions # 
    y_pos = y + (vy * dt) 

    if x_pos <= 0 or x_pos >= 1:  # collision detection # 
     vx = -vx 


    if y_pos <= 0 or y_pos >= 1: 
     vy = -vy 



    return x_pos, y_pos, vx , vy  # returns the new positions but the same speed so that they can be used again# 


def move_ball(ballen, tijd, dt): # takes a list of balls, time they move and the time steps they take# 

    positions_balls = { }  # hold my position in {0:{'x':[positions}, 'y':{...}}} 

    time_1 = 0 

    for i in range(len(ballen)) : 

     positions_balls[i] = None # make a key with empty value # 
     time_1 = 0 

     cordinates = {'x':[], 'y':[]}  # make the dictionary where my values go into # 

     while time_1 < tijd:  

      bal = ball_step(ballen[i] , dt) # call bal step to calculate my x and y position # 
      ballen[i] = bal 
      cordinates['x'].append(bal[0])  
      cordinates['y'].append(bal[1]) 
      time_1 += dt 



      if int(time_1) == tijd: 
       positions_balls[i] = cordinates # finally add the dictionary to my main dictionary # 

    print positions_balls 
    return positions_balls 






dic = move_ball(rand_tuples(30), 3, 0.01) 

plt.plot(dic[0]['x'], dic[0]['y']) 
plt.show() 

我沒有足夠的聲譽發佈的情節:(

+0

'list_balls [n]'是一個元組,而不是一個單獨的值 – 101 2014-12-04 21:21:55

+0

我是編程新手我對此有何評論? – 2014-12-04 21:41:55

+0

i將其更改爲列表,仍然是同樣的問題 – 2014-12-04 21:44:18

回答

0

ball_step的圖片()發生在(我認爲,意見是你的朋友)X, y,speed,rotation。它輸出x,y,速度x,速度y,分配回原來的列表

+0

我會評論我的代碼謝謝你看它! – 2014-12-04 21:27:26