0

Hello, I and doing a ball bounce project for Vpython and I am trying run the program I wrote. I keep getting an invalid syntax error, but I am unable to find any. Please help.

from visual import*
wallA = box(pos=vector(0,6,0), size=vector(12.1,0.2,12.1), color=color.red)
wallB = box(pos=vector(0,-6,0), size=vector(12.1,0.2,12.1), color=color.green)
wallC = box(pos=vector(6,0,0), size=vector(0.2,12.1,12.1), color=color.yellow)
wallD = box(pos=vector(-6,0,0), size=vector(0.2,12.1,12.1), color=color.yellow)
wallE = box(pos=vector(0,0,-6), size=vector(12.1,12.1,0.2), color=color.yellow)


ball=sphere(pos=vector(0,0,0), radius=0.20, color=color.white)
ball.vel=vector(1.5,-.9,-1.9)
vscale=3.0

dt=0.01

velarrow=arrow(pos=ball.pos, axis=ball.vel, color=color.blue)
ball.trail=curve(pos=[ball.pos], color=color.white)


t=0

scene.autoscale=false

while t<10:
    rate(1/dt)
    t=t+dt
    
    if ((ball.pos.y-ball.radius+ball.vel.y*dt)<wallB.pos.y+0.5*wallB.height):
        ddt=(wallB.pos.y+0.5*wallB.height-ball.pos.y+ball.radius)/ball.vel.y
        ball.pos=ball.pos+ball.vel*ddt
        ball.trail.append(ball.pos)
        ball.vel.y=-ball.vel.y
        ball.pos=ball.pos+ball.vel*(dt-ddt)
    else:
        ball.pos=ball.pos+ball.vel*dt

    if ((ball.pos.y+ball.radius+ball.vel.y*dt)>wallA.pos.y-0.5*wallA.height):
        ddt=(wallA.pos.y-0.5*wallA.height-ball.pos.y-ball.radius)/ball.vel.y
        ball.pos=ball.pos+ball.vel*ddt
        ball.trail.append(ball.pos)
        ball.vel.y=-ball.vel.y
        ball.pos=ball.pos+ball.vel*(dt-ddt)
    else:
        ball.pos=ball.pos+ball.vel*dt

    if ((ball.pos.x+ball.radius+ball.vel.x*dt)>wallC.pos.x-0.5*wallC.length):
        ddt=(wallC.pos.x-0.5*wallC.length-ball.pos.x-ball.radius)/ball.vel.x
        ball.pos=ball.pos+ball.vel*ddt
        ball.trail.append(ball.pos)
        ball.vel.x=-ball.vel.x
        ball.pos=ball.pos+ball.vel*(dt-ddt)
    else:
        ball.pos=ball.pos+ball.vel*dt

    if ((ball.pos.x-ball.radius+ball.vel.x*dt)<wallD.pos.x+0.5*wallD.length):
        ddt=(wallD.pos.x+0.5*wallD.length-ball.pos.x+ball.radius)/ball.vel.x
        ball.pos=ball.pos+ball.vel*ddt
        ball.trail.append(ball.pos)
        ball.vel.x=-ball.vel.x
        ball.pos=ball.pos+ball.vel*(dt-ddt)
    else:
        ball.pos=ball.pos+ball.vel*dt

    if ((ball.pos.z-ball.radius+ball.vel.z*dt)<wallE.pos.z+0.5*wallE.width):
        ddt=(wallE.pos.z+0.5*wallE.width-ball.pos.z+ball.radius)/ball.vel.z
        ball.pos=ball.pos+ball.vel*ddt
        ball.trail.append(ball.pos)
        ball.vel.z=-ball.vel.z
        ball.pos=ball.pos+ball.vel*(dt-ddt)
    else:
        ball.pos=ball.pos+ball.vel*dt

    if ((ball.pos.z+ball.radius+ball.vel.z*dt)>(-wallE.pos.z)-0.5*wallE.width):
        ddt=((-wallE.pos.z)-0.5*wallE.width-ball.pos.z-ball.radius)/ball.vel.z
        ball.pos=ball.pos+ball.vel*ddt
        ball.trail.append(ball.pos)
        ball.vel.z=-ball.vel.z
        ball.pos=ball.pos+ball.vel*(dt-ddt)
    else:
        ball.pos=ball.pos+ball.vel*dt

    ball.pos=ball.pos+ball.vel*dt
    velarrow.pos=ball.pos
    velarrow.axis=ball.vel
    ball.trail.append(ball.pos)
    
  
