Gamestudio Links
Zorro Links
Newest Posts
Blobsculptor tools and objects download here
by NeoDumont. 03/28/24 03:01
Issue with Multi-Core WFO Training
by aliswee. 03/24/24 20:20
Why Zorro supports up to 72 cores?
by Edgar_Herrera. 03/23/24 21:41
Zorro Trader GPT
by TipmyPip. 03/06/24 09:27
VSCode instead of SED
by 3run. 03/01/24 19:06
AUM Magazine
Latest Screens
The Bible Game
A psychological thriller game
SHADOW (2014)
DEAD TASTE
Who's Online Now
2 registered members (AndrewAMD, Nymphodora), 972 guests, and 8 spiders.
Key: Admin, Global Mod, Mod
Newest Members
sakolin, rajesh7827, juergen_wue, NITRO_FOREVER, jack0roses
19043 Registered Users
Previous Thread
Next Thread
Print Thread
Rate Thread
Page 1 of 5 1 2 3 4 5
quaternions #114062
02/27/07 10:55
02/27/07 10:55
Joined: Mar 2006
Posts: 3,538
WA, Australia
J
JibbSmart Offline OP
Expert
JibbSmart  Offline OP
Expert
J

Joined: Mar 2006
Posts: 3,538
WA, Australia
g'day guys!

i saw someone mention quaternions somewhere on the a6 forums, and i was getting really frustrated with some rotational stuff in my project (bloody Euler angles ) so i went on wikipedia and some other online resources and taught myself the theory behind quaternions, which is ridiculously easy if u know a little about complex numbers, and possibly even if you don't.
anyway, here is the fruit of my research! i decided anyone who wants to use quaternions would have figured it out one way or another, and considering most of these functions almost directly quote online sources i have no reason to keep this to myself. enjoy:
quaternions.c

a brief description for the contents:
QUATERNION
a quaternion (struct) containing w, x, y and z floats. there is no globally used convention but in this case "w" is the scalar part and "x,y,z" the axis.

quat_multiply(QUATERNION* end_result,QUATERNION* a,QUATERNION* b)
sets the quaternion "end_result" to the product of quaternions "a" and "b" (in that order -- remember quaternions are not commutative). this is effectively rotating "b" by "a", around the axis represented in "a". this is the main reason i am using quaternions.

quat_for_ang(QUATERNION* q,ANGLE* a)
meant to set the change the contents of "q" to represent the euler angle "a". untested, so please let me know if it doesn't work and point me in the right direction if you know what should be done. -- actually, doesn't work. everything else is tested quite reasonably i think, so they should work. i'll get back to you when i've had a look at this but i haven't found a need for quat_for_ang yet.

quaternize(var a,VECTOR* v,QUATERNION* q)
i don't know if quaternize is a real word, but this will set the contents of quaternion "q" to represent the rotation "a" degrees about the axis "v".

quat_to_unit(QUATERNION* q)
gives "q" a magnitude of "1". for a multiplication to accurately represent a quaternion rotation each quaternion needs a magnitude of 1. this does that for you.

quat_set(QUATERNION* q,QUATERNION* u)
sets quaternion "q" to equal "u".

just put #include "quaternions.c" after your default and acknex includes, and then these functions and quaternions should work beautifully

i hope at least a few of you get some use out of these they're good for rotating around any axis you want. if you find and confirm any errors, please notify me either by pm or in this thread!
i haven't been able to test it for super long but it seems to be working really well from what i can see!

back to my design & technology project.

julz


Formerly known as JulzMighty.
I made KarBOOM!
Re: quaternions [Re: JibbSmart] #114063
02/28/07 03:46
02/28/07 03:46
Joined: Mar 2003
Posts: 4,427
Japan
A
A.Russell Offline
Expert
A.Russell  Offline
Expert
A

Joined: Mar 2003
Posts: 4,427
Japan
This is really good. I thought GS used Euler angles, so could you show a code snippet of how you would use this?

Re: quaternions [Re: A.Russell] #114064
02/28/07 05:04
02/28/07 05:04
Joined: Mar 2006
Posts: 3,538
WA, Australia
J
JibbSmart Offline OP
Expert
JibbSmart  Offline OP
Expert
J

Joined: Mar 2006
Posts: 3,538
WA, Australia
thank you very much

