Python 轨迹球不';我不能正确地转弯

Python 轨迹球不';我不能正确地转弯,python,python-3.x,opengl,pygame,pyopengl,Python,Python 3.x,Opengl,Pygame,Pyopengl,我试图通过引用这个源代码和 但是在得到旋转轴后,它似乎没有正确地转动 下面是我的一些代码片段 def compute_z(x, y): # compute z from sphere model # sphere size = 1 z = math.sqrt(abs(1 - math.pow(x,2) - math.pow(y,2))) return z def get_rotation_axis(vect_1, vect_2): # determine

我试图通过引用这个源代码和

但是在得到旋转轴后,它似乎没有正确地转动

下面是我的一些代码片段

def compute_z(x, y):
    # compute z from sphere model
    # sphere size = 1
    z = math.sqrt(abs(1 - math.pow(x,2) - math.pow(y,2)))
    return z

def get_rotation_axis(vect_1, vect_2):
    # determine rotation direction
    axis = np.cross(vect_1, vect_2)
    return axis
这是主要的

    while True:
        mouse_pos = pygame.mouse.get_pos()
        
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                quit()
            if event.type == pygame.MOUSEMOTION:
                if arcball_on:
                    cur_mx = mouse_pos[0]
                    cur_my = mouse_pos[1]
                    last_conv = convert_range(last_mx, last_my)
                    cur_conv = convert_range(cur_mx, cur_my)
                    a = (last_conv[0], last_conv[1], compute_z(last_conv[0], last_conv[1]))
                    b = (cur_conv[0], cur_conv[1], compute_z(cur_conv[0], cur_conv[1]))
                    angle = compute_angle(a, b)
                    axis = get_rotation_axis(a, b)
                    print(axis)
                    glRotatef(angle, axis[0], axis[1], -axis[2])

通常,您的代码工作正常。根据您编写的引用,您的
compute_z
函数可能如下所示:

def compute_z(x, y):
    # compute z from sphere model
    op_squared = x ** 2 + y ** 2
    r_squared = SPHERE_R ** 2
    if op_squared > r_squared / 2:
        z = r_squared / 2 / math.sqrt (op_squared)
    else:
        z = math.sqrt(r_squared - op_squared)
    return z
或者更简单:

def compute_z(x, y):
    # compute z from sphere model
    op_squared = x ** 2 + y ** 2
    r_squared = SPHERE_R ** 2
    if op_squared > r_squared:
        return 0
    else:
        return math.sqrt(r_squared - op_squared)
其中,
SPHERE\u R
是假设球体的半径(默认值为1),因为当鼠标在球体外单击时,可能会发生奇怪的事情,而第一个代码将形状近似为双曲线图(如下)和第二个如下

检查时,正确计算了
角度

此外,旋转矢量应标准化:

def normalize(x):
    x = np.asarray(x)
    if np.linalg.norm(x):
        return x / np.linalg.norm(x)
    else:
        return x

# .....

a = (last_conv[0], last_conv[1], compute_z(last_conv[0], last_conv[1]))
b = (cur_conv[0], cur_conv[1], compute_z(cur_conv[0], cur_conv[1]))
a = normalize(a)
b = normalize(b)
axis = get_rotation_axis(a, b)
axis = normalize(axis)
glRotatef
功能工作正常(您也可以更改x轴的方向,但它仍然工作正常

当您执行大量旋转时,您将看到轴不是按常识放置的,因为这些旋转,但是当您使用鼠标小心地缓慢地向上/向下移动左/右时,您将看到轴已旋转,但立方体仍在按其应有的方式旋转

有一个技巧可能会改变你正在旋转的东西和它的使用。这样旋转是在对象坐标中进行的。我粘贴了描述,但遵循上面的细节

另一个技巧是将旋转轴从摄影机坐标转换为对象坐标。当摄影机和对象的位置不同时,这很有用。例如,如果在Y轴上将对象旋转90°(将其头部向右旋转),然后使用鼠标执行垂直移动,在相机X轴上旋转,但它应该成为对象Z轴上的旋转(平面桶滚动)。通过在对象坐标中转换轴,旋转将尊重用户在相机坐标(WYSIWYG)中的工作.为了将摄像机坐标转换为物体坐标,我们取MV矩阵的逆矩阵(来自MVP矩阵三元组)


太好了,我没有意识到它是以绝对位置的方式转动的。这就是为什么它看起来转动不正确的原因