球的碰撞代码不起作用,球粘在一起

2024-06-16 14:13:29 发布

您现在位置:Python中文网/ 问答频道 /正文

所以我试图创建一个代码,正确地显示两个不同质量的球的弹性碰撞。如果我给球相等的质量,代码就可以工作,但对不同的质量不起作用。我试过摆弄一大堆东西,但似乎什么都没用。我想得到一些帮助

这是我试图实现的公式: https://wikimedia.org/api/rest_v1/media/math/render/svg/14d5feb68844edae9e31c9cb4a2197ee922e409c

global w 
global h
global lst

w = 500
h = 500 
lst = [] 

def setup():
    size(w,h)
    global canvas
    canvas = createGraphics(1000,1000)
    canvas.beginDraw()
    canvas.background(255)
    canvas.endDraw()
    global ball1
    global ball2 
    ball1 = Ball(100,250,1,-.01,10,25)
    ball2 = Ball(300,250,-1,0,20,50)
    lst.append(ball1)
    lst.append(ball2)

def draw():
    background(255)
    textSize(32)
    fill(0, 102, 153);

    for y in range(0,len(last)):
        lst[y].show()
        lst[y].move()

        for z in range(0,y):
            lst[y].collide(lst[z])
        for z in range(y+1, len(last)):
            lst[y].collide(lst[z])
    





class Ball(object):
    def __init__(self,x,y,dx,dy,m,r):
        self.x = x
        self.y = y
        self.dx = dx
        self.dy = dy
        self.m = m
        self.r = r
        self.ball_last_hit = None
    
    
def show(self):
    fill(0)
    ellipse(self.x,self.y,self.r*2,self.r*2)
    
def move(self):
    self.x += self.dx
    self.y += self.dy
    #Makes balls unable to go off of screen
    if self.x >= ( w - self.r):
        self.dx = self.dx * -1
    if self.x <=  self.r:
        self.dx = self.dx * -1
    if self.y >= ( h - self.r):
        self.dy = self.dy * -1
    if self.y <=  self.r:
        self.dy = self.dy * -1
      
def collide(self, other):
    distanceSquared = (self.x - other.x)**2 + (self.y - other.y)**2
    
    initialPositionSelf = PVector(self.x,self.y)
    initialPositionOther = PVector(other.x,other.y)
    xDiff1 = PVector.sub(initialPositionSelf, initialPositionOther)
    xDiff2 = PVector.sub(initialPositionOther, initialPositionSelf)
    
    initialVelocitySelf = PVector(self.dx,self.dy)
    initialVelocityOther = PVector(other.dx,other.dy)
    vDiff1 = PVector.sub(initialVelocitySelf, initialVelocityOther)
    vDiff2 = PVector.sub(initialVelocityOther, initialVelocitySelf)
    
    xDiffSq1 = xDiff1.magSq()
    xDiffSq2 = xDiff2.magSq()
    
    mTot = self.m + other.m
    
    c1 = (2*other.m)/mTot
    c2 = (2*self.m)/mTot
    
    Dot1 = vDiff1.dot(xDiff1)
    Dot2 = vDiff2.dot(xDiff2)
    
    bigConstant1 = (c1*Dot1)/xDiffSq1
    bigConstant2 = (c2*Dot2)/xDiffSq2
    
    V1F = PVector.sub(initialVelocitySelf, xDiff1.mult(bigConstant1))
    V2F = PVector.sub(initialVelocityOther, xDiff2.mult(bigConstant2))
    
    if (self.r + other.r)**2 >= distanceSquared:
        self.dx = V1F.x
        self.dy = V1F.y
        
        other.dx = V2F.x
        other.dy = V2F.y
        
        
        
        
        
    

Tags: selfifdef质量globalcanvasotherlst
1条回答
网友
1楼 · 发布于 2024-06-16 14:13:29

我想您需要计算圆心之间的距离,但是您需要计算圆边界框左上角之间的距离。尝试将self.xself.y更改为self.x+self.rself.y+self.r(对于other也一样)。用于碰撞计算

相关问题 更多 >