rotating a cube in vpython

442 views
Skip to first unread message

ali lolo

unread,
Mar 16, 2018, 4:59:44 AM3/16/18
to VPython-users
This code:
from vpython import *
import time
class cubelet:
   def __init__(self,x,y,z,c1,c2,c3,c4,c5,c6,):
      self.x=x
      self.y=y
      self.z=z
      self.c1=c1
      self.c2=c2
      self.c3=c3
      self.c4=c4
      self.c5=c5
      self.c6=c6
   def draw(self):
      a=pyramid(pos=vector(self.x-3,self.y,self.z), size=vector(3,6,6),color=self.c1)
      b=pyramid(pos=vector(self.x+3,self.y,self.z), size=vector(3,6,6),color=self.c2,axis=vector(-1,0,0))
      c=pyramid(pos=vector(self.x,self.y,self.z-3), size=vector(3,6,6),color=self.c3,axis=vector(0,0,1))
      d=pyramid(pos=vector(self.x,self.y,self.z+3), size=vector(3,6,6),color=self.c4,axis=vector(0,0,-1))
      e=pyramid(pos=vector(self.x,self.y-3,self.z), size=vector(3,6,6),color=self.c5,axis=vector(0,1,0))
      f=pyramid(pos=vector(self.x,self.y+3,self.z), size=vector(3,6,6),color=self.c6,axis=vector(0,-1,0))
      self.list=[a,b,c,d,e,f]
   def rotate(self):
      for i in self.list:
         i.rotate(angle=90,axis=vec(0,0,1),origin=vector(0,0,0))
i=cubelet(0,0,0,color.white,color.yellow,color.red,color.magenta,color.blue,color.green)
i.draw()
time.sleep(0.2)
i.rotate()
My question is that why the cube doesn't rotate with angle 90 when we run the program? 

Bruce Sherwood

unread,
Mar 16, 2018, 11:42:17 AM3/16/18
to VPython-users
Angles must be specified as radians. Either say angle=pi/2 or angle=radians(90).

Another way to organize your program would like this:

def cubelet(....): # a "factory function"
    #create a list of the 6 pyramids, as you do already; suppose its name is L
    return compound(L)

c = cubelet(....)

This "factory function" (a function that returns an object) returns an object that has the usual properties of primitive objects such as box. You can manipulate this compound object by changing c.pos or c.axis, etc. You can create copies of this cubelet:

c2 = c.clone(pos=vec(10,5,0), size=vec(2,1,0.2))

Bruce
Reply all
Reply to author
Forward
0 new messages