如何为弹跳球创建碰撞检测? [英] How do I create collision detections for my bouncing balls?

查看:65
本文介绍了如何为弹跳球创建碰撞检测?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经编码了一个动画(用python编写),用于三个沙滩球在屏幕周围弹跳。我现在希望它们全部碰撞并能够相互反弹。

I have coded an animation (in python) for three beach balls to bounce around a screen. I now wish to have them all collide and be able to bounce off each other. I would really appreciate any help that can be offered.

import pygame
import random
import sys
class Ball:


    def __init__(self,X,Y):

        self.velocity = [1,1]
        self.ball_image = pygame.image.load ('Beachball.jpg'). convert()
        self.ball_boundary = self.ball_image.get_rect (center=(X,Y))
        self.sound = pygame.mixer.Sound ('Thump.wav')
        self.rect = self.ball_image.get_rect (center=(X,Y))

if __name__ =='__main__':

    width = 800
    height = 600
    background_colour = 0,0,0
    pygame.init()
    window = pygame.display.set_mode((width, height))
    pygame.display.set_caption("Bouncing Ball animation")
    num_balls = 3
    ball_list = []
    for number in range(num_balls):
        ball_list.append( Ball(random.randint(10, (width - 10)),random.randint(10, (height - 10))) )
    while True:
        for event in pygame.event.get():
                print event 
                if event.type == pygame.QUIT:
                        sys.exit(0)
        window.fill (background_colour)

        for ball in ball_list:
                if ball.ball_boundary.left < 0 or ball.ball_boundary.right > width:
                        ball.sound.play()
                        ball.velocity[0] = -1 * ball.velocity[0]
                if ball.ball_boundary.top < 0 or ball.ball_boundary.bottom > height:
                        ball.sound.play()
                        ball.velocity[1] = -1 * ball.velocity[1]

                ball.ball_boundary = ball.ball_boundary.move (ball.velocity)
                window.blit (ball.ball_image, ball.ball_boundary)
        pygame.display.flip()


推荐答案

对任意形状的碰撞检测通常非常棘手,因为您必须确定是否有任何像素碰撞。

Collision detection for arbitrary shapes is usually quite tricky since you have to figure out if any pixel collides.

使用圆圈实际上更容易。如果您有两个半径分别为r1和r2的圆,则如果中心之间的距离小于r1 + r2,则会发生碰撞。

This is actually easier with circles. If you have two circles of radius r1 and r2, a collision has occurred if the distance between the centers is less than r1+r2.

两个中心之间的距离(可以将x1,y1)和(x2,y2)进行计算并比较为:

The distance between the two centers (x1,y1) and (x2,y2) can be calculated and compared as:

d = sqrt((y2-y1) * (y2-y1) + (x2-x1) * (x2-x1));
if (d < r1 + r2) { ... bang ... }

或者,就像jfclavette指出的那样,平方根很昂贵,因此使用简单的操作进行计算可能会更好:

Or, as jfclavette points out, square roots are expensive so it may be better to calculate using just simple operations:

dsqrd = (y2-y1) * (y2-y1) + (x2-x1) * (x2-x1);
if (dsqrd < (r1+r2)*(r1+r2)) { ... bang ... }

计算新的运动向量(给定对象的(x,y)随时间变化的速率)是一个棘手的问题,因为您需要考虑当前的运动向量和

The tricky bit comes in calculating the new movement vectors (the rate at which (x,y) changes over time for a given object) since you need to take into account the current movement vectors and the point of contact.

我认为,首先,您应该反转运动矢量以测试碰撞检测是否首先起作用。

I think as a first cut, you should just reverse the movement vectors to test if the collision detection works first.

然后再问一个问题-最好保持个别问题的具体性,以便可以针对性地回答。

Then ask another question - it's better to keep individual questions specific so answers can be targeted.

这篇关于如何为弹跳球创建碰撞检测?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