**1**

# Aligning an object to a path

###
#1
Members - Reputation: **230**

Posted 06 March 2011 - 03:08 AM

Long time since I last posted around here.

I'm trying to make a object follow a certain path using opengl and C++.

The path I created is an 8 figure and as we speak, my object can follow the path through translation but I'm having a very hard time figuring out how to make sure the front of my object faces the direction of the path as it moves forward.

Basically, what I'm trying to figure out is how to rotate my object so that it faces the direction of the tangent of the position at which it is at.

The only thing I have ended up with so far is trying to figure out the angle between the 2 points by making those 2 points the ending points of vectors both having the origin as a starting point and calculating the angle between those and rotating my object. It doesn't well at all and I believe I do not have the proper mathematical knowledge to achieve what I want to do.

What should I look for in order to make my object face the proper direction when moving ?

Thanks!

###
#2
Crossbones+ - Reputation: **5587**

Posted 06 March 2011 - 04:12 AM

How to compute the tangent ... depends on what kind of path you have. If you have a curve that can be differentiated, then you may use the 1st derivative. The mathematical approach can be read in the Frenet Formulas article on wikipedia. Of course, you can ever step a little bit forward and a little bit backward on the curve and use the difference vector after normalization as approximation.

###
#3
Members - Reputation: **230**

Posted 06 March 2011 - 12:15 PM

I'm currently using a Lemniscate of Bernoulli for my path. The link also provides both the first and second derivatives but other than that, I have no clue how to obtain the exact information I want or what kind of information.

I find the Frenet-Serret Formulas extremely confusing. The issue also arise from the fact that I have no clue what is my T N and B information from the what I have in my scene or how to obtain it and what to do with it.

I understand the logic behind it from the formula but I can't relate this to the code with my path and objects.

The only thing I understand right now is that my parameter t will provide me with the proper X and Z position on the curve. After that I have no clue numbers I must get from what I have and where to plug them to get the rest of the data I need.

Where should I start?

###
#4
Crossbones+ - Reputation: **5587**

Posted 06 March 2011 - 01:21 PM

T(t) = r'(t) / ||r'(t)||

