• 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
Endemoniada

Y-Axis Rotation from Matrix

4 posts in this topic

Hi guys,

 

I'm trying to get the y-axis rotation from a matrix.

 

I found this:

 

r=atan2(-M._31, sqrt(M._32*M._32+M._33*M._33);

 

...but I don't know if that's for D3D9 matrices. It's tricky because of the return values of atan2 as well.

 

I looked at D3DXMatrixDecompose() but I don't know how to get the y-axis rotation from the quaternion.

 

I'm hoping to get the rotation in [0...twoPi]

 

Thanks.

 

 

 

0

Share this post


Link to post
Share on other sites

Are you just wanting the angle relative to the world? Here are some things that might help you figure it out:

 

+ You can get the absolute angle between two vectors using acos( dot( v1, v2 ) ).

+ If your world is composed of X=right, Y=up, Z=forward, then your world's forward vector is (0,0,1).

+ The vector you might be interested in is the forward/Z vector of the matrix (M._31,M._32,M._33).

+ If your matrix only contains Y rotations, the positive/absolute angle will be equal to acos( M._33 )

+ You'll know if the angle is flipped (rotating the opposite direction) if M._31 is negative or positive.

+ If your matrix contains other (X/Z) rotations, it may be necessary to *smash* the matrix forward vector to the X/Z plane, then get the angle.

+ To put it simply, smashing the vector would be something like normalize( m._31, 0, m._33 ). But take care that (m._31,0,m._33) isn't zero.

 

Hope that helps

Edited by Stephany
0

Share this post


Link to post
Share on other sites

Let me make sure I understand what you want. Assuming that +y is up, you want to know what angle something is facing. In that case you could just transform the forward vector by the matrix.

transformed = forward * matrix;

then use atan2 on the x and z component of the result. So if +z is forward, the angle would be 

atan2(transformed.x, transformed.z);

0 radians would be pointing down the z axis with a positive value rotating towards positive x.

 

If forward will always be positive z, then you can simply put.

atan2(matrix._13, matrix._33)

If you don't mind me asking, what do you need the angle for? Usually there are more elegant solutions to problems using matrices and vectors than finding angles and using trig functions. 

Edited by HappyCoder
0

Share this post


Link to post
Share on other sites

Hey guys,

 

I didn't think it was a good thing but figured I'd give it a shot. I'm trying to test something that's not working and think it might help.

 

A typical example is this:

 

 
D3DXMATRIX R, T, M;
 
D3DXMatrixTranslation(&T, 2.0f, 0.0f, 0.0f);
D3DXMatrixRotationY(&R, -D3DX_PI*0.5f);
M=R*T;
M=M*SomeOtherMatrix;
D3DXMatrixInverse(&M, NULL, &M);
 
float r=GetYRotation(&M); // the helper I want to make
 
// if it worked properly it would do this:
D3DXMatrixRotationY(&R, D3DX_PI*0.5f);
float r=GetYRotation(&M);
// r would equel pi*0.5
 

 

That's all I would be using it for.

 

Thanks.

Edited by Endemoniada
0

Share this post


Link to post
Share on other sites

 

 
D3DXMATRIX R
, T, M;
 
D3DXMatrixTranslation
(&T, 2.0f, 0.0f, 0.0f);
D3DXMatrixRotationY(&R, -D3DX_PI*0.5f);
M=R*T;
M=M*SomeOtherMatrix;
D3DXMatrixInverse(&M, NULL, &M);
 
float r=GetYRotation(&M); // the helper I want to make
 
// if it worked properly it would do this:
D3DXMatrixRotationY(&R, D3DX_PI*0.5f);
float r=GetYRotation(&M);
// r would equel pi*0.5

if the M matrix will always be Y rotation matrix, you could derive the parameter it is constructed from just as this : acos (M[0][0]).

But this parameter is not an angle with Y axis - it is actualy an angle in ZX plane - thus making objects revolve around Y axis if transfomed by M.

 

But if you will incompose also Z and X rotation matricies into the M matrix (multiply the M with them), then you will not be able to use this simple solution. You will need to solve 3 linear equations. I recomend Cramer method for solving them as it does not use division, so you will be zero safe. Following is a method that constructs world rotation and translation from the 3 axises sinuses and cosinuses and position

function ComposeWorld4x3(cx,cy,cz,sx,sy,sz,x,y,z,res,offr)
{
    var m1=cy*cz;
    var m2=cy*sz;
    var m3=-sy;
    
    var m4=sx*sy*cz-sz*cx;
    var m5=sx*sy*sz+cx*cz;
    var m6=sx*cy;
    
    var m7=cx*sy*cz+sz*sx;
    var m8=cx*sy*sz-sx*cz;
    var m9=cx*cy;
    
    res[offr]=m1;
    res[offr+1]=m2;
    res[offr+2]=m3;
    res[offr+3]=0.0;
    res[offr+4]=m4;
    res[offr+5]=m5;
    res[offr+6]=m6;
    res[offr+7]=0.0;
    res[offr+8]=m7;
    res[offr+9]=m8;
    res[offr+10]=m9;
    res[offr+11]=0.0;
    res[12+offr]=x;
    res[13+offr]=y;
    res[14+offr]=z;
    res[15+offr]=1.0;
}

the res is a 16 array in column memory layout (res[3]is 4th component of first column) and cx,cy,cz,sx,sy,sz are cosinuses of X,Y,Z and sinuses of X,Y,Z angles respectively

 

Now if you look at it you may take equations as those:

var m1=cy*cz;

var m9=cx*cy;

var m3=-sy;

you should find out cy through third simple equation m3=-sy such as sqrt(1.0-asin(-1.0*m3)) and use 2 linear equations system only:

M[0][0]=cy*cz;

M[2][2]=cx*cy; where cy is aleary existing constant you computed

 

this is cramer's rule

 

Consider a system of n linear equations for n unknowns, represented in matrix multiplication form as follows:

b291cc43372970e4a41e6baa698b86d2.png 09805d958fc7ab3d6c57c02f71afdc39.png

where the n by n matrix 7fc56270e7a70fa81a5935b72eacbe29.png has a nonzero determinant, and the vector 5420f1619fb676198b6a7d842ee5b87c.png is the column vector of the variables.

Then the theorem states that in this case the system has a unique solution, whose individual values for the unknowns are given by:

1bc630f43695ef96b162b72d7030b002.png

where e8aaf87d9a5c35b14cfbc370d3fd7b21.png is the matrix formed by replacing the ith column of 7fc56270e7a70fa81a5935b72eacbe29.png by the column vector 92eb5ffee6ae2fec3ad71c777531578f.png.

So you would perform b column to be M[0,0],M[2,2] and A matrix to be

[cy,0]

[0,cy]

 

you then compute x1 and x2 by applying last sentence of quoted Cramer's rule on them both. You will then poses cx,cy,cz and use acos on all three to know angles around all three axises of a rotation matrix

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