0

Im trying to make a pair of balls bounce in 1D space and let them colide. I'm running it on glowscript which uses webVPython, should be the same as VPython from what I can tell.

I keep running into this issue where the balls tend to rubber band causing some weird collision issues where they can slowly phase through other objects, also I'm too stupid to figure out how to calculate the momentum change, not sure if thats relavent to the rubber banding but if anyone knows an eqation I could use or a fix that would be appricated.

link to code: https://www.glowscript.org/#/user/amirdmgazzali/folder/bouncingballs/program/bouncingballs1 this is the code:

` Web VPython 3.2

#these are the graphs
###############################################################################################
g1 = graph(xtitle="t [s]",ytitle="y [m]", width=500, height=150)
fp = gcurve(color=color.green)
g2 = graph(xtitle="t [s]",ytitle="v [m/s]", width=500, height=150)
fv = gcurve(color=color.red)
g3 = graph(xtitle="t [s]",ytitle="E [J]", width=500, height=150)
fK = gcurve(color=color.blue)
fU = gcurve(color=color.red)
fE = gcurve(color=color.magenta)


#\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
g4 = graph(xtitle="t [s]",ytitle="y [m]", width=500, height=150)
fyA = gcurve(color=color.green)
fyB = gcurve(color=color.orange)

#g5 = graph(xtitle="t [s]",ytitle="y [m]", width=500, height=150)
#fyB = gcurve(color=color.red)
#\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\


###############################################################################################








#defining a floor to bounce on
#//////////////////////////////////////////////////////////////////////////////////////////
R = 0.02
floor = box(pos = vector(0,-0.005-R,0),size=vector(0.1,0.01,0.1))

#//////////////////////////////////////////////////////////////////////////////////////////

#some basic perameters/initial conditions
###############################################################################################
h = .3
#g = vector(0,-9.8,0)
g = 9.8
c = 0.5
t = 0
dt = 0.01
bt = 0

#ball definitions 
#\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
mA = 2
x0A=0
y0A=h
vx0A=0
vy0A=0
dtA = 0.01
btA = 0
#\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\

#ball2 definitions 
#\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
mB = .05
x0B=0
y0B=h+.1
vx0B=0
vy0B=0
dtB = 0.01
btB = 0
#\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\


###############################################################################################

#defining a ball
#//////////////////////////////////////////////////////////////////////////////////////////
ball = sphere(pos=vector(0,h,0), radius=R, color=color.green, make_trail=True, trail_type="points", 
interval=10, retain=10)
ball2 = sphere(pos=vector(0,h,0), radius=R, color=color.orange, make_trail=True, trail_type="points", 
interval=10, retain=10)
#//////////////////////////////////////////////////////////////////////////////////////////

#physics behind the ball
###############################################################################################

#t<x; x defines how many seconds

while t<2:
    
    #rate(x); x defines update rate
    rate(10)
    
#just the basic physics equations for the ball
#\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
    xA = x0A + vx0A*btA
    yA = y0A + vy0A*btA - .5*g*btA**2
    vxA = vx0A
    vyA = vy0A - g*btA
    vA = sqrt(vxA**2 + vyA**2)
#\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
    
    
#just the basic physics equations for the ball2
#\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
    xB = x0B + vx0B*btB
    yB = y0B + vy0B*btB - .5*g*btB**2
    vxB = vx0B
    vyB = vy0B - g*btB
    vB = sqrt(vxB**2 + vyB**2)
#\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
    
    
#defines ball new position based of the generated vector
#/////////////////////////////////////////////////////////////////////
    ball.pos = vector(xA,yA,0)
    ball2.pos = vector(xB,yB,0)
#/////////////////////////////////////////////////////////////////////
    
#ball bounce phyics
#\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
    
#pos.y<=x; x defines the height at which the ball bounces 
    if ball.pos.y<-.005-R:
        
#Ball bounce vector changes
#/////////////////////////////////
        vyA = -sqrt(c)*vyA
        vxA = sqrt(c)*vxA
        vy0A = vyA
        vx0A = vxA
        x0A = xA
        y0A = yA
#/////////////////////////////////
        bt = 0
        btA = 0
#\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\

#pos.y<=x; x defines the height at which the ball bounces  
    if ball2.pos.y<-.005-R:

#Ball2 bounce vector changes
#/////////////////////////////////
        vyB = -sqrt(c)*vyB
        vxB = sqrt(c)*vxB
        vy0B = vyB
        vx0B = vxB
        x0B = xB
        y0B = yB
#/////////////////////////////////
        bt = 0
        btB = 0
#\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\



#\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\

    if ball.pos.y>=ball2.pos.y: ##
        
        vyA = -sqrt(c)*vyA
        vxA = sqrt(c)*vxA
        vy0A = vyA
        vx0A = vxA
        x0A = xA
        y0A = yA
#/////////////////////////////////
        bt = 0
        btA = 0

    if ball2.pos.y<=ball.pos.y: ##
        
#Ball2 bounce vector changes
#/////////////////////////////////
        vyB = -sqrt(c)*vyB
        vxB = sqrt(c)*vxB
        vy0B = vyB
        vx0B = vxB
        x0B = xB
        y0B = yB
#/////////////////////////////////
        bt = 0
        btB = 0
        
    if vyA<=0.01:
        vyA=0
    if vyB<=0.01:
        vyB=0
        
# if ball2.pos.y<=ball.pos.y:
        


#updates graph and time
#/////////////////////////////////
    K = .5*mA*(vxA**2+vyA**2)
    U = mA*g*yA
    E = K+U
    fp.plot(t,yA)
    fv.plot(t,vyA)
    fE.plot(t,E)
    fK.plot(t,K)
    fU.plot(t,U)
    
    
    fyA.plot(t,vyA)
    fyB.plot(t,vyB)
    
    t = t + dt
    btA = btA + dt
    btB = btB + dt
    bt = bt + dt
#/////////////////////////////////
############################################################################################### 
    
#prints time    
print("t = ",t ,"seconds")`

1 Answer 1

1

Your program is way to (overly) complex to make it feasible to figure out what might be going wrong. I urge you to abandon using kinematics formulas to update positions. Here is a much simpler scheme: https://www.glowscript.org/#/user/Bruce_Sherwood/folder/Examples/program/00Demo/edit, where you calculate the net force F on an object and update an object's momentum as ball.p += F*dt and update the object's position as ball.pos += (ball.p/ball.m)*dt. You can detect collisions by finding that the distance between centers of the two balls is less than the sum of their radii. Note that a better place to ask about Web VPython is the forum https://groups.google.com/g/glowscript-users

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.