Hi,
I'm trying to do some raycasting in Novodex, so I can make the cube I click on with the mouse the selected actor. I'm using DirectX, specifically Direct Input to get the mouse position (x,y,x). Then my camera uses camerapoistion to specify position and PointCamera as the vector where the camera is pointing. I know I probably need to use the Pointcamera somehow, possibly through DX3DMatrixLookAtH.
Please can anyone point me in the right direction, or show me some code that already has it working, my deadlines approaching .... Thanks Jon
orig = NxVec3(camerapoistion.x, camerapoistion.y, camerapoistion.z);
dir = NxVec3(mousepoisition.x, mousepoisition.y, mousepoisition .z );
NxRay ray(orig, dir);
NxShape* closestShape = gScene->raycastClosestShape(ray, NX_ALL_SHAPES, hit);
if (closestShape)
{
gSelectedActor[0] = closestShape->getActor();
Novodex Raycasting Troubles
-
- Site Admin
- Posts: 4221
- Joined: Sun Jun 26, 2005 6:43 pm
- Location: California, USA
Looks like you need a proper unproject.
Please check this article, with demo/source:
http://www.mvps.org/directx/articles/im ... icking.htm
If this doesn't help, did you already try this Novodex forum?
http://www.novodex.com/forum/
Erwin
Please check this article, with demo/source:
http://www.mvps.org/directx/articles/im ... icking.htm
If this doesn't help, did you already try this Novodex forum?
http://www.novodex.com/forum/
Erwin
-
- Posts: 1
- Joined: Sat Feb 25, 2006 4:09 pm
You ll need to project the mouse position and direction back from screen space to world space! Here's an example how I do it in my app:
(pos=ray origin; dir= direction)
(pos=ray origin; dir= direction)
Code: Select all
float mouseX = (float)NativeMethods.LoWord((uint)lParam.ToInt32());
float mouseY = (float)NativeMethods.HiWord((uint)lParam.ToInt32());
Matrix matProj = App.ProjectionMatrix;
Matrix matView = App.ViewMatrix;
Vector3 vScreen;
vScreen.X = ((mouseX * 2f) / App.Device.Viewport.Width - 1f) / matProj.M11;
vScreen.Y = -((mouseY * 2f) / App.Device.Viewport.Height - 1f) / matProj.M22;
vScreen.Z = 1f;
Matrix matViewInv = Matrix.Invert(matView);
Vector3 dir = Vector3.TransformNormal(vScreen, matViewInv);
dir.Normalize();
Vector3 pos = new Vector3(matViewInv.M41, matViewInv.M42, matViewInv.M43);