我试图在pygame中编写一个粒子模拟程序,但是在编写粒子之间的碰撞时遇到了困难。所有的碰撞都是弹性的,所以动能应该守恒。然而,我遇到了两个主要问题:粒子不断加速直到失去控制
粒子聚集在一起停止运动
我真的很感激任何有助于解决这些问题的见解。在
我用这个文件(http://www.vobarian.com/collisions/2dcollisions2.pdf)来帮助计算碰撞后粒子的新速度。所有粒子的质量都是一样的,所以我在计算中忽略了它们的质量。
以下是我目前为止的代码:import pygame
pygame.init()
import random
import numpy
HEIGHT = 500
WIDTH = 500
NUM_BALLS = 2
#setup
win = pygame.display.set_mode((WIDTH,HEIGHT))
pygame.display.set_caption("Simulation")
class Particle(object):
def __init__(self, x, y , radius):
self.x = x
self.y = y
self.radius = radius
self.xvel = random.randint(1,5)
self.yvel = random.randint(1,5)
def Draw_circle(self):
pygame.draw.circle(win, (255,0,0), (self.x, self.y), self.radius