Re: [osg-users] Camera rotation on three axes

2008-11-13 Thread Steven Saunderson

- From: Paul Melis at 2008-11-05 20:06-

Hi Steven,

Maybe the following is an easier way to accomplish what you want, as I
don't see why you are trying to calculate angles...

snip



Hi Paul,

Thanks for all your help.  I've finally tried enough methods to 
appreciate the differences and how I can avoid gimbal lock 
problems.  I'm now using pitch to select the latitude in my sphere of 
view rather than an oblique great circle.  This seems to work well for 
my purposes.


Thanks again,
Steven

___
osg-users mailing list
osg-users@lists.openscenegraph.org
http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org


Re: [osg-users] Camera rotation on three axes

2008-11-05 Thread Steven Saunderson

- From: John Vidar Larring at 2008-10-30 21:42-

Hi Steven,

Here is a snipplet of code from the osgautocapture example that I just
sent to osg-submissions (if my email got through?), provided as is. I
hope it might be of help:


Hi John,

Thanks for the code.  I tried it (and it also rekindled my interest in 
ViewMatrixLookAt) but I still got gimbal-lock.  Perhaps my 
implementation of your code is faulty.


I've spent days now trying to learn enough about quaternions.  This 
almost works but my calculation for the rotation angle is still 
wrong.  Is there a function in OSG for calculating a quaternion from 
euler angles ?  I've searched but haven't found anything.


The standard code to calculate a quaternion uses w = cosx * cosy * cosz 
+ sinx * siny * sinz.  This doesn't seem right to me and it doesn't work 
in my testing.  I would expect the final rotation to be between the 
largest input rotation and PI.


I hope this post is sufficiently on-topic for the osg-users list.  I 
apologise if I should be looking elsewhere.


Thanks,
Steven

___
osg-users mailing list
osg-users@lists.openscenegraph.org
http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org


Re: [osg-users] Camera rotation on three axes

2008-11-05 Thread J.P. Delport

Hi,

Steven Saunderson wrote:

- From: John Vidar Larring at 2008-10-30 21:42-

Hi Steven,

Here is a snipplet of code from the osgautocapture example that I just
sent to osg-submissions (if my email got through?), provided as is. I
hope it might be of help:


Hi John,

Thanks for the code.  I tried it (and it also rekindled my interest in 
ViewMatrixLookAt) but I still got gimbal-lock.  Perhaps my 
implementation of your code is faulty.


I've spent days now trying to learn enough about quaternions.  This 
almost works but my calculation for the rotation angle is still wrong.  
Is there a function in OSG for calculating a quaternion from euler 
angles ?  I've searched but haven't found anything.


You can make a quat from three angles and three axes:

osg::Quat lori = osg::Quat(osg::DegreesToRadians(heading),
   osg::Vec3d(0,0,1),
   osg::DegreesToRadians(pitch),
   osg::Vec3d(0,1,0),
   osg::DegreesToRadians(roll),
   osg::Vec3d(1,0,0));

Make sure the order is what you need.



The standard code to calculate a quaternion uses w = cosx * cosy * cosz 
+ sinx * siny * sinz.  This doesn't seem right to me and it doesn't work 
in my testing.  I would expect the final rotation to be between the 
largest input rotation and PI.


I hope this post is sufficiently on-topic for the osg-users list.  I 
apologise if I should be looking elsewhere.


Thanks,
Steven


jp


--
This message is subject to the CSIR's copyright terms and conditions, e-mail legal notice, and implemented Open Document Format (ODF) standard. 
The full disclaimer details can be found at http://www.csir.co.za/disclaimer.html.


This message has been scanned for viruses and dangerous content by MailScanner, 
and is believed to be clean.  MailScanner thanks Transtec Computers for their support.


___
osg-users mailing list
osg-users@lists.openscenegraph.org
http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org


Re: [osg-users] Camera rotation on three axes

2008-11-05 Thread Paul Melis

Hi Steven,

Steven Saunderson wrote:

- From: John Vidar Larring at 2008-10-30 21:42-

