We are going to use Python and VPython to make the animation of the bouncing the ball in order to help understand more about the Python.
We start with the python program, and we import the visual from VPython.
We make an sphere with certain position, radius and color, and we name the sphere "ball".
We make an box with certain position, dimension and color, and we name the box "WallR".
We add the time dt into the program, and we create the vector for the velocity of ball. We set the position of ball equals the old position of ball plus the velocity of ball times the time, and we use while loop program to repeat the changing position of ball in order to move the ball.
To make the ball bounce at the surface of the wall, we set the velocity of the wall reverse when the position of the right end of the ball is equal to the position of left end of the box "WallR".
We add value in the y and z coordinate of the velocity vector so the ball can move in three dimension.
We add the arrow with the same position of ball and same axis of velocity of ball so the arrow can show the motion of ball.
To have a trajectory of the moving ball, we create a trail of the moving ball by using the curve in VPython, and we set the position of trail is sames as the position of ball.
We make four more walls with different position, dimension, color in order to form a box, and we set the velocity of the ball reverses when the position of the end of the ball is the same as the position of the inner end of the wall so the ball can collide with the surface of any wall.
We set the velocity of the z-direction of the ball is the old z-velocity plus -9.8 times the time dt to make the acceleration of gravity of -9.8ms^-2 pointing in the negative z direction as we use the equation v(1) = v(0) + gt.
Here is our final product that the ball is bouncing in 3 dimensional with a square box.
Here is our program code:
#-------------------------------------------------------------------------------
# Name: Bouncing Ball
# Purpose:Help me learn some basic features of VPython
#
# Author: Hiu Ching Tang, Kishan Karunaratne
#
# Created: 10/22/2011
#-------------------------------------------------------------------------------
from visual import *
ball=sphere (pos=(-5,0,0),radius=0.5,color=color.red)
wallR=box (pos=(6,0,0),size=(0.2,12,12), color=color.green)
wallL=box (pos=(-6,0,0),size=(0.2,12,12), color=color.blue)
wallT=box (pos=(0,6,0),size=(12,0.2,12), color=color.cyan)
wallB=box (pos=(0,-6,0),size=(12,0.2,12), color=color.orange)
wallBack=box (pos=(0,0,-6),size=(12,12,0.2), color=color.white)
dt=0.05
ball.velocity=vector(1,2,1.5)
bv=arrow(pos=ball.pos, axis=ball.velocity, color=color.yellow)
ball.trail = curve(color=ball.color)
while (1==1):
rate(100)
ball.velocity=ball.velocity
ball.pos = ball.pos + ball.velocity*dt
if ball.x+0.5>wallR.x-0.2:
ball.velocity.x=-ball.velocity.x
if ball.x-0.5
ball.velocity.y=-ball.velocity.y
if ball.y-0.5
ball.velocity.z=-ball.velocity.z
if ball.z-0.5
We do not successfully make the partially elastic collision as we do not know how to decrease the magnitude of velocity when the ball bounce the wall. I do not make 10 balls in the large box. However, we make successfully demonstrate the collision of the balls in three dimensional in a large box.
No comments:
Post a Comment