"Just use gluUnProject(). It takes the current modelview matrix, projection matrix, viewport and screen coords and returns object coords. You get the point on the near plane if
you pass 0.0f for winz.
And invert your current y-screen-coord as following: viewport.bottom - y
Gero Gerber"
I have been using gluUnProject() and gluProject() to convert between mouse coordinates and world coordinates. It took me a long time to figure out how to get gluProject() to give me the correct windows coordinates at any rotation(full 3D rotation around X and Y axis), but I eventually figured out a way to do that. So my problem is this...
I''m trying to make a 3D space based RTS, similar to Homeworld or Fargate. I want to be able to move my units in the world the same way they do in these games. If you haven''t played either of these games, first you select the units you want to move, then you press ''M'' or whatever and you move the mouse around to select a coordinate in the X/Z plane for the unit to move to. By holding down SHIFT and moving the mouse you can change which Y coordinate the unit is supposed to go to. Once you click the mouse it combines the information to get one 3D coordinate and the unit begins to move.
What I need to know how to do is how to convert my mouse coordinates into world X and Z coordinates instead of X and Y coordinates. And of course it needs to work at any rotation.
Any help would be greatly appreciated.
Dracle
converting screen coordinates to 3D coordinates
//Grab Matrices and Viewport Values GLdouble model[16]; glGetDoublev ( GL_MODELVIEW_MATRIX, model); GLdouble projection[16]; glGetDoublev ( GL_PROJECTION_MATRIX, projection); GLint viewport [4]; glGetIntegerv (GL_VIEWPORT, viewport); //Read the depth buffer GLfloat depth; glReadPixels(mouse_x, mouse_y, 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT, &depth); //Unproject GLdouble worldX, worldY, worldZ; gluUnProject (mouse_x, mouse_y, &depth, model, projection, viewport, &worldX, &worldY, &worldZ);
An easy way to do a homeworld like control thing is to
1. convert your screen coordinates into world space (using gluUnProject or whatever you like)
2. trace a ray from the view point through the worldspace point corresponding to the mouse
3. intersect the ray with a plane - say a horizontal plane y=1
4. take the coordinates of that point, fix them and when the user moves the mouse up and down (holding shift or whatever) simply add an offset along the planes normal to that point.
1. convert your screen coordinates into world space (using gluUnProject or whatever you like)
2. trace a ray from the view point through the worldspace point corresponding to the mouse
3. intersect the ray with a plane - say a horizontal plane y=1
4. take the coordinates of that point, fix them and when the user moves the mouse up and down (holding shift or whatever) simply add an offset along the planes normal to that point.
Thanks invective and JuNC. I was able to get it working using invectine''s method after playing with things for a while. Its a simple solution that I can understand, and it works, so its perfect for the time being. However I would like to understand how to do what JuNC suggested and I''m guessing that his way is faster and possibly more accurate too. Could someone please explain JuNC''s method to me from steps 2 to 4, and include code if possible? I''d like to be able to do stuff like that, but even though I have take linear algebra, I never understand what is happening or how to get it to work in my programs.
Again, thanks a lot.
Dracle
Again, thanks a lot.
Dracle
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement