Jump to content
  • Advertisement


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


please help about picking with mouse

This topic is 5745 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

i''m facing problem picking 3d objects with mouse in my own 3d engine. the pick example not that good.. please explain the idea for me. and thanks for advance..

Share this post

Link to post
Share on other sites
actually it is pretty easy

you have a nearest and a farest clipping plane

in order to pick something with the mouse you need to recieve te mouse message (see MSDN for details) and get the 2d coordinates
they are included in the message

once you got these coordinates
you need to trace from your view origin into the direction of your viewangle

remember a player always has an
origin ( origin of the model)
vieworigin aka viewoffset the distance between origin and

angle (the direction your model is facing)
view angle (the direction the player`s view is facing to)

the viewangle consists of the following
viewangle[0]==pitch +=up -=down
viewangle[1]==yaw +=left -=right
viewangle[2]==roll not used very often it rotates the view around the axis you get by pitch and yaw

there is a function in the maths.h called anglevectors(....)

i hope that enough of explanation

Share this post

Link to post
Share on other sites
If you''re using OpenGL, look up gluPickMatrix and OpenGL''s selection buffer in Google or something...

Movie Quote of the Week:

"You''ve been eating retard sandwiches again."
- Birdman, Beautiful Girls.
Try http://uk.geocities.com/mentalmantle. Seriously. It''s brilliant.
And I just completely redesigned it so it looks even better.
DarkVertex Beta 0.8 available soon!

Share this post

Link to post
Share on other sites
Using D3D? Take a look at my code:


void DXEngine::SelectObject(int x, int y)
m_nSelObject = -1;

m_pkDevice->GetTransform(D3DTS_WORLD, &m_kWorldMatrix);
m_pkDevice->GetTransform(D3DTS_PROJECTION, &m_kProjMatrix);
m_pkDevice->GetTransform(D3DTS_VIEW, &m_kViewMatrix);

kMouse.x = x;
kMouse.y = y;

kMouse.z = 0;
D3DXVec3Unproject(&akSelectObjRayOut[0], &kMouse, &m_pkViewPort, &m_kProjMatrix, &m_kViewMatrix, &m_kWorldMatrix);

kMouse.z = 100;
D3DXVec3Unproject(&akSelectObjRayOut[1], &kMouse, &m_pkViewPort, &m_kProjMatrix, &m_kViewMatrix, &m_kWorldMatrix);

D3DXVECTOR3 p1, p2, p3;
D3DXVECTOR3 vIntersectionPoint;
D3DXPLANE plane;

for(int i=0; i<m_vkCloneList.size(); i++)
CLONE *pkClone = m_vkCloneList[i];

Object* pkObject = m_pkObjectManager->GetObj(


BoundingBox kBox = pkObject->GetBoundingBox();

D3DXVECTOR3 cube_corners[] =
D3DXVECTOR3( kBox.x_center-kBox.x_half_size, kBox.y_center-kBox.y_half_size, kBox.z_center-kBox.z_half_size), // 0 = bottom left near

D3DXVECTOR3( kBox.x_center-kBox.x_half_size, kBox.y_center+kBox.y_half_size, kBox.z_center-kBox.z_half_size), // 1 = top left near

D3DXVECTOR3( kBox.x_center+kBox.x_half_size, kBox.y_center+kBox.y_half_size, kBox.z_center-kBox.z_half_size), // 2 = top right near

D3DXVECTOR3( kBox.x_center+kBox.x_half_size, kBox.y_center-kBox.y_half_size, kBox.z_center-kBox.z_half_size), // 3 = bottom right near

D3DXVECTOR3( kBox.x_center-kBox.x_half_size, kBox.y_center-kBox.y_half_size, kBox.z_center+kBox.z_half_size), // 4 = bottom left far

D3DXVECTOR3( kBox.x_center-kBox.x_half_size, kBox.y_center+kBox.y_half_size, kBox.z_center+kBox.z_half_size), // 5 = top left far

D3DXVECTOR3( kBox.x_center+kBox.x_half_size, kBox.y_center+kBox.y_half_size, kBox.z_center+kBox.z_half_size), // 6 = top right far

D3DXVECTOR3( kBox.x_center+kBox.x_half_size, kBox.y_center-kBox.y_half_size, kBox.z_center+kBox.z_half_size) // 7 = bottom right far


int indices[36] =
0, 1, 2,
2, 3, 0,

3, 2, 6,
6, 7, 3,

1, 5, 2,
5, 6, 2,

4, 5, 1,
1, 0, 4,

7, 6, 5,
5, 4, 7,

4, 0, 3,
3, 7, 4

D3DXVECTOR3 pVertices[36];

for(int k=0; k<12; k++)
pVertices[k*3] = cube_corners[indices[k*3]];
pVertices[k*3+1] = cube_corners[indices[k*3+1]];
pVertices[k*3+2] = cube_corners[indices[k*3+2]];

const float fSens = 2.5f; //2.5f; // 1.25f

for(int j=0; j<12; j++)
p1 = pVertices[j*3];
p2 = pVertices[j*3+1];
p3 = pVertices[j*3+2];

D3DXPlaneFromPoints( &plane, &p1, &p2, &p3);
D3DXPlaneIntersectLine( &vIntersectionPoint, &plane,
&akSelectObjRayOut[0], &akSelectObjRayOut[1]);

D3DXVECTOR3 InterPtToCorner[3];

InterPtToCorner[0] = p1 - vIntersectionPoint;
InterPtToCorner[1] = p2 - vIntersectionPoint;
InterPtToCorner[2] = p3 - vIntersectionPoint;

if( fabs(InterPtToCorner[0].x) < fSens &&
fabs(InterPtToCorner[0].y) < fSens )
float kvot, angele_sum, div;

kvot = D3DXVec3Dot(&InterPtToCorner[1], &InterPtToCorner[0]);
div = (D3DXVec3Length(&InterPtToCorner[1]) * D3DXVec3Length(&InterPtToCorner[0]));
kvot /= div;
angele_sum = (float) SafeCos(kvot);

kvot = D3DXVec3Dot(&InterPtToCorner[1], &InterPtToCorner[2]);
div = (D3DXVec3Length(&InterPtToCorner[1]) * D3DXVec3Length(&InterPtToCorner[2]));
kvot /= div;
angele_sum += (float) SafeCos(kvot);

kvot = D3DXVec3Dot(&InterPtToCorner[2], &InterPtToCorner[0]);
div = (D3DXVec3Length(&InterPtToCorner[2]) * D3DXVec3Length(&InterPtToCorner[0]));
kvot /= div;
angele_sum += (float) SafeCos(kvot);

if(angele_sum > 6.28f && angele_sum < 6.29f)
m_nSelObject = i;

Share this post

Link to post
Share on other sites

  • Advertisement

Important Information

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

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!