in reply to Re: Perl and Quaternions
in thread Perl and Quaternions
I apologize for the terrible formatting of the question, everyone.
I'm not used to posting here, and actually am more used to people knowing where my code is coming from(this familiarity coming from my time at Holowan Labs, at LucasForums). This is not an excuse, merely a statement about my own ignorance.
To address everything in your post, Ken, and hopefully everything else, let me try this again.I'm trying to convert between radians and degrees from a given degree, for use in a quaternion format, particularly in translation between Quaternion values and Euler angles. I took another look at how the games I mod, Star Wars Knights of the Old Republic it's sequel, use Quaternions. And it looks like I messed up:
Z = sin (angle/2) Y = 0.0 X = 0.0 Q = cos (angle/2)
In my eyes, it was a simple mistake, though you guys might judge differently. I had always assumed that the values I needed were X and Y because that was how the game referenced them in it's files.
So, some example code was asked for. This code can be run from the commandline and will ask for an input value. Be sure to input it in degrees.
use strict; use warnings; use Math::Complex; my $pi = 4*atan2(1,1); sub deg_to_rad { ($_[0]/180) * $pi; } sub rad_to_deg { ($_[0]/$pi) * 180; } print "Enter a value: "; my $value = <STDIN>; my $radvalue = deg_to_rad($value); my $degvalue = rad_to_deg($radvalue); my $quat1 = sin($radvalue); my $quat1_deg = rad_to_deg($quat1); my $quat2 = cos($radvalue); my $quat2_deg = rad_to_deg($quat2); print "Original value: $value\n"; print "Radian value: $radvalue\n"; print "Degree value: $degvalue\n\n"; print "Quaternion first value: \t$quat1\n"; print "Quaternion first value(in degrees): \t$quat1_deg\n"; print "Quaternion fourth value: \t$quat2\n"; print "Quaternion fourth value(in degrees): \t$quat2_deg\n";
Inputting 90 degrees should bring up the radian value, 1.570796 blahblahblah, but it doesn't. I'm using the Math::Complex module, so that might be the difference between the expected results, but I'm not sure...
About the example code I posted that the game uses, yes it is NWScript. And I'm known for being accurate when I script in it, but on this occasion I didn't copy-and-paste correctly, so now feel kinda stupid for that. Also, I posted it for the sole reason of showing anybody who might care to know how the game calculates the values, which might help me do the opposite and calculate the facing from the values.
I admit that the spinbox widget isn't really optimal for this; it was more of a test on that part. It's easy enough to switch it to an entry widget with a binding to the Enter key, though, so I'm not too worried about it. However, thanks for pointing it out, Ken.
I tend to not use the strict pragma because it interferes with the TK code, in that when it forces me to put quotes around certain text, the options aren't accepted by TK... However, warnings should definitely have been used. Sorry about that, Ken.
I alternated between either value of Pi to see if there was any functional difference in the conversion subroutines.
Sorry about the clutter in the code samples. I was trying to give you guys enough info without chucking the whole script in, and without leaving out something that might be important and then get me flamed for not adding it in when it was so essential for the people answering to know that ahead of time...
Also, could you please be more specific about what you meant when you mentioned Array slices? You mentioned them, but never where in my code samples they might be used...
And I found Math::Quaternion myself while doing research, but I have no clue how to interpret it's 3x3 or 4x4 output, given that I just need four values...
I apologize, Ken, and wish I could elaborate more and get some more help, but I need to head off to school now. I'll be checking back in at lunch, see if anything's new. Thanks for the helpful comments, Ken!
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^3: Perl and Quaternions
by kcott (Archbishop) on May 09, 2014 at 03:54 UTC |