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

updating world matrix, identity needed?

15 posts in this topic

Hi,

 

I'm a bit confused when it comes to d3d9 matrix translation, rotation and scaling.

My assumption:

 

- when I calculate a new matrix for translation/rotation/ scaling, I always first have to set the identity matrix

- this is because I'm passing absolute values when recalculating the matrix, not 'incremental/ changes'.

 

The strange this is that the results are always the same, when I do and when I don't set the identity matrix.

The code snippet is below.

 

What would you say?

bool CD3dmeshInst::UpdateWorldMatrix()
{
	if(!mDynamic) return false;
	if(!mIsMoved && !mIsRotated && !mIsScaled) return false;

	if(mIsMoved)
	{
//		D3DXMatrixIdentity(&mMatTranslate);
		D3DXMatrixTranslation(&mMatTranslate, mWorldPos.x, mWorldPos.y, mWorldPos.z);
		mIsMoved = false;
	}
	if(mIsRotated)
	{
//		D3DXMatrixIdentity(&mMatRotateX);
		D3DXMatrixRotationX(&mMatRotateX, D3DXToRadian(mRot.x));
//		D3DXMatrixIdentity(&mMatRotateY);
		D3DXMatrixRotationY(&mMatRotateY, D3DXToRadian(mRot.y));
//		D3DXMatrixIdentity(&mMatRotateZ);
		D3DXMatrixRotationZ(&mMatRotateZ, D3DXToRadian(mRot.z));
		mIsRotated = false;
	}
	if(mIsScaled)
	{
//		D3DXMatrixIdentity(&mMatScale);
		D3DXMatrixScaling(&mMatScale, mScale, mScale, mScale);
		mIsScaled = false;
	}
	mMatWorld = (D3DXMATRIXA16)(mMatScale*mMatRotateX*mMatRotateY*mMatRotateZ*mMatTranslate);

	D3DXMatrixInverse(&mMatWorldInv, NULL, &mMatWorld);
	D3DXMatrixTranspose(&mMatWorldInvTransp, &mMatWorldInv);
	return true;
}

Edited by cozzie
0

Share this post


Link to post
Share on other sites

This function should compose matrix from scale, axis-angle rotation and translation vectors in one step instead of lots of matrix multiplies (and what not) you currently have.

#include <cmath>
 
D3DXMATRIX* D3DXMatrixCompose(D3DXMATRIX* pout, CONST D3DXVECTOR3* pscale, CONST D3DXVECTOR3* paxis, FLOAT angle, CONST D3DXVECTOR3* ptranslation)
{
    D3DXVECTOR3 v;
    D3DXVec3Normalize(&v, paxis);

    pout->m[0][0] = ((1.0f - std::cos(angle)) * v.x * v.x + std::cos(angle)) * pscale->x;
    pout->m[0][1] = ((1.0f - std::cos(angle)) * v.y * v.x + std::sin(angle) * v.z) * pscale->x;
    pout->m[0][2] = ((1.0f - std::cos(angle)) * v.z * v.x - std::sin(angle) * v.y) * pscale->x;
    pout->m[0][3] = 0.0f;

    pout->m[1][0] = ((1.0f - std::cos(angle)) * v.x * v.y - std::sin(angle) * v.z) * pscale->y;
    pout->m[1][1] = ((1.0f - std::cos(angle)) * v.y * v.y + std::cos(angle)) * pscale->y;
    pout->m[1][2] = ((1.0f - std::cos(angle)) * v.z * v.y + std::sin(angle) * v.x) * pscale->y;
    pout->m[1][3] = 0.0f;

    pout->m[2][0] = ((1.0f - std::cos(angle)) * v.x * v.z + std::sin(angle) * v.y) * pscale->z;
    pout->m[2][1] = ((1.0f - std::cos(angle)) * v.y * v.z - std::sin(angle) * v.x) * pscale->z;
    pout->m[2][2] = ((1.0f - std::cos(angle)) * v.z * v.z + std::cos(angle)) * pscale->z;
    pout->m[2][3] = 0.0f;

    pout->m[3][0] = ptranslation->x;
    pout->m[3][1] = ptranslation->y;
    pout->m[3][2] = ptranslation->z;
    pout->m[3][3] = 1.0f;

    return pout;
}

 

Do you even need scale? If so, is it uniform? If yes, you can skip inverse-transpose part.

0

Share this post


