Hi! I'm currently writing some code to detect if a given point is within a non-axis aligned rectangle ( to see if I clicked on the rect ). Here's what I have so far:
VEC diff;
diff.x = xMouse - x;
diff.y = yMouse - y;
VEC boxAxisX, boxAxisY;
boxAxisX.x = COS( curRotation );
boxAxisX.y = SIN( curRotation );
boxAxisY.x = -boxAxisX.y;
boxAxisY.y = boxAxisX.x;
float aax = VEC::Dot(&diff, &boxAxisX);
float aay = VEC::Dot(&diff, &boxAxisY);
if( abs( aax ) < rectHalfWidth && abs( aay ) < rectHalfHeight )
{
return true;
}
return false;
Just FYI, my COS and SIN macros takes degrees rather than radians.
Anyways, that works just fine. However, now I'm adding functionality for rotating my rectangle around a specified pivot point rather than it's origin.
In order to do this, I have changed my rectangle render code to look like this:
glPushMatrix();
glTranslatef( xRect, yRect );
glRotatef( -curRotation, 0.0f, 0.0f, 1.0f );
glTranslatef( xPivot, yPivot, 0.0f );
// Now render my rectangle with no rotation at the origin...
glPushMatrix();
So, this all works great, except once the rectangle has a pivot point other than it's origin and is rotated about that new pivot point, my detection code to see if I have clicked on it no longer works ( of course ).
I'm trying to wrap my head around what I need to change in order to get it working again. I need help with the math :)
Any help would be great! Thanks!