![]() |
|
|
(#1)
|
||
|
Member
Posts: 392
Join Date: 2002.09
Location: Toronto
|
Quaternions in 2D -
2009.05.14, 02:43 AM
I'd like to continue this discussion after being scolded in the "How to handle entity rendering" thread for using quaternions to handle rotations in 2D.
First, I totally understand the objections and the fact that quaternions are overkill for 2D, but as worked to replace my rotation handling with scalar angles I ran into a major problem with interpolation. Here's a quote from the originating thread: Quote:
After my frustrations with scalar angles I came up with an interim solution: Use a half-quaternion (I'm making up words again, there's probably a real name for this - complex? spinor?) Anyway, I just took my quaternion code and cut it down to the Z and W components, since X and Y were always zero in the 2D cases. The math is greatly simplified, and the interpolation automagically picks the best/shortest path just like a quaternion. So, anyone care to share a decent scalar angle interpolation function? Or should I just be happy with the half-quaternion deal I got going? |
|
|
|
|
|
(#2)
|
|
|
Member
Posts: 219
Join Date: 2005.05
Location: Sweden
|
2009.05.14, 07:11 AM
Doesn't a quaternion in 2D simply reduce to ordinary complex numbers?
|
|
|
|
|
(#3)
|
||
|
Moderator
Posts: 3,541
Join Date: 2003.06
Location: usa
|
2009.05.14, 10:44 AM
Quote:
|
|
|
|
|
|
(#4)
|
|
|
Member
Posts: 1,310
Join Date: 2002.09
Location: Minnesota
|
2009.05.14, 11:17 AM
Yep. In the 2D case you just use complex numbers and complex multiplication. Very simple. I use it all over the place in Chipmunk to make things fast and simple.
Author of Chipmunk Physics - A fast and simple rigid body physics library in C. |
|
|
|
|
(#5)
|
|
|
Member
Posts: 392
Join Date: 2002.09
Location: Toronto
|
2009.05.14, 02:57 PM
Ditto - and even more so...
Thanks for the explanation guys - I recall reading that quaternions were "extended" complex numbers but I never realized how useful plain old complex numbers were for 2D stuff. I should've paid more attention to the Chipmunk source! |
|
|
|
|
(#6)
|
|
|
Member
Posts: 1,310
Join Date: 2002.09
Location: Minnesota
|
2009.05.14, 03:51 PM
Yep, it's pretty simple. Chipmunk uses the following two functions:
Code:
static inline cpVect
cpvrotate(const cpVect v1, const cpVect v2)
{
return cpv(v1.x*v2.x - v1.y*v2.y, v1.x*v2.y + v1.y*v2.x);
}
static inline cpVect
cpvunrotate(const cpVect v1, const cpVect v2)
{
return cpv(v1.x*v2.x + v1.y*v2.y, v1.y*v2.x - v1.x*v2.y);
}
Author of Chipmunk Physics - A fast and simple rigid body physics library in C. |
|
|
|
|
(#7)
|
|
|
Member
Posts: 392
Join Date: 2002.09
Location: Toronto
|
2009.05.15, 01:57 AM
So if I understand correctly, v2 in those cpvrotate functions are just normals (direction vectors)? That's useful in its own right but I ended up handling things a little differently.
I found this page that pretty much describes it: http://www.euclideanspace.com/maths/...orms/index.htm. I'm using the "Alternative (spinor representation)" and I've settled on calling these "spinors" in my math library to avoid ambiguous usage. |
|
|
|
| Thread Tools | |
| Display Modes | |
|
|