# Caluclating a characters heading

## Recommended Posts

discodowney    102
Im doing obstacle avoidance and i need to know a characters heading. No doubt i was supposed to be keeping track of this all along, but it hasnt come up until now. So im wondering how i work out a characters heading?

##### Share on other sites
alvaro    21266
How do you expect anyone to answer that? What do you know? How do you want the heading expressed? Can you post an example?

##### Share on other sites
discodowney    102
yeah sorry.

Okay. Its all in 3D space, so for example. If i have a character at (0,0,0) and they are moving to (10,0,0). The velocity is worked out from a path following algorithm. I also know the characters maxSpeed and acceleration, but i dont imagine theyll have any influence. So can the heading be caluclated from this?

##### Share on other sites
jyk    2094
Quote:
 Okay. Its all in 3D space, so for example. If i have a character at (0,0,0) and they are moving to (10,0,0). The velocity is worked out from a path following algorithm. I also know the characters maxSpeed and acceleration, but i dont imagine theyll have any influence. So can the heading be caluclated from this?
By 'heading', do you mean a single angle that represents the direction in which the object is facing?

If so, an object in a fully 3-d environment doesn't really have a heading, per se. What do you need the heading for? I'd be willing to bet that whatever it is, it can be done just as easily using (e.g.) the object's local coordinate frame instead.

##### Share on other sites
discodowney    102
Where im gonna use it is in obstacle avoidance. When an obstacle is found it gets the difference between the character and obstacles position. then gets a perpedicular vector from this and the heading so it will move towards that direction away from the obstacle. Ive also seen the heading used in mehtods getting character to rotate to face a specific position.

##### Share on other sites
jyk    2094
Is this basically a 2-d simulation? That is, even though the environment is 3-d (if only graphically), do the agents always move within a plane and maintain a fixed 'up' vector?

##### Share on other sites
discodowney    102
Yes they will always have the same up vector.

[Edited by - discodowney on March 16, 2010 4:46:34 PM]

##### Share on other sites
jyk    2094
Quote:
 When an obstacle is found it gets the difference between the character and obstacles position. then gets a perpedicular vector from this and the heading so it will move towards that direction away from the obstacle. Ive also seen the heading used in mehtods getting character to rotate to face a specific position.
Although I didn't quite follow this, I'm still not convinced that you actually need the heading in angle form (which I assume is what you're looking for).

However, since the up vector is fixed, you should be able to compute a heading angle using an 'atan2' function (assuming you have one available) and the object's current velocity or forward direction vector. As an example, if +z is up, the code might look something like this:
float heading = atan2(forward.y, forward.x);

##### Share on other sites
discodowney    102
Sorry, the heading in the code samples ive been looking at is in Vector form not angle.

##### Share on other sites
jyk    2094
Quote:
 Sorry, the heading in the code samples ive been looking at is in Vector form not angle.
Hm. Well, you must know something about the object's orientation, right? At the very least, you must have a matrix or some angles or something - otherwise you wouldn't be able to render the object very easily.

How is the object's orientation represented? Whatever representation you're using, you should be able to derive a 'heading' vector (by which I assume you mean a forward direction vector) from it.

##### Share on other sites
discodowney    102
This is the problem. the rendering is being done by another guy, who isnt going to be around much over the next week.
So given the orientation how do i caluclate the heading? Is there a formula, or some algorithm?

##### Share on other sites
jyk    2094
Quote:
 So given the orientation how do i caluclate the heading? Is there a formula, or some algorithm?
How to compute the forward direction vector depends on how the orientation is represented, so...how is the orientation represented? A matrix? A quaternion? Euler angles? Spherical angles? A set of basis vectors? A single 'heading' angle?

If you can tell us that, we can tell you how to get the forward direction vector.

kiniport    130

or,

##### Share on other sites
discodowney    102
Orientation is a single heading angle.

##### Share on other sites
jyk    2094
Quote:
Depending on what exactly is meant by 'heading' (which I'm not sure the OP has clarified), it's not a given that an object's heading vector will be aligned with its velocity vector.

That said, the problem as presented isn't really very clear, so who knows, maybe the answer you've given is exactly what the OP is looking for...

##### Share on other sites
jyk    2094
Quote:
 Orientation is a single heading angle.
Ok, then if by 'heading' you mean 'forward direction vector', it would be computed as follows (assuming +z is up - you'll need to adjust accordingly if another axis is considered to be the 'up' axis):
forward.x = cos(heading_angle);forward.y = sin(heading_angle);forward.z = 0;
Is that what you're looking for?

##### Share on other sites
discodowney    102
Yes that is what im looking for. Sorry i didnt make it clearer. You're a hero mate. Really appreciate it.

##### Share on other sites
discodowney    102
Im just wondering what the maths behind these adjustments is. I ask cos, here you are saying z is the up. In mine the y will be up and im wondering how i work out what sin/cos to use?

##### Share on other sites
jyk    2094
Quote:
 Original post by discodowneyIm just wondering what the maths behind these adjustments is. I ask cos, here you are saying z is the up. In mine the y will be up and im wondering how i work out what sin/cos to use?
Check out the Wikipedia article on the unit circle for more info.

As for adjusting for y up, you simply need to swap and/or negate the sine and cosine terms as appropriate to get the results that you want. However, if the simulation is basically 2-d with 3-d graphics, I'd recommend making z up, as it'll make things easier overall.

## Create an account or sign in to comment

You need to be a member in order to leave a comment

## Create an account

Sign up for a new account in our community. It's easy!

Register a new account