logo back up home forward   further reading more topics »

Maths - Quaternion to AxisAngle - Forum Discussion

By: Wayne C. Gramlich - wgramlich
file Bug in Quaternion to Angle Axis Conversion?  
2004-07-28 16:11

First, I have been finding the site incredibly useful. Thank
you very much!!!

In reading the Java code for converting a quaternion to
an angle axis, I ran across the following snippet of code:

<Pre>

public void set(Quat4d q1) {
angle = 2 * Math.acos(q1.w);
double s = Math.sqrt(1-q1.w*q1.w);
if (Math.abs(s) < 0.001) s=1;
x = q1.x / s;
y = q1.y / s;
z = q1.z / s;
}

</Pre>

It looks to me like the if statement is trying to
perform the small angle cosine optimization
where theta ~= 0 => cosine(theta) ~= 1.
However, the test is for abs(s) instead of
abs(angle).

My confidence on these topics is quite low,
so please forgive me if I've bungled my
understanding.

For completeness, here is the URL:

http://www.euclideanspace.com/maths/geometry/rotations/conversions/quaternionToAngle/index.htm

Thanks,

-Wayne

By: Martin Baker - martinbaker
file RE: Bug in Quaternion to Angle Axis Conversion?  
2004-07-29 01:25

Hi Wayne,

I think this is just to avoid the divide by zero when:
w = 1
s = 0
angle = 0
although perhaps this is the same thing?
Its an interesting question, what should we do when angle =0? Perhaps it would be better to set:
x=1
y=0
z=0
as this makes sure the axis is normalised even though its value should not matter?

Martin

By: Wayne C. Gramlich - wgramlich
file RE: Bug in Quaternion to Angle Axis Conversio  
2004-07-29 15:51

Martin:

I think you are right. What threw me for a loop is that
the sqrt() function can never return a positive number,
so the abs(s) is unnecessary. I think the code
should probably be something like:

if (s < .001) {
x = 1; y = 0; z = 0;
} else {
the usuual...

I think I get it now. (This is why I don't trust myself
on these issues; I typically get them wrong. ;-)

Thanks,

-Wayne

By: Martin Baker - martinbaker
file RE: Bug in Quaternion to Angle Axis Conversion?  
2004-07-29 23:16

Wayne,

Good point, I have removed the abs and updated the webpage:
http://www.euclideanspace.com/maths/geometry/rotations/conversions/quaternionToAngle/

I made the setting of x=1;y=z=0; an option depending on the importance of the axis being normalised vs. getting the direction as close as possible for very small angles.

Thanks very much,

Martin


metadata block
see also:

 

Correspondence about this page

Book Shop - Further reading.

Where I can, I have put links to Amazon for books that are relevant to the subject, click on the appropriate country flag to get more details of the book or to buy it from them.

cover 3D Math Primer - Aimed at complete beginners to vector and matrix algebra.

Commercial Software Shop

Where I can, I have put links to Amazon for commercial software, not directly related to the software project, but related to the subject being discussed, click on the appropriate country flag to get more details of the software or to buy it from them.

 

cover Dark Basic Professional Edition - It is better to get this professional edition

cover This is a version of basic designed for building games, for example to rotate a cube you might do the following:
make object cube 1,100
for x=1 to 360
rotate object 1,x,x,0
next x

cover Game Programming with Darkbasic - book for above software

Can you help?

Please send me any improvements to here. I would appreciate ideas to make the pages more useful including error correction, ideas for new pages, improvements to wording. It helps if you quote the full URL of the page.

 

progam

I am working on a project which uses these principles, if you would like to help me with this you are welcome to join in, here:

http://sourceforge.net/projects/mjbworld/

This site may have errors. Don't use for critical systems.

Copyright (c) 1998-2008 Martin John Baker - All rights reserved.