问题描述
这里是绘制功能,可绘制汽车零件,在此功能中,检查了汽车轮辋并检查了标志,并且在移动汽车时我需要旋转轮胎轮辋.由于轮辋已旋转但从汽车模型中取出,所以当我按向上箭头键,但汽车确实移动时,某事无法正常工作.
Here is the draw function which draws the parts of the car, in this function car rims is checked and flag is checked, and i need to rotate the tire rim as i move the car. Something is not working since the rims are rotated but taken out from the car model, when i press up arrow key, but the car does move.
我还在初始化函数中初始化了self.fFlag ="false":
I also initialized self.fFlag = "false" in initialize function:
def on_draw(self):
# Clears the screen and draws the car
# If needed, extra transformations may be set-up here
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
for name in self.parts:
colors = self.colors
color = colors.get(name, colors["default"])
glColor3f(*color)
if (name == 'Front Driver tire rim') & (self.fFlag == "true"):
bodyFace = self.mini.group(name)
glPushMatrix()
glRotatef(45,1,0,0)
# Drawing the rim
for face in bodyFace:
if len(face) == 3:
glBegin(GL_TRIANGLES)
elif len(face) == 4:
glBegin(GL_QUADS)
else:
glBegin(GL_POLYGON)
for i in face:
glNormal3f(*self.mini.normal(i))
glVertex3f(*self.mini.vertex(i))
glEnd()
glPopMatrix()
self.fFlag == "false"
else:
bodyFace = self.mini.group(name)
for face in bodyFace:
if len(face) == 3:
glBegin(GL_TRIANGLES)
elif len(face) == 4:
glBegin(GL_QUADS)
else:
glBegin(GL_POLYGON)
for i in face:
glNormal3f(*self.mini.normal(i))
glVertex3f(*self.mini.vertex(i))
glEnd()
def on_key_release(self, symbol, modifiers):
"""Process a key pressed event.
"""
if symbol == key.UP:
# Move car forward
# TODO
glTranslatef(0,-1,0)
self.fFlag = "true"
self.on_draw()
pass
当我按下向上箭头键时,我试图使汽车轮辋旋转,从而使汽车向前移动.
Edited: I am trying to make the car rims to rotate when i press the up arrow key, which moves the car forward.
推荐答案
要将零件绕其中心旋转,您需要将其平移到原点,然后旋转,然后再平移回去.
In order to rotate a part about its own center, you need to translate it to the origin, rotate it, and translate it back.
所以你的
glRotatef(45,1,0,0) # rotate 45 deg about x axis (thru the world origin)
需要在翻译之前和之后进行翻译.
needs to be preceded and followed by translations.
请参见接受的答案这个问题.
这篇关于汽车opengl转换的旋转轮胎轮辋的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!