Optimizing gravitation calculation for particles in a zero gravity 2d space

前端 未结 5 935
北荒
北荒 2021-01-13 07:28

I\'ve have created a small visualisation of particles in python. I\'m caclulation the movement of particels in a 2D space with zero gravity. As each particle attracts all o

5条回答
  •  囚心锁ツ
    2021-01-13 08:00

    (This may should go in a comment but I don't have the needed reputation to do that)

    I don't see how you do the time stepping. You have

    P.speedx -= acceleration * xc
    P.speedy -= acceleration * yc
    

    but to get the new speed at time t+delta_t you would do

    P.speedx -= acceleration * xc * delta_t
    P.speedy -= acceleration * yc * delta_t
    

    and then update the position like so:

    P.x = P.x + P.speedx * delta_t
    P.y = P.y + P.speedy * delta_t
    

    Then to your speed concern. Maybe it would be better to store the particle information not in a class but in numpy arrays? But I don't think you can avoid loops.

    Also, have you looked at wikipedia, there it describes some methods to speed up the calculation.

    (edited due to Mike's comment)

提交回复
热议问题