We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpOpenGL and 3D Libraries › Align to velocity - orientation
Pages: 1 2 
Align to velocity - orientation (Read 3209 times)
Re: Align to velocity - orientation
Reply #15 - Feb 4th, 2010, 4:16am
 
the rotate you mention isn't one of the standard rotate methods, is it, there are too many parameters.

rotate(-theAngle, crss.x, crss.y, crss.z);

can you post it here so people can see it.

and what are the 4 values it uses when it disappears? can you tell (i'm wondering whether you've got a (0, 0, 0) PVector somewhere which'll break the cross product. similarly the cross product of two identical or co-linear vectors is undefined.)
Re: Align to velocity - orientation
Reply #16 - Feb 4th, 2010, 4:46am
 
The code I use is the one I post here in prev. replies.

I found the problem, theAngle variable gets equal to NaN This is because (like koogy said) two parallel vectors equals to a undefiend cross product.

I am using toxiclibs Vec3D http://toxiclibs.org/docs/core/

So now I know but the if function is not working
Code:
float theAngle = new_dir.angleBetween(new_up); 
if (theAngle == Float.NaN) println("ERROR");


How can I compare the NaN that toxic's angleBetween returns?

Cheers
rS
Re: Align to velocity - orientation
Reply #17 - Feb 4th, 2010, 5:04am
 
i'd try

Float theAngle = new_dir...

then do the check for Nan. might need to use equals() rather than == as it's a class

then use -theAngle.floatValue() in the rotate() to get a float from a Float.

> two parallel vectors equals to a undefiend cross product.

hey, lucky guess! or years of experience. you decide 8)
Re: Align to velocity - orientation
Reply #18 - Feb 4th, 2010, 5:17am
 
I will say both, experience and lucky, if you get the result either way it always adds to the experience.

I am struggling to make the comparison. equals() only works for Strings, and I cant cast theAngle as string because I get an error.

I am so close I can almost taste it.

Cheers
rS
Re: Align to velocity - orientation
Reply #19 - Feb 4th, 2010, 5:24am
 
> equals() only works for Strings

no, every Class has an equals() method which lets you compare the value contained by the object rather than the objects themselves. (um, easier to explain if you know C... pointers vs contents)

let the professionals do it:
http://www.ensta.fr/~diam/java/online/notes-java/data/expressions/22compareobjects.html
Re: Align to velocity - orientation
Reply #20 - Feb 4th, 2010, 5:27am
 
actually, this is a better link

http://java.sun.com/j2se/1.4.2/docs/api/java/lang/Float.html

and there's an isNaN() menthod on Float which you should use instead of equals.

you can even leave theAngle as a float (small f)
and use

if (Float.isNaN(theAngle)) {
...
}
Re: Align to velocity - orientation
Reply #21 - Feb 4th, 2010, 5:31am
 
koogy, that works Grin now when theAngle is NaN should I make it = 0.0 ?

or what is the usual fix when that happens

Much appreciated all your help
rS
Re: Align to velocity - orientation
Reply #22 - Feb 4th, 2010, 6:54am
 
toxi said:

"If you're getting a NaN result (= Not a Number) then
your input vectors aren't normalized and you should use this version
of the function:

myVec.angleBetween(otherVec, true);

That will internally use normalized copies of both vectors to do the
calculation (but won't normalize your original vectors).
"

And so far no NaNs

Cheers
rS
Re: Align to velocity - orientation
Reply #23 - Feb 9th, 2010, 4:56am
 
Glad it helped! Smiley Just for the record, the javadocs for this method do mention the normalization aspect:

"Computes the angle between this vector and vector V. This function assumes both vectors are normalized, if this can't be guaranteed, use the alternative implementation angleBetween(Vec3D, boolean)"
Pages: 1 2