But the ptCursor.X and ptCursor.Y are the mouse coords.
Anyway, I currently use the the Original CamTarget - CamPos
(Vector3(0, 0, -200) - Vector3(0, 0, 0) for vPickRayOrig and
the and the current Direction CamTarget - CamPos for vPickRayDir.
It works sweet! Could this cause a problem later on maybe?
Z-axis always forward?
Sure .. it's in C#, but the names are nearly the same anyway:
I send in the 2D Mouse ordinates and out comes the position vector for the cursor over a flat terrain.
[edited by - leXor on January 16, 2004 12:40:40 PM]
I send in the 2D Mouse ordinates and out comes the position vector for the cursor over a flat terrain.
public Vector3 PickRay(float in_MouseX, float in_MouseY){ Matrix matProj; matProj = mDevice.GetTransform(TransformType.Projection); ////////////////////////////// Vector3 vPickRayDir = new Vector3(); Vector3 vPickRayOrig = new Vector3(); vPickRayDir = new Vector3(camTarget.X, camTarget.Y, camTarget.Z) - new Vector3(camPos.X, camPos.Y, camPos.Z); vPickRayOrig = new Vector3(0, 0, -200) - new Vector3(0, 0, 0); /////////////////////////////// /// Point ptCursor = new Point(0); ptCursor.X = (int)in_MouseX; ptCursor.Y = (int)in_MouseY; // Compute the vector of the pick ray in screen space Vector3 v; v.X = ( ( ( 2.0f * ptCursor.X ) / mDevice.PresentationParameters.BackBufferWidth ) - 1 ) / matProj.M11; v.Y = -( ( ( 2.0f * ptCursor.Y ) / mDevice.PresentationParameters.BackBufferHeight ) - 1 ) / matProj.M22; v.Z = 1.0f; // Get the inverse of the composite view and world matrix Matrix matView, matWorld, m; matView = mDevice.GetTransform(TransformType.View); matWorld = mDevice.GetTransform(TransformType.World); m = matWorld * matView; m = Matrix.Invert( m ); // Transform the screen space pick ray into 3D space vPickRayDir.X = v.X*m.M11 + v.Y*m.M21 + v.Z*m.M31; vPickRayDir.Y = v.X*m.M12 + v.Y*m.M22 + v.Z*m.M32; vPickRayDir.Z = v.X*m.M13 + v.Y*m.M23 + v.Z*m.M33; vPickRayOrig.X = m.M41; vPickRayOrig.Y = m.M42; vPickRayOrig.Z = m.M43; // 0 = terrain.Y return vPickRayOrig + (0 - vPickRayOrig.Y) / vPickRayDir.Y * vPickRayDir;}
[edited by - leXor on January 16, 2004 12:40:40 PM]
Ah...thought as much.
You're setting vPickRayDir and vPickRayOrig but that gets overwritten at the end, so you're not actually using CamTarget - CamPos anywhere.
I took out the world matrix stuff (our code was calculating object space coords) and i added an error check.
You had a lot of redundancy in your code:
vPickRayDir = new Vector3(camTarget.X, camTarget.Y, camTarget.Z) - new Vector3(camPos.X, camPos.Y, camPos.Z);
we don't need new objects here, we already have the right ones!
vPickRayDir = camTarget - camPos;
[edited by - Ghwerig on January 16, 2004 1:49:05 PM]
You're setting vPickRayDir and vPickRayOrig but that gets overwritten at the end, so you're not actually using CamTarget - CamPos anywhere.
I took out the world matrix stuff (our code was calculating object space coords) and i added an error check.
public Vector3 PickRay(float in_MouseX, float in_MouseY){ Matrix matProj = mDevice.GetTransform(TransformType.Projection); // Compute the vector of the pick ray in screen space Vector3 v; v.X = ( ( ( 2.0f * in_MouseX ) / mDevice.PresentationParameters.BackBufferWidth ) - 1 ) / matProj.M11; v.Y = -( ( ( 2.0f * in_MouseY ) / mDevice.PresentationParameters.BackBufferHeight ) - 1 ) / matProj.M22; v.Z = 1.0f; // Get the inverse of the view matrix Matrix m= mDevice.GetTransform(TransformType.View); // Transform the screen space pick ray into 3D space Vector3 vPickRayDir=new Vector3(v.X*m.M11 + v.Y*m.M21 + v.Z*m.M31, v.X*m.M12 + v.Y*m.M22 + v.Z*m.M32,v.X*m.M13 + v.Y*m.M23 + v.Z*m.M33); Vector3 vPickRayOrig=new Vector3(m.M41, m.M42,m.M43); // 0 = terrain.Y if(vPickRayDir.Y==0) return new Vector3(0,1000,0); // error, ray doesn't intersect XZ or lies in XZ plane //better: throw exception else return vPickRayOrig + (0 - vPickRayOrig.Y) / vPickRayDir.Y * vPickRayDir;}
You had a lot of redundancy in your code:
vPickRayDir = new Vector3(camTarget.X, camTarget.Y, camTarget.Z) - new Vector3(camPos.X, camPos.Y, camPos.Z);
we don't need new objects here, we already have the right ones!
vPickRayDir = camTarget - camPos;
[edited by - Ghwerig on January 16, 2004 1:49:05 PM]
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement