Jump to content
  • Advertisement
Sign in to follow this  
X5-Programmer

3D Picking

This topic is 5391 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 have made an terrain for my game and now I whant to move objects with the mouse in 3D, how can I get the x,z coords on the terrain by klick with the mouse? plz point me somewere. thx.

Share this post


Link to post
Share on other sites
Advertisement
Point -> http://gpwiki.org/index.php/VB:3Dmouse
The code is in Visual Basic, however, but it shouldn't be too hard to tranlate.

Google pointed to these:
http://www.beyond3d.com/articles/vs/
http://www.idevgames.com/forum/showthread.php?t=5178
http://download.blender.org/documentation/html/interface_3d.html

Share this post


Link to post
Share on other sites
This is how I do it.

// Render everything to the screen
RenderScene();


// Get world cooordinates from mouse coordinates
float posX, posY, posZ; // holds actual 3d coordinates

void GetGLPos(int x, int y)
{
static GLint viewport[4];
static GLdouble modelview[16];
static GLdouble projection[16];
static GLfloat winX, winY, winZ;

glGetDoublev( GL_MODELVIEW_MATRIX, modelview );
glGetDoublev( GL_PROJECTION_MATRIX, projection );
glGetIntegerv( GL_VIEWPORT, viewport );

int winX = (float)x;
int winY = (float)viewport[3] - (float)y;

glReadPixels(int(winX), int(winY), 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT, &winZ );
gluUnProject( winX, winY, winZ, modelview, projection, viewport, &posX, &posY, &posZ);
}

Share this post


Link to post
Share on other sites
oconnellseanm:

Hi, Yes its just return the coords in one point not every point on the terrain, I need to return x,z when I hit mouse button on some point on the Terrain.

Share this post


Link to post
Share on other sites
That's why every time you click the terrain, you get the current mouse coordinates and call the function.

When you call the function and it fills in the posX, posY, and posZ values, you can then find the actual vertice location on the terrain using simple math.

Simply divide posX and posZ by the terrain dimensions, then multiply by the number of rows and columns and that will give you the vertice location.

vertexX = (posX/terrainWidth) * cols;
vertexZ = (posZ/terrainLength) * rows;

Share this post


Link to post
Share on other sites
The problem with reading the Z value is that there might be other objects in the way that are selected instead of the terrain. If you use this method to calculate the new location of an object on the ground of the terrain then in the next frame you will be pointing at the new object instead of the terrain, and the distance will be less, so you will move the object closer and then next frame your will be pointing again at the new object, but this time closer, so you move it forward again and again until it slips under the mouse.

There are many assumptions in the above about what you are doing, but it proves my point.

Of course, you can draw the terrain, flush the command list so that it is all drawn, get the pixel, then draw the objects. But that's not that nice.

If your terrain is flat then you can use a simple algebraic function to figure out the intersection point, but I assume your terrain has height.

The alternative that I can think of is to find the point below the viewer, and the normal vector in the direction the user is looking projected onto the plane of the terrain. From there you move across a "line" over the terrain, checking the height of every piece you cross. If the height means that the terrain is higher than the crosshair at that point then that is where the mouse is pointing. You can do some more algebra to get an accurate sub-map-element location once you have found this.

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.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!