# rotating mouse co-ordinates

I am working on something where I am rotating a rectangle (drawing it inside a transformation matrix), but I still want to accurately detect mouse clicks on it.

I am thinking that the easiest way is to transform the mouse input to match the transformed output. I am just having troubles getting the correct transformation… I am getting results that seem to be somewhat close, but it’s proving to be a tad tricky to nail down.

here’s my current translateMouseCoords method…

``````

void StencilInstance::translateMouseCoords(int& x, int& y){
float xOff = this->x + this->width/2.0;
float yOff = this->y + this->height/2.0;

x -= xOff;
y -= yOff;

x = x * cosT - y * sinT;
y = y * cosT + x * sinT;

x += xOff;
y += yOff;
}

``````

here’s the draw that does the transform and what not…

``````

void StencilInstance::draw(float x, float y, float w, float h, bool borders){
ofPushMatrix();
ofTranslate(x+w/2.0, y+h/2.0, 0);
ofRotateZ(this->rotation);
ofTranslate((x+w/2.0)*-1.0, (y+h/2.0)*-1.0, 0);

ofRect(x, y, w, h);

ofPopMatrix();
}

``````

wondering if anyone has worked with anything like this or has any other ideas?

it’s “really” easy if you use ofxVectorMath

haven’t tested but something like:

``````
ofxVec2f mouse(x,y);
mouse -= ofxVec2f(this->x+w/2.0, this->y+h/2.0, 0);
mouse.rotate(-this->rotation);
mouse += ofxVec2f((this->x+w/2.0), (this->y+h/2.0));

``````

should work

dude!!! worked like a freakin charm! I’m stoked now. Damn, I’ve been banging my head against the wall with this all morning. I’m still a tad confused, because I was using the same rotation formulas in my calculations that ofxVec2f uses… but at this point I’m just happy it’s working

Thanks so much man! you’re a life saver!!