Reputation: 63
I'm using pyopengl and glut for this project. I just added a mouse event handler in order to rotate the object. What it does is when the cursor reaches the edge of the window, it rotates. However, when I move the cursor back to the center of the window, the object starts to keep zooming in and out alternatively. I checked and I'm sure that I didn't make that feature.
I tried to modify the event handler. What I found out is that the speed that the object zooms in and out is the rotating speed that I put into glRotatef()
. The object moves in and out of the screen like a pendulum, whose speed is proportional to a sin()
. I just found out that the object's speed is proportional to the sin()
of the rotating speed that I put into glRotatef()
.
this is my mouse event handler
def mouse_passive_motion(self, *args):
print(args)
# rotate left
if args[0] < self.OFFSET_TO_CHANGE_ANGLE:
self.yaw = True
self.yaw_rotate_speed = self.ROTATION_SPEED
else:
self.yaw = False
self.pitch_rotate_speed = 0
# rotate right
if args[0] > self.WINDOW_DIMENSIONS[0]-self.OFFSET_TO_CHANGE_ANGLE:
self.yaw = True
self.yaw_rotate_speed = -self.ROTATION_SPEED
else:
self.yaw = False
self.pitch_rotate_speed = 0
this is my display function, which is called in the glut main loop.
def display():
global input_manager
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
glMatrixMode(GL_MODELVIEW)
glLoadIdentity()
glTranslatef(0.0, -WINDOW_DIMENSIONS[1]/2, -3500)
input_manager.update_room_position()
glTranslatef(input_manager.xTranslate, input_manager.yTranslate, input_manager.zTranslate)
glRotatef(input_manager.yawSpinAngle, 0, input_manager.yaw, 0)
draw_a_room()
glFlush()
glutSwapBuffers()
I don't know if these codes are enough to identify and fix the problem or not. If anyone needs any additional part of the program please tell me, I'll add that in. Can someone help me to make the object stops rotating and not zooming in and out when the cursor moves back to the center of the window?
Upvotes: 0
Views: 65
Reputation: 63
I don't know how this makes any difference but it just solved the problem.
In display()
, instead of glRotatef(input_manager.yawSpinAngle, 0, input_manager.yaw, 0)
, replace input_manager.yaw
with True
. Whether the object is rotating or not is now just determined by input_manager.yaw_rotate_speed
.
I just don't understand why because input_manager.yaw
is also a boolean variable, which means it shouldn't make any difference to just put True
there.
Upvotes: 0