Math question

Started by
7 comments, last by Hostility 22 years, 8 months ago
given three points a,b,c a being (1.98675, 0.0, -1.72185) b being (1.94939, 0.0, 7.68485) c being (11.2583, 0.0, -0.132453) how could I figure out how to glRotate (with a being the point of rotation) so that line ab and line ac match up? Sorry if I''m wording any of this wrong. Hope that''s clear enough for someone to be able to help me. Thanks in advance to anyone who can help me out. Any help will do. -Hostile
Advertisement
Find the angle between vectors AB and AC, then rotate point c (-Angle) about the y-axis of point a.
Thanks for the reply...

Well, I don''t know the math behind finding the angle between the two, and also you should know that in this example I was lucky enough to have the y values all 0, but that most certainly will not always be the case in the program I''m doing.

Again, any comments are welcome.
Here's how to find the angle between two vectors.

If A and B are vectors, the formula you use is this:

cos(angle)= A.B / (|A | * |B |)

By |A |, I mean the length of the vector A (between the two coordinates of the vector), which is simply sqrt((x2 - x1)^2 + (y2 - y1)^2 + (z2 - z1)^2).

By A.B , I mean the dot product of vectors A and B . You can find that by taking your two vectors and finding the differences in the x, y, and z components of the endpoints. (For example, for YOUR vector AB, the components would be (rounded) x = 0.04, y = 0, z = 9.4.) So if vector A 's components are {a1, b1, c1} and vector B 's components are {a2, b2, c2}, then the dot product A.B is

a1 * a2 + b1 * b2 + c1 * c2

Now all that's left for you to do is do the division and the arc cosine, both of which should be trivial.

Hope this helps!

~ Dragonus

Edited by - Dragonus on August 9, 2001 9:56:35 AM

If I understand what you're asking, you want to know: Given three points (a, b, c), the rotation angle ( θ ) and axis through point a (W) that would rotate the vector [ba] (U) to point in the same direction as the vector [ca] (V).

This is a simple matter of some vector algebra. Given these three points:

    a = [ 1.98675, 0.0, -1.72185 ]    b = [ 1.94939, 0.0,  7.68485 ]    c = [11.2583,  0.0, -0.132453]

We define the two vectors and obtain their lengths:

    U = [b[0] - a[0], b[1] - a[1], b[2] - a[2]]||U|| = sqrt(u[0] * u[0] + u[1] * u[1] + u[2] * u[2])    V = [c[0] - a[0], c[1] - a[1], c[2] - a[2]]||V|| = sqrt(v[0] * v[0] + v[1] * v[1] + v[2] * v[2])     U = [-0.03736,  0.0,    9.4067]||U|| = 9.40677    V = [ 9.27155,  0.0,    1.5894]||V|| = 9.4068

Using the dot-product we can obtain the angle between these two vectors.

U ⋅ V = u[0] * v[0] + u[1] * v[1] + u[2] * v[2]    θ = arcCos( (U ⋅ V) / (||U|| * ||V||) )     θ = 80.5°

This gives us an angle between 0° and 180° (remember to convert from radians to degrees). If the angle between the two vectors in one rotational direction is between 180° and 360°, then the direction of the axis vector would be reversed, which reverses the direction of rotation and keeps it between 0° and 180°.

The axis of rotation is orthogonal to both of these vectors and is obtained by performing a cross-product on them.

    W = [u[1] * v[2] - u[2] * v[1], u[2] * v[0] - u[0] * v[2], u[0] * v[1] - u[1] * v[0]]     W = [0.0, 87.2741, 0.0]

If you wanted to normalize the axis vector (glRotatef() does this for you), you would simply divide the components by the vector length. No big deal in this case since the axis turns out to be the Y-axis.

||W|| = sqrt(w[0] * w[0] + w[1] * w[1] + w[2] * w[2]) ||W|| = 87.2741    W = [0.0, 1.0, 0.0]

The call to the rotation function would look like this:

    glRotatef(80.5, 0.0, 1.0, 0.0);

I hope that helps.



Edited by - LlelanD on August 9, 2001 10:42:54 AM
thx guys, you''ve helped me tremendously!
could we not make a lib or something with these things?
might be helpfull for others.
something where everyone can contribute.
Either that or I was thinking a .h/.cpp file so they could actually see the source code included. And I''d be willing to help.

What would you think we''d need for it besides vector calculations (angle between two vectors, length of a vector, dot product, cross product, projections?) and matrix calcs (rotations, translations, multiplication, addition?, identity)?

~ Dragonus
it''s a start.
i got a nice little func here that basicly works like
glulookat, you give it 2 points in space and it gives you the x/y rotations needed. i''m using it for my space game.
why not make multiple *.h''s that deal with different needs.
like physics, math, collision detection, whatever!
usually it is quite hard to find such functions, or even
figure out how to write them yourself.
thoughts?

Stephan-Frank HENRY
-------------------
Frank.Henry@gmx.net

This topic is closed to new replies.

Advertisement