Measure rate of change (Mouse movement speed)

I have been following ofBook, I am new to statistics. In one of the book chapters “Drawing Brushes” it was proposed for the reader to write a code that handles drawing of shapes differently based on mouse movment speed.

What I don’t understand is how to calculate mouse move speed. I can measure change from previous to next bu comparing in ofVec2f but then I only have 1 value of the speed, how do I measure tendency. Could someone give me a simple iteration example of how to collect these values and measure them for realtime implementation.

Hey @opensourceartist,

A simple approach would be to calculate the distance between two mouse positions (current and previous) at some regular interval (like every 10th frame). A few if-else statements could then select the type of shape to draw based on the distance (or its abs value), including an option not to draw any shape (because the mouse hadn’t moved very much).

oF applications tend to run at 60ish frames per second on many platforms. So the sampling frequency can be every frame, or some other interval. An easy way to define a longer or variable sampling intervale is to execute certain code when the modulus is zero for ofGetFrameNum() with a value:

// in ofApp::update()
int value = 10; // this could be a class variable defined in ofApp.h
if(ofGetFrameNum() % value == 0) { // calculate mouse distance and update the int that determines the shape to draw;}

@TimChi Thank you for your suggestion! Although I already had comparing previous with next, the framerate aspect was very helpful. But what I need is not to compare previous and next but I need to compare a few values in order to get the second order differential (acceleration).

Any value in the current frame can be stored in a variable for use in a later frame(s). So maybe you need two variables to store previous values: 1 for the previous position and 1 for the previous acceleration (or change in position).

If you need to store more than a few values, a container (like std::vector) is helpful; ofBook has a chapter on them.

Also just fyi more recent versions of oF have incorporated the glm class as an alternative for ofMath types (ofVec2f, etc). Many ofBook chapters have the ofMath types, but you’ll see both types in forum posts and the projects in the examples folder (which is also a great way to explore and learn oF).

.

Hi,
My first try was:

void ofApp::update()
{
	glm::vec2 newPos( ofGetMouseX(), ofGetMouseY() ) ;
	float newSpeed = glm::length( newPos - pos ) ;
	acc = newSpeed - speed ;
	radius = acc * 10.f ;
	speed = newSpeed ;
	pos = newPos ;
}
void ofApp::draw()
{
	ofDrawCircle( pos, radius ) ;
}

But the result was not good. This is better when I smooth both the mouse position value and the radius value. To smooth a value I use this simple method:

// newValue is the current value of any physical measure 
r = 0.98; // something between 0 and 1
smoothedValue = r * smoothedValue + ( 1 - r ) * newValue;

For example:

void ofApp::update()
{
	glm::vec2 newPos( ofGetMouseX(), ofGetMouseY() ) ;
	newPos = 0.97f * pos + 0.03f * newPos ;
	float newSpeed = glm::length( newPos - pos ) ;
	acc = newSpeed - speed ;
	radius = 0.97f * radius + 0.03f * acc * 50.f ;
	speed = newSpeed ;
	pos = newPos ;
}

Here’s my test app, that allows to enable and disable the smoothing to see the differences:
src.zip (1,3 Ko)

1 Like

Yeah its a great idea (and fun) to play around with different approaches until you get a type of interaction that you like. The smoothing function is simple and is a good way if it works well. You can also sample over longer intervals (like once every 10th or 20th frames) using the modulo operator. Or you can store lots of values in a std::vector and then average them together, or use them to compute a moving average for the mouse speed or position. Glad you’ve made progress!

I can see why my post confused you: I’m not opensourceartist who opened the thread :wink:

Oh sorry about the confusion; I was really tired when I wrote the above and not paying attention. Thanks for posting your code though. It always helps to have code to look at. :wink:

@TimChi @lilive I’m working with audio files and Kinect. The code you posted is amazing, thanks! It give me immediate ideas.

Next thinking about keeping track (vector) of all the speed values or acceleration that have crossed a threshold for a particular duration like 10 seconds and then use that to do something impactful.

1 Like