Advertisement Jump to content
Sign in to follow this  
Chaosenemy

I need help with matrices

This topic is 1744 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hey guys,

I would greatly appreciate any help you can offer me. I'm writing a big Entity/Sprite/Model/Rendering system and I am having the hardest time understanding how to use matrices properly. I guess my main questions is: how can I use only a single matrix per object to define where it is located, how it is rotated, and how it is scaled? Since matrix multiplication is cumulative, I have come to the conclusion that I have no choice but to store a (Vector3D) position, (Quaternion) rotation, and (Vector3D) scale, and then re-calculate the matrix (for rendering) each frame from these values. This seems.... cumbersome and ugly. Is there some way to simply have a Matrix for each object and modify it directly when I call SetPosition(), RotateX(), etc.? I'm not that great with Matrix manipulation, so maybe this is no-brainer, but I can't seem to find any direct answer to this no matter where I search.

 

Side note: This isn't meant to sound rude at all, but please don't suggest that I use SDL or some matrix library. I get that a lot, but I want to make this myself. I just need some help in the right direction. Believe me, I've tried learning this myself, but I learn by example, so reading books explaining matrices just doesn't make it click for me.

 

Thanks in advance!

Share this post


Link to post
Share on other sites
Advertisement

Storing the model-to-world transform as (position, rotation, scale) has advantages (although I would argue that scale should be a single number, or not exist at all). But you can certainly use matrices instead.

 

I am not sure what you are having trouble with, though. To SetPosition, you just modify the "translation vector" part of the affine matrix. To RotateX, you multiply the 3x3 submatrix corresponding to the endomorphism of the vector space (ignore the big words: You probably know perfectly well what 3x3 submatrix I am talking about) by a 3x3 matrix that rotates around the x axis.

 

That probably doesn't help you, but I need to know what you know about how a 4x4 matrix of a special form can be used to encode the transformations you are interested in.

Share this post


Link to post
Share on other sites
Because your input had no meaning in the context of the conversation.
You said you use floats.
Vector3D uses floats.
Quaternion uses floats.
Matrix uses floats.

In other words, everyone uses floats.
Your reply is nonsensical at best, detrimental at worst to anyone who understands it to mean he or she should replace a Vector3D with 3 floats. That is absolutely 100% not the way it “seems to go”.

That is absolutely terrible advice.
What sane person would do that?
Vectors provide encapsulation, simplify the code, and make its intent clearer.

With 3 separate floats you have basically a pool of ungrouped floats rather than a clear separation between rotation values, position values, and scale values.

With 3 separate floats you have to do this:
object->addPos( off_x, off_y, off_z );
instead of this:
object->pos() += off;
Vector3D operators make manipulating code easier and faster.

What if one day you want to add SIMD optimizations to vector operations? Good luck if you just use raw floats everywhere.


That is why I down-voted you. And trust me, as rare as it is that I down-vote, you should take it as a serious hint.


L. Spiro Edited by L. Spiro

Share this post


Link to post
Share on other sites

I think the core question here is, how to directly modify a matrix, instead of storing an additional data for position, rotation, scale and then rebuilding the matrix

 

For this, i'll point you to what i guess is the canonical resource on matrices: http://www.cs.princeton.edu/~gewang/projects/darth/stuff/quat_faq.html

If you look at the breakdown of the matrix composition, M[3][0] M[3][1] M[3][2] holds the translation vector. So, a function to set the position would simply modify these three bits of the matrix.

 

I hope this addresses the question.

Good luck!

Edited by uglybdavis

Share this post


Link to post
Share on other sites

You don't have to recalculate the matrix every frame, only when the position, translation or scale changes.

 

I think that doing operations with only some indexes of the matrix will give you more troubles than solutions, as you say, the transformations are cumulative, so it's not the same to have a rotation of 90° and then a translation, or a rotation of 45° a translation and another rotation of 45°. If you change only some values of a precalculated matrix you'll get something more like the second case when it sounds you want the first case.

Share this post


Link to post
Share on other sites

For the most part you will have to build your matrix each frame unless the object is completely static. The order I build matrices is:

scaling

rotation

position

The scaling is not usually going to change. If your object doesn't change its scale, then you could store that matrix as the object's origin matrix.

The rotation (judging from you post) is done with a quaternion and then you build a matrix from that.... Multiply your scaling matrix by this one.

Position is simply:

matrix(3,0) is x

matrix(3,1) is y

and matrix(3,2) is z

It seems like a lot to do, but matrix math is fast.

 

Another note: If you are using a quaternion for rotation, have that quaternion as part of the structure of your object and make it cumulative.

Share this post


Link to post
Share on other sites


I guess my main questions is: how can I use only a single matrix per object to define where it is located, how it is rotated, and how it is scaled?

You really need to get a well-written book to teach you the basics of linear algebra, especially as it applies to games.  Perhaps "3D Math Primer for Graphics and Game Development", or the math section in "Physics for Game Developers", or many others.

 

All of those values are present in a transformation matrix.

 

A transformation matrix has three "basis vectors", that encode the rotation. It also has a vector showing where it is located. It has a value that can serve as a flag to indicate if something is a point or a vector. 

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!