a bug in ofxVec2f angle?

I wanted to get the angle between two points so used the ofxVec2f class and the angle() function. I had strange results. Unless I am completely miss understanding how it should work.

// way i'd normally do it  
float distx = startPos.x -x;  
float disty = startPos.y - y;  
float angleRadians = atan2(distx, disty);  
float angleDegrees = (angleRadians/PI)*180;  
printf("method one=%f\n", angleDegrees);  
// using vec2f  
ofxVec2f pos1, pos2;  
pos1.x = startPos.x;  
pos1.y = startPos.y;  
pos2.x = x;  
pos2.y = y;  
float angle = pos1.angle(pos2);  
printf("method two=%f\n", angle);  

I tested this using the mouse position and a static point.

In the first method, I get results I would expect. Going clockwise around the point from 0 to -180 (at the 6 oclock position) then around from 180 to 0 (back at the top). These angle results are consistent no matter how far away from the point you are.

In the second method results are all over the place and the angle gets bigger the further away the two points are from each other.

Any ideas?

I think the difference is that you are looking for the angle between two points, but ofVec2f is the angle between two vectors. It seems to work fine for me, to give the angle between an up vector and the vector between the middle of the screen and the mouse, ie:

void testApp::mouseMoved(int x, int y ){  
	ofxVec2f up;    
	ofxVec2f mouse;  
	mouse.set(ofGetWidth()/2 - x,ofGetHeight()/2 - y);  // a vector from the middle of the canvas to the mouse....  
	cout << up.angle(mouse) << endl;  

it returns values between -180 and 180…

hope that helps!


I think the problem is that the ofxVec2f::angle is for calculating the angle between two vectors not two points.

Thats why the results don’t make sense.

There should probably be a ofxVec2f::angle method that just returns the angle of the vector so you could do:

ofxVec2f deltaVec = pos - startPos;  
float angle = deltaVec.angleAsDeg();  

Ah I see. Thanks. ofxPoint2f doesn’t have an angle function, maybe we should add one?

All I’m trying to do is get an object to move from one point to another, but either my basic maths is failing me or something else is wrong.

For example, basic movement code is:

angle = 0;  
myPos.x += cos((PI*angle)/180);  
myPos.y += sin((PI*angle)/180);  

angle test results…
0 = move to right
90 = move down
180/-180 = move left
-90 = move up

From my angle calculations, up should be 0 and down should be 180. Does that make sense?

Just playing around with numbers it works, but not sure why. This is the variation on the above…

angle = 0;  
myPos.x -= sin((PI*angle)/180);  
myPos.y -= cos((PI*angle)/180);  

Sorry I know this is really basic. I’ve done this in Flash before. Have I got my coordinate system in my head wrong?

0 actually ends up being right.
That is normal based on that usage of sine and cosine.

angle = 0, cos(0) = 1 (what you use for x) , sin(0) = 0 (what you use for y)

so with nothing in y and +1 in x you have a vector pointing right.

If you want to have up being 0 then you can just subtract 90 degrees (or HALF_PI ) - from the angle that you are inputting.

That should do the trick!

Thanks, this seems to work now (as well):

myPos.x -= cos((PI*(angle-90))/180);  
myPos.y += sin((PI*(angle-90))/180);   

I am trying to draw the box facing the way the angle is pointing. How does glRotatef() work?

So far I have

glTranslatef(myPos.x, myPos.y,0);  
glRotatef(angle, 0, 0, 1);  
ofRect(-(myWidth/2), -(myLength/2), myWidth, myLength);  

What system does glRotatef use for angles?


Hey chris - you can find the documentation for glRoatef here:

Short story is the angle argument should be in degrees.

If you want to rotate something around its center you have to translate to its center, do the rotation, then draw from the center.

You can do this the easy way with ofSetRectMode(OF_RECTMODE_CENTER)

glTranslatef(myPos.x, myPos.y,0);   
glRotatef(angle, 0, 0, 1);   
ofRect(0, 0, myWidth, myLength);   

Or the more difficult way where you have to add half width and height to the translation

glTranslatef(myPos.x + myWidth/2, myPos.y  + myLength/2,0);   
glRotatef(angle, 0, 0, 1);   
ofRect(-myWidth/2, -myLength/2, myWidth, myLength);   

Note you should probably be subtracting 90 degrees from your angle if you are also doing that in your calculations.