Convert vector to angle?

Member
Posts: 105
Joined: 2007.03
Post: #1
How do I convert a vector with a begin and endpoint into a angular value in a 2d coordinate system?
Quote this message in a reply
Sage
Posts: 1,403
Joined: 2005.07
Post: #2
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

Sir, e^iπ + 1 = 0, hence God exists; reply!
Quote this message in a reply
Member
Posts: 105
Joined: 2007.03
Post: #3
Would you mind writing that out in code for me, I am absolutely terrible when it comes to any kind of math.

Thanks.
Quote this message in a reply
Sage
Posts: 1,482
Joined: 2002.09
Post: #4
It's pretty tricky. Rasp
Code:
atan2(y, x)

Scott Lembcke - Howling Moon Software
Author of Chipmunk Physics - A fast and simple rigid body physics library in C.
Quote this message in a reply
Sage
Posts: 1,403
Joined: 2005.07
Post: #5
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...

Sir, e^iπ + 1 = 0, hence God exists; reply!
Quote this message in a reply
Moderator
Posts: 771
Joined: 2003.04
Post: #6
Would you mind clicking that link for me? Rasp
Quote this message in a reply
Member
Posts: 105
Joined: 2007.03
Post: #7
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.
Quote this message in a reply
Member
Posts: 105
Joined: 2007.03
Post: #8
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;
}
Quote this message in a reply
Member
Posts: 156
Joined: 2002.10
Post: #9
atan2(y, x) is not the same as atan2(x, y)

The first is right, the second is wrong.
Quote this message in a reply
Member
Posts: 105
Joined: 2007.03
Post: #10
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.
Quote this message in a reply
Luminary
Posts: 5,143
Joined: 2002.04
Post: #11
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
Quote this message in a reply
Member
Posts: 105
Joined: 2007.03
Post: #12
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)?
Quote this message in a reply
Luminary
Posts: 5,143
Joined: 2002.04
Post: #13
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!
Quote this message in a reply
Member
Posts: 100
Joined: 2006.05
Post: #14
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.
Quote this message in a reply
Member
Posts: 105
Joined: 2007.03
Post: #15
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
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  Formula for converting angle to vector? komirad 2 9,858 Jul 29, 2011 07:29 AM
Last Post: ThemsAllTook
  Question Regarding the Reflect Angle of a Transition iBaby 3 3,511 Apr 27, 2010 03:15 PM
Last Post: JustinFic
  ending location from angle and speed Kazooless 5 4,482 Apr 3, 2009 02:40 PM
Last Post: Gillissie
  Angle between two points? Graphic Ace 6 5,374 Nov 8, 2008 12:11 PM
Last Post: macnib
  calculating X and Y coordinates w/ an angle and distance ferum 13 14,904 Jun 25, 2008 10:53 PM
Last Post: rosenth