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



所以我正在尝试创建一个代码,以正确显示两个不同质量的球的弹性碰撞。如果我给球相同的质量,代码有效,但不适用于不同的质量。我尝试过弄乱很多东西,但似乎没有任何效果。我希望得到一些帮助;

这是我尝试实现的公式: 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






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

最新更新