• 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
d h k

Quaternions: At which angle is cam looking at ship

5 posts in this topic

Hello there!

 

 

The problem

 

I'm working on a retro space action game in Unity3D. I say retro because we do not use 3D models to visualize other ships in space but sprites, rendered from a total of 544 angles. I need to find the right sprite (ie. the right angle) for each ship in space, relative to my own camera orientation. Both the camera and other ships have six degrees of freedom.

 

While I made some progress using Matrix math and some help I've gotten in the past here on the forums a while back, we have since switched to Unity3D, which works with quaternions. I'm well aware of the fact that I can simply stick to my old attempts and convert unit quaternions to rotation matrices and back, but I would optimally like to hear if there may be a smart solution that works with quaternions throughout.

 

 

My attempt so far

 

 

Vector3 getEulerAnglesFromQuaternion(Quaternion q1)

{

        float sqw = q1.w*q1.w;

        float sqx = q1.x*q1.x;

        float sqy = q1.y*q1.y;

        float sqz = q1.z*q1.z;

        float unit = sqx + sqy + sqz + sqw; // if normalised is one, otherwise is correction factor

        float test = q1.x*q1.y + q1.z*q1.w;

        

        float pitch, yaw, roll;

        

        if (test > 0.499*unit)

        // singularity at north pole

        {

            yaw = 2 * Mathf.Atan2(q1.x,q1.w);

            roll = Mathf.PI/2;

            pitch = 0;

        }

        else if (test < -0.499*unit)

        // singularity at south pole

        {

            yaw = -2 * Mathf.Atan2(q1.x,q1.w);

            roll = -Mathf.PI/2;

            pitch = 0;

        }

        else

        {

            yaw = Mathf.Atan2(2*q1.y*q1.w-2*q1.x*q1.z , sqx - sqy - sqz + sqw);

            roll = Mathf.Asin(2*test/unit);

            pitch = Mathf.Atan2(2*q1.x*q1.w-2*q1.y*q1.z , -sqx + sqy - sqz + sqw);

        }

        

        return new Vector3(pitch, yaw, roll);

}
 
 
// in Ship::Update():
 
Quaternion q1 = Quaternion.LookRotation(Camera.main.transform.position - transform.position);

Quaternion q2 = transform.rotation;

Quaternion q = q1 * q2;

Vector3 eulerAngles = getEulerAnglesFromQuaternion(q) * Mathf.Rad2Deg;
// use eulerAngles to pick the right sprite

 

My idea was to combine the ship with the camera rotation by multiplication and then use a standard formula to retrieve euler angles from the resulting quaternion. This, however, simply does not yield the expected angle values. I'm not sure if my problem can even be solved in this fashion.

 

 

In short

 

I would like to know if anybody has an idea how to solve my problem of finding the angle at which a camera is looking at something, combining that with the orientation of the entity itself and using the result to find the right sprite to pick for it. I have access to what seems to be very good and complete math functionality in Unity3D and would like, if possible, to find a solution following the engine's quaternion-based approach. Are quaternions even usable in this case? Or am I forced to use matrix math here?

0

Share this post


Link to post
Share on other sites
How are those 544 angles defined?

I've given this problem some thought before, and what I would try is to define the positions based on the quaternion, not Euler angles. Similarly to how a cube is used for environment mapping:
* Start with a unit quaternion (q[0] + q[1]*i + q[2]*j + q[3]*k).
* Identify the component with largest absolute value (say it's q[2]).
* Divide the other components by q[2].

Now your rotation space has been divided into 4 3-dimensional blocks. You can now divide the 3-dimensional blocks into cells. For instance, if you use 5x5x5 encoding within each block, you need a total of 500 positions.

I originally thought of this scheme for compressing animations, but it seems to fit the bill here nicely.
0

Share this post


Link to post
Share on other sites

That sounds intriguing, excellent idea!

 

I'm not exactly an expert with quaternions yet, but I do know the basics (x, y, z define an arbitrary axis around which we rotate by w). How does dividing the other components by the largest one (x, y, z, with w or without?) leave me with a 4 cube division? I don't understand that very fundamental step!

 

The 544 angles are 32 yaws and 17 pitches!

Edited by d k h
0

Share this post


Link to post
Share on other sites

I'm not exactly an expert with quaternions yet, but I do know the basics (x, y, z define an arbitrary axis around which we rotate by w). How does dividing the other components by the largest one (x, y, z, with w or without?) leave me with a 4 cube division? I don't understand that very fundamental step!

Yes, w is included. Imagine x has the largest absolute value. Then you compute (y/x, z/x, w/x), which has each coordinate between -1 and 1. In that sense, it is a cube. Since there are 4 variables, you get 4 different cubes. This is similar to the situation with cubemaps, where you map a point in a sphere to one of 6 square faces.

The 544 angles are 32 yaws and 17 pitches!

Hmmm... Nothing for roll? Edited by Álvaro
0

Share this post


Link to post
Share on other sites


I'm not exactly an expert with quaternions yet, but I do know the basics (x, y, z define an arbitrary axis around which we rotate by w). How does dividing the other components by the largest one (x, y, z, with w or without?) leave me with a 4 cube division? I don't understand that very fundamental step!

Yes, w is included. Imagine x has the largest absolute value. Then you compute (y/x, z/x, w/x), which has each coordinate between -1 and 1. In that sense, it is a cube. Since there are 4 variables, you get 4 different cubes. This is similar to the situation with cubemaps, where you map a point in a sphere to one of 6 square faces.


Okay so I see how I end up with a Vector3(y/x, z/x, w/x) if x is larger than y/z/w for example. Let's say the vector is (-1, -1, 1). I understand how this always lies within a unit cube around the origin.

But then you say there are four variables, that means we get four of these cubes. This sounds, to me, like I need to then do the same procedure but not with Vector3(y/x, z/x, w/x) but Vector3(x/y, z/y, w/y) for example? Is this correct or am I misunderstanding you? If this is the correct procedure, then the largest absolute value part is confusing me, do I just let that determine the quaternion element that I start with?

Lots of questions here, sorry! tongue.png I hope you have (or someone else has) some time to explain the concept some more!


The 544 angles are 32 yaws and 17 pitches!

Hmmm... Nothing for roll?


In order to save memory we want to rotate the billboards via code ourselves! The 17 pitches also do not describe a full 360 deg turn, since we only need half a circle. Then the other half of pitches can be translated to a different set of pitches with a 180 deg yaw rotation, if that makes sense! So: 32 yaw angles = 360 deg yaw rotation, 17 pitch angles = 180 deg pitch rotation, no roll angles = rotating via code! Edited by d k h
0

Share this post


Link to post
Share on other sites
I think you do understand what I described earlier. So yes, the variable with the highest absolute value determines which cube you are in, and within that cube you use the other three variables divided by the one with largest absolute value.

If you are going to rotate the billboards, perhaps going with Euler angles wasn't such a bad idea after all. If you use the right order of the axes, you can manage to separate one of the angles into being a billboard rotation, and then you only have two degrees of freedom to discretize.

So let me read your original question again... smile.png

My idea was to combine the ship with the camera rotation by multiplication and then use a standard formula to retrieve euler angles from the resulting quaternion. This, however, simply does not yield the expected angle values. I'm not sure if my problem can even be solved in this fashion.

That sounds reasonable, except you probably need to multiply one of the quaternions by the inverse of the other, or something like that. If the ship is not in the center of the image you may have to do something slightly different... So, in short, I need to think about it a little more.
0

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