XNA Creators Club Online
Page 1 of 1 (4 items)
Sort Posts: Previous Next

Mouse picking with Third person camera?

Last post 7/30/2009 1:18 PM by Charles Humphrey. 3 replies.
  • 7/27/2009 7:16 PM

    Mouse picking with Third person camera?

    Hi All,

    I am trying to make a combat system similar to WoW and want to code spells that have AoE.  Right now the camera is in a third person mode, always a few units behind the player and pointing at the player.  I want to have the player target a certain area on the ground (which is a 3D terrain) to cast an AoE spell, but I can't figure out the terrain position based on the mouse coordinates.  I've searched a lot this morning and have found this code:

    //----------------------------------------------------------------  
        // GetPickedPosition() - gets 3D position of mouse pointer  
        //                     - always on the the Y = 0 plane       
        //----------------------------------------------------------------  
        public static Vector3 GetPickedPosition(Vector2 mousePosition, float nearClip, Matrix projectionMatrix, Matrix viewMatrix)  
        {  
      
            // create 2 positions in screenspace using the cursor position. 0 is as  
            // close as possible to the camera, 10 is as far away as possible  
            Vector3 nearSource = new Vector3(mousePosition, 0f);  
            Vector3 farSource = new Vector3(mousePosition, nearClip);  
      
            // find the two screen space positions in world space  
            Vector3 nearPoint = Globals.graphicsDevMgr.GraphicsDevice.Viewport.Unproject(nearSource,  
                                                                                         chaseCamera.projectionMatrix,  
                                                                                         chaseCamera.viewMatrix,  
                                                                                         Matrix.Identity);  
      
            Vector3 farPoint = Globals.graphicsDevMgr.GraphicsDevice.Viewport.Unproject(farSource,  
                                                                                        projectionMatrix,  
                                                                                        viewMatrix,  
                                                                                        Matrix.Identity);  
      
            // normalized direction vector from nearPoint to farPoint  
            Vector3 direction = farPoint - nearPoint;  
            direction.Normalize();  
      
            // create a ray using nearPoint as the source  
            Ray r = new Ray(nearPoint, direction);  
      
            // calculate the ray-plane intersection point  
            Vector3 n = new Vector3(0f, 1f, 0f);  
            Plane p = new Plane(n, 0f);  
      
            // calculate distance of intersection point from r.origin  
            float denominator = Vector3.Dot(p.Normal, r.Direction);  
            float numerator = Vector3.Dot(p.Normal, r.Position) + p.D;  
            float t = -(numerator / denominator);  
      
            // calculate the picked position on the y = 0 plane  
            Vector3 pickedPosition = nearPoint + direction * t;  
      
            return pickedPosition;  
        }  

    However, this makes several assumptions that I don't think I can use in my game.  The code above (designed for an RTS) assumes that everything is on a plane y=0, and that the camera is pointed downwards - like a bird's eye point of view.  Does anyone know how to adapt it for a Third person or first person mode, where the objects can be anywhere in 3D space (not necessarily just y = 0)?
  • 7/27/2009 7:57 PM In reply to

    Re: Mouse picking with Third person camera?

    JCW:
    The code above (designed for an RTS) assumes that everything is on a plane y=0, and that the camera is pointed downwards


    That is not the case. It uses the Viewport.Unproject method to take the camera projection and view matrix into account, so this code will work with any camera position.
    XNA Framework Developer - blog - homepage
  • 7/30/2009 12:13 AM In reply to

    Re: Mouse picking with Third person camera?

    As far as I can see, the only problem with this algorithm is that you need to find the intersection between the ray and your terrain rather than just a plane. Assuming you're using a regular heightmap based terrain I think the approach I would go for if I was trying to do this myself would be using this algorithm to limit the number of triangles to test for intersections and implementing something like this to find the actual intersection(s).
  • 7/30/2009 1:18 PM In reply to

    Re: Mouse picking with Third person camera?

    I used a similar method in a sample I put up ages ago, doing ray intersections on terrain, you can find the post here. The clip there shows the terrain being modified, you can't see the mouse position in the clip though..  Hope it helps :)
Page 1 of 1 (4 items) Previous Next