Advertisement

lookat function problem

Started by April 08, 2001 10:01 PM
3 comments, last by Kestrel 23 years, 10 months ago
I trying to write a lookat function so a I can have my Ai ships look at other ships,destination,target etc, but I don''t know exactly how to do it. I''ve spent the last day trying to figure it out.
I did something like this a while back, but I did it in 2D, I'll explain that the best I can, and I can't imagine it would be too tough to convert to 3D. Here goes.

Okay, I'll assume you have a vector for your ship's direction, I'll call it shipdir, now you need to get the vector from your ship to whatever you wanna look at, which is (target's position) - (ship's position) [wishdir.x = target.x - ship.x; //etcc.]

Now to get the angle between them, you do the following:
dist = shipdir.x * wishdir.x + shipdir.y * wishdir.y;

I hope your still following along.

angle = acos( dist ) * 180 / PI;

That get's you the angle you need to rotate, but ONLY for the XY plane, you would probably have to do that 3 time (once for each plane) and then rotate each of them.

Oh, a side note, that will get you an angle between 0 and 180, you'll also need to figure out which direction to rotate (left or right), the way I did it, was just to store what direction the ship would be pointing if you were to rotate it 10 degrees and get the angle, if that new angle is less than the first one you got, rotate it that way, otherwise, rotate it the other way..

gee, hope that helped at all, and wasn't too confusing, of course, someone'll probably show me some even easier way =P

Cheez

-------------------------------

If you were supposed to understand it, we wouldn''t call it code

Edited by - Cheez on April 8, 2001 11:42:52 PM
Cheez------------------------------If you were supposed to understand it, we wouldn''t call it code
Advertisement
Thanks for the help but my problem is a little bit more complicated because it''s in 3d and I using quaternions. I know what I want to do, I just don''t know how to do it. I can get a vector to were I want my craft to look. Convert that data to a quaternion then multiply that with my current quaternion. I just don''t know how to convert the vector correctly.
go take a look there, it will be helpful if you didn''t check it already :
http://www.gamedev.net/reference/programming/features/qpowers/default.asp
I think I have it?
I don''t know if this is the way it should be don''t but it seems to work.

find angle between the two vectors - dot product
find normal of the two vectors - cross product

and using the normal as an axis. built a quaternion the axis and angle method?

This topic is closed to new replies.

Advertisement