   [MDX] Check if camera "intersects" a mesh
  Hi, :)I am programming a game using C# and MDX. I create a mesh from an X-file containing my 3d map. It's like a corridor: walls and floor. At first the "player" is just moving the camera around. Now, I want to check wether the camera "intersects" with the scene-mesh and where if it does. So I looked up what to do and found:Mesh.Intersect(Vector3 rayVector, Vector3 rayDir, out IntersectInformation closest, out IntersectInformation[] all);Now, I don't know how exactly to do with the two vectors but I have an idea: rayVector is my camera-position-vector and the direction is the camera-look-at-vector perhaps? Overall: how do I check if my camera intersects/collides with a wall on my scene-mesh?Btw. there might be other ways to accomplish this.Thank you in advance! :)Regards,SirErugor   
  First, welcome to GameDev!  :)You're correct in that there are better/cleaner ways to do this kind of test rather than using Mesh.Intersect, but for a starters, it should be ok.The Mesh.Intersect function accesses the particular vertices of the mesh directly.  So the vertices of the mesh are exactly the same as when they were first loaded from the file; no transformations have been applied to them.You are correct in that the rayVector is the position and the rayDir is the direction.  Remember that any ray has two components: a position and direction.  Since the Mesh.Intersect function directly checks these against the vertices of the mesh, both the mesh vertices and the ray itself must be in the same space.You have two choices.  You can either manually transform all the vertices in the mesh to world space (bad!), or you can transform the two components of the ray into model space (good!).  To transform the ray into model space, you transform both the position and the direction by the inverse of the world matrix of the mesh.  I'm not sure what the MDX functions are, but the unmanaged functions to do this are D3DXMatrixInverse, D3DXVec3TransformCoord, and D3DXVec3TransformNormal.  I'm sure you'll be able to find the MDX versions of these without much trouble.neneboricua   ;  Hey there and thank you very much! :)I'm a little new with MDX9, however not entirely though. I did as you said, or at least what I thought you meant, created the following method:private bool isColliding(Mesh inputMesh, Vector3 position, Vector3 direction){IntersectInformation closestHit;IntersectInformation[] allHits;float worldDeterminant = device.Transform.World.Determinant;Matrix invertedWorld = Matrix.Invert(ref worldDeterminant, device.Transform.World);position.TransformCoordinate(invertedWorld);direction.TransformCoordinate(invertedWorld);return inputMesh.Intersect(position, direction, out closestHit, out allHits);}When I fire it I get: D3DERR_INVALIDCALL -2005530516, and it dies.. :SSo am I doing something wrong? Maybe you have one of those better ways you wanna explain to me? :)[Edited by - Muhammad Haggag on December 7, 2005 2:35:17 PM];  The position is a coordinate.  The direction is a vector.  You need to treat them differently mathematically when you transform them.position.TransformCoordinate(invertedWorld);direction.TransformNormal(invertedWorld);Also, are you running the Debug Runtime?  If so, you should see some data coming in the debug spew telling you what kind of error occured and exactly why it occurred.  If you're not sure about the Debug Runtime, check out the Forum FAQ.  It has a lot of good info on that.neneboricua;  Okay, so I transformed the direction vector by TransformNormal(); instead. But I'm getting pretty much frustrated right now. :-/Whenever I fire my game I get the following error: http://mplus.dk/D3DERR_INVALIDCALL.JPGThe "OnFrameRender()" is my "drawing_loop" and the "isColliding()" is my method.I cannot get my VS to show debug informaton on DirectX and I've looked over the Forum FAQ and set the DX debug output to max and tried to set it to autoexpand. I'vre tried other debuggers. The CLR debugger won't even open my game for debug getting "File is not a valid .NET frameworks or COM component". The only thing that happens is that the D3DERR_INVALIDCALL shows.. and the game dies. I'm using a custom UI that came with the DX9 SDK October Update 2005, so that I can use buttons and textboxes. If I tried to do the operation in new, clean solution without using the UI I don't get the error.. So I figure it might be the UI?Also, a little general question: do you write your own UI for every game? Or is there some kind of other solution.. i'm really blank :/Thanks!SirErugor[Edited by - SirErugor on December 8, 2005 4:26:11 AM];  Or you could just use this nifty little method I wrote that handles everything for you ;)public bool Intersect(Microsoft.DirectX.Direct3D.Device device, Mesh mesh){//intersection informationIntersectInformation closest;//the near vectorVector3 near;//the far vectorVector3 far;near = new Vector3(cursor.X, cursor.Y, 0); //the mouse coordinatesfar = new Vector3(cursor.X, cursor.Y, 1); //the mouse coordinatesnear.Unproject(device.Viewport, device.Transform.Projection, device.Transform.View, worldMatrixOfMesh);far.Unproject(device.Viewport, device.Transform.Projection, device.Transform.View, worldMatrixOfMesh);far.Subtract(near);if(mesh.Intersect(near, far, out closest))return true;return false;}And if you are struggling to get the mouse coordinates of the mouse. You could just use this methodprivate void ValidateMouseCoordinates(Form window){//Computes the screen space coordinates to client coordinatescursor = window.PointToClient(Cursor.Position);if(cursor.X < 0) //if the x position is less than 0. set it to the window widthcursor.X = window.Width;if(cursor.Y < 0) //if the y position is less than 0. set it to the window heightcursor.Y = window.Height;}I really hope this helps.Take care.   ;  Seems like the way to do it maybe. Actually my problem is to get the worldmatrix of the mesh? No matter how I try to get the Device.Transform.World. Whenever I try I get the D3DERR_INVALIDCALL and it dies. It doesn't matter if it's done in my game-app or an empty-start-app from the dx9 sdk october update 2005 (their custom UI).:S.. is there a difference on WorldMatrix and WorldMatrix_of_mesh?Thanks   ;  If you create a pure device when you created the device you cannot read back render states and transforms.Its better that you just use the stored copy of the world matrix of each mesh - you must already know it since you drew it.   ;Quote:Actually my problem is to get the worldmatrix of the mesh?Generally a Mesh doesn't have a Transformation Matrix but you will when you start placing objects in your world set a transformation for the objects to be rendered with. The world transformation is nothing more than a set of coordinates to place the object into the world.The general way to go about this is1) Set World Transformation Matrix2) Render object that will be placed in the world by the transformation matrix set.3) Set other World Transformation Matrix4) Render object 2Quote:is there a difference on WorldMatrix and WorldMatrix_of_mesh?See my above answer for specifics to this question.I hope this helps.Take care.   ;  thezbuffer >> If I create the device not including the Pure-attribute and try to get the WorldMatrix from the device it still gives the same error?Quote:Its better that you just use the stored copy of the world matrix of each mesh - you must already know it since you drew it.Well I did draw my own map but still I'm a little new with this. The term 'Matrix' is still a bit abstract for me. So maybe I know the size and where the map is located, but I still don't know how to translate it to a world matix?Armadon >> Okay, now I get the idea a bit better.. But I still cannot set nor get the worldmatrix without it crashes on me? :S First, I tried to set it to e.g. the viewmatrix and then tried to access it from the device but, still, the same error. (that was just for testing reasons)This shouldn't be that difficult.. I also debugged the app using PIX. The weird part is that it never gets past frame one before crashing. Also, if I do the same method-call from a "clean" solution it runs perfectly. Seems that when I use the custom UI provided with the dx9 sdk october update 2005 it crashes.. Does that even make sense?   