From curt@infoplane.com  Thu Oct 14 22:58:11 1999
X-VM-v5-Data: ([nil nil nil nil nil nil nil nil nil]
	["2121" "Thu" "14" "October" "1999" "17:31:57" "-0500" "Steve Baker" "sjbaker1@airmail.net" nil "66" "Re: HPR from vector" "^From:" nil nil "10" nil nil nil nil nil]
	nil)
Received: from wiley.infoplane.com (mail@[204.120.151.12])
	by mail.me.umn.edu (8.9.3/8.9.3) with ESMTP id WAA32804
	for <curt@me.umn.edu>; Thu, 14 Oct 1999 22:58:10 -0500 (CDT)
	(envelope-from curt@infoplane.com)
Received: from curt by wiley.infoplane.com with local (Exim 2.05 #1 (Debian))
	id 11byVN-0007Y3-00; Thu, 14 Oct 1999 22:58:09 -0500
Received: from mail.airmail.net [206.66.12.40] 
	by wiley.infoplane.com with smtp (Exim 2.05 #1 (Debian))
	id 11byVM-0007Xv-00; Thu, 14 Oct 1999 22:58:09 -0500
Received: from airmail.net from [207.136.25.125] by mail.airmail.net 
	(/\##/\ Smail3.1.30.16 #30.405) with esmtp for <curt@infoplane.com> sender: <sjbaker1@airmail.net>
	id <mS/11byVK-000A5yS@mail.airmail.net>; Thu, 14 Oct 99 22:58:06 -0500 (CDT)
Message-ID: <380659DD.6B00E17A@airmail.net>
Reply-To: sjbaker1@airmail.net
Organization: Steve at Home
X-Mailer: Mozilla 4.61 [en] (X11; I; Linux 2.2.10 i586)
X-Accept-Language: en
MIME-Version: 1.0
References: <14342.38734.560026.344336@pinky.infoplane.com>
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit
From: Steve Baker <sjbaker1@airmail.net>
Sender: "Curtis L. Olson" <curt@infoplane.com>
To: "Curtis L. Olson" <curt@infoplane.com>
Subject: Re: HPR from vector
Date: Thu, 14 Oct 1999 17:31:57 -0500

"Curtis L. Olson" wrote:

> Hmmm, took another look at sg and found:
> 
>     void sgHPRfromVec3 ( sgVec3 hpr, sgVec3 src ) ;
> 
> Close, but I need to control roll.  Looked at the source for
> sgHPRfromVec3() and here's what I think needs to be done to set the
> roll angle from my up vector.

You have a 'forward vector' and an 'up vector' - and you want
heading, pitch and roll?

Simplest way is to first generate a matrix.  Since a pure 
rotation matrix (3x3) is just three vectors mutually at right
angles and all of length 1.0...

  1) Normalize both vectors - so they are length 1.0

  2) If you already know they are at right angles, just
     do a vector product to generate a third vector at
     right angles to the first two.  Pack the three
     vectors into an sgMat3 and you're done.

  3) If they aren't at right angles, then do the vector
     product anyway to generate a vector at right angles
     to them both...then remake one of the other two
     using a  second vector product.  Now, all three
     vectors are at right angles and we have a matrix.
     ...hmmmm - you should probably re-normalise the
     result of that first vector product.

Now you can use the SG routine that turns a matrix into
three angles...but actually, the matrix is what you *actually*
wanted anyway.

Tadaaa!

Once it all works, if you need more speed then toss the
equations into MathCAD or something - switch on the
"Julienne Fries" button and tell it to simplify!

> 1.  computer the plane equation given by sgVec3 src.
> 
> 2.  map the up vector onto this plane.
> 
> 3.  angle = acos( dotprod(u,v) / ( ||u||  *  ||v|| ) )
> 
> Not sure if I'll need to do additional work to get sign or quadrant
> correct, but we'll see.

Er - whatever...I don't understand this - which by no means makes it
wrong!
 
> If I get grandly inspired to code this up in the near future would
> this be something you'd want to add to sg?

If it isn't there already - then YES!


-- 
Steve Baker                  http://web2.airmail.net/sjbaker1
sjbaker1@airmail.net (home)  http://www.woodsoup.org/~sbaker
sjbaker@hti.com      (work)



