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

Rotate 4x4 matrix formula

4 posts in this topic

I am trying to figure out how to work with a 4x4 matrix, in particular rotating them. I have been searching and reading differnt things but I keep hitting a brick wall when I see the formulas. I can see which parts of the matrix need to be changed depending on if I rotate on the x, y or z axis. But I cannot interpet their formulas so as to use them in code.

 

I have tried several experiments and I am not geting the right numbers. And since they use letters and greek letters and give no point of refernce for them I am sure additional calculation are need but there is no clear reference to the correct calculation. In one fomula I saw they used cosine and sine with the theta character in parenthases which previously in the article it was defined as pi/2 (which makes no sense at all) but that didn't work when I used it.

 

I am hoping someone has some nice clearly defined formulas I can work with.

 

0

Share this post


Link to post
Share on other sites

A piece of code is worth a thousand words. The following comes from my C++ math library. It's templated, but it should be fairly readable. It takes a unit vector representing the axis to rotate around and an angle in radians and returns a rotation matrix. See also the entries on Wikipedia.

//Creates a rotation matrix from radians
template <typename type_elements, typename type_float1, typename type_float2>
Matrix<type_elements,4,4> get_mat4_rotation_rad(const type_float1 vec[3], type_float2 theta) {
    type_elements c = cos( (type_elements)(theta) );
    type_elements s = sin( (type_elements)(theta) );
    type_elements C = (type_elements)(1) - c;
    type_elements x   = vec[0], y   = vec[1], z   = vec[2];
    type_elements xs  =   x* s, ys  =   y* s, zs  =   z* s;
    type_elements xC  =   x* C, yC  =   y* C, zC  =   z* C;
    type_elements xyC =   x*yC, yzC =   y*zC, zxC =   z*xC;
    return Matrix<type_elements,4,4>(
        (double)(x*xC+c),(double)(xyC-zs),(double)(zxC+ys),0.0,
        (double)(xyC+zs),(double)(y*yC+c),(double)(yzC-xs),0.0,
        (double)(zxC-ys),(double)(yzC+xs),(double)(z*zC+c),0.0,
                     0.0,             0.0,             0.0,1.0
    );
}

-G

Edited by Geometrian
0

Share this post


Link to post
Share on other sites

Thanks Geometrian for posting that code. I haven't worked in C++ for about a year so I will have to take a bit of time to dig into that and see what I can glean from it.

Since my post I have found a few more examples and articles but I am still a bit off on my calculatios. I am sure some will laugh but the formula I found that cracked everything open was not a formula to create the matrix but a formula that allowed one to pull the degrees of rotation out of the matrix. I took it and just reversed it, then from there I knew where to place the results and I figured out matrix multiplicaion.

 

But like I said things are bit off. There are some built in functions for simplifing rotation (they are limiting what I can do), and I am comparing my numbers to them. If I just rotate on one axis the numbers are fine it is when I rotate on more than one that things get messed up.

 

I have my code outputing the numbers from the built in rotation fuctions (first matrix below) and from my calculations (second matrix below), here's what I am seeing

X: 2deg
Y: 2deg

0.9993908270190958, 0.001217974870087876, -0.03487823687206265, 0,
0, 0.9993908270190958, 0.03489949670250097, 0,
0.03489949670250097, -0.03487823687206265, 0.9987820251299122, 0,
99.93908270190957, 100.06088018891836, 0.002125983043832047, 1

X cosine: 0.9993908270190958
X sine: 0.03489949670250097
Y cosine: 0.9993908270190958
Y sine: 0.03489949670250097

0.9993908270190958, 0, -0.03489949670250097, 0,
0.001217974870087876, 0.9993908270190958, 0.03487823687206265, 0,
0.03487823687206265, -0.03489949670250097, 0.9987820251299122, 0,
100, 100, 0, 1

Note that there is a 100 translate on both the X and Y axis so I can throughtly test my numbers.

I looks like the X and Y cosine and sine calculations are correct but something is not being calculated correctly after that. Nor is the calculation being applies to the translate values (I suspect I need to just research a bit to solve this). Hmm, as I look a little closer it looks like maybe some numbers are in the wrong spot ... but why?

 

Here is a pseudo version of the code I currently have

basematrix = [
    [1, 0, 0, 0],
    [0, 1, 0, 0],
    [0, 0, 1, 0],
    [100, 100, 0, 1]
];

function RotateMatrix(y, x)
{
    halfrot = 3.141592653589793 / 180;	// pi divided by 180

    xcos = cos(x * halfrot);
    xsin = sin(x * halfrot);

    ycos = cos(y * halfrot);
    ysin = sin(y * halfrot);

    ymatrix = [
        [ycos, 0, -ysin, 0],
        [0, 1, 0, 0],
        [ysin, 0, ycos, 0], 
        [0, 0, 0, 1]
    ];

    xmatrix = [
        [1, 0, 0, 0],
        [0, xcos, xsin, 0],
        [0, -xsin, xcos, 0], 
        [0, 0, 0, 1]
    ];

    calcmatrix = MatrixMultiply(ymatrix, basematrix);

    calcmatrix = MatrixMultiply(xmatrix, calcmatrix);

    for (i = 0; i < 4; i++)
    {
        for (j = 0; j < 4; j++)
        {
            //output calcmatrix[i][j]
        }
    }
}

function MatrixMultiply(matrixa, matrixb)
{
    newmatrix = [];
    for (i = 0; i < 4; ++i)
    {
        for (j = 0; j < 4; ++j)
        {
            newmatrix[i][j] = matrixa[i][0] * matrixb[0][j] 
                            + matrixa[i][1] * matrixb[1][j] 
                            + matrixa[i][2] * matrixb[2][j] 
                            + matrixa[i][3] * matrixb[3][j];
        }
    }	
    return newmatrix;
}

Maybe someone can see what I did wrong.

Edited by Krik
0

Share this post


Link to post
Share on other sites

Hi.

Maybe some thing like this

float4x4 rotateX = { 1.0, 0.0, 0.0, 0.0,                0.0, cos(rot.x), -sin(rot.x), 0.0,                0.0, sin(rot.x), cos(rot.x), 0.0,                0.0, 0.0, 0.0, 1.0  };
float4x4 rotateY = { cos(rot.y), 0.0, sin(rot.y), 0.0,                0.0, 1.0, 0.0, 0.0,                -sin(rot.y), 0.0, cos(rot.y), 0.0,                0.0, 0.0, 0.0, 1.0   };
float4x4 rotateZ = { cos(rot.z), -sin(rot.z), 0.0, 0.0,                sin(rot.z), cos(rot.z), 0.0, 0.0,                0.0, 0.0, 1.0, 0.0,                0.0, 0.0, 0.0, 1.0   };

0

Share this post


Link to post
Share on other sites

My guess is that your problem has something to do with notation and order of operations.

 

 

Remember that  [A][X][Y] != [Y][X][A] != [X][Y][A] != [A][Y][X]

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