I have some code that is working pretty well to find the angle between two points temp and newpoint:
CGPoint newpoint=CGPointMake(newX,newY );
CGPoint diff = ccpSub(temp.position, newpoint);
float rads = atan2f( diff.y, diff.x);
float degs = CC_RADIANS_TO_DEGREES(rads);
CCLOG(@"now: %f,%f to:%f,%f degs:%f",temp.position.x,temp.position.y,newX,newY,degs);
It correctly works most of the time, but for some points it has errors. Here is part of the log:
2011-12-13 11:05:19.401 Bells[2303:c503] now: 368.603271,147.813110 to:265.566284,210.476471 degs:-30.655481
2011-12-13 11:05:20.417 Bells[2303:c503] now: 77.030159,167.413544 to:184.491669,117.072098 degs:154.254135
2011-12-13 11:05:20.417 Bells[2303:c503] now: 355.119720,121.963585 to:307.802826,49.591988 degs:56.634174
2011-12-13 11:05:20.418 Bells[2303:c503] now: 465.000000,89.771843 to:373.258270,15.000000 degs:39.249508
2011-12-13 11:05:20.419 Bells[2303:c503] now: 307.277588,69.095749 to:363.791168,174.386093 degs:-118.026299
2011-12-13 11:05:21.417 Bells[2303:c503] now: 190.993607,194.947479 to:101.720551,255.007492 degs:-33.194214
2011-12-13 11:05:21.418 Bells[2303:c503] now: 23.930172,27.319054 to:85.588303,15.000000 degs:167.615768
2011-12-13 11:05:21.418 Bells[2303:c503] now: 365.033630,218.904007 to:277.799194,141.605042 degs:41.585751
2011-12-13 11:05:21.419 Bells[2303:c503] now: 221.239227,153.457306 to:116.308037,87.374733 degs:32.344673
2011-12-13 11:05:22.433 Bells[2303:c503] now: 140.302948,260.575500 to:191.313812,208.167419 degs:133.118576
2011-12-13 11:05:22.433 Bells[2303:c503] now: 16.781921,224.473648 to:90.456161,153.657745 degs:135.340363
2011-12-13 11:05:22.434 Bells[2303:c503] now: 307.802826,49.591988 to:391.818512,15.000000 degs:156.793350
2011-12-13 11:05:22.435 Bells[2303:c503] now: 265.566284,210.476471 to:311.953583,114.193481 degs:115.011368
2011-12-13 11:05:22.435 Bells[2303:c503] now: 140.599457,296.268768 to:39.459061,305.000000 degs:-4.328590
2011-12-13 11:05:22.436 Bells[2303:c503] now: 459.438629,190.859222 to:465.000000,96.320923 degs:92.733452
This line in particular is an example of the problem:
2011-12-13 11:05:21.418 Bells[2303:c503] now: 23.930172,27.319054 to:85.588303,15.000000 degs:167.615768
This distance between these two points is very approximately horizontal, thus you’d expect an angle that is quasi horizontal. Yet, the angle is nearly vertical. I’m not a whiz at trig but clearly there is a math issue that is affecting only some points here. Any ideas?
you can use ccpAngle(<#CGPoint a#>, <#CGPoint b#>) or ccpAngleSigned(<#CGPoint a#>, <#CGPoint b#>), and yes there is an angle between 2 points, a point is a vector.
remember that cocos use degrees and math done with CGPoints uses Radians. also remember that cocos uses rotations clockwise and CGPoints angles are anticlockwise.
You can use as suggested helpers as CC_RADIANS_TO_DEGREES