[PyOpenGL-Users] recommendation for setting correctly the new direction of an object
Brought to you by:
mcfletch
|
From: Roland E. <r.e...@gm...> - 2010-08-02 20:57:51
|
Hi,
I am currently writing a third person view space shooter where the
player can freely fly in the "map". I want to define an arcade flying
model like in most space shooter or spaceship simulation, but I face the
problem of specifying the transformation right.
For the moment, my scene contains the player space ship pointing toward
the negative z axis, with the camera a little bit behind it. In front of
the ship there are 2 objects. When I apply some thrust to the player
spaceship it goes forward, but if I turn the ship, it is not changing
direction, while the ship and the camera are turning.
My questions are the following:
1. Given the code below, why have I to call the method
self.__chasingView() after rendering the player's ship?
2. How to change the direction of the ship? Am I missing some glRotate()
functions somewhere in the code or should I change the code of method
setNewPosition() in such a way that the position of the ship is computed
on all 3 axis?
Below is the main code rendering the scene:
glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity(); # /* clear the matrix */
playerShipPos = self.__player.getPosition()
x, y, z = self.__player.getAngle()
self.__lightVP.switchOn()
glTranslate(0.0, -3.0, -20.0)
glRotate(10.0, 1.0, 0.0, 0.0)
self.__player.render()
self.__chasingView(playerShipPos[0], playerShipPos[1],
playerShipPos[2], x, y, z)
for entity in self.__entities:
self.__updateRadar(entity)
glPushMatrix()
entity.render()
glPopMatrix()
glFlush ()
- Method render() for both self.__player and entity is:
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL)
#Load vertex List and Normals List
self.vbo_vert.bind()
glEnableClientState(GL_VERTEX_ARRAY)
glVertexPointerf(self.vbo_vert) # Specify the vertex list to be
used to draw the model
self.vbo_vert.unbind()
self.vbo_norm.bind()
glEnableClientState(GL_NORMAL_ARRAY)
glNormalPointerf(self.vbo_norm) # Specify the normal list to be
used to draw the model
self.vbo_norm.unbind()
# Activate face culling
glEnable(GL_CULL_FACE)
glCullFace(GL_BACK)
# Apply transformation
if not(self.isPlayer):
glTranslate(*self.position)
glRotatef (self.x_angle, 1.0, 0.0, 0.0);
glRotatef (self.y_angle, 0.0, 1.0, 0.0);
glRotatef (self.z_angle, 0.0, 0.0, 1.0);
start_pos = 0
# In For .. Loop
for obj in self.modelData:
# Set Object Material
glMaterialfv(GL_FRONT, GL_SPECULAR, obj.matdata.specular)
glMaterialfv(GL_FRONT, GL_SHININESS, obj.matdata.specularCoeff)
glMaterialfv(GL_FRONT, GL_AMBIENT, obj.matdata.ambient)
glMaterialfv(GL_FRONT, GL_DIFFUSE, obj.matdata.diffuse)
# Draw object
glDrawArrays(GL_TRIANGLES, start_pos, obj.nbrVertices)
start_pos += obj.nbrVertices
glDisableClientState(GL_VERTEX_ARRAY)
glDisableClientState(GL_NORMAL_ARRAY)
# vbo class usage
The function I use to set the new location of the ship based on the
current thrust is:
def setNewPosition(self):
self.addToPosition(z=-(self.thrust * self.tick))
The above method is called in the main loop before the rendering of the
scene.
The self.__chasingView() method is:
def __chasingView(self, planex, planey, planez, pitch, heading, roll):
glRotatef(roll, 0.0, 0.0, 1.0)
glRotatef(heading, 0.0, 1.0, 0.0)
glRotatef(pitch, 1.0, 0.0, 0.0)
glTranslate(-planex, -planey, -planez)
That code comes from the opengl programmer's guide with a few changes to
adapt to the fact that the player's ship is looking down the negative Z
axis.
Hope this lengthy email doesn't break any rules of this mailing list.
Thanks for your help,
Roland.
|