is mentioned (the r' means the derivative dr/dt).

The T is the normalized tangent vector along the curve, the N is the normal (i.e. it is perpendicular to the curve at location t) with a relation to the curvature of the curve at location t, and B is the bi-normal (i.e. also perpendicular to the curve at t). All together, the point at location t, T, N, B define a Cartesian axis, i.e. a local co-ordinate system similar to one used for e.g. model vertices.

Assume that you have a unit-length forward vector

**f**and a unit-length up vector

**u**and both are perpendicular (so

**f**.

**u**=

**0**). Then you can compute a side vector

**s**using the cross-product

**s**:=

**f**x

**u**

These 3 vectors are pairwise perpendicular and all of unit length. So they build a basis, written as the column vectors (or row vectors, depending on whether you use column or row vector math) of a matrix

[

**s f u**]

and you'll have the rotational part of a local space transformation. Append a position

**p**and you have the full transformation matrix:

[

**s f u p**]

If the forward vector

**f**and the up vector

**u'**are not perpendicular by itself, you can compute the side vector as usual but with a additional normalization

**s'**:=

**f**x

**u'**

**s**:=

**s'**/ ||

**s'**||

and compute the actual up vector from them

**u**:=

**s**x

**f**

So the question is how to get a pair of those vectors.

One way (the simple one) is to compute the forward vector as difference vector between two points on the curve that are both close to the current point

**p**at location t

_{0},

**p**:=

**r**( t0 )

so that

**d'**:=

**r**( t

_{0}+ 0.05 ) -

**r**( t0 - 0.05 )

**d**:=

**d'**/ ||

**d'**||

and choosing an up vector

**u'**, e.g. [ 0 1 0 ] in your case where the curve is in x-z. Then follow the steps above. Of course, picking a delta (here 0.05) is a bit of voodoo.

Another, more exact way would be to compute dx/dt and dy/dt from your curve to determine the tangent T and use it as forward vector. Choose an up vector like above and complete the matrix as usual.

The 3rd way would be to compute the 2nd derivatives as well to yield in N, and use that as side vector.

Now it's up to you to decide how precise you want the tangent to be, and whether the up vector should be global or curvature dependent or what else. Its easy to mix up the vectors in the above, so check what I wrote if you want to use it.

###
#5
Members - Reputation: **230**

Posted 08 March 2011 - 12:29 PM

The linked article does show derivatives dx/dy and dy/dx, but you're using the parametric form of the curve. Hence you need dx/dt and dy/dt. Setting in t into the both derivatives gives you the x and y components of the unnormalized tangent (which you are using as x and z components, if I understood your latest post correctly). The Frenet Frame article shows this in the section "Other expressions of the frame", where

T(t) = r'(t) / ||r'(t)||

is mentioned (the r' means the derivative dr/dt).

The T is the normalized tangent vector along the curve, the N is the normal (i.e. it is perpendicular to the curve at location t) with a relation to the curvature of the curve at location t, and B is the bi-normal (i.e. also perpendicular to the curve at t). All together, the point at location t, T, N, B define a Cartesian axis, i.e. a local co-ordinate system similar to one used for e.g. model vertices.

Assume that you have a unit-length forward vectorfand a unit-length up vectoruand both are perpendicular (sof.u=0). Then you can compute a side vectorsusing the cross-products:=fxu

These 3 vectors are pairwise perpendicular and all of unit length. So they build a basis, written as the column vectors (or row vectors, depending on whether you use column or row vector math) of a matrix

[s f u]

and you'll have the rotational part of a local space transformation. Append a positionpand you have the full transformation matrix:

[s f u p]

If the forward vectorfand the up vectoru'are not perpendicular by itself, you can compute the side vector as usual but with a additional normalizations':=fxu's:=s'/ ||s'||

and compute the actual up vector from themu:=sxf

So the question is how to get a pair of those vectors.

One way (the simple one) is to compute the forward vector as difference vector between two points on the curve that are both close to the current pointpat location t_{0},p:=r( t0 )

so thatd':=r( t_{0}+ 0.05 ) -r( t0 - 0.05 )d:=d'/ ||d'||

and choosing an up vectoru', e.g. [ 0 1 0 ] in your case where the curve is in x-z. Then follow the steps above. Of course, picking a delta (here 0.05) is a bit of voodoo.

Another, more exact way would be to compute dx/dt and dy/dt from your curve to determine the tangent T and use it as forward vector. Choose an up vector like above and complete the matrix as usual.

The 3rd way would be to compute the 2nd derivatives as well to yield in N, and use that as side vector.

Now it's up to you to decide how precise you want the tangent to be, and whether the up vector should be global or curvature dependent or what else. Its easy to mix up the vectors in the above, so check what I wrote if you want to use it.

Thanks for the in depth information. I wanted to have tried something before posting back. I have to admit the math isn't too easy for me but manageable on paper but relating this to OpenGL is confusing as hell.

So far my train is following the curve but it's a bit crappy at points. At 4 spots on my curve, the train suddenly flips over 180degrees and still follows the curve backwards and then will flip back again when it reached another one of those points.

I'll explain what I'm doing. Perhaps you could see what is wrong.

At the moment. I keep the position X and Z as global variables by obtaining the value from T in my parametric equations( Y is always 0). Then, using both points from the position, I pass them in their respective derivative to obtain a set of points that is a vector tangent to the X Z position on my curve.

I then normalize the tangent vector by its length but I'm not sure I'm doing this part right. I know the magnitude of the vector is sqrt(x^2+y^2+z^2) but isn't that only we have a point (X,Y,Z) and the other at (0,0,0) ? At the moment, I have that my length is sqrt(X^2+Z^2) since Y is always 0.

Following from this, I calculate the dot product between my new derived points and the vector (-1,0,0) which is the direction vector in which my train is pointing at initially or pretty much all the time before being translated and rotated.

With the dot product result, I calculate the rotation angle = acos(dotproduct)*(180/pi) and use this angle to rotate my train after I translated it at the proper position. It's not perfect though and no matter how long I try to figure out what is wrong I have no clue what else to do.

Also, For the train flipping issue. I just managed to do a kind of hackjob that detects if the angle change is large and toogle a boolean that I check to rotate the train back 180 degree while it is on. I would like to get rid of this too with proper calculations.

I've also tried to have my camera follow the train from the back but I have no idea how to achieve this. The camera can follow the train from it's origin by changing the eye position but if I try to move the camera as well it becomes erratic and goes crazy.