Matrix trouble......

Okay, so I’m good in math but I dont understand Opengl that great…

I’m currently working on a battleship game in OpenGL. I want to make it so that you can place different ships (that are 3-d) onto a 3-d grid. I have the ships already made with specific points for each vertex. I know I can move around the ship with glTranslatef and glRotatef but what happens if I want one and only one ship to sink???

I have a method that draws out all the polygons for the ship. Is there a way to load all those points into a specified matrix??? If so, I can solve this easily.

glPushMatrix and glPopMatrix are your key (push, transform the ship, pop, go onto the next ship, repeat)…each time you pop, you go back to the state the matrix was when you pushed

The typical way to think of this is with a “hierarchy” of transforms. This is basically the famous “robot arm” demo in the Red Book, which sounds like is something you need to get/read. It’s one of the best ways to learn OGL.


I have the red book and have looked at the robot arm example. I understand it…sort of. Will this solve my probs? I need something that keeps track of specific objects. Thank you for the help so far!!!

The two ideas are principly:

  1. there is a hierarchy of coordinate systems

  2. each logical object has it’s own coordinate system

For example, you can have a “global” coordinate system where you place your ships independently of each other. Each ship has it’s own local coordinate system, and maybe things like turrets (or whatever ship-parts you fancy)are placed in that local system.

The relationship between coordinate systems are specified in your transformations. For example, each ship is probably placed via a rotation and a translation in your global coorindate space.

Now think about the hierarchy of coordinate systems. It’s shaped like a tree. On top is the “root” or global coordinate system. Beneath that are each of the ship coordinate systems. If you have turrets, each turret coorindate system has a transform relative to the ship coordinate system. This kind of organization is a simple “scene-graph.” Each node in the tree represents a coorindate system (and associate geometry) and the edges represent transformation.

The way you draw your ships, then, is simply by traversing the tree, depth-first. Whenever you go down an edge, use PushMatrix and apply the relative tranformation. Whenever you go up an edge, simply PopMatrix. You probably don’t have to worry too much about overflowing your modelview stack, but you might want to glGet the depth just in case.

You might be able to make some fairly drastic simplifications to what I described (which is probably pretty complicated to someone who doesn’t already know what I’m talking about, especially without helpful diagrams). For example, if all you care about are ships (no turrets) floating in global space, you just need to maintain a per-ship tranformation which you LoadMatrix in before you draw the ship. This way, you have independent control over the ship transformations.


Okay, this is my last post…
Thank you for all the help.

I understand what you’re trying to explain in your post but I need to know one thing. When I say Push Matrix after transforming does that create an individual coord. sys. where only the things in that coord. sys. are manipulated until I say PopMatrix??? If this is the case I’m well on my way…thank you again.