VPython 3D Objects: Difference between revisions

From Physics Book
Jump to navigation Jump to search
(Created page with "The VPython programming language contains a number of 3-dimensional objects that can be displayed in a 3D graphics module called Visual. ==Overview== VPython is packaged wit...")
 
No edit summary
 
(25 intermediate revisions by 2 users not shown)
Line 1: Line 1:
The VPython programming language contains a number of 3-dimensional objects that can be displayed in a 3D graphics module called Visual.
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.  


==Overview==
Before start coding, make sure to have


VPython is packaged with a variety of 3-Dimensional objects. These objects include:
from __future__ import division
#[http://www.glowscript.org/docs/VPythonDocs/arrow.html Arrow]
from visual import *
#[http://www.glowscript.org/docs/VPythonDocs/box.html Box]
#[http://www.glowscript.org/docs/VPythonDocs/cone.html Cone]
#[http://www.glowscript.org/docs/VPythonDocs/convex.html Convex]
#[http://www.glowscript.org/docs/VPythonDocs/curve.html Curve]
#[http://www.glowscript.org/docs/VPythonDocs/cylinder.html Cylinder]
#[http://www.glowscript.org/docs/VPythonDocs/ellipsoid.html Ellipsoid]
#[http://www.glowscript.org/docs/VPythonDocs/extrusion.html Extrusion]
#[http://www.glowscript.org/docs/VPythonDocs/faces.html Faces]
#[http://www.glowscript.org/docs/VPythonDocs/helix.html Helix]
#[http://www.glowscript.org/docs/VPythonDocs/label.html Label]
#[http://www.glowscript.org/docs/VPythonDocs/points.html Points]
#[http://www.glowscript.org/docs/VPythonDocs/pyramid.html Pyramid]
#[http://www.glowscript.org/docs/VPythonDocs/ring.html Ring]
#[http://www.glowscript.org/docs/VPythonDocs/sphere.html Sphere]
#[http://www.glowscript.org/docs/VPythonDocs/text.html Text]
The ease with which these objects can be manipulated using python makes them popular in physics classes.


on the top of your script.


==Important Objects==
This document is written based on VPython 7.
The following objects appear often in Physics 2.
==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.
The objects are listed in alphabetical order.
 
