# How to use XNAMath?

## Recommended Posts

I need to store a 4x4 matrix in a class but I don't even know how to do this. Here is basically the problem:

class foo{public:  XMMATRIX  matrix;}void func(){  foo* instance = new foo;  instance->matrix = XMMatrixRotationRollPitchYaw( 0, 0, 0 );}

This does not work so I instead used XMFLOAT4X4 but I don't know how to convert the XMMATRIX to a XMFLOAT4X4

I don't understand?...

##### Share on other sites
What do you mean by "does not work"? By the way, you can store an XMMATRIX in an XMFLOAT4x4 using XMStoreFloat4x4.

##### Share on other sites
Quote:
 Original post by MJPWhat do you mean by "does not work"? By the way, you can store an XMMATRIX in an XMFLOAT4x4 using XMStoreFloat4x4.

Though that is one way to do it I don't think it is the right way. Here is the error I'm getting:

Quote:
 1>c:\program files (x86)\microsoft visual studio 10.0\vc\include\vector(870): error C2719: '_Val': formal parameter with __declspec(align('16')) won't be aligned

##### Share on other sites
Ah, this old problem.

The issue is that classes with XMMaths types in them need to be stored aligned, which is something std::vector can't garentee. The std::vector implementation which comes with VS2010 is setup to throw an error in cases where an aligned type is used.

You could store pointers to the class just fine in the std::vector however, just not instances.

##### Share on other sites
Quote:
 Original post by phantomAh, this old problem.The issue is that classes with XMMaths types in them need to be stored aligned, which is something std::vector can't garentee. The std::vector implementation which comes with VS2010 is setup to throw an error in cases where an aligned type is used.You could store pointers to the class just fine in the std::vector however, just not instances.

That explains the problem though the data needs to be actually contained in the class or I will have to write a bunch of code to create new instances and then delete them which I don't believe is worth the optimization so probably using the store function is really the best option...

##### Share on other sites
On x64 mode it will work fine..

##### Share on other sites
Quote:
 Original post by Icebone1000On x64 mode it will work fine..

You mean if the project is compiled as an x64 bit application?

##### Share on other sites
http://www.qsoftz.com/mirza/?p=59

##### Share on other sites
phantom, unless I missed something, he never once mentioned std::vector.

He is trying to use the default new with aligned data. Use _aligned_malloc or overload new.

##### Share on other sites
Quote:
 Original post by Chris_Fphantom, unless I missed something, he never once mentioned std::vector.He is trying to use the default new with aligned data. Use _aligned_malloc or overload new.

I never mentioned it though it was one of the other problems I was having but I thought they were the same problem. Also I'm probably just going to avoid using malloc or new for this because it will create a huge mess to clear up and I don't think it's worth such a small optimization.

[Edited by - SteveDeFacto on November 13, 2010 4:36:38 AM]

##### Share on other sites
Quote:
 Original post by Chris_Fphantom, unless I missed something, he never once mentioned std::vector.

I direct you to his 2nd post and error message in it [smile]