print "End of program. Ending time is ", t,"seconds."
print "Final Position of ball is ", ball.pos,"."
3
Contributors
3
Replies
4
Views
5 Years
Discussion Span
Last Post by HiHe
0
from visual import*
wallA = box(pos=vector(0,6,0), size=vector(12.1,0.2,12.1), color=color.red)
wallB = box(pos=vector(0,-6,0), size=vector(12.1,0.2,12.1), color=color.green)
wallC = box(pos=vector(6,0,0), size=vector(0.2,12.1,12.1), color=color.yellow)
wallD = box(pos=vector(-6,0,0), size=vector(0.2,12.1,12.1), color=color.yellow)
wallE = box(pos=vector(0,0,-6), size=vector(12.1,12.1,0.2), color=color.yellow)


ball=sphere(pos=vector(0,0,0), radius=0.20, color=color.white)
ball.vel=vector(1.5,-.9,-1.9)
vscale=3.0

dt=0.01

velarrow=arrow(pos=ball.pos, axis=ball.vel, color=color.blue)
ball.trail=curve(pos=[ball.pos], color=color.white)


t=0

scene.autoscale=false

while t<10:
    rate(1/dt)
    t=t+dt
    
    if ((ball.pos.y-ball.radius+ball.vel.y*dt)<wallB.pos.y+0.5*wallB.height):
        ddt=(wallB.pos.y+0.5*wallB.height-ball.pos.y+ball.radius)/ball.vel.y
        ball.pos=ball.pos+ball.vel*ddt
        ball.trail.append(ball.pos)
        ball.vel.y=-ball.vel.y
        ball.pos=ball.pos+ball.vel*(dt-ddt)
    else:
        ball.pos=ball.pos+ball.vel*dt

    if ((ball.pos.y+ball.radius+ball.vel.y*dt)>wallA.pos.y-0.5*wallA.height):
        ddt=(wallA.pos.y-0.5*wallA.height-ball.pos.y-ball.radius)/ball.vel.y
        ball.pos=ball.pos+ball.vel*ddt
        ball.trail.append(ball.pos)
        ball.vel.y=-ball.vel.y
        ball.pos=ball.pos+ball.vel*(dt-ddt)
    else:
        ball.pos=ball.pos+ball.vel*dt

    if ((ball.pos.x+ball.radius+ball.vel.x*dt)>wallC.pos.x-0.5*wallC.length):
        ddt=(wallC.pos.x-0.5*wallC.length-ball.pos.x-ball.radius)/ball.vel.x
        ball.pos=ball.pos+ball.vel*ddt
        ball.trail.append(ball.pos)
        ball.vel.x=-ball.vel.x
        ball.pos=ball.pos+ball.vel*(dt-ddt)
    else:
        ball.pos=ball.pos+ball.vel*dt

    if ((ball.pos.x-ball.radius+ball.vel.x*dt)<wallD.pos.x+0.5*wallD.length):
        ddt=(wallD.pos.x+0.5*wallD.length-ball.pos.x+ball.radius)/ball.vel.x
        ball.pos=ball.pos+ball.vel*ddt
        ball.trail.append(ball.pos)
        ball.vel.x=-ball.vel.x
        ball.pos=ball.pos+ball.vel*(dt-ddt)
    else:
        ball.pos=ball.pos+ball.vel*dt

    if ((ball.pos.z-ball.radius+ball.vel.z*dt)<wallE.pos.z+0.5*wallE.width):
        ddt=(wallE.pos.z+0.5*wallE.width-ball.pos.z+ball.radius)/ball.vel.z
        ball.pos=ball.pos+ball.vel*ddt
        ball.trail.append(ball.pos)
        ball.vel.z=-ball.vel.z
        ball.pos=ball.pos+ball.vel*(dt-ddt)
    else:
        ball.pos=ball.pos+ball.vel*dt

    if ((ball.pos.z+ball.radius+ball.vel.z*dt)>(-wallE.pos.z)-0.5*wallE.width):
        ddt=((-wallE.pos.z)-0.5*wallE.width-ball.pos.z-ball.radius)/ball.vel.z
        ball.pos=ball.pos+ball.vel*ddt
        ball.trail.append(ball.pos)
        ball.vel.z=-ball.vel.z
        ball.pos=ball.pos+ball.vel*(dt-ddt)
    else:
        ball.pos=ball.pos+ball.vel*dt

    ball.pos=ball.pos+ball.vel*dt
    velarrow.pos=ball.pos
    velarrow.axis=ball.vel
    ball.trail.append(ball.pos)
    
  
print "End of program. Ending time is ", t,"seconds."
print "Final Position of ball is ", ball.pos,"."

This code works just fine with vpython and python27. What is your exact traceback error message?

0

I kept getting a syntax error message, once I deleted the print lines:

print "End of program. Ending time is ", t,"seconds."
print "Final Position of ball is ", ball.pos,"."

it was able to run

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.