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

Camera Movement with Right-Click on Mouse

Last post 07-05-2008 5:32 PM by Greg the Mad. 8 replies.
  • 07-04-2008 2:58 AM

    Camera Movement with Right-Click on Mouse

    I'm using a simple Vector2 for my Camera and I'm using XNA on WinForms with the sample that I got from here. I want to be able to move the Camera with a right mouse click and drag. Is it possible? How would I do this?

    My application is already setup to the Camera Vector2 so all I need is the code to move it. I can move it with the keyboard already, I just want to move it with the right mouse click/drag method.

    If anyone can help, that would be great.

    Thanks,

    Dro Sarhadian :)

    -----------------------
    I develop games :)
  • 07-04-2008 5:21 AM In reply to

    Re: Camera Movement with Right-Click on Mouse

    When you first click right you have to get the position of the mouse and store it in a Vector2. In the next frame you again get the mouse position and store it in a nother value. To get the actual movement you now have to substract LastPosition -  CurrentPosition and just add the result to the camera position. At the end of the frame you set the LastPosition equal the CurrentPosition.

     

    Greg the Mad

  • 07-04-2008 5:37 AM In reply to

    Re: Camera Movement with Right-Click on Mouse

    Greg the Mad:

    When you first click right you have to get the position of the mouse and store it in a Vector2. In the next frame you again get the mouse position and store it in a nother value. To get the actual movement you now have to substract LastPosition -  CurrentPosition and just add the result to the camera position. At the end of the frame you set the LastPosition equal the CurrentPosition.

     

    Greg the Mad

    Thanks, I'll try this in a little bit.

    -----------------------
    I develop games :)
  • 07-04-2008 2:45 PM In reply to

    Re: Camera Movement with Right-Click on Mouse

    I tried to do what you said, but I got stuck.

    Here is the current code that I have so far:

            public void CameraUpdate()  
            {  
                MouseState mouse = Mouse.GetState();  
     
                Vector2 currentPosition = new Vector2(mouse.X, mouse.Y);  
     
                Vector2 lastPos = new Vector2(oldState.X, oldState.Y);  
     
                if (mouse.RightButton == Microsoft.Xna.Framework.Input.ButtonState.Pressed)  
                {  
                    CameraPosition = lastPos - currentPosition;  
                }  
            } 

    I don't know what to do now. Am I doing everything right or am I doing something wrong.

    Can you please help me?

    -----------------------
    I develop games :)
  • 07-05-2008 12:44 AM In reply to

    Re: Camera Movement with Right-Click on Mouse

    Try "CameraPosition += currentPosition - lastPos;"

    See when you subtract currentPosition - lastPos, you get a vector that tells you how much the mouse moved.  By adding that to the camera's position, you move the camera by the amount that the mouse has moved (and in the same direction of movement).

  • 07-05-2008 4:27 AM In reply to

    Re: Camera Movement with Right-Click on Mouse

    MJP:

    Try "CameraPosition += currentPosition - lastPos;"

    See when you subtract currentPosition - lastPos, you get a vector that tells you how much the mouse moved.  By adding that to the camera's position, you move the camera by the amount that the mouse has moved (and in the same direction of movement).

    Doesn't seem to work. I changed that line to told me to change but it didn't work. Am I doing everything right? Is the code correct? I'm not sure if the code is correct and how to fix it. Would I go add mouse support like I do to game pad support or what? I'm gonna try some different types of code and see what happens. Also, does the code goes in the Draw method correct, since I'm using the WinForms sample from this site (Not the content manager one)?

    -----------------------
    I develop games :)
  • 07-05-2008 10:23 AM In reply to

    Re: Camera Movement with Right-Click on Mouse

    Hmm ...

    using Microsoft.Xna.Framework.Input;  
     
    MouseState lastMouse;  
    public void CameraUpdate()  
    {  
    MouseState currentMouse = Mouse.GetState();  
     
    Vector2 currentPosition = new Vector2(currentMouse.X, currentMouse.Y);  
     
    Vector2 lastPosition = new Vector2(lastMouse.X, lastMouse.Y);  
    if (currentMouse.RightButton == ButtonState.Pressed)  
     
    CameraPosition += lastPosition - currentPosition; //or: CameraPosition -= currentPosition - lastPosition   
     
    lastMouse = currentMouse;  

     

    Try this code, but the only realy thing I've changed was the lastMouse = currentMouse at the end (and that using Microsoft ... but thats just a hint if you need the ButtonState more often then once)

     

    Greg the Mad

  • 07-05-2008 2:53 PM In reply to

    Re: Camera Movement with Right-Click on Mouse

    This doesn't seem to work for some reason. Is it because I'm using this sample: http://creators.xna.com/en-us/sample/winforms_series1? I don't know how to do it? I just called that method in the Draw method but it doesn't seem to work. Any ideas?
    -----------------------
    I develop games :)
  • 07-05-2008 5:32 PM In reply to

    Re: Camera Movement with Right-Click on Mouse

    I already used that sample once too (for a editor), and had too my problems.

    About that code now, I don't thing that the bug is in that code, its may how you use it:

    Is the Vector3 you use for the view matrix the same as in that code? Do you set lastMouse = currentMouse somewhere else in the code?

    If its non of those things, could you post the whole draw method?

     

    Greg the Mad

Page 1 of 1 (9 items) Previous Next