Jump to content
  • Advertisement
Sign in to follow this  
industrion

OpenGL gluUnproject with worms-style game in 3D space

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi. I'm making a 2D game - a mixture of Soldat and Worms, if you like - and I need to use gluUnproject or an equivalent method to get a mouse position into 3D. I've tried *countless* implementations of gluUnproject featured on forums, websites etc.. I'm making the game in 3D space, not orthographic view, for a number of reasons... but that means I'm having to use gluUnproject (or a similar method) to obtain a mouse position that can interact with the game world (for a targeting reticle, level editing, etc.). What's happening so far is: I'm drawing a small quad as a marker in 3D space, where the generated 3D coordinates (from the mouse) are. They are equal to the mouse position when the mouse cursor is at the center of the window, but when I move outwards in any direction, the marker quad moves out by a larger factor, as if its position is being multiplied or something. Everything in my game is at a depth of 0.0. This means that gluLookAt only ever has to move in the X and Y axes to follow the player. The lookatX and lookatY parameters are therefore just the same as the X and Y position of the camera. The Z position is determined from a variable g_fZoom, controlled by me just now, but by the game in future. I should mention that I thought a ray intersection test wasn't necessary for my situation, because it doesn't matter when the Z position 3D mouse coordinates differs from the Z position of something I'm testing collision with - because the game's 2D, and everything's at the same depth. I also tried setting the third parameter of gluUnproject - windowZ - to 1.0, and then to 0.0. When it's at 0.0, the quad barely moves with respect to the mouse cursor position. When windowZ is at 1.0, the quad does what I previously described - it moves similarly to the mouse cursor, but amplified. I won't paste much code, because, seriously, what I've got is the same as any other standard implenentation, so I wondered if, perhaps, the problem is being caused by other OpenGL parameters? My gluPersective is like so: gluPerspective( 45.0f, width/height, 0.1f, 1000 ); I have a feeling that when the camera (gluLookAt) is at a Z position of 0 (i.e. the same depth as the game entities), the position of the marker quad properly reflects the mouse position. I can't fully test this, though, because, if I'm at a depth of 0, I can't see what's going on. Something else I noticed was: - If the gluUnproject windowZ argument is 1.0, and the cursor X position is at the leftmost point on the window, the 3D x position is approximately -554, and if the X position is at the rightmost point on the window, the 3D x position is approximately 554. Similarly, the results for the y axis are -412 and 412. - If the windowZ argument is 0.0, these results come out as +/- 0.0555 and +/- 0.0412. And one more thing: I'm do have a good idea about glReadPixels and what it does, but I'm not entirely sure. It may help a diagnosis if I were to mention that I have a backround drawn first, but at the same depth as everything else. A huge post, I know, but I didn't think I'd ever find a standard solution anywhere on the net, since nearly everyone requires 2D to 3D mouse coordinate conversion for 'real' 3D graphics. I couldn't quite tell if the problem was something to do with my pseudo-2D situation, or what. Any ideas? EDIT: Just as I finished typing this, I realised that I'm getting the result of glReadPixels mixed up with Z position in 3D world coordinates. Will my objects at a Z position of 0 - trees, terrain, players etc. - be given, and therefore return, different Z depths, even though their Z positions all overlap?

Share this post


Link to post
Share on other sites
Advertisement
I decided to use a slightly different fix, for the time being.

Now, I'm snapping the real cursor position to the center of the client window, and calculating the cursor position delta every time the mouse is moved from that position. Then, I alter the '3D' cursor's position by a fraction or a factor of that point. It seems to work accurately enough, and it's very easy to change the 'mouse sensitivity' for the game.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!