# Finding the forward vector

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

## Recommended Posts

Sure, Sounds easy enough. And maybe it is, but i cant figure it out. Perhaps my math skills are on the weak side. I have an objects position i have the normal of the plain below him(which is not necessarily his normal) I have the objects Yaw based on the worlds Y axis naturally. Thats it. I know it can be done, and ideally with as little trig as possible as I think trig is ugly. But if it cant be avoided, oh well. I would appreciate an help. Please no code, thats not what im here for. But a plan of attack and maybe some psuedocode would be great. I dont care if i find the forward or the right facing vector, but i need one of them. Regards

##### Share on other sites
Is the character always aligned with the world up axis? Also, can you clarify what you mean by forward vector? Do you mean the vector that moves the character in the direction he's facing, but also up or down depending on the orientation of the terrain beneath him?

##### Share on other sites
his normal, is not always the world Y, he can be rotated any direction

I just want one of the other two vectors, not his normal, either his look at vector, or the vector protruding out his right side. If i had one of these, i could figure out his orientation completely, but i am not sure how to go from point and yaw to a directional vector.

##### Share on other sites
You can't do it. There isn't enough information. Consider the two cases of a guy located at the origin facing north and looking upwards at a 45 degree angle, and another guy located at the origin facing north and looking downwards at a 45 degree angle. These guys have the same position, yaw, and "plane below them", but have different forward vectors.

##### Share on other sites
You should know your righ vector first before you could get your forward vector. Just do a cross and viola. But as they said, you can't calculate your forward vector unless given more info.

##### Share on other sites
what other information would i need? i need to get this guy to follow terrain. I can find the yaw, pitch, and roll of the plane. I have the guys position and yaw. His velocity(which doesnt help as it can be forward and backward). I know I should store his normal from the beginning, but I dont.
So should i go back and store it when I know what his position and orientation is(init), or can i some how figure it out without storing it. If i store it i have to store that same info on hundreds of other objects which i dont need to do/wont use it.
Edit:
dont have other vec for the guy, but im storing the previous pitch yaw and roll as well.

##### Share on other sites
On a planar terrain, you could just subtract the objects last postition to your new position to get the forward vector. As for following the terrain, there are different approaches for that.

##### Share on other sites
Quote:
 Original post by Tang of the MountainI have the guys position and yaw.

Err.... so when he looks up or down, what data changes?

##### Share on other sites
Quote:
 Original post by Tang of the MountainI can find the yaw, pitch, and roll of the plane...
Ack :-| Forget about Euler angles - they aren't the right tool for this job. You're on the right track with the rest of your post though, with tracking the character's orientation from frame to frame. You mentioned being concerned about storing that data for hundreds of objects. Unless your available memory is very limited, that really shouldn't be a problem; and if you're really concerned about it, you can always store the orientation as a quaternion instead (or otherwise compress the data).

One consideration is that 'following the terrain' can mean a couple of different things. A humanoid character might always be aligned with the world up axis, and just have the vertical component of his position determined by the terrain. On the other hand, a vehicle might also orient itself to be aligned with the terrain normal. It sounds like it's the latter that you want.

The standard way to do this is to track the orientation from frame to frame, and each frame apply a rotation to align the object's local coordinate system with the normal of the current triangle. Also, you'll want to interpolate to the new orientation; otherwise the object will 'snap' when going over triangle boundaries.

##### Share on other sites
so i created a forward facing vector and init it to 1 0 0 cause x is forward facing

on every pass through update
convert yaw to a rotation matrix
mult by forward vec
stick that in forawrd vec

this, seems right, but at the same time, it isnt accounting for the other euler angles

I was going to do this
yaw = phi
X = F
Y = U
Z = R

F' = cos(phi) F + sin(phi) U
U' = cos(phi) U - sin(phi) F
R' = R

but are they the same thing? Am i missing something? will this break the second i gain some pitch or roll? or am I WAAY off base?

##### Share on other sites
Why are you doing this? Don't build up your orientation data in such a strange, piecemeal manner. It gains you nothing. Store your orientation as euler angles if you don't intend to be doing any rotations that can't be easily expressed with them, or a quaternion if most of your rotations will be object-local.

##### Share on other sites
ok, i store euler angles, i store position as noted above

my problem is finding a forward vector with them....i cant..

i only have the yaw and position for my terrain following func...the rest are old roll and pitch, which isnt helpful....thus not enough data as they stated...