Here is a snipplet of code from the osgautocapture example that I just
sent to osg-submissions (if my email got through?), provided as is. I
hope it might be of help:


Thanks for the code.  I tried it (and it also rekindled my interest in 
ViewMatrixLookAt) but I still got gimbal-lock.  Perhaps my 
implementation of your code is faulty.


I've spent days now trying to learn enough about quaternions.  This 
almost works but my calculation for the rotation angle is still 
wrong.  Is there a function in OSG for calculating a quaternion from 
euler angles ?  I've searched but haven't found anything.


Maybe the following is an easier way to accomplish what you want, as I 
don't see why you are trying to calculate angles...


Assume that you want to apply the pitch, roll and yaw rotations to your 
plane in that order. Also assume that you have your plane centered at 
the origin with its nose looking down the positive Y axis and its 
top-side pointing in direction of the positive Z axis. The pitch 
rotation can then simply be around the global X axis.


However, the roll and yaw rotations will need to be around axes other 
than the global Y and Z, as otherwise they don't take the changed plane 
orientation due to pitch (and roll for the yaw rotation) into account.


For this, keep track of two helper points (osg::Vec3's): the first one, 
p1, is at (0, 1, 0), the second one, p2, is at (0, 0, 1).


When you have calculated your pitch transformation, using for example, 
osg::Quat(pitch_angle /* in radians */, osg::X_AXIS), you also transform 
p1 with this quaternion. Let's call this transformed point p1t, and you 
can simply p1t = q * p1 to get the transformed point. To apply the roll 
rotation to the plane we then need to rotate around the vector that goes 
through pt1 and the origin. This vector is simply p1t. So the roll 
rotation would then be osg::Quat(roll_angle, p1t).


To apply the yaw rotation we need to transform p2 first by the pitch 
rotation followed by the roll rotation: p2t = pitch_transform * 
roll_transform * p2.

Then the yaw rotation will be osg::Quat(yaw_angle, p2t).

We now have three rotations defined by three quaternions. We only have 
to combine these into a single transformtion to get the final plane 
transformation:

q_final = q_pitch * q_roll * q_yaw

To convert q_final to a transformation matrix that you can pass to e.g. 
osg::MatrixTransform::setMatrix() you can use

osg::Matrix m;
q_final.get(m);
// m will now contain the quaternion in matrix form

Hope this helps,
Paul


___
osg-users mailing list
osg-users@lists.openscenegraph.org
http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org


Re: [osg-users] Camera rotation on three axes

2008-10-30 Thread Paul Melis

Steven Saunderson wrote:
I'm trying to rotate a camera on all axes but my code fails when the 
x-axis or y-axis rotation approaches 90 degrees.  I am using a 
viewer.frame loop to avoid the default manipulator supplied by 
viewer.run.  My current camera code is :


cameraRotation.makeRotate (
  yrads,osg::Vec3 (0, 1, 0) ,  // roll  Y
  xrads,osg::Vec3 (1, 0, 0) ,  // pitch X
  zrads,osg::Vec3 (0, 0, 1) ); // yaw   Z
cameraTrans.makeTranslate (xdist, ydist, zdist);
myCameraMatrix  = cameraRotation * cameraTrans;
inverseMatrix   = myCameraMatrix.inverse (myCameraMatrix);
inverseMatrix  *= osg::Matrixd::rotate (-(osg::PI_2), 1, 0, 0);
viewer.getCamera ()-setViewMatrix (inverseMatrix);

When the pitch (xrads) is 0 everything works properly.  But when xrads 
is PI/2 the effect of yrads is identical to zrads (both do z-axis 
rotation).  I've tried applying the rotations in various orders but 
the code fails when one of the rotations is PI/2.


Can anyone suggest what I am doing wrong here or point me to a sample 
of working code ?

You've hit upon a phenomenon called 'gimbal lock'.
See 
http://web.archive.org/web/20041029003853/http:/www.j3d.org/matrix_faq/matrfaq_latest.html#Q34 
for an explanation (and possibly some different ways of controling your 
camera).


Paul

___
osg-users mailing list
osg-users@lists.openscenegraph.org
http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org


Re: [osg-users] Camera rotation on three axes

2008-10-30 Thread Tomlinson, Gordon
As Paul mentions the is the age old Gimbal Lock problem

The quick answer to this is to build a quaternion  and use that populate
the oration matrix

Or the really old nasty trick of limiting rotations to -89.5 thru +89.5
or so ...


Gordon

__
Gordon Tomlinson

Product Manager 3D
Email  : gtomlinson @ overwatch.textron.com
__
(C): (+1) 571-265-2612
(W): (+1) 703-437-7651

Self defence is not a function of learning tricks 
but is a function of how quickly and intensely one 
can arouse one's instinct for survival 
- Master Tambo Tetsura

 

-Original Message-
From: [EMAIL PROTECTED]
[mailto:[EMAIL PROTECTED] On Behalf Of Paul
Melis
Sent: Thursday, October 30, 2008 6:10 AM
To: OpenSceneGraph Users
Subject: Re: [osg-users] Camera rotation on three axes

Steven Saunderson wrote:
 I'm trying to rotate a camera on all axes but my code fails when the 
 x-axis or y-axis rotation approaches 90 degrees.  I am using a 
 viewer.frame loop to avoid the default manipulator supplied by 
 viewer.run.  My current camera code is :

 cameraRotation.makeRotate (
   yrads,osg::Vec3 (0, 1, 0) ,  // roll
Y
   xrads,osg::Vec3 (1, 0, 0) ,  // pitch
X
   zrads,osg::Vec3 (0, 0, 1) ); // yaw
Z
 cameraTrans.makeTranslate (xdist, ydist, zdist);
 myCameraMatrix  = cameraRotation * cameraTrans;
 inverseMatrix   = myCameraMatrix.inverse (myCameraMatrix);
 inverseMatrix  *= osg::Matrixd::rotate (-(osg::PI_2), 1, 0, 0);
 viewer.getCamera ()-setViewMatrix (inverseMatrix);

 When the pitch (xrads) is 0 everything works properly.  But when xrads

 is PI/2 the effect of yrads is identical to zrads (both do z-axis 
 rotation).  I've tried applying the rotations in various orders but 
 the code fails when one of the rotations is PI/2.

 Can anyone suggest what I am doing wrong here or point me to a sample 
 of working code ?
You've hit upon a phenomenon called 'gimbal lock'.
See
http://web.archive.org/web/20041029003853/http:/www.j3d.org/matrix_faq/m
atrfaq_latest.html#Q34
for an explanation (and possibly some different ways of controling your
camera).

Paul

___
osg-users mailing list
osg-users@lists.openscenegraph.org
http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.or
g
___
osg-users mailing list
osg-users@lists.openscenegraph.org
http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org


Re: [osg-users] Camera rotation on three axes

2008-10-30 Thread John Vidar Larring

Hi Steven,

Here is a snipplet of code from the osgautocapture example that I just 
sent to osg-submissions (if my email got through?), provided as is. I 
hope it might be of help:

...

// Compute eye point in world coordiantes
osg::Vec3d eye;
	csn-getEllipsoidModel()-convertLatLongHeightToXYZ(lat, lon, alt, 
eye.x(), eye.y(), eye.z());


// Build matrix for computing target vector
	osg::Matrixd target_matrix = osg::Matrixd::rotate(-heading, 
osg::Vec3d(1,0,0),

  -lat, 
osg::Vec3d(0,1,0),
  lon,  
osg::Vec3d(0,0,1));

// Compute tangent vector ...
osg::Vec3d tangent = target_matrix.preMult(osg::Vec3d(0, 0, 1));

// Compute non-inclined, non-rolled up vector ...
osg::Vec3d up(eye);
up.normalize();

	// Incline by rotating the target- and up vector around the 
tangent/up-vector

// cross-product ...
osg::Vec3d up_cross_tangent = up ^ tangent;
	osg::Matrixd incline_matrix = osg::Matrixd::rotate(incline, 
up_cross_tangent);

osg::Vec3d target = incline_matrix.preMult(tangent);

// Roll by rotating the up vector around the target vector ...
	osg::Matrixd roll_matrix = incline_matrix * osg::Matrixd::rotate(roll, 
target);

up = roll_matrix.preMult(up);

viewerWindow-getCamera()-setViewMatrixAsLookAt(eye, eye+target, up);
...

Best regards,
John

Steven Saunderson wrote:
I'm trying to rotate a camera on all axes but my code fails when the 
x-axis or y-axis rotation approaches 90 degrees.  I am using a 
viewer.frame loop to avoid the default manipulator supplied by 
viewer.run.  My current camera code is :


cameraRotation.makeRotate (
  yrads,osg::Vec3 (0, 1, 0) ,  // roll  Y
  xrads,osg::Vec3 (1, 0, 0) ,  // pitch X
  zrads,osg::Vec3 (0, 0, 1) ); // yaw   Z
cameraTrans.makeTranslate (xdist, ydist, zdist);
myCameraMatrix  = cameraRotation * cameraTrans;
inverseMatrix   = myCameraMatrix.inverse (myCameraMatrix);
inverseMatrix  *= osg::Matrixd::rotate (-(osg::PI_2), 1, 0, 0);
viewer.getCamera ()-setViewMatrix (inverseMatrix);

When the pitch (xrads) is 0 everything works properly.  But when xrads 
is PI/2 the effect of yrads is identical to zrads (both do z-axis 
rotation).  I've tried applying the rotations in various orders but the 
code fails when one of the rotations is PI/2.


Can anyone suggest what I am doing wrong here or point me to a sample of 
working code ?


Thanks,
Steven

___
osg-users mailing list
osg-users@lists.openscenegraph.org
http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org




--
Best regards,
John
WeatherOne


--
This message has been scanned for viruses and
dangerous content by MailScanner, and is
believed to be clean.

___
osg-users mailing list
osg-users@lists.openscenegraph.org
http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org


Re: [osg-users] Camera rotation on three axes

2008-10-30 Thread Steven Saunderson

- From: Paul Melis at 2008-10-30 21:10-

You've hit upon a phenomenon called 'gimbal lock'.
See
http://web.archive.org/web/20041029003853/http:/www.j3d.org/matrix_faq/matrfaq_latest.html#Q34 


for an explanation (and possibly some different ways of controling your
camera).


Hi Paul,

Thanks for you quick answer.  I've tried loading the matrix manually but 
I still get stuck when both x-axis and y-axis are PI/2.  I am doing a 
mult after to apply the x,y,z offsets and then getting the inverse 
matrix before loading the camera.  Will these actions thwart the fix ?


Thanks,
Steven

___
osg-users mailing list
osg-users@lists.openscenegraph.org
http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org


Re: [osg-users] Camera rotation on three axes

2008-10-30 Thread Steven Saunderson

- From: Paul Melis at 2008-10-30 23:45-

Steven Saunderson wrote:
 Hi Paul,

 Thanks for you quick answer.  I've tried loading the matrix manually
 but I still get stuck when both x-axis and y-axis are PI/2.
I'm not sure what you mean here. It sounds like you're building your
matrix from Euler angles manually, but this will still not solve your
gimbal lock problem.


You're quite right here.  I'm now reading the entire FAQ especially 
about quaternions.



To quote Q39 in the link above: Don't let the spectre of Gimbal Lock
fool you: Euler angles are still a complete representation of any
rotation in 3D space; it's just that the actual Euler angles needed to
achieve some particular desired rotation may be rather unintuitive.


Does this mean that instead of rotating both x and y 90 degrees I should 
find another axis and rotate around this ?



What exactly are you trying to achieve? Where do the pitch, rol, yaw
values come from, are they user-specified?


It is a flight simulator.  The pitch, roll, and yaw values come from an 
external source so any combination is possible.


Thanks for the FAQ link; it is a great resource.

-- Steven

___
osg-users mailing list
osg-users@lists.openscenegraph.org
http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org