![]() |
|
How to find angle between two points.. |
blargmob
Member #8,356
February 2007
![]() |
Howdy, I think I knew this at one point but forgot Thanks.. --- |
kazzmir
Member #1,786
December 2001
![]() |
atan2((y2-y1)/(x2-x1)); |
blargmob
Member #8,356
February 2007
![]() |
Then I simply pass atan2's return to sin() cos() etc. for velocity vectors correct? --- |
SiegeLord
Member #7,827
October 2006
![]() |
If the only reason you are calling atan2 is to then use the angle in sin and cos functions then you are better off doing the following: float mag = sqrt((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1)); float sine = (y2 - y1) / mag; float cosine = (x2 - x1) / mag; Last time I checked one square root call is much faster than atan2 followed by sin and cos calls. EDIT: And also, appropriate form for atan2 call is: atan2(y2 - y1, x2 - x1);
"For in much wisdom is much grief: and he that increases knowledge increases sorrow."-Ecclesiastes 1:18 |
blargmob
Member #8,356
February 2007
![]() |
Sweet thanks --- |
adam450
Member #5,460
January 2005
|
This question doesnt make sense. You cant find the angle between two points. You have to have two vectors. Now if you have a third point that is a "center" point (maybe your using the middle of the screen?), then you can do it. |
Slartibartfast
Member #8,789
June 2007
![]() |
adam, one vector is the one between both points. The other vector is t*(1,0). ---- |
SiegeLord
Member #7,827
October 2006
![]() |
Judging by the way the angle was meant to be used by the OP, the third point would be (x1 + dx, y1) where dx is a positive number. But yeah, there is no way to answer the question as written. Although, the answer to the question that must have actually been asked is there. "For in much wisdom is much grief: and he that increases knowledge increases sorrow."-Ecclesiastes 1:18 |
Elverion
Member #6,239
September 2005
![]() |
Quote: Last time I checked one square root call is much faster than atan2 followed by sin and cos calls. Just checked this out. The code you gave runs roughly 3 times faster than this:
-- |
Trezker
Member #1,739
December 2001
![]() |
I don't understand all this about a third point and second vector... |
SiegeLord
Member #7,827
October 2006
![]() |
From geometry, an angle is always defined by two vectors. When only one vector is specified, then by convention we take the second vector to be the positive x axis. This vector can be of any length (hence the dx), but for ease of calculation you generally pick its length to be 1 so that you can pretty much forget about it. http://www.allegro.cc/files/attachment/593373 EDIT: (Of course this is somewhat inaccurate, as atan2 is more general than that, but since any vector pair can be rotated so one of them coincides with the x axis, this still applies) "For in much wisdom is much grief: and he that increases knowledge increases sorrow."-Ecclesiastes 1:18 |
|