[http://www.glowscript.org/docs/VPythonDocs/arrow.html Arrow]
[[File:RedArrow.png|thumb|none|alt=Yellow Arrow|Example arrow]]
 
[http://www.glowscript.org/docs/VPythonDocs/box.html Box]
[[File:OrangeBox.png|thumb|none|alt=Yellow Arrow|Example box]]
 
[http://www.glowscript.org/docs/VPythonDocs/cone.html Cone]
[[File:YellowCone.png|thumb|none|alt=Yellow Arrow|Example cone]]
 
[http://www.glowscript.org/docs/VPythonDocs/curve.html Curve]
[[File:GreenCurve.png|thumb|none|alt=Yellow Arrow|Example curve]]
 
[http://www.glowscript.org/docs/VPythonDocs/cylinder.html Cylinder]
[[File:CyanCylinder.png|thumb|none|alt=Yellow Arrow|Example cylinder]]
 
[http://www.glowscript.org/docs/VPythonDocs/ellipsoid.html Ellipsoid]
[[File:BlueEllipsoid.png|thumb|none|alt=Yellow Arrow|Example ellipsoid]]
 
[http://www.glowscript.org/docs/VPythonDocs/extrusion.html Extrusion]
[[File:MagentaExtrusion.png|thumb|none|alt=Yellow Arrow|Example extrusion]]
 
[http://www.glowscript.org/docs/VPythonDocs/helix.html Helix]
[[File:GoldHelix.png|thumb|none|alt=Yellow Arrow|Example helix]]
 
[http://www.glowscript.org/docs/VPythonDocs/label.html Label]
[[File:WhiteLabel.png|thumb|none|alt=Yellow Arrow|Example label]]
 
[http://www.glowscript.org/docs/VPythonDocs/points.html Points]
[[File:IvoryPoints.png|thumb|none|alt=Yellow Arrow|Example points]]
 
[http://www.glowscript.org/docs/VPythonDocs/pyramid.html Pyramid]
[[File:BrownPyramid.png|thumb|none|alt=Yellow Arrow|Example pyramid]]
 
[http://www.glowscript.org/docs/VPythonDocs/ring.html Ring]
[[File:AquaMarineRing.png|thumb|none|alt=Yellow Arrow|Example ring]]
 
[http://www.glowscript.org/docs/VPythonDocs/sphere.html Sphere]
[[File:LightGreenSphere.png|thumb|none|alt=Yellow Arrow|Example sphere]]
 
[http://www.glowscript.org/docs/VPythonDocs/text.html Text]
[[File:KhakiText.png|thumb|none|alt=Yellow Arrow|Example text]]
 
===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.96,0.73,0.14))
 
Label
l = label(pos=vector(0,0.25,0), text='This is a Label', xoffset=20, yoffset=50, space=30, height=16, border=4,font='sans')
 
Points
p = points(pos=[vector(-1,0,0), vector(1,0,0)], radius=50, color=(1,1,.94))
 
Pyramid
p = pyramid(pos=vector(5,2,0), size=vector(3,3,2), color=(0.82, 0.41, 0.12))
 
Ring
r = ring(pos=vector(1,1,1), axis=vector(0,1,0),radius=0.5, thickness=0.1, color=(0.14, 1, 0.83))
 
Sphere
s = sphere(pos=vector(1,1,1), radius=5, color=(0.5, 1, 0.5))
 
Text
t = text(text='This is \ntext', #use \n for changing line
    align='center', color=(0.94, 0.90, 0.55))
 
 
==Commonly Used Objects==
The following objects are often used in class lab.


===Sphere===
===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.
The sphere object can be used to represent a wide variety of things, but it is mostly used to represent spherical objects such as planets or particles such as protons and electrons. Two things must be set; the position (vector), and the radius (numerical value).


===Arrow===
===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.  
The arrow is perhaps the most important object for physics simulation. It can be used to represent various forces as well as visualizing vectors. To create an arrow, the position (vector), axis (vector), and the shaftwidth (a numerical value) must be set.
 
===Curve===
The curve is often used to create a trail mark that represents a path that the object has moved, and it is also frequently used to create graphs. To create a curve, put the initial position vector of the curve, and append the next positions while updating the position in the while loop.


==Object Uses==
==Application==
Following code is an example usage and application of object creation for lab simulations.


===Examples of Simple Objects===
## objects
<code>
ceiling = box(pos=vector(0,0,0), size = vector(0.2, 0.01, 0.2))        # origin is at ceiling
:sphere1 = sphere(pos=vector(4,8,15), radius=16)
ball = sphere(pos=vector(.1,-.2,.3), radius=0.025, color=color.orange) # note: spring initially compressed
:
spring = helix(pos=ceiling.pos, color=color.cyan, thickness=.003, coils=40, radius=0.015)
:arrow1 = arrow(pos=vector(4,8,15), axis=vector(16,23,42)
parrow = arrow(color=color.magenta, pos=ball.pos, axis=vector(0,0,0))
:
farrow = arrow(color=color.red, pos=ball.pos, axis=vector(0,0,0))
:helix1 = helix(pos=vector(4,8,15), axis=vector(16,23,42) radius=.23)</code>
trail = curve(color=ball.color)
#graphs
Kgraph=gcurve(color=color.red)
Ugraph=gcurve(color=color.cyan)
KUgraph=gcurve(color=color.yellow)


===Modeling Fields===
#graph updates are done in the while loop.
<code>
: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)</code>


== See also ==
== See also ==
Line 65: Line 143:
#[http://www.physicsbook.gatech.edu/VPython VPython]
#[http://www.physicsbook.gatech.edu/VPython VPython]
#[http://www.physicsbook.gatech.edu/VPython_Animation VPython Animation]
#[http://www.physicsbook.gatech.edu/VPython_Animation VPython Animation]
#[http://www.physicsbook.gatech.edu/VPython_Objects VPython Objects]


===Further reading===
===Further reading===
Line 70: Line 149:
#[http://www.glowscript.org/docs/VPythonDocs/primitives.html 3D object details]
#[http://www.glowscript.org/docs/VPythonDocs/primitives.html 3D object details]
#[http://vpython.org VPython Information]
#[http://vpython.org VPython Information]


==References==
==References==
http://www.glowscript.org/docs/VPythonDocs/primitives.html
http://www.glowscript.org/docs/VPythonDocs/primitives.html
http://vpython.org/contents/docs/shapes.html
http://vpython.org/contents/docs/shapes.html


[[Category:Modeling With VPython]]
http://vpython.org/

Latest revision as of 02:52, 22 October 2019

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. The objects are listed in alphabetical order.

Arrow

Error creating thumbnail: sh: /usr/bin/convert: No such file or directory Error code: 127
Example arrow

Box

Error creating thumbnail: sh: /usr/bin/convert: No such file or directory Error code: 127
Example box

Cone

Error creating thumbnail: sh: /usr/bin/convert: No such file or directory Error code: 127
Example cone

Curve

Error creating thumbnail: sh: /usr/bin/convert: No such file or directory Error code: 127
Example curve

Cylinder

Error creating thumbnail: sh: /usr/bin/convert: No such file or directory Error code: 127
Example cylinder

Ellipsoid

Error creating thumbnail: sh: /usr/bin/convert: No such file or directory Error code: 127
Example ellipsoid

Extrusion

Error creating thumbnail: sh: /usr/bin/convert: No such file or directory Error code: 127
Example extrusion

Helix

Error creating thumbnail: sh: /usr/bin/convert: No such file or directory Error code: 127
Example helix

Label

Error creating thumbnail: sh: /usr/bin/convert: No such file or directory Error code: 127
Example label

Points

Error creating thumbnail: sh: /usr/bin/convert: No such file or directory Error code: 127
Example points

Pyramid

Error creating thumbnail: sh: /usr/bin/convert: No such file or directory Error code: 127
Example pyramid

Ring

Error creating thumbnail: sh: /usr/bin/convert: No such file or directory Error code: 127
Example ring

Sphere

Error creating thumbnail: sh: /usr/bin/convert: No such file or directory Error code: 127
Example sphere

Text

Error creating thumbnail: sh: /usr/bin/convert: No such file or directory Error code: 127
Example text

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.96,0.73,0.14))

Label

l = label(pos=vector(0,0.25,0), text='This is a Label', xoffset=20, yoffset=50, space=30, height=16, border=4,font='sans')

Points

p = points(pos=[vector(-1,0,0), vector(1,0,0)], radius=50, color=(1,1,.94))

Pyramid

p = pyramid(pos=vector(5,2,0), size=vector(3,3,2), color=(0.82, 0.41, 0.12))

Ring

r = ring(pos=vector(1,1,1), axis=vector(0,1,0),radius=0.5, thickness=0.1, color=(0.14, 1, 0.83))

Sphere

s = sphere(pos=vector(1,1,1), radius=5, color=(0.5, 1, 0.5))

Text

t = text(text='This is \ntext', #use \n for changing line
    align='center', color=(0.94, 0.90, 0.55))


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 it is mostly used to represent spherical objects such as planets or particles such as protons and electrons. Two things must be set; the position (vector), and the radius (numerical value).

Arrow

The arrow is perhaps the most important object for physics simulation. It can be used to represent various forces as well as visualizing vectors. To create an arrow, the position (vector), axis (vector), and the shaftwidth (a numerical value) must be set.

Curve

The curve is often used to create a trail mark that represents a path that the object has moved, and it is also frequently used to create graphs. To create a curve, put the initial position vector of the curve, and append the next positions while updating the position in the while loop.

Application

Following code is an example usage and application of object creation for lab simulations.

## objects
ceiling = box(pos=vector(0,0,0), size = vector(0.2, 0.01, 0.2))         # origin is at ceiling
ball = sphere(pos=vector(.1,-.2,.3), radius=0.025, color=color.orange) # note: spring initially compressed
spring = helix(pos=ceiling.pos, color=color.cyan, thickness=.003, coils=40, radius=0.015)
parrow = arrow(color=color.magenta, pos=ball.pos, axis=vector(0,0,0))
farrow = arrow(color=color.red, pos=ball.pos, axis=vector(0,0,0))
trail = curve(color=ball.color)

#graphs
Kgraph=gcurve(color=color.red)
Ugraph=gcurve(color=color.cyan)
KUgraph=gcurve(color=color.yellow)
#graph updates are done in the while loop.

See also

  1. VPython Basics
  2. VPython
  3. VPython Animation
  4. VPython Objects

Further reading

  1. 3D object details
  2. VPython Information

References

http://www.glowscript.org/docs/VPythonDocs/primitives.html

http://vpython.org/contents/docs/shapes.html

http://vpython.org/