Link to post
Share on other sites


The strange this is that the results are always the same, when I do and when I don't set the identity matrix

 

As long as those functions don't care about the matrix that's passed in, and always set every component of the matrix on output, then of course it won't matter. Basically, a function like D3DXMatrixTranslation is just going to stomp every value in the matrix that's passed in. 

 

However, your code has conditionals in it. For instance, if mIsMoved is not true, you'll never set any value into mMatTranslate. So it will keep whatever value it had last time (I'm assuming based on the name that it's a member variable). So you have a bug if any of those conditionals is ever false. You should move all the D3DXMatrixIdentity calls outside of the conditionals so they are set every time.

1

Share this post


Link to post
Share on other sites
@Phil_t: thanks, I think you're right that the d3dxmatrix functions 'start from scratch'. What I dont understand in that case, is why one would need the d3dxmatrixidentity function. For now I'll just leave out the d3dxmatrixidentity calls (I execute them once when the world matrix is created initially). When a mesh instance is not moved etc, indeed the existing matrix is kept and used in calculating the end result world matrix.

@belfegor: thanks, I'll try it out, sounds good that it saves me a lot of matrix multiplications.
How can I pass in different rotation angles for x, y and z? (I now see one float for angle)
For now I need the inverse transpose just for my lighting shader, if I can change this I could skip it, because your right, I'm never scaling with different values for x/y/z.
0

Share this post


Link to post
Share on other sites
Ps; I think I know why I might need the d3dxmatrixidentity matrix.
In my case I dont, because I'm always doing a translation, but say I wanted only rotation as component in a (world)matrix, I would need to set translation to identity or not use the translation matrix in the end multiplication.
Makes sense?
0

Share this post


Link to post
Share on other sites

Yup, that's kind of what I tried to explain in the 2nd paragraph of my response.

0

Share this post


Link to post
Share on other sites

How can I pass in different rotation angles for x, y and z? (I now see one float for angle)

 

I think that this should work for euler angles:

D3DXMATRIX* D3DXMatrixCompose(D3DXMATRIX *pout, CONST D3DXVECTOR3* pscale, FLOAT pitch, FLOAT yaw, FLOAT roll, CONST D3DXVECTOR3* ptranslation)
{
    pout->m[0][0] = std::cos(yaw) * std::cos(roll) * pscale->x;
    pout->m[0][1] = std::cos(yaw) * std::sin(roll) * pscale->x;
    pout->m[0][2] = -std::sin(yaw) * pscale->x;
    pout->m[0][3] = 0.0f;

    pout->m[1][0] = (((std::sin(pitch) * std::sin(yaw)) * std::cos(roll)) + (std::cos(pitch) * -std::sin(roll))) * pscale->y;
    pout->m[1][1] = (((std::sin(pitch) * std::sin(yaw)) * std::sin(roll)) + (std::cos(pitch) * std::cos(roll))) * pscale->y;
    pout->m[1][2] = std::sin(pitch) * std::cos(yaw) * pscale->y;
    pout->m[1][3] = 0.0f;

    pout->m[2][0] = (((std::cos(pitch) * std::sin(yaw)) * std::cos(roll)) + (-std::sin(pitch) * -std::sin(roll))) * pscale->z;
    pout->m[2][1] = (((std::cos(pitch) * std::sin(yaw)) * std::sin(roll)) + (-std::sin(pitch) * std::cos(roll))) * pscale->z;
    pout->m[2][2] = std::cos(pitch) * std::cos(yaw) * pscale->z;
    pout->m[2][3] = 0.0f;

    pout->m[3][0] = ptranslation->x;
    pout->m[3][1] = ptranslation->y;
    pout->m[3][2] = ptranslation->z;
    pout->m[3][3] = 1.0f;

    return pout;
}

And here is the version with quaternion rotation:

