• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
alvaro

Packing a 3D rotation into 32 bits

1 post in this topic

After seeing [url="http://www.gamedev.net/topic/627464-decompressing-quaternion-problem/"]this thread[/url], I was trying to think of how I would pack a 3D rotation into a fixed number of bits, for instance to store animation data. I wrote a little piece of code with the result and I think it's neat enough to share.

The idea is to encode a unit-length quaternion in a manner analogous to [url="http://en.wikipedia.org/wiki/Cube_mapping"]cube mapping[/url], but with one extra dimension and taking advantage of the property that a sign flip doesn't change which rotation is being represented.

One can think of cube mapping as consisting of an encoding of points in a sphere by first indicating which coordinate has largest absolute value and what sign it has (i.e., which of the 6 faces of the axis-aligned cube the point projects to) and then the remaining coordinates divided by the largest one (i.e., what point of the face the point projects to).

In our case, we don't need to encode the sign of the largest component, so we only need to use 2 bits to encode what the largest component is, and we can use the remaining bits to encode the other three components.

I think 32 bits is probably good enough for animation data in a game, and it's convenient that 30 is a multiple of 3, so it's easy to encode the other components. Actually, even if we didn't have that convenience, it wouldn't be a big deal to use a resolution that is not a power of 2, but some integer divisions would be involved in the unpacking code.

Here's the code, together with a main program that generates random rotations and measures how bad the dot product between the original and the packed and unpacked gets (the dot product seems to be > 0.999993, although I haven't made a theorem out of it):
[code]#include <iostream>
#include <cstdlib>
#include <boost/math/quaternion.hpp>

typedef boost::math::quaternion<double> quaternion;

int double_to_int(double x) {
return static_cast<int>(std::floor(0.5 * (x + 1.0) * 1023.0 + 0.5));
}

double int_to_double(int x) {
return (x - 512) * (1.0 / 1023.0) * 2.0;
}

struct PackedQuaternion {
// 2 bits to indicate which component was largest
// 10 bits for each of the other components
unsigned u;

PackedQuaternion(quaternion q) {
int largest_index = 0;
double largest_component = q.R_component_1();
if (std::abs(q.R_component_2()) > std::abs(largest_component)) {
largest_index = 1;
largest_component = q.R_component_2();
}
if (std::abs(q.R_component_3()) > std::abs(largest_component)) {
largest_index = 2;
largest_component = q.R_component_3();
}
if (std::abs(q.R_component_4()) > std::abs(largest_component)) {
largest_index = 3;
largest_component = q.R_component_4();
}

q *= 1.0 / largest_component;

int a = double_to_int(q.R_component_1());
int b = double_to_int(q.R_component_2());
int c = double_to_int(q.R_component_3());
int d = double_to_int(q.R_component_4());

u = largest_index;
if (largest_index != 0)
u = (u << 10) + a;
if (largest_index != 1)
u = (u << 10) + b;
if (largest_index != 2)
u = (u << 10) + c;
if (largest_index != 3)
u = (u << 10) + d;
}

quaternion get() const {
int largest_index = u >> 30;
double x = int_to_double((u >> 20) & 1023);
double y = int_to_double((u >> 10) & 1023);
double z = int_to_double(u & 1023);

quaternion result;
switch (largest_index) {
case 0:
result = quaternion(1.0, x, y, z);
break;
case 1:
result = quaternion(x, 1.0, y, z);
break;
case 2:
result = quaternion(x, y, 1.0, z);
break;
case 3:
result = quaternion(x, y, z, 1.0);
break;
}

return result * (1.0 / abs(result));
}
};

double rand_U_0_1() {
return std::rand() / (RAND_MAX + 1.0);
}

quaternion random_rotation() {
quaternion result;
do {
result = quaternion(rand_U_0_1()*2.0-1.0, rand_U_0_1()*2.0-1.0, rand_U_0_1()*2.0-1.0, rand_U_0_1()*2.0-1.0);
} while (norm(result) > 1.0);
return result*(1.0/abs(result));
}

double dot_product(quaternion q, quaternion p) {
return q.R_component_1() * p.R_component_1() +
q.R_component_2() * p.R_component_2() +
q.R_component_3() * p.R_component_3() +
q.R_component_4() * p.R_component_4();
}

int main() {
double worst_dot_product = 1.0;
for (int i=0; i<1000000000; ++i) {
quaternion q = random_rotation();
PackedQuaternion pq(q);
quaternion p = pq.get();
if (dot_product(p,q) < 0)
p *= -1.0;
if (dot_product(p,q) < worst_dot_product) {
worst_dot_product = dot_product(p,q);
std::cout << i << ' ' << q << ' ' << p << ' ' << worst_dot_product << '\n';
}
}
}
[/code]

Any comments are welcome, and feel free to use the idea or the code if you find them useful. Edited by alvaro
2

Share this post


Link to post
Share on other sites
A quaternion with [s]8[/s]10[/stealthedit] bites per component runs into the following issues:

1) You're optimizing for a problem you don't even know you have.
2) Extracting bytes from a register may well slow things down significantly (where significantly = no real increase).
3) If you're so low on memory you can't spare 128 bits (even on cell phones, 8,589,934,592 bits is easy) for animations, you've probably got other, more important problems.
4) Rotation might be slightly choppy.

So, in short:

1) Make sure you know what the problem actually is before you try to fix it.
2) At least static-test your solution concepts.
3) Don't optimize where you don't need to.
4) Something to check. Edited by Narf the Mouse
1

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0