#### Archived

This topic is now archived and is closed to further replies.

# Tracking a point on a model

This topic is 5404 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hey fokes, as a few of you may know I''m trying to create a spaceshooter. I need some help on getting something working right. I''m trying to set up the players craft so the shots come from predefined points on the ship, or gunports. Right now they come from the center of the player, and when I try to set a location to fire from it works oddly. I''ll try to explain. Say as if I set the gunport at -1,-1, 3. I can get the rotations and such to match so it fires straight, but the shot comes from a location that is relitive to the globel x,y,z axis. So when I turn in a direction the gunport doesn''t move with the ship. The world location does but it fires from the globel location. if anyone understands this (or even the way I wrote it) please give me an idea on what I am missing. I had thought about creating a model for the player that isn''t drawn and tracking points to fire from but I''m not sure that''s possible.

##### Share on other sites
I didn''t read the explanation in your post too thoroughly, but (based on the rest of your post) if you''re using a static model (such as a space ship) that has its guns always fixed in the same position relative to the origin of the model, you could translate to the model''s origin, add the ship''s rotations on each axis and then translate to the guns themselves. Use several nested glPushMatrix() and glPopMatrix() calls to make your job easier.

Once you''ve translate to the mouth of the gun, you add its rotation relative to the ship''s forward heading and you''ve go yourself the point where the projectiles come from and the direction in which they travel (this way you can have guns that can be adjusted (aimed) without rotating the ship itself).

If you''re having a hard time understanding this, don''t hesitate to ask.

##### Share on other sites
I think I understand, but could you explain a little more clearly. I tried to translate the players rotations to the model and then add the starting location. That didn''t work very well. Could you give a better explaination of the useage of a model and how would I track the points to use?

##### Share on other sites
Sure.

Let's bring a 2D example with the following shape:

   ***** ***   *** | * o * |   *   *    ***

"o" is the origin of the spaceship (a pivot that is always relative to the (0, 0, 0) position (origin) of the world). Let's suppose each letter placeholder is one unit in gl space and that the straight lines are turrets. In that case the left gun has an offset of (-3, 0) relative to "o" and the right gun has an offset of (3, 0) relative to "o".

When you draw your scene, you "reset the world" (the modelview matrix) by calling glLoadIdentity(). At this point your translation matrix has no practical meaning (it points to (0, 0, 0)). If you now translate to "o", you're at the origin of the ship. If the ship is rotated in any way, you need to add the rotations now. Now all of the points on the ship are rotated (more precisely, it's the modelview matrix that is rotated). If you now translate (-3, 0), you'll end up at the tip of the left turret (left on the ASCII image above). If you then want to get to the right turret, you'll have to either 1) make up for the previous translation, eg translate (6, 0), or 2) memorize the modelview matrix prior to your last translation and translate (3, 0).

Let's covert this to pseudo code:
LoadIdentity();PushMatrix(); Translate(Ship_Origin); Rotate(Ship_Rotation_on_all_axis); Render(Ship);  PushMatrix();  Translate(-3, 0); //the left turret  Rotate(Left_Turret_Rotation);  //wanna rotate your turrets as well?  Spawn(Projectile); PopMatrix();  PopMatrix();  Translate(3, 0); //the right turret  Rotate(Right_Turret_Rotation);    Spawn(Projectile); PopMatrix();PopMatrix();//now the modelview matrix is centered back on the world origin

Did that make it clearer?

edit: formatting

[edited by - crispy on September 2, 2003 6:16:46 PM]

##### Share on other sites
I apoligize for my ignorance but exactly what are the glPushMatrix() and glPopMatrix() for?

##### Share on other sites
So let me get this straight.

I need to first translate the ship, then push matrix and translate the shot, poping then translates to the ship, then popping again moves the ship the the location in the world.

Does this apply to a first person system?

[edited by - Steelrose on September 2, 2003 6:57:30 PM]

##### Share on other sites
Do you know what a stack is? Anyway - a stack is mostly a FIFO or FILO strcuture (FIFO - first-in-first-out, FILO - first-in-last-out). OpenGL allows you to push the currently selected matrix into a FILO stack. As a rule items are _pushed_ into a stack and _popped_ from the stack. Hence, when you call glPushMatrix(), you store the current modelview matrix (world rotation/translation/scale) in an internal stack. When you call another glPushMatrix() right after it, you''ll push the first matrix a notch deeper and to access it again, you''d now have to call glPopMatrix() twice.

What this means is that - when you call glTranslatef(), glScalef() or glRotatef(), you modify the current modelview matrix (you morph the world).

Usually you want to add many objects to your scene, but you don''t always want to calculate the necessary coordinates for each of the vertices. Instead you know one point that you have a very good understanding where it has to reside in the virtual world relative to the absolute origin (the (0, 0, 0) after you call glLoadIdentity()). In that case, you use glTranslatef() to move to that location and render the object there. By this you, however, modify the modelview matrix (you move the entire world to a new location). To draw another object elsewhere in the scene using the same method, you''ll need to know where the previous "world" was. This is what glPushMatrix() and glPopMatrix() do. Strange to thing that everything in your little virtual world is described by a puny 4x4 matrix, isn''t it?

##### Share on other sites
quote:
Original post by Steelrose
I need to first translate the ship, then push matrix and translate the shot, poping then translates to the ship, then popping again moves the ship the the location in the world.

0) push the matrix
1) you translate to the ship''s coordinates
2) push the matrix
4) popping now takes you back to the ship''s coordinates (1)
5) popping one more time takes you back to the world origin (0)

quote:

Does this apply to a first person system?

this applies to any system.

##### Share on other sites
Thanks! I get it now I still want to do some reading to find out a little more I think I read some of this on the OpenGL Red Book. Don''t know where though, I''m still going through it anyway, thanks for the help again.

##### Share on other sites
Ok, I''ll give this a shot and see if I can get it to work correctly. Thanks.

• 11
• 19
• 12
• 34
• 9
• ### Forum Statistics

• Total Topics
631398
• Total Posts
2999837
×