Page 1 of 1

Novodex Raycasting Troubles

Posted: Fri Feb 24, 2006 6:53 pm
by jon
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();

Posted: Fri Feb 24, 2006 7:10 pm
by Erwin Coumans
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

Posted: Sat Feb 25, 2006 4:20 pm
by Robert Isele
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)

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);