Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupport News AboutSign UpSign In
| Download
Views: 4666
Kernel: Python 2 (SageMath)

In addition to spheres you can create an array of other shapes in VPython. Some of the shapes include:

  • box()

  • cyinder()

  • cone()

  • ring()

A complete list of shapes and properties can be found in the VPython documentation

Each shape has different properties that need to be specified. For the box, you can specify the length, width, height, as well as a vector representing the axis of the box. The axis defines the direction of the length measurement. The command to create a box is:

box_name=box(pos=vector(0,0,0),axis=vector(1,0,0),length=1,width=1,height=1,color=color.red)

There are default values for each property so if you don't specify the axis vector or the length, VPython will use a default value.

You can also attach arrows to objects. We'll see a later example where it is used to represent velocity, but here it shows the direction of the axis vector for the box. The attach_arrow() command needs to know which object it should be attached to and which property of the object to use for the direction of the arrow.

from vpython import * scene = canvas() box1=box(pos=vector(2,0,0),axis=vector(1,1,1),length=.75,width=.5,height=.5,color=color.red) attach_arrow(box1,"axis",color=color.blue,scale=2) #Arrow uses box1.axis for it's direction and the magnitude is 2x as big as box1.axis
<IPython.core.display.Javascript object>
<IPython.core.display.Javascript object>
<IPython.core.display.Javascript object>
<IPython.core.display.Javascript object>
<IPython.core.display.Javascript object>
<IPython.core.display.Javascript object>
<IPython.core.display.Javascript object>
<IPython.core.display.Javascript object>
<IPython.core.display.Javascript object>

Try It Yourself

Try adding a cone and a cylinder to the scene above, along with arrows pointing along the axis of each object. Rather then specifying length, width and height, you need to give a radius for the cone and cylinder. The length of the axis vector will determine the height of the cone and cylinder. You can add your code in the cell above and re-evaluate the cell.

In the Intro to VPython in SageMathCloud activity, you learned how to make an object move using a while loop to run through a set of code many times. Each time the program runs through the loop it updates the position of the objects based on the velocity of the object and the time step size.

The code below is set up to animate a ball moving towards the left at a constant speed. Use the attach_arrow() command to add a velocity vector. Rather than specifying "axis" as the vector, specify "vel" as the property to use for the arrow. Make the arrow yellow so it stands out (color.yellow') and scale the arrow by a factor of 2. Note that ball1.velmust be defined *before* you can attach an arrow to the velocity so place your code where it says"###Add your code here###"`

from vpython import * scene1=canvas() #Create canvas to draw on ground=box(pos=vector(0,-1,0),axis=vector(1,0,0),length=20,height=0.1,color=color.green) #Grass-colored box for reference ball1=sphere(color=color.blue) #Create sphere ball1.vel=vector(1,0,0) #Give ball a velocity ###Add your code here### dt=0.01 #Time step size t=0 #Set initial time to zero tf=3 #Time when motion will stop while t<tf: rate(30) #Frames per second ball1.pos=ball1.pos+ball1.vel*dt #Update position t=t+dt #Update time
<IPython.core.display.Javascript object>
ERROR! Session/line number was not unique in database. History logging moved to new session 16

Try It Yourself

Add a box to the scene above, give it an initial velocity, and attach an arrow to the box. Use the cell above for this.

Note: When a cell is running you should see an asterisk next to the word 'In' near the left of the cell. When the code has finished a number will appear next to 'In'.

Try It Yourself

In addition to adding arrows to moving objects you can also add a trail that shows where the object has been. The command for this is attach_trail(). You can change the color of the trail and you can have the trail be a solid line or a series of dots. Here are some examples of the code to do this:

  • tail = attach_trail(ball1,color=color.yellow) #Solid yellow line

  • tail=attach_trail(ball1,type="points") #Trail is series of dots rather than solid line

  • tail=attach_trail(ball1,retain=50) #Trail only keeps last 50 points of data

  • tail=attach_trail(ball1,type=points",pps=1) #Trail is series of dots with one point added per second (pps)

Try attaching a trail to the ball and box in the scene above.

Being able to interpret a graph and relate it to the motion of an object is a key skill for students in physics. Fortunately VPython has graphing capabilities baked right in. There are only three things you need to do to graph data: 1) Create the graph, 2) specify the type of graph, and 3) plot the data points.

The graph is created using graph() and you can specify the size of the window, the axes titles, and the minimum and maximum extent of the graph. If you specify maximum x-values and y-values that are too small the graph will expand to fit all data

from vpython import * scene2=canvas() #Create canvas to draw on graph_screen=graph(width=400,height=400,xtitle='time', ytitle='Position',xmin=0,xmax=10,ymin=0,ymax=14) pos2_graph = gdots(color=color.orange, size=2) #Create graph to plot position vs. time ###Create new graph here### ground=box(pos=vector(0,-1,0),axis=vector(1,0,0),length=20,height=0.1,color=color.green) #Grass-colored box for reference ball2=sphere(color=color.blue) #Create the ball ball2.vel=vector(1,.1,0) #Give ball a velocity attach_arrow(ball2,"vel",scale=2,color=color.yellow) #Attach an arrow to show velocity attach_trail(ball2,color=color.yellow,type="points",retain=10,pps=1) dt=0.01 #Time step size t=0 #Initial time tf=3 #Final time (stop going through while loop) while t<tf: rate(30) ball2.pos=ball2.pos+ball2.vel*dt #update position position=ball2.pos.x #For plotting pos2_graph.plot(pos=(t,position)) #Plot a single data point on the graph t=t+dt #Advance time step
<IPython.core.display.Javascript object>
ERROR! Session/line number was not unique in database. History logging moved to new session 17

You can create multiple graphs. To create a second graph, call the graph() command a second time after you have defined the curves for the first graph. After you have called the graph() command a second time you can then define the curves that will appear on that graph. You can repeat this process for each graph that you would like to add.

For You To Try

Add a second graph to display the x-component of the velocity of the ball. Rather than using the gdots(color=color.orange, size=2) command, use gcurve(color=color.blue, width=4)

When you are done you can move on to Constant Acceleration.