CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 11 of 11
  1. #1
    Join Date
    Aug 2005
    Posts
    134

    calculate mouse distance

    How can i calculate the distance that the mouse travelled? The mouse should always start at point x:0, y:0. When i move and stop later on, i want to see how much points i moved the cursor.

    Can someone please help me on how to do this...??

  2. #2
    Join Date
    Nov 2006
    Posts
    357

    Re: calculate mouse distance

    Usually when doing this sort of stuff with games (for camera movement) you lock the mouse at a given position (0,0 or ScreenCenterX, ScreenCenterY). Then when the mouse move event is fired you would do:

    Code:
    Point MouseDistance = new Point();
    MouseDistance.X = NewMousePosition.X - LockedMousePosition.X;
    MouseDistance.Y = NewMousePosition.Y - LockedMousePosition.Y;
    
    // Reset mouse position
    Cursor.Location = LockedMousePosition;
    The LockedMousePosition being the fixed position where the mouse should stay, and the NewMousePosition being the location of the mouse when the event was fired or when you checked how far it had travelled. Not sure about the cursor reset code, but its easy enough to see whats going on.

    Im not sure if that is exactly what you want, but that will give you the distance the mouse has travelled between when you lock it and when you read it.

    There is no built in way that im aware of to start the mouse moving and only update when they stop moving, as they may stop a small amount as they move, or there may be one update with very little difference. So you *may* want to start a timer or something and every time the mouse move event is fired reset the timer, and if it maked it to about 500 milliseconds without being reset it would fire a distance event... but i dont know when exactly you would need to do that...
    Last edited by Grofit; June 29th, 2009 at 05:27 AM.

  3. #3
    Join Date
    Aug 2005
    Posts
    134

    Re: calculate mouse distance

    Quote Originally Posted by Grofit View Post
    Usually when doing this sort of stuff with games (for camera movement) you lock the mouse at a given position (0,0 or ScreenCenterX, ScreenCenterY). Then when the mouse move event is fired you would do:

    Code:
    Point MouseDistance = new Point();
    MouseDistance.X = NewMousePosition.X - LockedMousePosition.X;
    MouseDistance.Y = NewMousePosition.Y - LockedMousePosition.Y;
    
    // Reset mouse position
    Cursor.Location = LockedMousePosition;
    The LockedMousePosition being the fixed position where the mouse should stay, and the NewMousePosition being the location of the mouse when the event was fired or when you checked how far it had travelled. Not sure about the cursor reset code, but its easy enough to see whats going on.

    Im not sure if that is exactly what you want, but that will give you the distance the mouse has travelled between when you lock it and when you read it.

    There is no built in way that im aware of to start the mouse moving and only update when they stop moving, as they may stop a small amount as they move, or there may be one update with very little difference. So you *may* want to start a timer or something and every time the mouse move event is fired reset the timer, and if it maked it to about 500 milliseconds without being reset it would fire a distance event... but i dont know when exactly you would need to do that...
    Thanks that is exactly what i need! But i'm trying to implement it into a timer. In that timer i should be able to see if the mouse is moving or not. And then do that calculation.

    But i'm a bit confused about how to do this.. How can i tell if the mouse is currently moving? And how do you lock it at x:0,y:0? Could you please help me out a bit with this??

  4. #4
    Join Date
    Nov 2006
    Posts
    357

    Re: calculate mouse distance

    You have 2 options... either use the MouseMove event (if you are using forms or some .net UI element) or act directly on the cursor... There is no way *to my knowledge* of doing Cursor.IsMoving or anything similar, you have to just poll the mouse or get the data pushed to you as and when it happens...

    If you were going to purely use a timer i would do something like this each tick:

    - Get the cursor position
    - Compare it to previous position to get distance
    - Add the distance to a static X/Y or Point
    - Update the previous position to current position

    This way every time the timer ticks it will see how far they have moved and add it into some sort of static variable that tracks the total movement, then if the tick fires and the cursor position == previous position they have not moved, so you can just stop the timer and get how much total distance has been covered in your static container...

    If you were going to use the mouse event i would do the same but you would need to have a timer to timeout if they dont move for a while, as the mousemove event only fires when you move, which is great for most cases, but in this one you want to know when they are not moving, so if you do as i suggested before and have a timer that is unable to tick (due to it resetting with each movement) unless the mouse stays still... hardly an elegant solution but does what you need...

    I would recommend doing the 1st option given the criteria you mentioned and that you just want some total distance not lots of small updates, it would be faster as well. Only problem is you need to work out when to start the timer going and grab the cursors location when you start it and put it into the previous location if that makes sense...

  5. #5
    Join Date
    Aug 2005
    Posts
    134

    Re: calculate mouse distance

    Ok, i sort of get an idea of what i'm going to do. But there are 1 thing though.

    When i lock the cursor at a point, let's say: x20,y70. Then how does it know if i move my cursor to a negative direction like, x:-20, y:-30? So when i move my cursor 'off the screen'.
    Which is pretty much impossible, cause it hangs when you reach the edge of the screen. The X or Y position stays 0 that way, so no more new mouse positions right?

    Any idea about that?
    Last edited by Trainwreck; June 29th, 2009 at 08:26 AM.

  6. #6
    Join Date
    Nov 2006
    Posts
    357

    Re: calculate mouse distance

    You shouldnt be able to move your mouse off the screen, so if you try it would just stay in the same place. If you are talking about the value of your TotalDistanceMoved going to be a value > || < than then screen bounds then you cant really do the mouse locking without alot of checks... if you want to track a free flowing mouse then just do the timer tick method i mentioned... the mouse will move freely and will keep feeding you the distance travelled until they stop moving...

    Im not 100% clear on what you want... well i know you want to track the distance but i dont know how you want the mouse to react or if you are wanting an absolute distance or a distance withing a contained area etc...

    If you can put more info on your scenario and mouse usage someone may be able to give you a better idea.

  7. #7
    Join Date
    Aug 2005
    Posts
    134

    Re: calculate mouse distance

    I have the code now that perfectly tracks my cursor. But i'm still stuck with the boundarys of the screen. But i'll try to sketch a clear scenario.

    I have a Timer which constantly checks the position of the cursor every 50 miliseconds. When i move the cursor the timer will see it:
    Code:
    //When the cursor is moved
    if(lastPosX != CurrentPosX || lastPostY != CurrentPosY)
    {
         //calculate the distance
    }
    So every 50 miliseconds it gets my current mouse position, substracts that from my old cursor position which gives me the amount my cursor travelled in the X direction and Y direction.

    When i move really slow along the X axis, i only see that i move 1 ~ 3 pixels every 50 miliseconds in my textfield.

    This all is exactly what i want and it works perfectly now. But the problem is when i reach the end of the screen. If my screen width is 1024 x 768, and i'm at 1024. Then it can never calculate new movements because im at the end of the screen.

    And that is something i don't want. Cause it's indeed to controll a camera in a game. I want to be able to move my mouse as far as i want.

  8. #8
    Join Date
    Nov 2006
    Posts
    357

    Re: calculate mouse distance

    If its for a game camera then i would use the mousemove event... if you are using C# and XNA or MDX then im sure both have support for mousemove events.

    Although saying that with the code you have at the moment all you need to do is at the end of every tick reset the cursor to the previous position... so each timer update it will track how far it has moved then reset it to move again, so they could keep scrolling in any direction for infinity.

  9. #9
    Join Date
    Aug 2005
    Posts
    134

    Re: calculate mouse distance

    Thanks alot, we have it totally working now!

    And for the game engine, we use Truevision3D, but they appearently use the DirectX input engine. It's also known that the Truevision input engine is buggy with some Videocards. Which was our case. That's why i was looking for another solution.

    Thanks alot for your help!
    Last edited by Trainwreck; June 30th, 2009 at 05:47 AM.

  10. #10
    Join Date
    Mar 2011
    Posts
    1

    Re: calculate mouse distance

    Distance = Square Root ( (X difference)2 + (Y difference)2 ) using this formula you can calculate the difference

  11. #11
    Join Date
    Aug 2011
    Posts
    1

    Re: calculate mouse distance

    Hi,

    Try this: All you need to do is create a MovieClip, drag it on the stage (to x:400, y:300 - for the purpose of this demo) and give it an instance name 'box_mc'.

    Code:

    var point:Point = new Point(400, 300);

    this.addEventListener(Event.ENTER_FRAME, checkDistance);

    function checkDistance(e:Event):void
    {
    var dx:Number;
    var dy:Number;
    var alph:Number;

    if (mouseX > point.x) dx = point.x - mouseX;
    else dx = mouseX - point.x;

    if (mouseY > point.y) dy = point.y - mouseY;
    else dy = mouseY - point.y;

    alph = (dx + dy) / (point.x + point.y) + 1;

    box_mc.alpha = alph;
    }


    http://www.postcode.org.uk

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured