VPython 3D Objects
Claimed by Do Won Kim (Fall 2017) (In Progress)
The main purpose of the VPython programming language is to create 3D simulation with creating the 3D objects. To achieve this purpose, VPython already contains a number of built-in 3-dimensional objects, and these objects are displayed in a 3D graphics module called Visual. Familiarizing with these objects is important for lab coding.
Before start coding, make sure to have
from __future__ import division from visual import *
on the top of your script.
This document is written based on VPython 7.
List of 3D Objects
Image of Each Objects
VPython is packaged with a variety of 3-Dimensional objects. By clicking each names of the objects, you can go to the official description of each objects from Glowscript.
Example Code of Each Objects
Arrow
a = arrow(pos=(0,2,1),axis=(5,0,0), color = color.red)
Box
b = box(pos=(0,0,0),length=3, height=3, width=3, color = color.orange)
Cone
c = cone(pos=(0,0,0),axis=vector(2,0,0), radius=1, color = color.yellow)
Curve
c = curve(pos=[(-1,-1,0), (1,-1,0)], color = color.green) #create object c.append(pos=(3,2,-2)) #append next position vector c.append(pos=(4,1,2))
Cylinder
c = cylinder(pos=vector(0,2,1), axis=vector(5,0,0), radius=1, color=color.cyan)
Ellipsoid
e = ellipsoid(pos=vector(0,0,0), length=3, height=5, width=7, color=color.blue)
Extrusion
#define polygons first tri = Polygon( [(-2,0), (0,4), (2,0)] ) circ = shapes.circle(pos=(0,1.5), radius=0.8) straight = [(0,0,0),(0,0,-4)] #create extrusion e = extrusion(pos=straight, shape=tri-circ, color=color.magenta)
Helix
h = helix(pos=vector(0,2,1), axis=vector(5,0,0), radius=0.5, color=(0.5,1,0))
Label
Points
Pyramid
Ring
Sphere
Text
Commonly Used Objects
The following objects are often used in class lab.
Sphere
The sphere object can be used to represent a wide variety of things, but perhaps the most important for use in Physics 2 is point charges and protons/electrons. Two things must be set; the position, a vector; and the radius, a numerical value.
Arrow
The arrow is perhaps the most important object for Physics 2. It can be used to represent Electric and magnetic fields as well as visualizing vectors. To create an arrow the position (a vector), axis (a vector), and the shaftwidth (a numerical value) must be set.
Object Uses
Examples of Simple Objects
- sphere1 = sphere(pos=vector(4,8,15), radius=16)
- arrow1 = arrow(pos=vector(4,8,15), axis=vector(16,23,42)
helix1 = helix(pos=vector(4,8,15), axis=vector(16,23,42) radius=.23)
Modeling Fields
- from visual import *
- mzofp = 1e-7
- oofpez = 9e9
- qe = 1.6e-19
- proton = sphere(pos=vector(3e-10,0,0), radius=1e-11, color=color.red)
- velocity = vector(-5.2e4,0,0) #The proton's velocity
- r_obs = vector(0,8e-11,0) #The observation location
- deltat = 1e-19 #Timestep
while proton.x < 5e-10:
- r = r_obs - proton.pos
- rhat = r/mag(r)
- Efield = oofpez*qe*rhat/(mag(r)**2)
Bfield = mzofp*qe*cross(velocity,that)/(mag(r)**2)
See also
Further reading
References
http://www.glowscript.org/docs/VPythonDocs/primitives.html http://vpython.org/contents/docs/shapes.html