3

I am just doing a cube in vpython that has some option like change the position, rotate in the x,y or z-axis But the problem is that I don't actually know how to rotate the cube using the rotation matrix that is usually used in math. I made the math correct, but I am not sure how to give my result to the cube's properties to rotate in the x-axis. I tried to give the vector that is result of the math to the cube's property called axis but that is not what I actually want, I also tried with the property pos but that is not neither. I have just worked with the rotation in the x-axis. Also, to watch it correct, I always change the position first, then I tried to make the rotation.

Can you help me please?

Here is my code:

from visual import *
from visual.controls import *
from math import *
from numpy import *

positionGlobalX=0
positionGlobalY=0
positionGlobalZ=0
def changeActualPosition(figure):
    global positionGlobalX
    global positionGlobalY
    global positionGlobalZ
    posX=input("X: ")
    posY=input("Y: ")
    posZ=input("Z: ")
    positionGlobalX=posX
    positionGlobalY=posY
    positionGlobalZ=posZ
    figure.pos=(posX,posY,posZ)


def rotateX(figure):
    global positionGlobalX
    global positionGlobalY
    global positionGlobalZ
    degrees=input("How many degrees: ")
    radians=pi*degrees/180
    posActual=array([[positionGlobalX],[positionGlobalY],[positionGlobalZ]])
    mX=array([[1,0,0],[0,cos(radians),-sin(radians)],[0,sin(radians),cos(radians)]])
    mXm=matrix(mX)
    posActualM=matrix(posActual)
    result=mXm*posActualM
    inX=float(result[0][0])
    inY=float(result[1][0])
    inZ=float(result[2][0])
    print inX
    print inY
    print inZ
    figure.axis=(inX,inY,inZ)
    #figure.rotate(angle=radians,axis=figure.axis, origin=figure.pos)



scene=display(title="Rotation",x=0, y=0, width=600, height=600,cinter=(2*pi,pi,pi))
figure = box(pos=(0,0,0), axis=(2,0,0),make_trail=True,material=materials.earth, length=4, height=5, width=6)

b1 = button(pos=(0,70), height=30, width=100, text='Change position', action=lambda: changeActualPosition(figure))
b2 = button(pos=(0,40), height=30, width=100, text='Rotation X', action=lambda: rotateX(figure))
b3 = button(pos=(0,10), height=30, width=100, text='Rotation Y', action=lambda: changeActualPosition(figure))
b4 = button(pos=(0,-20), height=30, width=100, text='Rotation Z', action=lambda: changeActualPosition(figure))
b5 = button(pos=(0,-50), height=30, width=100, text='Traslation', action=lambda: changeActualPosition(figure))

Sorry for my English. And thank you for your help.

Albert
  • 141
  • 3
  • 12
  • From the [documentation](http://vpython.org/contents/docs/rotation.html) it sounds like all you have to do is `figure.rotate(angle=radians)` to rotate it about its own origin and axis. – martineau May 26 '14 at 01:25
  • 1
    @martineau But that doesn't answer the very specific question of how you could do the same thing with a rotation matrix. I am curious about this, too. – Translunar Aug 14 '14 at 22:08
  • To rotate something about a given point, you would need to first translate it so that the point is the origin--subtract the point's coordinate values from each of the object's-- then apply the rotation matrix, then un-translate all the rotated object coordinates. In other words it can't be done with just a rotation matrix. – martineau Aug 14 '14 at 22:33

0 Answers0