so I create a forward vector so I can actually find out where i need to be facing when I terrain follow...

storing eulers is what I need to do, after i get the new right and up vectors from forward and polygons normal i then have to extract euler angles...

My initial problem was a lack of information, which I was hoping the previous post would fix, giving me one more piece of the puzzle. What I wanted to know was if I was rotating the vector correctly, or not? Will I be able to use the vector i created above along with my yaw and the polygon normal to figure out where i should be rotated to follow the polygon correctly?

##### Share on other sites
Why do you have a lack of information? It's your system. Re-architect it so that you have the information you need. ex:
Define a default forward vector as (1.0,0.0,0.0).
Create a rotation matrix with your euler's.
Transform the default forward vector by said matrix.
result: the current forward vector.

Orientation should be a central piece of your code. How can you move the object if you don't know the forward vector? To move you apply an acceleration/velocity in the direction of the forward vector...

Basically it sounds like you need to re-write your base class so you have access to all the information you need.

-me

##### Share on other sites
Quote:
 Original post by Tang of the Mountainok, i store euler angles, i store position as noted above

If you have the euler angles, you have the forward vector. Just compose your rotation matrix, then transform the global forward-vector by that rotation.

##### Share on other sites
Quote:
 Original post by Tang of the Mountainok, i store euler angles, i store position as noted abovemy problem is finding a forward vector with them....i cant..i only have the yaw and position for my terrain following func...the rest are old roll and pitch, which isnt helpful....thus not enough data as they stated...so I create a forward vector so I can actually find out where i need to be facing when I terrain follow...storing eulers is what I need to do, after i get the new right and up vectors from forward and polygons normal i then have to extract euler angles...My initial problem was a lack of information, which I was hoping the previous post would fix, giving me one more piece of the puzzle. What I wanted to know was if I was rotating the vector correctly, or not? Will I be able to use the vector i created above along with my yaw and the polygon normal to figure out where i should be rotated to follow the polygon correctly?
If you get this working using Euler angles, let me know - I'll be curious as to how you did it :-) Otherwise, take the advice of myself and others and ditch the Euler angles!

No one ever has to use Euler angles. If you're unfortunate enough to be saddled with an API or third-party library that only uses Euler angles, then convert to them right before you transfer data to the API, and convert from them as soon as you transfer the data back. But there's no reason at all to use them on your end if they're not the best tool for the job, and again, they're not the best tool for this job.

##### Share on other sites
This code hasn't been tested, but it should work.
Depending on your conventions, you may need to change it around slightly.

Vector3 CalcForward(Vector3 up, float yaw){    // The right vector will always be parallel to the xz-plane.    Vector3 right;    right.x = cos(yaw + PI/2.0f)    right.y = 0.0f;    right.z = sin(yaw + PI/2.0f)    // The forward vector is perpendicular to both the up vector    // and the right vector.    Vector3 forward = Vector3::CrossProduct(up, right)    return forward;}

##### Share on other sites
this is the plan me and a fellow mathmetician wannabe came up with

right = (0, 1, 0)
right * YawRotationMatrix
right cross polyNormal = forward
forward * yawRotationMatrix to get true forward
polyNormal cross forward to get true right
build quaternion out of poolyNormal, forward, right
extract euler
bake for 45-50 minutes on 350
serves 10-12

if this looks broken, ill try the method above for finding forward

##### Share on other sites
Quote:
 Original post by Tang of the Mountainright = (0, 1, 0)

Er... are you sure the positive Y-axis points to the right?
Quote:
 right * YawRotationMatrix

Okay. Assuming you meant right = right * YawRotationMatrix (and assuming you're using a row-vector representation so you don't have to do right = YawRotationMatrix * right) the new "right" vector will be yawed (it will point north or south-ish, instead of east) and still be perpendicular to the ground.
Quote:
 right cross polyNormal = forward

Uh... okay, this produces a "forward" vector which is yawed by the Yaw rotation matrix but is not affected by pitch or roll. Of course, it's a useless step; you could just transform the global forward vector by the yaw transformation.
Quote:
 forward * yawRotationMatrix to get true forward

Uh, no. What the hell? What are you talking about with this line?
Quote:
 polyNormal cross forward to get true right

Now you're just making stuff up.
Quote:
 build quaternion out of poolyNormal, forward, right

Very funny.
Quote:
 extract euler

Oh, sure! And then build a rotation matrix from the euler angles, then extract a quaternion from THAT, and then do some more stuff with yaw!

I will try to make this clearer. You are doing things which are inefficient, overly complicated, and just plain wrong. You are wasting your time on doing stupid stuff. Throw out your strange ideas about how to represent orientation, and just maintain euler angles or a quaternion.

##### Share on other sites
Quote:
 Original post by Sneftel...
Hehe, very entertaining :-)

