I wrote the matrix class a while ago, it really simplifies a lot of the generic coding you’d have to otherwise do to convert the matrix classes between PhysX and DirectX.

The basic idea is to inherit from the PhysX matrix/vector classes (NxMat34/NxVec3) and add a few constructors and overloaded operators to make it play nice with its DirectX counterparts.

Header file for EmMatrix:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#pragma once
 
#ifndef NX_CALL_CONV
#define WIN32
#endif
 
#define NOMINMAX
 
#include "NxMat34.h"
#include <d3dx9.h>
#include "NxPhysics.h"
class EmMatrix : public NxMat34
{
public:
	EmMatrix();
	EmMatrix(const D3DXMATRIX& m);
	EmMatrix(const NxMat34& m);
 
        /*takes in an actor and gets the globalPose*/
	EmMatrix(const NxActor& act);
	D3DXMATRIX D3DMat() const;
 
	//operator overloads
	D3DXMATRIX operator*(const EmMatrix& m);
	D3DXMATRIX operator*(const D3DXMATRIX& m);
 
	EmMatrix operator*=(const EmMatrix& m);
	D3DXMATRIX operator*=(const D3DXMATRIX& m);
};[/sourcecode]
 
The implementation is fairly straight forward,
 
[sourcecode language='cpp']#include "EmMatrix.h"
 
EmMatrix::EmMatrix()
{
	t.zero();
	M.id();
}
 
EmMatrix::EmMatrix(const D3DXMATRIX& m)
{
	//D3DXMATRIX is a struct with some functions, it has an overloaded typecast
	//operator which returns the matrix in a float array of size 16. PhysX's NxMat34 can be
	//set through an NxReal array of size 16, float typecasts to NxReal and there we go.
	NxMat34::setColumnMajor44((const float*)m);
}
 
EmMatrix::EmMatrix(const NxActor& act)
{
	//This function is a little wierd, it takes in an NxActor and gets it's matrix (through 
	//NxActor::getGlobalPose)
	*this = act.getGlobalPose();
 
	//One generic unit in DirectX is 0.5 units in PhysX, to compensate for this we multiply the 
	//translation vector by 2.
	this->t *= 2;
}
 
//gets the D3DXMATRIX equivalent of the matrix class
D3DXMATRIX EmMatrix::D3DMat() const
{
	D3DXMATRIX d3dmat;
	//(float*)d3dmat Makes use of an overloaded operator FLOAT* ();
	getColumnMajor44((float*)d3dmat);
 
	return d3dmat;
}
 
//Given the above the remaining functions should be self explanatory...
 
//operator overloads (note it's better to return D3DXMATRIX in case we are dealing with d3d matrices,
//since EmMatrix is derived from NxMat34 we are not presented with any conversion issues when using them
//with physx.
D3DXMATRIX EmMatrix::operator*(const EmMatrix& m)
{
	D3DXMATRIX multiple = this->D3DMat() * m.D3DMat();
 
	return multiple;
}
 
D3DXMATRIX EmMatrix::operator*(const D3DXMATRIX& m)
{
	D3DXMATRIX curr = this->D3DMat();
	D3DXMatrixMultiply(&curr, &curr, &m);
 
	return curr;
}
 
EmMatrix EmMatrix::operator *=(const EmMatrix& m)
{
	EmMatrix multiple = *this * m;
	*this = multiple;
 
	return multiple;
}
 
D3DXMATRIX EmMatrix::operator *=(const D3DXMATRIX& m)
{
	D3DXMATRIX curr = this->D3DMat();
	D3DXMatrixMultiply(&curr, &curr, &m);
 
	this->setColumnMajor44((float*)curr);
 
	return curr;
}

Download Code

2 Responses to “A PhysX Matrix class for the DirectX programmer”

  1. gonzalo krosnyak Says:

    hi, thank you so much fpr posting all this work of yours.
    i am trying to implement the emmatrix class…
    got a linker:
    1>Linking…
    1>EmMatrix.obj : error LNK2019: unresolved external symbol “public: __thiscall EmMatrix::EmMatrix(class NxMat34 const &)” (??0EmMatrix@@QAE@ABVNxMat34@@@Z) referenced in function “public: __thiscall EmMatrix::EmMatrix(class NxActor const &)” (??0EmMatrix@@QAE@ABVNxActor@@@Z)
    1>Debug\MyGame1.exe : fatal error LNK1120: 1 unresolved externals

    if you could point me in the right direction it would be fantastic.

    ps i do have the include path right… i hope

    thank you for your work!

  2. Meds Says:

    Hi Gonzalo,

    I’m not sure what your problem is, it could be an issue with your include files.

    I just looked at my code and realized that you *should* be able to simply remove the constructor EmMatrix::EmMatrix(const NxMat34& m) and it won’t effect how the class works, the constructor itself is already provided in the parent NxMat34 class from which it inherits. So it was rather silly to put it there.

    So in the header file delete:

    EmMatrix(const NxMat34& m);

    and in the source file delete:

    EmMatrix::EmMatrix(const NxMat34& m)
    {
    D3DXMATRIX d3dmat;
    m.getColumnMajor44((float*)d3dmat);
    setColumnMajor44((float*)d3dmat);
    }

    andit should hopefully work.

Leave a Reply