Fair Strides has asked for the wisdom of the Perl Monks concerning the following question:
Hello again. It's been a long time since I last logged on, but I'm in another bit of trouble over small coding errors...
I'm building an application to help mod a Star Wars game, and the game stores an object's directional facing as a form of a quaternion, with four numbers: the first is the X-orientation, the fourth the Y-orientation, and the two in the middle are always 0.
I want to allow the users to enter the facing in degrees, calculate the quaternion values from the degrees, and then eventually put that into the file. Currently, the program reads the info from the file correctly, but upon my editing the info, writing it back to the file, and then re-reading it, the numbers aren't right...
The game has a built-in scripting language, and the code to get the Quaternion X and Y goes like this:
float fOrient = GetFacing( GetFirstPC() -90.0 ); float fX = cos( fOrient/2 ); float fY = sin( fOrient/2);
The reason for the -90.0 from the Facing is that the Quaternion values are used for rotation, with the game using East as the 0.0 rotation, and the GetFacing function using North as the 0.0 rotation.
Just a heads-up, using the code below, I operate off of the degree returned by converting the radian value for the Quaternion's Y-orientation. When I set the degrees to 10 in the spinbox, and then re-read the information from the file after writing, it's 9.98 and then some more numbers. When I set the degrees to 55, the new value read in is 52.91 and then some numbers, so the margin for error is growing...
Here's the code I have for reading the info from a hash-of-hashes I already populated when I processed the file:
# The below section should do the math right. my $pi = 3.14159265358979; #my $pi = 4*atan2(1,1); sub deg_to_rad { ($_[0]/180) * $pi;} sub rad_to_deg { ($_[0]/$pi) * 180; } sub calc_quatcam { my $camx = shift; my $camy = shift; print "\nRad X:Y\t $camx : $camy\n"; # my $qx = rad_to_deg(acos($camx)); # my $qy = rad_to_deg(asin($camy)); my $qx = rad_to_deg($camx); my $qy = rad_to_deg($camy); print "\nRadian X:Y\t $qx : $qy\n"; print "Total: " . ($qx + $qy) . "\n"; $qx = ($qx * 2);# + 90.0; $qy = ($qy * 2);# + 90.0; my @calc_cam = ($qx, $qy); # print "\nQuat X:Y\t $qx : $qy\n"; return @calc_cam; } # Read in from the Hash. The function that reads from # the file returns an anon Array, so I have to access # as a scalar, then turn it into an Array. my $can_ori = $gff_files{Cameras}{$num}{Orientation}; @cam_ori = @$can_ori; my $cam_ori1 = @cam_ori[0]; # X-orientation my $cam_ori2 = @cam_ori[1]; # Always 0 my $cam_ori3 = @cam_ori[2]; # Always 0 my $cam_ori4 = @cam_ori[3]; # Y-orientation # Debugging print-out print "Orientation:\n\t$cam_ori1\n\t$cam_ori2\n\t$cam_ori3\n\t$cam +_ori4\n\n"; # The orientation values are stored as radians, so # calc_quatcam returns the degree equivalent, or should... my @cam_quats = calc_quatcam($cam_ori1, $cam_ori4); my $qx = @cam_quats[0]; my $qy = @cam_quats[1];
and the code that takes the measure of degrees from a spinbox widget and writes back the quaternion values in radians...:
my $ori_deglab = $ori_framey->Label(-text=>"Facing (in Degrees):") +->pack(-side=>left); my $ori_deg = $ori_framey->Spinbox(-from=>0.0, -to=>360.0, -increm +ent=>0.5, -width=>12, -command=>sub { my $deg = shift; my $degg = $deg; my $fl = $degg/2; my $flo = deg_to_rad($fl); print "Order:\n\t$deg\n\t$f +l\n\t$flo\n"; $quatx = cos($fl); $quaty = sin($fl); my @cal = calc_quatcam($quatx, $quaty); print "The deg is: $deg\n"; $ori_xlab->configure(-text=>"X Quaternion Value: $quat +x"); $ori_ylab->configure(-text=>"Y Quaternion Value: $quat +y");})->pack(-side=>right); $ori_deg->set($qy); $ori_xlab = $ori_framey1->Label(-text=>"X Quaternion Value: $quatx +", -font=>[-family=>'fixed', -size=>10], -width=>30)->pack(-expand=>1 +, -fill=>x); $ori_ylab = $ori_framey2->Label(-text=>"Y Quaternion Value: $quaty +", -font=>[-family=>'fixed', -size=>10], -width=>30)->pack(-expand=>1 +, -fill=>x);
Now, I found How can I get sine, cosine, and tangent to return values in degrees? and applied the info there, but I am still not getting the right values from my equations...
I fear I'm not being very clear, so I'll restate my problem: How do I get the PROPER Quaternion values from a given degree?
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Perl and Quaternions
by kcott (Archbishop) on May 08, 2014 at 11:37 UTC | |
by Fair Strides (Acolyte) on May 08, 2014 at 13:39 UTC | |
by kcott (Archbishop) on May 09, 2014 at 03:54 UTC | |
|
Re: Perl and Quaternions
by salva (Canon) on May 08, 2014 at 08:03 UTC | |
by AppleFritter (Vicar) on May 08, 2014 at 09:50 UTC | |
by salva (Canon) on May 08, 2014 at 10:21 UTC | |
|
Re: Perl and Quaternions
by RichardK (Parson) on May 08, 2014 at 10:20 UTC |