@The OP: I must admit it does sort of seem like you're not listening. Why bother posting to a forum such as this if not to take advantage of the feedback you receive? True, sometimes you have to sort the wheat from the chaff, but there are many knowledgeable people here who know what they're talking about, and you'd do well to listen to them.

Now, I'll give this one more shot. Maybe what you want can be accomplished through some convoluted misuse of Euler angles and quaternions, but here is a method that I know, for a fact and from experience, works for aligning a character or vehicle to an arbitrarily oriented surface such as terrain:

1. Maintain the object's orientation as a matrix or quaternion (not Euler angles!)
2. Each frame, take the up vector of the object and the normal of the surface below it...
3. Find the axis-angle combination that rotates the former onto the latter...
4. Apply it to the object's orientation matrix or quaternion...
5. And orthonormalize to prevent drift

Once you get this working, you'll notice that the object 'snaps' from one orientation to another as it goes over triangle boundaries. At that point you can start thinking about adding interpolation to smooth the transitions out (this is easiest with quaternions).

It may be that you're avoiding this method because it involves some math you're not familiar with. Well, c'est la vie; this sort of math is the foundation of computer graphics and 3D simulation, so you'll have to deal with it sooner or later :-)

##### Share on other sites
ok, maintain euler angles

i only have what my euler angles were the last time i made a pass, i need to calc what they are now based on the polygon im clamping to.

i know my yaw, i need to calculate my roll and pitch. This is where i would theorhetically be maintaining them. The question is how. I appreciate the help, but Either im not understanding you, or vice versa.

my position is say 4, 5, 6
my yaw is say: .785
the normal for the polygon im on is 0, 0, 1 which is straight up.(dont ask me about my conventions I didnt make them)

my roll is ?
my pitch is ?
(regardless of what it was the last pass, its changed and is unknown to me as far what I need to do, cause what i need to do is calculate them. I dont change them...they get changed by the terrain only. I can only change the yaw. So they must be calculated to reflect the terrain)

now, im trying to calculate a forward vector so i can use it and the normal to get a right vector. In my mind, this doesnt seem like it will work, so i planned on storing the forward vector and maintaining it but run into the same problem, it never has pitch or roll applied to it.

My normal will eventually become the poly's normal, i get that.

im lookng for a method of grabbing pitch and roll out of what I have to work with.

Im not by any means trying to belittle you or anything, nor am i asking you to stop posting, i dont think criticising me will help. maybe a little, but not as much as youd think.

I gather my math skills arent up to par and i hit all the google news groups before coming here, im not looking for you guys to cure all that ails me. But im sure there has to be a way to do it given my information, im just having trouble finding it.

##### Share on other sites
Quote:
 i know my yaw, i need to calculate my roll and pitch.
No. Stop it. Stop right there. If you're maintaining euler angles for your object, you know the yaw, pitch, AND roll of your object, because that's what euler angles are. (This is the last time I'm going to mention the word "yaw" in this post. I suggest you do the same. It seems to be a concept which is causing you some problems.) It sounds like what you WANT is a method of determining a NEW orientation, with the same heading but parallel to the ground. But it's very important that you explicitly differentiate between the different orientations, because the fact that you're using the same name for multiple things is confusing you (and us).

So here is what you do. First, find the object's forward-vector from its original orientation (which may be stored as a quaternion or euler angles or a rotation matrix). Project that vector onto the plane to produce the plane-forward vector, and normalize it. (This is where a singularity will crop up if you are looking straight up or down.) Produce the plane-right vector as the cross product of the plane-forward vector and the plane-up vector. Use those three vectors as the basis vectors for a rotation matrix. Process that rotation matrix to determine either a quaternion or euler angles (whichever one you're using to store your orientation, or just keep it as is if you're storing your orientation as a rotation matrix) for the new orientation.

EDIT: jyk's method will also work.

[Edited by - Sneftel on March 30, 2006 2:02:07 PM]