Archived

This topic is now archived and is closed to further replies.

Aybara

3D Math Problem - Rotating an Arrow

Recommended Posts

Hi, hopeing someone here can help me out. I''ve been working on this same problem on and off for a long time, and I can''t see any flaws in my idea or in my code, yet it isn''t working as expected. If you have any ideas, please let me know. The problem is this: I''m writing a simple 3d space flight simulation where you can be flying in one direction and looking another (i.e. inertia is there, not your typical starwars-esque sim where you always fly forward). I want to be able to have a 3d arrow drawn somewhere on the display that shows the user which way he is moving relative to which way he is facing. So, I have a 3d arrow modelled that points down the z axis (and is centered at the origin). I''m trying to render it pointing the right way by just setting the world matrix with the appropriate rotation. I''m calculating this matrix as follows: First, I have 2 vectors, my heading vector and my facing vector. I create a quaternion that rotates from the facing vector to the Z axis. I then rotate the heading vector by this quaternion, and make a new quaternion which rotates from the Z Axis to this new rotated heading vector. I''ve tried the same method with matrices and get the exact same result: As long as you are moving along the Z axis, everything seems to work fine. Once your heading vector moves off the Z axis, weird things happen. So, does anybody have any idea what''s wrong with my logic here? I don''t think its code since I ended up reworking the whole thing to use quaternions and got the exact same result I did with matrices. Any help would be much appreciated, this is driving me crazy.

Share this post


Link to post
Share on other sites
try

Quaternion::FromVectors(Vector From, Vector To);

Quat Q0;
Quat Q1;
Quat Q2;

Q0.FromVectors(Vector(0, 0, 1), Facing);
Q1.FromVectors(Vector(0, 0, 1), Heading);

Q2 = -Q0 * Q1;

[edited by - oliii on October 10, 2003 11:21:26 AM]

Share this post


Link to post
Share on other sites
quote:
Original post by oliii
Q2 = -Q0 * Q1;



First, thanks for your help. I really appreciate it. Now, by -Q0 do you mean the inverse of Q0? If so, no go. Seems to be doing the same thing (or something similar) as my old code.

Share this post


Link to post
Share on other sites
Hmmm, you know what? I think the problem is I was misunderstanding exactly what this arrow is supposed to look like when you move, and also how the ship itself would move. I think your method does work (and mine seems to still have issues). Thanks alot!

Oh, and I have no idea what that code is actually doing, so an explanation of how it works would be great If not, I'm ok with leaving a little bit of magic in my code, hehe.

[edited by - Aybara on October 10, 2003 1:54:49 PM]

Share this post


Link to post
Share on other sites
quote:
Original post by Aybara
Hmmm, you know what? I think the problem is I was misunderstanding exactly what this arrow is supposed to look like when you move, and also how the ship itself would move. I think your method does work (and mine seems to still have issues). Thanks alot!

Oh, and I have no idea what that code is actually doing, so an explanation of how it works would be great If not, I'm ok with leaving a little bit of magic in my code, hehe


Right, I'll try not to get confused Frankly, it was kind of a wild guess :D


The orientation of the arrow, in world space is

[O]w is the object orientation in absolute world space
[C]w is the orientation of the camera in absolute world space
[O]c is the orientation of the object in camera space

you want [O]c (object in camera space)

first the orientation of the arrow in world space is

[O]w = [C]w * [O]c

-[C]w * [O]w = -[C]w * [C]w * [O]c = [O]c

[O]c = -[C]w * [O]w

hmmm... I always thought [O]w = [O]c * [C]w

which would give

[O]c = [O]w * -[C]w

unless someone spotted a mistake.

[edited by - oliii on October 10, 2003 2:43:57 PM]

Share this post


Link to post
Share on other sites