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 | ||
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 |
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 from mouseReleased listener
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. |