Thread: [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. |
From: Mike C. F. <mcf...@vr...> - 2010-08-03 15:25:14
|
On 10-08-02 04:57 PM, Roland Everaert wrote: > 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? > The problem you are facing is that you want to make the motion of the ship (x,y,z) modify by currentrotation * motionvector. You *could* do that with an ever-increasing set of glRotate/glTranslate calls (you'd need one for every change in direction), but what you really want to do is to track your position in "world" space and change that position by currentrotationmatrix * motionvector. I normally do that with a wrapped Quaternion class in OpenGLContext. See OpenGLContext/move/viewplatform.py for the code that wraps it (particularly the relativePosition() method, on which all of the various forward/sideways/up/down movements are built. http://bazaar.launchpad.net/~mcfletch/openglcontext/trunk/annotate/head:/OpenGLContext/move/viewplatform.py <http://bazaar.launchpad.net/%7Emcfletch/openglcontext/trunk/annotate/head:/OpenGLContext/move/viewplatform.py> > def setNewPosition(self): > self.addToPosition(z=-(self.thrust * self.tick)) > that should be something like self.addToPosition( self.thrust * self.tick * self.orientation ) HTH, Mike -- ________________________________________________ Mike C. Fletcher Designer, VR Plumber, Coder http://www.vrplumber.com http://blog.vrplumber.com |
From: Roland E. <r.e...@gm...> - 2010-08-03 20:35:10
|
Thanks for the example and the explanation, I will have a look at it asap. Roland. Le 08/03/10 17:01, Mike C. Fletcher a écrit : > On 10-08-02 04:57 PM, Roland Everaert wrote: >> 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? >> > The problem you are facing is that you want to make the motion of the > ship (x,y,z) modify by currentrotation * motionvector. You *could* do > that with an ever-increasing set of glRotate/glTranslate calls (you'd > need one for every change in direction), but what you really want to > do is to track your position in "world" space and change that position > by currentrotationmatrix * motionvector. > > I normally do that with a wrapped Quaternion class in OpenGLContext. > See OpenGLContext/move/viewplatform.py for the code that wraps it > (particularly the relativePosition() method, on which all of the > various forward/sideways/up/down movements are built. > > http://bazaar.launchpad.net/~mcfletch/openglcontext/trunk/annotate/head:/OpenGLContext/move/viewplatform.py > <http://bazaar.launchpad.net/%7Emcfletch/openglcontext/trunk/annotate/head:/OpenGLContext/move/viewplatform.py> >> def setNewPosition(self): >> self.addToPosition(z=-(self.thrust * self.tick)) >> > that should be something like self.addToPosition( self.thrust * > self.tick * self.orientation ) > > HTH, > Mike > -- > ________________________________________________ > Mike C. Fletcher > Designer, VR Plumber, Coder > http://www.vrplumber.com > http://blog.vrplumber.com > > > > ------------------------------------------------------------------------------ > The Palm PDK Hot Apps Program offers developers who use the > Plug-In Development Kit to bring their C/C++ apps to Palm for a share > of $1 Million in cash or HP Products. Visit us here for more details: > http://p.sf.net/sfu/dev2dev-palm > > > _______________________________________________ > PyOpenGL Homepage > http://pyopengl.sourceforge.net > _______________________________________________ > PyOpenGL-Users mailing list > PyO...@li... > https://lists.sourceforge.net/lists/listinfo/pyopengl-users > |
From: Roland E. <r.e...@gm...> - 2010-08-05 17:58:59
|
Mike, By reading the code you linked in your answer I am puzzle, and I don't really see how to integrate that concept into my own code without shamelessly copying some of your methods directly into my code and and adapt it :( So, have you any book recommendation that discuss such matter for someone that have not played with stuff like matrices for 15 years and never heard of quaternions until learning OpenGL? There is so much books on math for computer graphics, game development, physics, ai, ... that I don't know which one to choose. I should have maybe precised that self.thrust and self.tick are float, so I don't see how that will interact with self.orientation that is a quaternion if I have understand correctly. Thanks, Roland. Le 08/03/10 22:34, Roland Everaert a écrit : > Thanks for the example and the explanation, I will have a look at it asap. > > Roland. > > > Le 08/03/10 17:01, Mike C. Fletcher a écrit : >> On 10-08-02 04:57 PM, Roland Everaert wrote: >>> 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? >>> >> The problem you are facing is that you want to make the motion of the >> ship (x,y,z) modify by currentrotation * motionvector. You *could* >> do that with an ever-increasing set of glRotate/glTranslate calls >> (you'd need one for every change in direction), but what you really >> want to do is to track your position in "world" space and change that >> position by currentrotationmatrix * motionvector. >> >> I normally do that with a wrapped Quaternion class in OpenGLContext. >> See OpenGLContext/move/viewplatform.py for the code that wraps it >> (particularly the relativePosition() method, on which all of the >> various forward/sideways/up/down movements are built. >> >> http://bazaar.launchpad.net/~mcfletch/openglcontext/trunk/annotate/head:/OpenGLContext/move/viewplatform.py >> <http://bazaar.launchpad.net/%7Emcfletch/openglcontext/trunk/annotate/head:/OpenGLContext/move/viewplatform.py> >>> def setNewPosition(self): >>> self.addToPosition(z=-(self.thrust * self.tick)) >>> >> that should be something like self.addToPosition( self.thrust * >> self.tick * self.orientation ) >> >> HTH, >> Mike >> -- >> ________________________________________________ >> Mike C. Fletcher >> Designer, VR Plumber, Coder >> http://www.vrplumber.com >> http://blog.vrplumber.com >> >> >> >> ------------------------------------------------------------------------------ >> The Palm PDK Hot Apps Program offers developers who use the >> Plug-In Development Kit to bring their C/C++ apps to Palm for a share >> of $1 Million in cash or HP Products. Visit us here for more details: >> http://p.sf.net/sfu/dev2dev-palm >> >> >> _______________________________________________ >> PyOpenGL Homepage >> http://pyopengl.sourceforge.net >> _______________________________________________ >> PyOpenGL-Users mailing list >> PyO...@li... >> https://lists.sourceforge.net/lists/listinfo/pyopengl-users >> |