Computer Science Canada

Calculating speed of mouse

Author:  jondmml [ Sat Dec 15, 2007 1:42 am ]
Post subject:  Calculating speed of mouse

Hi, I have a question related to calculating the speed at which the mouse is moving right before it is released. I know that if I can find the distance the mouse moves in a certain period of time (by finding the position of the mouse at one point in time and then subtracting from the distance when it is released) I should be able to calculate it. However, I do not know how I can easily keep track of this period of time since I do not know when the user will be releasing his mouse. I will be using mousepressed, mousedragged and mouselistener methods for my canvas so I potentially have access to the mouse's location at any time. If anyone has any ideas as to how I could implement this, it would help me quite a bit.

Thanks again

Author:  HeavenAgain [ Sat Dec 15, 2007 10:15 am ]
Post subject:  RE:Calculating speed of mouse

code:
    // Get current time
    long start = System.currentTimeMillis();
   
    // Do something ...
   
    // Get elapsed time in milliseconds
    long elapsedTimeMillis = System.currentTimeMillis()-start;
   
    // Get elapsed time in seconds
    float elapsedTimeSec = elapsedTimeMillis/1000F;

after you get time, nd you already have the distance, after that is just simple physics

Author:  jondmml [ Sat Dec 15, 2007 11:20 am ]
Post subject:  RE:Calculating speed of mouse

Yes, I had considered doing it similar to this, however, I am using a mousedragged listener and a mousreleased listener, and since mousdragged is constantly being called, when I determine the end time in mousreleased, it is always equal to the last time I recorded in mousedragged, so my time difference is zero.

Author:  HeavenAgain [ Sat Dec 15, 2007 11:47 am ]
Post subject:  RE:Calculating speed of mouse

oh, i see, in that case, ummmmm
did you put the starting time in the mousedragged listener, and ending time in the mousereleased listener? uhhhh Shifty

Author:  jondmml [ Sat Dec 15, 2007 11:54 am ]
Post subject:  Re: Calculating speed of mouse

Yes I did, here is a snippet of the code so that you can take a look


Code from mouseDragged listener
code:
if (movingObject) {
                // sets the centre of the currently moving circle to the location of the mouse
                circles.get(circles.size()-1).setXCentre(evt.getX());                         
                circles.get(circles.size()-1).setYCentre(evt.getY());


                //   if (System.currentTimeMillis() - currentTime > 20) {

                // get current time
                currentTime = System.currentTimeMillis();

                // get current x and y coordinates of mouse
                xCurCentre = circles.get(circles.size()-1).getXCentre();
                yCurCentre = circles.get(circles.size()-1).getYCentre();
                //System.exit(0);
                // }
        }



Code from mouseReleased listener

code:
if (movingObject) {

            // find time difference
            long time = (System.currentTimeMillis()- currentTime);

            // stop moving circle
            movingObject = false;

             // set velocity of moving circle using previous velocity of mouse
            circles.get(circles.size()-1).setXVelocity((circles.get(circles.size()-1).getXCentre()-xCurCentre)/time);
            circles.get(circles.size()-1).setYVelocity((circles.get(circles.size()-1).getYCentre()-yCurCentre)/time);

             // debugging code
            System.out.println("x: "+(circles.get(circles.size()-1).getXCentre()-xCurCentre));
            System.out.println("y: "+(circles.get(circles.size()-1).getYCentre()-yCurCentre));
            System.out.println("time: "+time+ " curre:"+System.currentTimeMillis());
            System.exit(1);
        }


My debugging output usually looks something like:

Quote:
x: 0.0
y: 0.0
time: 16 curre:1197735772275


EDIT: the commented if condition was an idea of mine where I would only record the time and location after a certain time interval. However, this too did not work reliably.

Author:  OneOffDriveByPoster [ Sat Dec 15, 2007 12:22 pm ]
Post subject:  Re: Calculating speed of mouse

Maybe you can have mouseDragged() keep the info for the last two times it was called. So when mouseDragged() is called, some "curPosAndTime" becomes "prevPosAndTime" and "curPosAndTime" gets set with new info. mouseReleased() will just read the info.


: