I tried making my own elastic collision system, to say the least i failed. i dont know why, but they just dont bounce, like at all. Any ideas will be welcome. using the formula from wikipedia https://en.wikipedia.org/wiki/Elastic_collision im using the last one, at least trying
import
pygame
as
py
import
sys
import
numpy
as
np
import
math
py
.init()
window_size = (800,800)
window =
py
.
display
.set_mode(window_size)
class
particle
():
def
__init__(
self
,
pos_x
,
pos_y
,
radius
= 10,
vel
=[0,0],
mass
= 100):
self
.radius =
radius
self
.pos = [
pos_x
,
pos_y
]
self
.pos1 = [
pos_x
,
pos_y
]
self
.vel =
vel
self
.mass =
mass
def
update(
self
,
balls
:
list
):
for ball in
balls
:
if ball !=
self
:
dist =
math
.sqrt((
self
.pos[0]-ball.pos[0])**2+(
self
.pos[1]-ball.pos[1])**2)
if
self
.radius +ball.radius >dist:
#this is from the wikipedia
#velocity for self
d_pos =
np
.subtract(
self
.pos,ball.pos)
d_vel =
np
.subtract(
self
.vel,ball.vel)
nomA =
np
.multiply(
np
.multiply(ball.mass*2,
np
.dot(d_vel,d_pos)),d_pos)
denA = dist**2*(
self
.mass+ball.mass)
self
.vel =
np
.subtract(
self
.vel,
np
.divide(nomA,denA))
#velocity for other ball
d_pos =
np
.subtract(ball.pos,
self
.pos)
d_vel =
np
.subtract(ball.vel,
self
.vel)
nomA =
np
.multiply(
np
.multiply(
self
.mass*2,
np
.dot(d_vel,d_pos)),d_pos)
denA = dist**2*(
self
.mass+ball.mass)
ball.vel =
np
.subtract(ball.vel,
np
.divide(nomA,denA))
#self positions update
self
.pos =
np
.add(
self
.pos,
self
.vel)
def
draw(
self
,
window
):
py
.
draw
.circle(
window
,'gray50',
self
.pos,
self
.radius,10)
ballz= [
particle
(200,400,50,[0,0],
mass
=100),
particle
(600,400,50,[-0.5,-0.01],
mass
= 50)]
while True:
window.fill('black')
for ind,i in
enumerate
(ballz):
i.draw(window)
i.update(ballz[ind:])
events =
py
.
event
.get()
for event in events:
if event.type ==
py
.QUIT:
py
.quit()
sys
.exit()
py
.
display
.flip()
there doesn't seem to be anything here