Pygame球碰撞
我正在尝试模拟两个球之间的弹性碰撞。问题是,这些球有时不会遵循适当的行为,有时会“振动”或“粘在”彼此之间,或者当发生碰撞时它们甚至只是彼此之间。我不知道这是否与我使用的方程式有关(应该是标准的,但我真的不知道)。这是我的代码:
import pygame
import random
import math
def checkcirclecollide(x1, y1, r1, x2, y2, r2):
return (x1 - x2)**2 + (y1 - y2)**2 == (r1 + r2)**2
def ballcollision(m1, m2, v1, v2):
v2f = (2*m1*v1+m2*v2-m1*v2)/(m1+m2)
v1f = (m1*v1+m2*v2-m2*v2f)/m1
return v1f, v2f
class Ball:
def __init__(self, x, y, vx, vy, r, m):
self.x = x
self.y = y
self.vx = vx
self.vy = vy
self.r = r
self.m = m
def change_attribute(self, x=None, y=None, vx=None, vy=None, r=None, m=None):
if x!=None: self.x = x
if y!=None: self.y = y
if vx!=None: self.vx = vx
if vy!=None: self.vy = vy
if r!=None: self.r = r
if m!=None: self.m = m
pygame.init()
screen = pygame.display.set_mode((500, 500))
x, y = random.randint(11, 489), random.randint(11, 489)
vx, vy = random.randint(-5, 5), random.randint(-5, 5)
animationTimer = pygame.time.Clock()
balls = []
num_balls = 2
for x in range(num_balls):
balls.append(Ball(random.randint(11, 489), random.randint(11, 489), random.randint(-5, 5), random.randint(-5, 5), random.randint(30, 50), random.randint(1, 4)*5))
while True:
for e in pygame.event.get():
if e.type == pygame.QUIT:
break
for ball in balls:
pygame.draw.circle(screen, (200, 0, 0), (ball.x, ball.y), ball.r)
if ball.x-ball.r<=0 or ball.x+ball.r>=500:
ball.change_attribute(vx = -ball.vx)
if ball.y-ball.r<=0 or ball.y+ball.r>=500:
ball.change_attribute(vy = -ball.vy)
for other in balls:
if other != ball and checkcirclecollide(ball.x, ball.y, ball.r, other.x, other.y, other.r):
new_v1x, new_v2x = ballcollision(ball.m, other.m, ball.vx, other.vx)
new_v1y, new_v2y = ballcollision(ball.m, other.m, ball.vy, other.vy)
ball.change_attribute(vx = new_v1x, vy = new_v1y)
other.change_attribute(vx = new_v2x, vy = new_v2y)
ball.change_attribute(x = ball.x+ball.vx, y = ball.y+ball.vy)
animationTimer.tick(100)
pygame.display.update()
screen.fill((0, 0, 0))
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我对您的脚本进行了一些修改。我所做的修改在脚本本身上进行了解释。由于动能不在我的专业领域,因此我不知道它是否真的有效,也许仍然需要一些修改。但是,至少球不再融合或粘在墙壁上。
另外,我为每个球添加了自定义颜色。它们在测试中要好得多,并查看它们彼此相撞的位置以及如何相撞。
另外,这是我在
andy_balls()
上使用的想法。我的油漆不是很好,但是我们去了:x
在脚本上为diffr
。我使用此值将每个球融合后将每个球彼此移开。I did some modifications to your script. The modifications I made are explained on the script itself. As Kinetic Energy is outside of my field of expertise, I have no idea to say if it's really working, maybe it still needs a few modifications. But at least the balls do not merge or stick to the walls anymore.
Also, I added custom colors for each ball. They are much better for testing and see where and how they collide to each other.
Also, this is the idea I used on

separate_balls()
. I'm not very good with Paint, but here we go:x
on the picture isdiffR
on the script. I use this value to move each ball away from each other once they merge.