Skip to main content
GameDev.net gamedev.net
🔒 Locked

Picking problem...

Started by FogGobbler Jul 31, 2002 at 7:56 AM 4 replies 350+ views
Original Post
FogGobbler
FogGobbler
Hi! Well, I´m trying things out with Jim´s code and have run into another problem. I´m using the 3D-in-2D example and wanted to add a "picking the monster" task (with the mouse). You should be able to click the monster-thing and something like a messagebox should tell the user that you´ve picked it (just for testing).
  
  D3DXVECTOR3 vecRay, vecDir, vecTemp;
  D3DXMATRIX matProj, matView, m;
  float u,v;
  BOOL Hit;
  DWORD Face; 
  ...
  m_Graphics.GetDeviceCOM()->GetTransform(D3DTS_PROJECTION, &matProj);
  m_Graphics.GetDeviceCOM()->GetTransform(D3DTS_VIEW, &matView);
  
  D3DXMatrixInverse(&m, NULL, &matView);
  m_Mouse.Read();
  if (m_Mouse.GetButtonState(MOUSE_LBUTTON) == TRUE) {
  
  vecTemp.x = (((2.0f * m_Mouse.GetXPos()) / m_Graphics.GetWidth())-1.0f) / matProj._11;
  vecTemp.y = (((2.0f * m_Mouse.GetYPos()) / m_Graphics.GetHeight())-1.0f) / matProj._22;
  vecTemp.z = 1.0f;

  vecRay.x = m._41;
  vecRay.y = m._42;
  vecRay.z = m._43;

  vecDir.x = vecTemp.x * m._11 + 
	         vecTemp.y * m._21 + 
			 vecTemp.z * m._31;
  vecDir.y = vecTemp.x * m._12 + 
	         vecTemp.y * m._22 + 
			 vecTemp.z * m._32;
  vecDir.z = vecTemp.x * m._13 + 
	         vecTemp.y * m._23 + 
			 vecTemp.z * m._33;
   
  D3DXIntersect(m_Meshes.GetParentMesh()->m_Mesh, &vecRay,     &vecDir, &Hit, &Face, &u, &v, &Dist,NULL,NULL);
  
  if (Hit)
	  MessageBox(GethWnd(), "test", "Yes", MB_OK);
 }
  
I just pushed everything in the "cApp::Frame()" part, just for testing. When I click on monster-thing nothing happens; only a few inches in front of it (beeing towards the user) seems to work. Well, I hope you can help me. Michael
FogGobbler
FogGobbler
Well, I just wanted to tell you, I´m going off to eat my hat..
I simply forgot a "-" in front of the v.y vector!!
AHHHHH!

So, one problem is solved, it recognizes the mesh at the beginning. Allthough, when I move it (the monster thing), it doesn´t seem to update. It still remembers the old spot, where it was to start off with. Where and what do I have to alter anything?

Thanks.
Michael

FogGobbler
FogGobbler
Hi!

Well I´ve checked if the mouse get´s new coordinates and it seems to update everytime I click somewhere. The rays also get new info. To get this right in my head..it shouldn´t realy matter, if the mesh I want to check moves or not, or am I wrong? I define a starting pont of the ray (which is fix) and the end point is defined by the mouse click coordinates. Then the intersect function checks if the ray goes through the mesh and returns a TRUE. ´So I´m puzzeled why it works the first time, but when I move the mesh it still remembers the old spot, although it has new ray-point data.
Any ideas are very welcome...
Michael
Jim Adams
Jim Adams
You''re not transforming the ray based on the mesh''s world position. Inverse the world transformation and apply to the ray origin and direction and that should work. Chapter 18 code shows this inside the cApp::GetCharacterAt function (inside WinMain.cpp).



Jim Adams
home.att.net/~rpgbook
Author, Programming Role-Playing Games with DirectX

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.