模拟两个方块碰撞以计算 PI 时的动画故障

2024-01-09

我在用pygame创建两个方块碰撞的模拟质量比为 100 次方,明确地说,这意味着较大块与较小块的比率可以是100**0, 100**1, 100**2等等。我在必要时添加了注释和文档字符串,以使逻辑易于理解。

import pygame

# from collisions import *
pygame.init()

s1, s2 = 100, 50  # block sides
x1, y1 = 1000, 250  # bigger block coords
x2, y2 = 500, y1 + s1 - s2  # smaller block coords

power = int(input('enter: '))  # mass ratio
v1 = (-0.5)  # initial velocity of block 1

m1, m2 = 100 ** (power - 1), 1  # mass of blocks
v2 = 0  # initial velocity of block 2

# temp_x1 = 0
red = (255, 0, 0)
blue = (0, 0, 255)


def message_to_print(msg, color):
    font = pygame.font.SysFont(None, 40)
    text = font.render(msg, True, color)
    win.blit(text, [10, 10])


def reverse_vel(vel):
    '''
    reversing velocity of block
    '''
    vel *= -1
    return vel


def exchange_vel(v1, m1, v2, m2):
    '''
    this function is calculating the new velocity of the block after collision,
    based on law of conservation of momentum and kinetic energy
    '''
    v1 = ((m1 - m2) / (m1 + m2)) * v1 + ((2 * m2) / (m1 + m2)) * v2

    return v1  # returning new velocity after collision


win = pygame.display.set_mode((1200, 500))
win.fill((255, 255, 255))
pygame.display.set_caption('simulation')

Collisions = 0  # counting number of collisions
run = True
while run:
    # click_sound = pygame.mixer.Sound("rss/click.wav")
    # pygame.time.delay(10)
    # sound_collide, sound_reverse = True, True

    message_to_print('collision ' + str(Collisions), (0, 0, 0))
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False

    # biger block
    x1 += v1  # changing block coordinates according to velocity
    if x1 > s2:  # this prevents block 1 from moving out of window
        t = x1

    if not x2 + s2 < x1 or x1 + s1 < x2:
        '''
        changing velocity after collision,
        storing them in temp variable for each block,
        then assiging new velocity
        '''
        v2_temp = exchange_vel(v2, m2, v1, m1)
        v1_temp = exchange_vel(v1, m1, v2, m2)
        # if sound_collide:
        # click_sound.play()
        # sound = False

        v2, v1 = v2_temp, v1_temp  # assigning new velocities
        Collisions += 1

    # smaller Block
    x2 += v2
    if x2 <= 0:
        '''
        if block 1 touch left wall, its velocity reverses, 
        '''
        v2 = reverse_vel(v2)
        # if sound_reverse:
        # click_sound.play()
        # sound_reverse = False
        Collisions += 1
    pygame.draw.rect(win, blue, (x2, y2, s2, s2))
    pygame.draw.rect(win, red, (t, y1, s1, s1))

    pygame.display.update()
    win.fill((255, 255, 255))

pygame.quit()

问题一:

For the smaller value of power simulation is working fine. But for values of power>=3 animation of block 2(blue block) is getting weird(as shown in image) and I am unable to find the cause and fix it. enter image description here

如图所示,蓝色块始终是这个位置,不断消失和出现。

更新:发生的碰撞次数是该计划的组成部分。无论如何,发生的碰撞数量不应受到影响


我明白你在做什么。当您阻止块 1 移出窗口时,只需添加 else 语句即可实现此目的。

if x1 >= s2:
    t = x1
    t2 = x2
else:
    t2 = 0

和改变

pygame.draw.rect(win, blue, (t2, y2, s2, s2)) # x2-> t2

解释:当你的方块 1 坐标大于 s2 时,它们将正常移动,但如果不是这种情况,只需将较小方块的坐标固定为 0。

推荐: 由于实际上在此期间会发生碰撞,因此要在模拟中显示这一点,您可以执行类似的操作。

    if x1 >= s2 +2:   # this prevents block 1 from moving out of window
    t = x1
    t2 = x2
else:
    t2 = z%2
z+=1

这将创建一个小型碰撞效果。我用过2是任意选择。同时初始化z=0主循环之前

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

模拟两个方块碰撞以计算 PI 时的动画故障 的相关文章

随机推荐