these are part of what i used to test it (comments added so you know what i'm doing):
Code:

VECTOR camera_axis;

function cam_control()
{
temp.x = - 300; // put the camera 300 quants behind the ball, based on camera's orientation
temp.y = 0;
temp.z = 0;
vec_rotate(temp,camera.pan);
vec_add(temp,my.x);
vec_lerp(camera.x,camera.x,temp,0.5);
camera.pan -= mouse_force.x * 5; // mouse controls pan and tilt of camera
camera.tilt -= mouse_force.y * 5;
temp.x = 0; // this sets the global VECTOR "camera_axis", which is
temp.y = 1; // relative to the camera and used to test quaternion
temp.z = 0; // rotations. play with these values for different axes :P
vec_rotate(temp,camera.pan);
vec_set(camera_axis,temp);
}

action ball()
{
wait(1);
c_setminmax(me);
vec_set(camera_axis,nullvector); // sorts out initialization problems
QUATERNION my_rot; // this quaternion contains my orientation
QUATERNION my_spin;// and this contains how i want to spin the ball
ANGLE my_angle; // euler orientation
vec_set(my_angle,my.pan);
my_rot.w = 1; // this is like a default orientation, as quat_for_ang doesn't work yet
my_rot.x = 0;
my_rot.y = 0;
my_rot.z = 0;
var js = 0; // you'll see what this is for...
while(1)
{
js += time_frame*50/16; // basically this loop will run 50 times a second, which
while(js>0) // isn't needed for quaternions at all, but just something i do.
{
quaternize((key_w*2),camera_axis,my_spin); //create a quaternion that will rotate around the camera-controlled axis, and spin if "w" is pressed
quat_to_unit(my_spin); // normalize both
quat_to_unit(my_rot);
quat_multiply(my_rot,my_spin,my_rot); // rotate
ang_for_quat(my_angle,my_rot); // and convert to euler for GS use
vec_sub(my_angle,my.pan); // just finds the rotation needed for c_rotate
c_rotate(me,my_angle,IGNORE_PASSENTS|IGNORE_PASSABLE);
cam_control(); // control the camera
js -= 1; // don't want an infinite loop!
}
wait(1);
}
}

GS does use Euler angles, and in fact most sources use a different Euler rotation order, so it wasn't easy finding what i needed it still ends up using a Euler angle, but the quaternion representation is unaffected by the Euler translation, and is MUCH easier to use when rotating around an arbitrary axis. i plan on using these for my car game i'm making, as Euler angles were proving to be really annoying and often unrealistic.

julz

EDIT: i realized that x- and y- seem to be inverted when using quaternize, which doesn't seem to be fixing as easily as i thought i am certain the euler representations of the quaternions are correct, tho. when rotating around a vector using quaternize, i think the x and y values need to each be multiplied by -1 first. i'm pretty sure this won't be too much of a problem.

EDIT 2: above edit no longer applicable!

Last edited by JulzMighty; 02/28/07 10:09.

Formerly known as JulzMighty.
I made KarBOOM!
Re: quaternions [Re: JibbSmart] #114065
02/28/07 09:25
02/28/07 09:25
Joined: May 2002
Posts: 7,441
ventilator Offline
Senior Expert
ventilator  Offline
Senior Expert

Joined: May 2002
Posts: 7,441
Quote:

[...] which is ridiculously easy if u know a little about complex numbers, and possibly even if you don't.


really? damn, i didn't get them when i looked into them a while ago. maybe i should (re)learn complex numbers first.

interesting contribution but what can be done with it that can't be done with the built-in quaternion functions ang_add/ang_rotate?

maybe you could add a spherical interpolation function?

Re: quaternions [Re: ventilator] #114066
02/28/07 10:07
02/28/07 10:07
Joined: Mar 2006
Posts: 3,538
WA, Australia
J
JibbSmart Offline OP
Expert
JibbSmart  Offline OP
Expert
J

Joined: Mar 2006
Posts: 3,538
WA, Australia
lol the ridiculous ease of quaternions was really the basic theory behind it... not the theory behind the derivations. that really got me. at first i was using ang_add and ang_rotate, and they weren't working so well for me. the trouble with them, is they only rotate around the new axis. these quaternions allow you to define your own arbitrary axis. that's y i did all this.

haha!! yes!!! i've just figured out i made a blissfully stupid mistake! the basic explanation is that the x and y values are NOT inverted for quaternize. i just made a couple of misinterpretations of my own

julz

EDIT: example for quaternion advantages: wheels in a car which is accelerating rotate forward at varying rates, depending on speed (doable with quaternions OR ang_add) but also rotate left and right about an axis which is in the same direction as "up" in relation to the car uses these wheels. quaternion rotations make this easy, but it is impossible (or extremely difficult) with ang_add or ang_rotate if the wheels don't have the same tilt as the car. i'm sure u know what i mean

Last edited by JulzMighty; 02/28/07 10:21.
Re: quaternions [Re: JibbSmart] #114067
02/28/07 23:51
02/28/07 23:51
Joined: Apr 2006
Posts: 136
San Francisco
T
Tor Offline
Member
Tor  Offline
Member
T

Joined: Apr 2006
Posts: 136
San Francisco
Quaternions are really awesome. They really aren't that more complicated and with the right functions and a math book you can be up and running in almost no time. There is an excellent example of quaternion rotation on www.ultimategameprogramming.com under the OpenGL examples/tutorials.

Definately saved my butt a few times in classes already. So much easier to rotate things with Quat's than with euelers or any other method I've seen so far. Especially if you have to take into account matrixies.

Cheers!


"Towlie, you're the worst character ever." I know...
Re: quaternions [Re: Tor] #114068
03/01/07 01:45
03/01/07 01:45
Joined: Mar 2006
Posts: 3,538
WA, Australia
J
JibbSmart Offline OP
Expert
JibbSmart  Offline OP
Expert
J

Joined: Mar 2006
Posts: 3,538
WA, Australia
yeh im slightly embarrassed by the amount of time it took me to set these up, but i have no maths books that cover anything outside of high-school and almost every online source i found was either converting quaternions to matrices or was using a different Euler order than GS. wikipedia had the best information i could find, but it wasn't working so i tried other stuff. in the end, wikipedia had the most accurate info, except i needed to change a positive value to a negative, and almost everything was solved.

another issue i had was the lack of an atan2 function in GS, but i just wrote a basic replacement (which you will find in quaternions.c). jcl said a proper atan2 function will be implemented in a future update, so in that case mine will probably have to be commented out to prevent crashes in that update.

julz

EDIT: 300 posts! senior member now

Last edited by JulzMighty; 03/01/07 01:47.

Formerly known as JulzMighty.
I made KarBOOM!
Re: quaternions [Re: JibbSmart] #114069
03/01/07 11:01
03/01/07 11:01
Joined: Apr 2002
Posts: 4,801
Richmond B.C., Canada
Captain_Kiyaku Offline

Dichotomic
Captain_Kiyaku  Offline

Dichotomic

Joined: Apr 2002
Posts: 4,801
Richmond B.C., Canada
*shaking* quaternions... my nightmare came true @_@ nooo *runs away*

i'm SO glad someone implemented them into 3dgs, cause i will never touch them again D:

Thanks a lot for the script, i will test it out soon!


My Blog

"Tag und Nacht schrei ich mich heiser,
Wind weht alle Worte fort,
Tag und Nacht schrei ich mein Krähenwort!"

Subway To Sally - Krähenkönig
Re: quaternions [Re: Captain_Kiyaku] #114070
03/02/07 22:36
03/02/07 22:36
Joined: Mar 2006
Posts: 3,538
WA, Australia
J
JibbSmart Offline OP
Expert
JibbSmart  Offline OP
Expert
J

Joined: Mar 2006
Posts: 3,538
WA, Australia
haha, i'm glad some people appreciate it. i know it has very few functions, and one of them doesn't work, but i will expand on it soon (maybe some slerp, definitely fix quat_for_angle, and some vector functions). i can only begin doing so, however, after thursday, as i have left a pretty big school assessment task too late so that's all i'll be doing until then (besides a glance at the forums on occasion )

julz


Formerly known as JulzMighty.
I made KarBOOM!
Re: quaternions [Re: JibbSmart] #114071
03/13/07 09:42
03/13/07 09:42
Joined: Mar 2006
Posts: 3,538
WA, Australia
J
JibbSmart Offline OP
Expert
JibbSmart  Offline OP
Expert
J

Joined: Mar 2006
Posts: 3,538
WA, Australia
okay! a decent sized update:

quat_add, quat_sub, quat_conjugate, and vec_rot_by_quat were all added (vec_rot_by_quat is particularly useful), and quat_for_ang was fixed, so it is fully functional. it doesn't have slerp, but maybe i'll add that in the future if i can find a proper formula for quaternions to the power of something <1 and >0. i don't need slerp so don't count on it i'm busy enough as it is.

it is a new link, because now it is zipped with a readme explaining the functions.

get it here: quaternions.zip (2k)

let me know if anything does something it isn't supposed to. i hope some of you get some use out of these!

julz


Formerly known as JulzMighty.
I made KarBOOM!
Page 1 of 5 1 2 3 4 5

Moderated by  HeelX, Lukas, rayp, Rei_Ayanami, Superku, Tobias, TWO, VeT 

Gamestudio download | chip programmers | Zorro platform | shop | Data Protection Policy

oP group Germany GmbH | Birkenstr. 25-27 | 63549 Ronneburg / Germany | info (at) opgroup.de

Powered by UBB.threads™ PHP Forum Software 7.7.1