Is it possible to make a spinner?


#1

I want to make a spinner that can spun by dragging the mouse in a circular motion and track the revolutions. Is this possible to implement in openframeworks?


#2

certainly. and it should be quite simple actually.
ofApp.h

#pragma once

#include "ofMain.h"

class ofApp : public ofBaseApp{

	public:
		void setup();
		void draw();
	
		void mouseDragged(int x, int y, int button);
		void mousePressed(int x, int y, int button);
		
	
	float angle, spinnerRadius = 100;
	glm::vec2  prevMouse, currentMouse, center;

};

ofApp.cpp

#include "ofApp.h"

//--------------------------------------------------------------
void ofApp::setup(){
 center = {ofGetWidth()/2, ofGetHeight()/2};
	ofSetCircleResolution(100);
}

//--------------------------------------------------------------
void ofApp::draw(){

	
	ofBackground(50);
	ofPushMatrix();
	ofTranslate(center);
	ofRotateRad(-angle);
	
	ofFill();
	ofSetColor(220);
	ofDrawCircle(0,0, spinnerRadius);
	
	ofSetLineWidth(3);
	ofNoFill();
	ofSetColor(150);
	ofDrawCircle(0,0, spinnerRadius);
	ofDrawLine(0,0,spinnerRadius,0);

	
	ofPopMatrix();
	
	
	stringstream s;
	s << "Angle: " << angle; 
	
	s << "\nmouse - c: " << (currentMouse-center);
	s << "\np mouse - c: " << (prevMouse-center);
	s << "\ncurrent mouse: " << currentMouse;
	s << "\np mouse: " << prevMouse;
	s << "\nRevolutions: " << angle/glm::two_pi<float>(); 
	
	ofDrawBitmapStringHighlight(s.str(), 20,20);					 
	
}
//--------------------------------------------------------------
void ofApp::mouseDragged(int x, int y, int button){
	if(ofDist(x, y, center.x, center.y) <= spinnerRadius){
		currentMouse = {x,y};
		if(currentMouse != prevMouse){
		//		b = glm::acos(glm::dot(glm::normalize(m - c), glm::normalize(prevMouse - c)));
		auto d = currentMouse - center;
		auto pd = prevMouse - center;
		float diff = ofAngleDifferenceRadians(atan2(d.y, d.x), atan2(pd.y, pd.x));
		if(!isnan(diff)){
			angle += diff;
		}
		}
	prevMouse = currentMouse;
	}
	
}

//--------------------------------------------------------------
void ofApp::mousePressed(int x, int y, int button){
	prevMouse = {x,y};
}

#3

Thanks for the help, I tried testing this code, and it tracks the position of the mouse but not the angle and revolutions. In the mouseDragged, why is there the b = glm::acos(glm::dot(glm::normalize(m - c), glm::normalize(prevMouse - c))); commented out? After some testing, it seems that angle does not add the difference correctly, but I’m not sure why that might be the case. Figured out the problem, just had to initialize angle. All good now.