iDevGames Forums
Convert vector to angle? - Printable Version

+- iDevGames Forums (http://www.idevgames.com/forums)
+-- Forum: Development Zone (/forum-3.html)
+--- Forum: Game Programming Fundamentals (/forum-7.html)
+--- Thread: Convert vector to angle? (/thread-3215.html)

Pages: 1 2 3


Convert vector to angle? - Leroy - Jun 22, 2007 03:45 PM

How do I convert a vector with a begin and endpoint into a angular value in a 2d coordinate system?


Convert vector to angle? - unknown - Jun 22, 2007 04:00 PM

tan(angle) = opposite/adjacent, in a right angle triangle.

x is one side, y is another, your vector is the hypotenuse.

use atan (inverse tangent) to find the angle given +x/+y, then check if x and y are positive or negative to get your rotation in the correct quadrant.

edit: ah, found it!
this thread should be interesting to you http://www.idevgames.com/forum/showthread.php?t=10652


Convert vector to angle? - Leroy - Jun 22, 2007 04:07 PM

Would you mind writing that out in code for me, I am absolutely terrible when it comes to any kind of math.

Thanks.


Convert vector to angle? - Skorche - Jun 22, 2007 04:44 PM

It's pretty tricky. Rasp
Code:
atan2(y, x)



Convert vector to angle? - unknown - Jun 22, 2007 05:44 PM

Leroy Wrote:Would you mind writing that out in code for me, I am absolutely terrible when it comes to any kind of math.

Thanks.

The link I gave you...


Convert vector to angle? - PowerMacX - Jun 22, 2007 07:57 PM

Would you mind clicking that link for me? Rasp


Convert vector to angle? - Leroy - Jun 22, 2007 10:43 PM

PowerMacX Wrote:Would you mind clicking that link for me? Rasp

I'm not good with with math or linksCry Well, thanks for the link, I guess I was in a rush and didn't notice it the first time.


Convert vector to angle? - Leroy - Jun 23, 2007 01:45 PM

Ok, I'm still very confused about this. Here is the code I'm using(or at least its the basic idea) to move the player around, maybe someone could tell me what I'm doing wrong still. Oh, and everything is done in the upper right quadrant of opengl.

float angle;
float radians

void move_player()
{

NSPoint vector;

vector.x = new_world_dst.x - player.x;
vector.y = new_world_dst.y - player.y;

angle = atan2(vector.x, vector.y);
radians = PI / 180 * angle;
position.x -= sin(radians) * SPRITE_SPEED;
position.y += cos(radians) * SPRITE_SPEED;
}


Convert vector to angle? - IBethune - Jun 24, 2007 04:12 AM

atan2(y, x) is not the same as atan2(x, y)

The first is right, the second is wrong.


Convert vector to angle? - Leroy - Jun 24, 2007 02:12 PM

IBethune Wrote:atan2(y, x) is not the same as atan2(x, y)

The first is right, the second is wrong.

Actually I'm using the second on and it works just fine now.


Convert vector to angle? - OneSadCookie - Jun 24, 2007 02:16 PM

That's 'cos you're *also* using -sin for x and cos for y, where you should be using cos for x and sin for y Rasp


Convert vector to angle? - Leroy - Jun 24, 2007 03:26 PM

OneSadCookie Wrote:That's 'cos you're *also* using -sin for x and cos for y, where you should be using cos for x and sin for y Rasp

I'm well aware, but it works out to the same thing. Is there some hidden danger though in doing it this way or is this just bad coding practice(which I can live with)?


Convert vector to angle? - OneSadCookie - Jun 24, 2007 06:12 PM

The hidden danger is that the first person to sit beside you and read your code will clout you so hard around the head that you'll be in a coma for a month.

Just stick to convention!


Convert vector to angle? - Nevada - Jun 24, 2007 07:37 PM

OneSadCookie Wrote:The hidden danger is that the first person to sit beside you and read your code will clout you so hard around the head that you'll be in a coma for a month.

Just stick to convention!

LOL. Readability is good. You will probably smack yourself if you ever return to this code later on. I've had this happen to me on more than one occasion and decided to stick to clean code from then on.


Convert vector to angle? - Leroy - Jun 24, 2007 09:16 PM

Nevada Wrote:LOL. Readability is good. You will probably smack yourself if you ever return to this code later on. I've had this happen to me on more than one occasion and decided to stick to clean code from then on.

Well considering my near 1st grade math level, and since I can barely interpret that code when written conventionally, it's every bit as confusing to me when I see it invertedRasp