Python OpenGL glRotatef does not rotate the correct amount
Question:
I use Pycharm and coupled OpenGL with pygame.
I have a problem with the OpenGL method glRotatef. It seems like it does not rotate the right amount. I have a class which has the 3-dimensional coordinates xPos,yPos,zPos. The class is drawn with this method:
def draw(self):
OpenGL.GL.glPushMatrix()
OpenGL.GL.glTranslate(self.xPos, self.yPos, self.zPos)
OpenGL.GL.glRotatef(self.remindturn, 0, 1, 0)
OpenGL.GL.glColor3f(1,0,0)
OpenGL.GL.glBegin(OpenGL.GL.GL_TRIANGLES)
OpenGL.GL.glVertex3f(self.xPos-100, self.yPos+50, self.zPos)
OpenGL.GL.glVertex3f(self.xPos + 100, self.yPos + 50, self.zPos)
OpenGL.GL.glVertex3f(self.xPos, self.yPos -50, self.zPos)
OpenGL.GL.glEnd()
OpenGL.GL.glPopMatrix()
remindturn is defined by this method:
def remindtheturn(self):
if self.keypressed == True :
self.remindturn = self.mouseturnX
keypressed is defined by this method:
def feel(self , event):
if event.type == pygame.KEYDOWN :
if event.key == pygame.K_w :
self.keypressed = True
if event.type == pygame.KEYUP :
if event.key == pygame.K_w :
self.keypressed = False
mouseturnX and mouseturnY are defined that way:
def mouseturn(self):
mouse_pos = pygame.mouse.get_pos()
if mouse_pos[0] > 250 :
self.mouseturnX = (mouse_pos[0] - 250) * 0.1
if mouse_pos[0] < 250 :
self.mouseturnX = (-250 + mouse_pos[0])*0.1
if mouse_pos[1] > 250 :
self.mouseturnY = -(mouse_pos[1] -250) *0.5
if mouse_pos[1] < 250 :
self.mouseturnY = -(-250 + mouse_pos[1])*0.5
This all leads to the camera method. The camera has the coordinates xCam, yCam, and zCam.
def camera(self):
self.xCam = math.sin(self.mouseturnX) * self.camdist + self.xPos
self.yCam = self.yPos + self.mouseturnY
self.zCam = math.cos(self.mouseturnX) * self.camdist + self.zPos
OpenGL.GL.glViewport(0,0, 500, 500)
OpenGL.GL.glMatrixMode(OpenGL.GL.GL_PROJECTION)
OpenGL.GL.glLoadIdentity()
OpenGL.GLU.gluPerspective(60, (500/500), 0.1, 10000.0)
OpenGL.GL.glMatrixMode(OpenGL.GL.GL_MODELVIEW)
OpenGL.GL.glLoadIdentity()
OpenGL.GLU.gluLookAt(self.xCam, self.yCam, self.zCam, self.xPos, self.yPos, self.zPos, 0, 1, 0)
Following maths, the drawn object should always face the camera, when the key w is pressed. But it does not do so. It rotates, but not in the right amount.
I tried multiplying remindturn with 45, 90, 180, 270 and 360 and other famous angle numbers, nothing worked. I even translated the object back to the origin after rotating it, it did not help. I tried tan and atan as functions on the numbers I mentioned before, I even tried Pi. I do not know what is wrong with this code. I used the math behind this in Processing long ago, there it worked completely fine.
Answers:
It needs a multiplicator for remindturn = mouseturnX*multiplicator. The multiplicator is 56.5. It is not perfect, but it works.
I use Pycharm and coupled OpenGL with pygame.
I have a problem with the OpenGL method glRotatef. It seems like it does not rotate the right amount. I have a class which has the 3-dimensional coordinates xPos,yPos,zPos. The class is drawn with this method:
def draw(self):
OpenGL.GL.glPushMatrix()
OpenGL.GL.glTranslate(self.xPos, self.yPos, self.zPos)
OpenGL.GL.glRotatef(self.remindturn, 0, 1, 0)
OpenGL.GL.glColor3f(1,0,0)
OpenGL.GL.glBegin(OpenGL.GL.GL_TRIANGLES)
OpenGL.GL.glVertex3f(self.xPos-100, self.yPos+50, self.zPos)
OpenGL.GL.glVertex3f(self.xPos + 100, self.yPos + 50, self.zPos)
OpenGL.GL.glVertex3f(self.xPos, self.yPos -50, self.zPos)
OpenGL.GL.glEnd()
OpenGL.GL.glPopMatrix()
remindturn is defined by this method:
def remindtheturn(self):
if self.keypressed == True :
self.remindturn = self.mouseturnX
keypressed is defined by this method:
def feel(self , event):
if event.type == pygame.KEYDOWN :
if event.key == pygame.K_w :
self.keypressed = True
if event.type == pygame.KEYUP :
if event.key == pygame.K_w :
self.keypressed = False
mouseturnX and mouseturnY are defined that way:
def mouseturn(self):
mouse_pos = pygame.mouse.get_pos()
if mouse_pos[0] > 250 :
self.mouseturnX = (mouse_pos[0] - 250) * 0.1
if mouse_pos[0] < 250 :
self.mouseturnX = (-250 + mouse_pos[0])*0.1
if mouse_pos[1] > 250 :
self.mouseturnY = -(mouse_pos[1] -250) *0.5
if mouse_pos[1] < 250 :
self.mouseturnY = -(-250 + mouse_pos[1])*0.5
This all leads to the camera method. The camera has the coordinates xCam, yCam, and zCam.
def camera(self):
self.xCam = math.sin(self.mouseturnX) * self.camdist + self.xPos
self.yCam = self.yPos + self.mouseturnY
self.zCam = math.cos(self.mouseturnX) * self.camdist + self.zPos
OpenGL.GL.glViewport(0,0, 500, 500)
OpenGL.GL.glMatrixMode(OpenGL.GL.GL_PROJECTION)
OpenGL.GL.glLoadIdentity()
OpenGL.GLU.gluPerspective(60, (500/500), 0.1, 10000.0)
OpenGL.GL.glMatrixMode(OpenGL.GL.GL_MODELVIEW)
OpenGL.GL.glLoadIdentity()
OpenGL.GLU.gluLookAt(self.xCam, self.yCam, self.zCam, self.xPos, self.yPos, self.zPos, 0, 1, 0)
Following maths, the drawn object should always face the camera, when the key w is pressed. But it does not do so. It rotates, but not in the right amount.
I tried multiplying remindturn with 45, 90, 180, 270 and 360 and other famous angle numbers, nothing worked. I even translated the object back to the origin after rotating it, it did not help. I tried tan and atan as functions on the numbers I mentioned before, I even tried Pi. I do not know what is wrong with this code. I used the math behind this in Processing long ago, there it worked completely fine.
It needs a multiplicator for remindturn = mouseturnX*multiplicator. The multiplicator is 56.5. It is not perfect, but it works.