簡體   English   中英

從OpenGL中的Python旋轉多維數據集以“查看”鼠標

[英]Rotate Cube to 'look at' mouse from Python in OpenGL

我正在制作概念證明泡泡玩游戲,所以我需要大炮跟隨鼠標。 我目前正在嘗試使多維數據集繞z軸旋轉以跟隨鼠標。 我正在使用下面的代碼,它會在下面產生結果。 大炮位於550 x 550窗口底部的中間。 代碼下方顯示的結果是鼠標位於右下角,窗口中心和左下角。 因此,我預計最終的角度為-90, ~0, 90但是,不是很多。 這可能是編程問題,也可能是數學問題。 我非常確定該數學運算是否有效,因為我在鼠標位置之外進行了測試,從而得出了正確的結果。 你看到問題了嗎?

我還嘗試過先對向量進行歸一化,然后交換將哪些點放在第一位,它什么也沒做。

我還提供了用於設置窗口和繪圖空間的代碼。

    def cannon_rotation(self):
        vector1 = self.points_to_vector((self.width/2, 20), (self.width/2, 30))
        vector2 = self.points_to_vector((self.width/2, 20), self.mouse_location)
        print 'vector1', vector1
        print 'vector2', vector2
        a = self.angle(vector1, vector2)
        print a
        return a

    def points_to_vector(self, point1, point2):
        return point2[0] - point1[0], point2[1] - point1[1]

    def dot_product(self, vector1, vector2):
        return vector1[0] * vector2[0] + vector1[1] * vector2[1]

    def length(self, vector):
        return (self.dot_product(vector, vector)) ** .5

    def angle(self, vector1, vector2):
        dot_a_b = self.dot_product(vector1, vector2)
        len_a_b = (self.length(vector1)) * (self.length(vector2))
        angle = dot_a_b / len_a_b
        print 'dot_a_b', dot_a_b
        print 'len_a_b', len_a_b
        print 'angle', angle
        angle_in_degrees = acos(angle) * 180 / pi
        print angle_in_degrees
        return angle_in_degrees


    ###Create Window
    def reshape(self, height, width):
        if height >= 90 and width >= 90:
            self.index_location_dict = self.create_index_location_dict(height, width)
            self.height = height
            self.width = width
            glViewport(0, 0, height, width)
            glMatrixMode(GL_PROJECTION)
            glLoadIdentity()
            glOrtho(0.0, height, width, 0.0, -20.0, 20.0)
            glEnable(GL_DEPTH_TEST)
        else:
            self.game_over = True

結果:

Mouse Position (535, 536)
vector1 (0, 10) 
vector2 (260, 516) 
dot_a_b 5160 
len_a_b 5778.02734504 
result 0.893038348881
angle 26.7424369246

Mouse Position (276, 386) 
vector1 (0, 10) 
vector2 (1, 366) 
dot_a_b 3660 
len_a_b 3660.01366118 
result 0.999996267452
angle 0.15654545612

Mouse Position(9, 535) 
vector1 (0, 10) 
vector2 (-266, 515) 
dot_a_b 5150 len_a_b
5796.38680559 
result 0.888484528851
angle 27.316573085

(self.width/2, 20)指定頂部邊緣的中心。 您需要將其交換為(self.width/2, height - 20)

您無需以這種方式計算vector1 始終可以將其設置為(0, 1) (指向下方)。

此外,檢查您的投影矩陣:

glOrtho(0.0, height, width, 0.0, -20.0, 20.0)

您使用的是與我不同的OpenGL,或者混合使用了以下參數:

void glOrtho( GLdouble   left,  
              GLdouble   right,  
              GLdouble   bottom,  
              GLdouble   top,  
              GLdouble   nearVal,  
              GLdouble   farVal); 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM