Rotate Cube to 'look at' mouse OpenGL python
I am working on making a proof of concept bubble popper game. So I need
the cannon to follow the mouse. I am currently trying to have a cube
rotate around the z axis to follow the mouse. I am using the code bellow
and it produces the results bellow that. The cannon sits in the middle of
the bottom of a 550 x 550 window. The results printed bellow the code are
when the mouse is at the lower right corner, the center of the window, and
the lower left. So I would anticipate the resulting angles to be -90, ~0,
90. But well not so much. This may turn out to be a programing issue or it
may turn out to be a math issue but I don't know what it is. I am pretty
sure the math works because I tested it outside of the mouse position and
it gave me the proper results. Can you see the problem?
I have also included the code I use to set up the window and drawing space.
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
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
###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
No comments:
Post a Comment