D3DXMATRIX* D3DXMatrixCompose(D3DXMATRIX* pout, CONST D3DXVECTOR3* pscale, CONST D3DXQUATERNION* prot, CONST D3DXVECTOR3* ptranslation)
{
    pout->m[0][0] = (1.0f - 2.0f * (prot->y * prot->y + prot->z * prot->z)) * pscale->x;
    pout->m[0][1] = (2.0f * (prot->x *prot->y + prot->z * prot->w)) * pscale->x;
    pout->m[0][2] = (2.0f * (prot->x * prot->z - prot->y * prot->w)) * pscale->x;
    pout->m[0][3] = 0.0f;

    pout->m[1][0] = (2.0f * (prot->x * prot->y - prot->z * prot->w)) * pscale->y;
    pout->m[1][1] = (1.0f - 2.0f * (prot->x * prot->x + prot->z * prot->z)) * pscale->y;
    pout->m[1][2] = (2.0f * (prot->y *prot->z + prot->x * prot->w)) * pscale->y;
    pout->m[1][3] = 0.0f;

    pout->m[2][0] = (2.0f * (prot->x * prot->z + prot->y * prot->w)) * pscale->z;
    pout->m[2][1] = (2.0f * (prot->y * prot->z - prot->x * prot->w)) * pscale->z;
    pout->m[2][2] = (1.0f - 2.0f * (prot->x * prot->x + prot->y * prot->y)) * pscale->z;
    pout->m[2][3] = 0.0f;

    pout->m[3][0] = ptranslation->x;
    pout->m[3][1] = ptranslation->y;
    pout->m[3][2] = ptranslation->z;
    pout->m[3][3] = 1.0f;

    return pout;
}
Edited by belfegor
1

Share this post


Link to post
Share on other sites
Great, thanks.
I'll use the euler angle version and study it to understand :)

I didn't use quaternion rotations yet.
0

Share this post


Link to post
Share on other sites

@belfegor: I got it working, thanks.

One strange thing though, rotation around Z and Y where switched, yaw and pitch (either in the compose matrix function or in my engine/ models :))

 

It seems to work fine.

I assume I will now always have to create a new world matrix if either a mesh instance has moved, rotated or scaled?

(won't be better in performance to keep older matrices and check which of the three things has happened)

0

Share this post


Link to post
Share on other sites

It will simplify your code if you just create a new world matrix from scratch each time. Don't optimize unless you have a reason to believe this is a performance bottleneck.

0

Share this post


Link to post
Share on other sites

Thanks, got it in right away.

Strangely there's something weird with the X/Y and Z rotations, when I adapt the function as is, I read:

 

- Y rotation, Z rotation, X rotation

 

But I have to pass my rotation values, in this order for:

 

- X rotation, Y rotation, Z rotation

 

I have to get into that, and compare the result with before (when I used the separate rotation matrices).

0

Share this post


Link to post
Share on other sites

Well, you had before in this order:

mMatRotateX*mMatRotateY*mMatRotateZ

so i assumed it is what you want?

 


One strange thing though, rotation around Z and Y where switched, yaw and pitch...

I assume pitch = X, yaw = Y and roll = Z

Edited by belfegor
0

Share this post


Link to post
Share on other sites

Got it, sorry. Probably messed it up, while changing pitch/yaw/roll to X/Y/Z rot.

Thanks again.

 

It all looks good now, I see a little difference for a specific rotation of a mesh instance compared to the earlier lot of multiplications with the d3dx functions. I'll get into that and see what causes the difference.

 

This is now the cleaned up function, to update the world matrix for dynamic mesh instances:

bool CD3dmeshInst::UpdateWorldMatrix()
{
	if(!mDynamic) return false;
	if(!mIsMoved && !mIsRotated && !mIsScaled) return false;
	else
	{
		D3DXMatrixCompose(&mMatWorld, &D3DXVECTOR3(mScale, mScale, mScale), mRot.x, mRot.y, mRot.z, &mWorldPos);
	}

	D3DXMatrixInverse(&mMatWorldInv, NULL, &mMatWorld);
	D3DXMatrixTranspose(&mMatWorldInvTransp, &mMatWorldInv);
	return true;
}

Later on I might remove the inverse transpose part, after changing my pixelshader/ lighting shader.

0

Share this post


Link to post
Share on other sites

Got it, stupid!!! I passed radians instead of degrees in the new improved function wacko.png

Edited by cozzie
0

Share this post


Link to post
Share on other sites

You should pass radians not degrees, like you did before with D3DXToRadian macro (if your mRot is in degrees):

D3DXMatrixCompose(&mMatWorld, &D3DXVECTOR3(mScale, mScale, mScale), D3DXToRadian(mRot.x), D3DXToRadian(mRot.y), D3DXToRadian(mRot.z), &mWorldPos);

Like Phil_t said, rebuild your matrix every time, there is no point to check if it is "moved".

Edited by belfegor
1

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