From sjbaker1@airmail.net  Thu Oct 14 22:58:18 1999
X-VM-v5-Data: ([nil nil nil nil t nil nil nil nil]
	["2404" "Thu" "14" "October" "1999" "17:52:49" "-0500" "Steve Baker" "sjbaker1@airmail.net" "<38065EC1.CAACB2A7@airmail.net>" "68" "Re: sg question" "^From:" nil nil "10" nil nil nil nil nil]
	nil)
Received: from mail.airmail.net (mail.airmail.net [206.66.12.40])
	by mail.me.umn.edu (8.9.3/8.9.3) with SMTP id WAA32815
	for <curt@me.umn.edu>; Thu, 14 Oct 1999 22:58:18 -0500 (CDT)
	(envelope-from sjbaker1@airmail.net)
Received: from airmail.net from [207.136.25.125] by mail.airmail.net 
	(/\##/\ Smail3.1.30.16 #30.405) with esmtp for <curt@me.umn.edu> sender: <sjbaker1@airmail.net>
	id <mS/11byVQ-0009jYS@mail.airmail.net>; Thu, 14 Oct 99 22:58:12 -0500 (CDT)
Message-ID: <38065EC1.CAACB2A7@airmail.net>
Reply-To: sjbaker1@airmail.net
Organization: Steve at Home
X-Mailer: Mozilla 4.61 [en] (X11; I; Linux 2.2.10 i586)
X-Accept-Language: en
MIME-Version: 1.0
References: <14342.24386.724680.185045@kenai.me.umn.edu>
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit
From: Steve Baker <sjbaker1@airmail.net>
Sender: steve
To: "Curtis L. Olson" <curt@me.umn.edu>
Subject: Re: sg question
Date: Thu, 14 Oct 1999 17:52:49 -0500

"Curtis L. Olson" wrote:
> 
> Steve,
> 
> Just took a brief glance at the sg docs and didn't see anything
> immediate so I thought I'd ask.
> 
> Is there an easy way that given two points in 3d and an up vector, to
> generate an sgCoord represent the vector from point a to b and the hpr
> given up is the specified up vector?

  /*
    From memory...
  */

  sgCoord result ;
  sgMat4 temp ;

  sgMakeIdentMat4 ( & temp ) ;

  sgSubVec3 ( temp[1], b, a ) ;
  sgNormaliseVec3 ( temp[1] ) ;
  sgCopyVec3 ( temp[2], up_vector ) ;
  sgNormaliseVec3 ( temp[2] ) ;

  sgVectorProductVec3 ( temp[0], temp[1], temp[2] ) ;
  sgNormaliseVec3 ( temp[0] ) ;
  sgVectorProductVec3 ( temp[2], temp[0], temp[1] ) ;

  sgCopyVec3 ( temp[3], b ) ;
  sgSetCoord ( & result, temp ) ;

It's 23% certain that I have the order of the parameters to
sgVectorProductVec3 wrong...so you'll come out inverted or
left-right mirrored or something. If so, don't be tempted to
just add 180 degrees to the roll angle - go back and swap
the Vector products parameters around!

> I'm thinking about placing the camera location somewhere in space and
> having it always point at the current aircraft location.

Yep - that'll work.
 
> Hmmm, maybe I should look at the TuX source code since I bet you do
> something like that there ...

You'd think so wouldn't you!  But in fact, (IIRC) I do it backwards
- I set the range and angle that which Tux wants to be viewed from,
and use that to compute the camera position. Since the camera
always translates along with him, the pointing issue "goes away".

But the code above is OK - you'll just get some violent spinning
when the camera has to point straight up or something. If the
vector a->b ends up parallel to the "upvector", you'll get a
divide-by-zero in the normalization code somewhere...you can
test for that though.  I generally try to 'damp' the angles
that come out of the code to limit the rate of change of
angular velocity - which reduces the "unnatural" feel when
the aircraft you are looking at is *perfectly* nailed to
the center of the screen. (Get it all working first though!)
This has the handy side-effect of getting rid of some of the
violent 'swirl' you get in the image when the plane flies
directly overhead.

-- 
Steve Baker                  http://web2.airmail.net/sjbaker1
sjbaker1@airmail.net (home)  http://www.woodsoup.org/~sbaker
sjbaker@hti.com      (work)

