vpython初探

vpython 是python默认的3D模块,和python有一样的风格。与PyOpenGL相比,容易上手。

vpython下载:vpython的官网(www.vpython.org)。顺便说一句,官网强大的模块,一般都强大。

安装好了之后,可以在C:\Python27\Lib\site-packages\visual(视python的安装目录而定)中的doc,examples里查看一些文档和例子。

第一个vpython程序

from visual import *
sphere()

 这里要注意,导与vpython模块,不是import vpython,而是import visual。

sphere函数创建一个球,然后就没然后了,连窗体都不用构建,vpython自动帮你生成,当然自己也可设置。

sphere函数的参数有pos(球心的坐标位置)、color(颜色)、radius(半径)、material(材质)等。

类似的我们可以创建其他的一些几何体,如下表

arrow 箭头
box 长方体 
cone 圆锥
convex 凸面体(由点决定)
curve 曲线
cylinder 圆柱
ellipsoid 椭球体
faces 多面体(由面决定)
helix 螺旋线
label 标签
points
pyramid 正四棱锥
ring
text 文字(3d效果)

 

更多可以在http://www.vpython.org/contents/docs_vp5/visual/index.html中查阅,这里啥都有。

当然对与类似于圆锥来说,还有一个参数axis决定圆锥的指向,当然这个参数也可以决定圆锥的高。

此外,对于参数materials,它的值可以是

materials.wood
materials.rough
materials.marble
materials.plastic
materials.earth
materials.diffuse
materials.emissive (looks like it glows)
materials.unshaded (unaffected by lighting)

在 VPython 5.50中还有

materials.shiny
materials.chrome
materials.blazed
materials.silver
materials.BlueMarbe (earth with clouds)
materials.bricks

————————————————————————————————————————————

gameobjects 的下载地址:https://code.google.com/p/gameobjects/downloads/list

————————————————————————————————————————————

vpython中运动

vpython中的坐标系:

from visual import *
a=sphere()
a.pos.x+=10

直接改变几何体的pos参数,就可实现几何体的平移。若想实现动画

from visual import *
a=sphere()
while True:
    rate(100)
    a.pos.x+=0.1

这里的rate(100)是设置动画每秒的帧数为100,即延时0.01秒。

除了平移,我们还能用rotate函数实现旋转

from visual import *
a=box(length=5,width=3,height=1)
while True:
    rate(100)
    a.rotate(axis=(1,0,0),angle=math.pi/400,origin=(0,0,0))

rotate函数中的参数axis确定旋转的方向,其值为旋转面的法向量,angle是转动的角度,而origin则是旋转的圆心。需要说明的是,旋转是几何体的中心对圆心旋转。如果设置origin=a.pos,则是几何体a绕自己的中心旋转;如果设置origin的值为几何体的边缘,则是几何体a绕该边缘旋转。

能够实现平移和旋转,我们可以利用以前物理中学过的运动的合成与分解,实现几何体的很多运动了。

 先写到这吧。

print "Good luck!"
Good luck!

posted on 2015-09-08 09:44  SuperZhang828  阅读(6988)  评论(0编辑  收藏  举报

导航