0

I was wondering how to yaw gear object about Y-axis in vpython. I have been following some tutorials and I have managed to rotate the gear. The gear rotates about Z axis in vpython. I want the gear to remain rotating about Z axis but I would like to yaw the gear around Y-axis. When xxx.rotate(axis=vector(0,0,1), it works as it should do. As soon as I alter anything it goes crazy. I am not sure what xxx.rotate(origin) is meant to do. In my case, I have messed around with the origin vector but it doesn't make any sense to me. I am not sure if the origin vector expects parameter values in x,y,z or some other sequence. I hope I am making makes sense.

The code below is a working code and you can see what I am trying to do here. I have been debugging for a while so please ignore unnecessary comments.

'

# https://www.youtube.com/watch?v=0Sw8sxsN984
# https://www.youtube.com/watch?v=MJiVtz4Uj7M

from vpython import *
import numpy as np

axisOpac = 0.4

def makeAxes(length):
    
    global axes

    xaxis = arrow(pos=vector(0, 0, 0), axis=length*vector(1,0,0), shaftwidth = 0.1*(length), color=color.red, opacity = axisOpac)
    negXaxis = xaxis.clone()
    negXaxis.axis *= -1

    yaxis = arrow(pos=vector(0, 0, 0), axis=length*vector(0,1,0), color=color.green, opacity = axisOpac)
    negYaxis = yaxis.clone()
    negYaxis.axis *= -1

    zaxis = arrow(pos=vector(0, 0, 0), axis=length*vector(0,0,1), color=color.magenta, opacity = axisOpac)
    negZaxis = zaxis.clone()
    negZaxis.axis *= -1    

    fudge = 0.01*length
    xlabel = label(text = "x", color = xaxis.color, pos=xaxis.axis+vector(0, fudge, 0),box = False)
    ylabel = label(text = "y", color = yaxis.color, pos=yaxis.axis+vector(0, fudge, 0),box = False)
    zlabel = label(text = "-z", color = zaxis.color, pos=zaxis.axis+vector(0, fudge, 0),box = False)
    
    # To make axes visible or invisible
    axes = [xaxis, negXaxis, yaxis, negYaxis, zaxis, negZaxis, xlabel, ylabel, zlabel]

    return

makeAxes(1)

g = shapes.gear(n = 20)

gear1 = extrusion (path = [vector(0, 0, 0), vector(0, 0, 0.1)], shape=g, opacity = 0.4)
# gear1.axis = vector(0, 0, 0)

debugArrow=arrow(length=6,shaftwidth=.1,color=color.white,axis=vector(0,0,0))
gearAxisArrow=arrow(shaftwidth=.1, color=color.red)

inputYawAngleYaxis = 0 # int(input("Enter an angle: "))

while True:
    for cir in np.arange(0, 2*np.pi, 0.01):
        rate(10)
        # sleep(1)

        pitch=0
        # k = vector(cos(yaw)*cos(pitch), sin(pitch),sin(yaw)*cos(pitch))  ''' with pitch input '''
        k = vector(cos(cir), 0 ,sin(cir))  # ''' without pitch input '''
        y=vector(0,0,0)
        s=cross(k,y)
        v=cross(s,k)



        rad = radians(45)
        # gear1.axis = vector(1.23, -1.77418, 0) #vector(cos(inputYawAngleYaxis * pi/180), sin(pitch),sin(inputYawAngleYaxis* pi/180)*cos(pitch))
        # gear1.pos = vector(-2.94957, 0, 2.70185)
        
        # Gear Pos0:  <0.707107, 0, 0.707107>  Gear Axis0:  <2.15647, 0.101243, -0.00237659>
        gear1.pos = vector(0.707107, 0, 0.707107)
        # gear1.pos =  debugArrow.axis #frontArrow.axis # k # vector(0.707107, 0, 0.707107)
        gear1.rotate(angle = -0.1, axis = vector(0, 0, 1), origin = vector(0, 0, 1 ))
        # gear1.axis = vector(cos(radians(-45)), 0, sin(radians(-45))) 

        ''' DebugArrow represents the position of the gear's center '''
        debugArrow.axis = vector(cos(rad), 0, sin(rad))


        ''' Arrow inside the gear to track rotational axis of gear - This is to debug the yaw issue facing with gear object'''
        gearAxisArrow.pos = gear1.pos
        gearAxisArrow.axis = gear1.axis # vector(cos(radians(-45)), 0, sin(radians(-45))) # gear1.axis
        gearAxisArrow.length = gear1.length/2
        
            ## Debugg 
        # print("Front Arrow Axis: ", frontArrow.axis, " K vec: ", k, " Ball pos: ", ball.pos )
        print("Gear Pos0: ", gear1.pos, " Gear Axis0: ", gear1.axis, "gearAxisArrow: ", gearAxisArrow.pos, "gearAxisArrow: ", gearAxisArrow.axis)

'

1 Answer 1

0

I don't understand "I want the gear to remain rotating about Z axis but I would like to yaw the gear around Y-axis". If by "yaw" you mean that the axis of the gear should rotate about the y axis (while the gear itself rotates about that rotating axis), then obviously the axis of the gear can't remain in the direction of the z axis. Instead, the axis of the gear has to rotate. If it's of any help, here is the relevant page of the documentation:

https://www.glowscript.org/docs/VPythonDocs/rotation.html

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.