Jump to content
  • Advertisement
Sign in to follow this  
Aleister55

Converting a vector to an angle?

This topic is 4190 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Basically I have a 2d game world in opengl, where I want to be able to convert the point in the world which I've selected for the player to move to, into an angular value which I then multiply by a speed every frame. The trouble is, I don't quite have a grasp on how to flawlessly do this, here's the code I've used below which there is obviously something wrong with because its not working properly. float angle; float radians void move_player() { Point 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) * SPEED; position.y += cos(radians) * SPEED; }

Share this post


Link to post
Share on other sites
Advertisement
why don't you just store a unit vector for the 'direction' he's moving, then position += direction * speed * deltaT;

Share this post


Link to post
Share on other sites
i'm not sure, but aren't you converting that 'moving vector' to angle and after that BACK to the same vector?

if so, how about to just normalize it?

vec2 norm(vec2 v)
{
float d = sqrt(v.x*v.x + v.y*v.y);
if (d == 0) return vec2(1, 0); // or what..
return vec2(v.x/d, v.y/d);
}


[if i'm wrong, then sry, i didnt sleep for a while]

Share this post


Link to post
Share on other sites
Quote:
Original post by psycho_svk
i'm not sure, but aren't you converting that 'moving vector' to angle and after that BACK to the same vector?

if so, how about to just normalize it?



First of all I'm not sure if you meant to ask whether or not I converting the moving vector to an angle and then back again or if you were suggesting that that's all I needed to do. Secondly would you mind being more explicit in what I should to correct my code. Please keep in mind that I am terrible at math and struggle to grasp most of these concepts which may come easy to many of you.

Thanks a lot.

Share this post


Link to post
Share on other sites
hm, dont know how to explain it without actually doing all the programming for you..

to get the distance from current position to target position, you have to do this:
distance = target_position - current_position;

now distance is vector, which points to the destination position
that is exactly what you need, innit? orientation vector to add to the position
you want to multiply it with the speed (because different objects have different speeds)
so if some object's speed is 2, then you want to move it for example 2 units per second
in order to get vector which is 2 units long (and aims towards destination point), you have to mupltiply unit vector of length one with the speed
you will get unit vector by normalizing the distance vector (normalization means to keep orientation, but change vector length to one - see the function i wrote for you in previous submission)

so once again, what you need:

distance = target_position - current_position;
orientation = normalize(distance);
add = orientation * speed;
// btw, you should multiply 'add' by frametime: add *= frametime;
object_position += add;

i hope you got it now..

Share this post


Link to post
Share on other sites
I think you misunderstood my intent. If you review my code again you'll notice that I'm purposefully not going the route which would entail normalizing to get an orientation vector, though I am quite aware of it. My code takes a single angular value as input( not a vertex ). What I'm simply trying to is figure out this angular value using only the below vertex. :

vertex1.x = dst_x - pos_x;
vertex1.y = dst_y - pos_y;


Again, I am very aware of the other ways of going about this but I have chosen this one due to certain designs and constraints inherent in my engine.



Share this post


Link to post
Share on other sites
Quote:
Original post by Aleister55
Anyone, please?
I didn't read the thread that carefully, but I did notice a couple of things:
// Typically you would want (y,x) here rather than (x,y). I'm not sure
// if that has anything to do with your problem, but I thought I'd mention it.

angle = atan2(vector.x, vector.y);

// At this point, 'angle' is in radians...

radians = PI / 180 * angle;

// Now it's...um...some meaningless value? It looks like you're converting
// to radians a value that's already in radians.

// In other words, try ditching the 'radians' variable, and just replace
// 'radians' with 'angle' in the following two lines.

position.x -= sin(radians) * SPEED;
position.y += cos(radians) * SPEED;

// That said, there's little point in computing an angle using atan2(),
// and then turning around and computing a (possibly scaled) normalized
// vector using sin() and cos(). If that's what you're after, just subtract
// the start point from the end point, normalize, and scale by SPEED (I
// think this was already mentioned earlier in the thread).

Share this post


Link to post
Share on other sites
Quote:
Original post by jyk
Quote:
Original post by Aleister55
Anyone, please?
I didn't read the thread that carefully, but I did notice a couple of things:
// Typically you would want (y,x) here rather than (x,y). I'm not sure
// if that has anything to do with your problem, but I thought I'd mention it.

angle = atan2(vector.x, vector.y);

// At this point, 'angle' is in radians...

radians = PI / 180 * angle;

// Now it's...um...some meaningless value? It looks like you're converting
// to radians a value that's already in radians.

// In other words, try ditching the 'radians' variable, and just replace
// 'radians' with 'angle' in the following two lines.

position.x -= sin(radians) * SPEED;
position.y += cos(radians) * SPEED;

// That said, there's little point in computing an angle using atan2(),
// and then turning around and computing a (possibly scaled) normalized
// vector using sin() and cos(). If that's what you're after, just subtract
// the start point from the end point, normalize, and scale by SPEED (I
// think this was already mentioned earlier in the thread).





Let me be more clear with what I'm trying to do. Ignore the atan2() for a minute, what I want is to be able to move the player by only providing the angle, which works fine with the previous code(excluding atan2()) if I type in the angle manually(given the x and y may be inversed or whatever, doesnt reall matter). What I want, is to be able to figure out the angle I needed in order to move the player toewards the destination which he clicked on. And I assumed tbhe way to do this was somehow through the vector:

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

now the reason I was using(or misusing) atan2 was because it was suggested to me by someone else, but thats besides the point. I need to know how to determine the angle to input into:

radians = PI / 180 * angle;


That's all. My question is how do I determine this angle from the vector variable above?

Share this post


Link to post
Share on other sites
Quote:
Original post by Aleister55
My question is how do I determine this angle from the vector variable above?
You were doing it right. The only issues were that the arguments to atan2() were transposed (although this may be ok, depending on what conventions you're using for orientation and rotation), and that you're converting a value to radians that's already in radians (the value returned from this conversion is essentially meaningless, and is almost certainly throwing off your results).

If that doesn't answer your question, you might have to explain it a bit more clearly (or at least in different terms). There might also be some confusion regarding units here - all C/C++ math functions work with radians, so there's no need to perform any conversions at all in your given example.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!