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

Problem with transformation matrix

4 posts in this topic

I'm making a lightweight 3D engine (nothing serious) in HTML5 and something is wrong with my rotations. I'm working with a Camera class that contains properties for rotation, translation and scaling in each of the tree axes. On every frame, the class creates a new transformation matrix from these properties, which is then applied to every point in order to draw them.

I have [url="http://www.juliosepia.com/demos/plane/index.html"]a demo[/url] that shows a simple 3D model with th camera rotating around the Y axis. The current transformation matrix can be seen overlaid on top of the canvas.


Scaling and translating work great so far, but rotations seem very wonky. I believe the problem lies in the function used to create the transformation matrix (which I took from [url="http://www.songho.ca/opengl/gl_anglestoaxes.html"]http://www.songho.ca...glestoaxes.html[/url] ):
(the properties this.rx, ry, rz represent the angles of rotation around each axis in degrees)


[code]this._calculateMatrix = function()
{
// reset to identity matrix
this.m = j3dMat.getIdentity();

// translation
this.m[3][0] = this.tx;
this.m[3][1] = this.ty;
this.m[3][2] = this.tz;

// scale
this.m[0][0] = this.sx;
this.m[1][1] = this.sy;
this.m[2][2] = this.sz;

// rotation begins here
this.rx = this.rx % 360;
this.ry = this.ry % 360;
this.rz = this.rz % 360;
var DEG2RAD = 3.141593 / 180;
var rm = j3dMat.getIdentity(); // a separate rotation matrix

// rotation angle about X-axis (pitch)
var theta = this.rx * DEG2RAD;
var sx = Math.sin(theta);
var cx = Math.cos(theta);

// rotation angle about Y-axis (yaw)
theta = this.ry * DEG2RAD;
var sy = Math.sin(theta);
var cy = Math.cos(theta);

// rotation angle about Z-axis (roll)
theta = this.rz * DEG2RAD;
var sz = Math.sin(theta);
var cz = Math.cos(theta);

// determine left axis
rm[0][0] = cy*cz;
rm[0][1] = sx*sy*cz + cx*sz;
rm[0][2] = -cx*sy*cz + sx*sz;

// determine up axis
rm[1][0] = -cy*sz;
rm[1][1] = -sx*sy*sz + cx*cz;
rm[1][2] = cx*sy*sz + sx*cz;

// determine forward axis
rm[2][0] = sy;
rm[2][1] = -sx*cy;
rm[2][2] = cx*cy;

// multiply trans matrix by rotation matrix
this.m = j3dMat.mult(this.m, rm);
}[/code]

What am I doing wrong? Any pointers/resources on how to do this the right way?

Thanks!
0

Share this post


Link to post
Share on other sites
[url="http://en.wikipedia.org/wiki/Rotation_matrix#General_rotations"]http://en.wikipedia.org/wiki/Rotation_matrix#General_rotations[/url]

Your terms appear to be slighly different, I'm looking at the second column, top row and I don't see a similar term in yours (-cos*sin + sin*sin*cos). Check all your terms again.

0

Share this post


Link to post
Share on other sites
Haven't looked at the rotational terms, but: The transformation order shown in the code snippet is unusual. Assuming that column vectors are used, then the matrix is initially set to
[b]T[/b] * [b]S[/b]
and then multiplied by the combined R (presumably) on the right, yielding in
[b]T[/b] * [b]S[/b] * [b]R[/b]

This means scaling is applied along the already rotated model axes, what is probably not wanted. The usual order is [b]T[/b] * [b]R[/b] * [b]S[/b] instead.
1

Share this post


Link to post
Share on other sites
[quote name='Nanoha' timestamp='1302715162' post='4798027']
[url="http://en.wikipedia.org/wiki/Rotation_matrix#General_rotations"]http://en.wikipedia....neral_rotations[/url]

Your terms appear to be slighly different, I'm looking at the second column, top row and I don't see a similar term in yours (-cos*sin + sin*sin*cos). Check all your terms again.
[/quote]

This helped a lot. My terms were wrong. Thanks!


[quote name='haegarr' timestamp='1302715998' post='4798032']
Haven't looked at the rotational terms, but: The transformation order shown in the code snippet is unusual. Assuming that column vectors are used, then the matrix is initially set to
[b]T[/b] * [b]S[/b]
and then multiplied by the combined R (presumably) on the right, yielding in
[b]T[/b] * [b]S[/b] * [b]R[/b]

This means scaling is applied along the already rotated model axes, what is probably not wanted. The usual order is [b]T[/b] * [b]R[/b] * [b]S[/b] instead.
[/quote]

Both work the same for me, even with scaling != 1. :mellow: I guess something else must be wrong, but I'll get to that bridge when I cross it.
0

Share this post


Link to post
Share on other sites
[quote name='jsepia' timestamp='1302728567' post='4798136']
[quote name='haegarr' timestamp='1302715998' post='4798032']
Haven't looked at the rotational terms, but: The transformation order shown in the code snippet is unusual. Assuming that column vectors are used, then the matrix is initially set to
[b]T[/b] * [b]S[/b]
and then multiplied by the combined R (presumably) on the right, yielding in
[b]T[/b] * [b]S[/b] * [b]R[/b]

This means scaling is applied along the already rotated model axes, what is probably not wanted. The usual order is [b]T[/b] * [b]R[/b] * [b]S[/b] instead.
[/quote]

Both work the same for me, even with scaling != 1. ...
[/quote]
If sx==sy==sz==1, then [b]S[/b] is the identity matrix, and then of course [b]T[/b] * [b]S[/b] * [b]R [/b]== [b]T[/b] * [b]R[/b] * [b]S[/b], because [b]M[/b] * [b]I[/b] == [b]M[/b] for any [b]M[/b].

If sx==sy==sz where sx!=1 (i.e. uniform scaling), then again [b]T[/b] * [b]S[/b] * [b]R [/b]== [b]T[/b] * [b]R[/b] * [b]S[/b].

[i]But[/i], if sx!=sy, sy!=sz, or sx!=sz (i.e. non-uniform scaling, what I assume is allowed because you use 3 different variables here) then the order [i]does[/i] have an effect. For example, if you have an airplane that should be made longer, and you apply the scaling on the rotated model, then the airplane gets e.g. wider instead of longer.

This may not be the primary issue you're currently facing, but it will pop up later.
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