# Need Help Converting a 2dmatrix function to a 3dmatrix function its for my objects motion

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

## Recommended Posts

Well I have this class here

[source]
class C2DMatrix
{
private:

struct Matrix
{

double _11, _12, _13;
double _21, _22, _23;
double _31, _32, _33;

Matrix()
{
_11=0.0; _12=0.0; _13=0.0;
_21=0.0; _22=0.0; _23=0.0;
_31=0.0; _32=0.0; _33=0.0;
}

};

Matrix m_Matrix;

other members
};

[/source]

this is the function I need to convert into a D3DXMATRIX type but I dont know what its doing and how I should treat the elements to get the same funtionality

//2d one
[source]
//--------------------- PointToWorldSpace --------------------------------
// Transforms a point from the agent's local space into world space
//------------------------------------------------------------------------
Vector2 PointToWorldSpace(const Vector2 &point,
const Vector2 &AgentSide,
const Vector2 &AgentPosition)
{
//make a copy of the point
Vector3 TransPoint = point;

//create a transformation matrix
C2DMatrix matTransform;

//rotate

//and translate
matTransform.Translate(AgentPosition.x, AgentPosition.y);

//now transform the vertices
matTransform.TransformVector2Ds(TransPoint);

return TransPoint;
}//end PointToWorldSpace
//////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////

//create a rotation matrix from a 2D vector
inline void C2DMatrix::Rotate(const Vector2 &fwd, const Vector2 &side)
{
C2DMatrix::Matrix mat;

mat._11 = fwd.x; mat._12 = fwd.y; mat._13 = 0;

mat._21 = side.x; mat._22 = side.y; mat._23 = 0;

mat._31 = 0; mat._32 = 0;mat._33 = 1;

//and multiply
MatrixMultiply(mat);
}

//create a transformation matrix
inline void C2DMatrix::Translate(double x, double y)
{
Matrix mat;

mat._11 = 1; mat._12 = 0; mat._13 = 0;

mat._21 = 0; mat._22 = 1; mat._23 = 0;

mat._31 = x; mat._32 = y; mat._33 = 1;

//and multiply
MatrixMultiply(mat);
}

//applies a 2D transformation matrix to a single Vector2D
inline void C2DMatrix::TransformVector2Ds(Vector3 &vPoint)
{

double tempX =(m_Matrix._11*vPoint.x) + (m_Matrix._21*vPoint.y) + (m_Matrix._31);

double tempY = (m_Matrix._12*vPoint.x) + (m_Matrix._22*vPoint.y) + (m_Matrix._32);

vPoint.x = tempX;

vPoint.y = tempY;
}

//multiply two matrices together
inline void C2DMatrix::MatrixMultiply(Matrix &mIn)
{
C2DMatrix::Matrix mat_temp;

//first row
mat_temp._11 = (m_Matrix._11*mIn._11) + (m_Matrix._12*mIn._21) + (m_Matrix._13*mIn._31);
mat_temp._12 = (m_Matrix._11*mIn._12) + (m_Matrix._12*mIn._22) + (m_Matrix._13*mIn._32);
mat_temp._13 = (m_Matrix._11*mIn._13) + (m_Matrix._12*mIn._23) + (m_Matrix._13*mIn._33);

//second
mat_temp._21 = (m_Matrix._21*mIn._11) + (m_Matrix._22*mIn._21) + (m_Matrix._23*mIn._31);
mat_temp._22 = (m_Matrix._21*mIn._12) + (m_Matrix._22*mIn._22) + (m_Matrix._23*mIn._32);
mat_temp._23 = (m_Matrix._21*mIn._13) + (m_Matrix._22*mIn._23) + (m_Matrix._23*mIn._33);

//third
mat_temp._31 = (m_Matrix._31*mIn._11) + (m_Matrix._32*mIn._21) + (m_Matrix._33*mIn._31);
mat_temp._32 = (m_Matrix._31*mIn._12) + (m_Matrix._32*mIn._22) + (m_Matrix._33*mIn._32);
mat_temp._33 = (m_Matrix._31*mIn._13) + (m_Matrix._32*mIn._23) + (m_Matrix._33*mIn._33);

m_Matrix = mat_temp;
}

[/source]

##### Share on other sites
Hi there!

The method you posted creates a matrix representing the local coordinate system of the agent (also known as the agent's world matrix). A multiplication with that matrix transforms a point from the local system into world coordinates (and that’s what your function does.)
Now, let’s see how to create such matrix in D3D. First, a little background: world matrices represent affine transformations. An affine transformation is the combination of a linear transformation (e.g. rotation, scale, shear) and a translation. Each affine transformation consists of three orthogonal basis vectors spanning the 3D space and an additional translation. These four vectors are written in the columns of said matrix, in the order “forward, up, right, translation”.
AgentSite = D3DXVector2(-AgentHeading.y, AgentHeading.x)
Thus, we already have the basis vectors
D3DXVector3(AgentHeading.x, AgentHeading.y, 0)
and
D3DXVector3(AgentSite.x, AgentSite.y, 0)
We will do all transformations in the xy-plane, thus the z.component is zero. Now, we need a third basis vector that is orthogonal to the other two. The simplest choice would be
D3DXVector3(0,0,1)
The fourth vector is the translation, which is also given so:
D3DXVector3(AgentPosition.x, AgentPosition.y, 0)
We can now put it all together in one 4D matrix, which will be in homogeneous coordinates. So for that first three basis vectors the w-component will be 0, for the translation it is 1. This ensures that all 4 vectors are orthogonal to each other.
Alright and that’s the code for this:
D3DXVECTOR2 PointToWorldSpaceDx(const D3DXVECTOR2 &point, const D3DXVECTOR2 &AgentHeading, const D3DXVECTOR2 &AgentSide, const D3DXVECTOR2 &AgentPosition) { //create a transformation matrix D3DXMATRIX matTransform; matTransform._11 = AgentHeading.x; matTransform._21 = AgentSide.x; matTransform._31 = 0; matTransform._41 = AgentPosition.x; matTransform._12 = AgentHeading.y; matTransform._22 = AgentSide.y; matTransform._32 = 0; matTransform._42 = AgentPosition.y; matTransform._13 = 0; matTransform._23 = 0; matTransform._33 = 1; matTransform._43 = 0; matTransform._14 = 0; matTransform._24 = 0; matTransform._34 = 0; matTransform._44 = 1; D3DXVECTOR4 TransPoint; D3DXVECTOR3 point3 = D3DXVECTOR3(point.x, point.y, 0); D3DXVec3Transform(&TransPoint, &point3, &matTransform); return D3DXVECTOR2(TransPoint.x, TransPoint.y); }

##### Share on other sites
Thank you for this great explanation.

The method you posted creates a matrix representing the local coordinate system of the agent (also known as the agent's world matrix). A multiplication with that matrix transforms a point from the local system into world coordinates (and that’s what your function does.)

[/quote]

I'm now thinking I should not use it, because the agents position is set in world coords,

but the thing is in the 2d version I have running it use's the said function([color="#008000"][color="#008000"]PointToWorldSpace(...))

and that also I thought was just using one coordinate system, As I just set it pos like so Vector2(100, 100); and also a target set the same way.

I think I was not clear in my first post but all the input also need to be Vector3 and the return type.

So I will now try to convert your code to a vector3 type and get back.

It's going to be at the end of the week so please dont help until I get back with the converted code.( I should do some work my self or try to.)Unless you think I should not use this function.

//here is the function that its used in the Target passed in will be at world position

[source]

//--------------------------- Wander -------------------------------------
// This behavior makes the agent wander about randomly
//------------------------------------------------------------------------
D3DXVECTOR3 SteeringBehaviors::Wander(D3DXVECTOR3 &TargetPos)
{
cComponentMotion *ComponentMotion = m_Vehicle->GetComponentMotion();
if(ComponentMotion == NULL)
return D3DXVECTOR3(0.0f, 0.0f, 0.0f);

//this behavior is dependent on the update rate, so this line must
//be included when using time independent framerate.
//double JitterThisTimeSlice = m_dWanderJitter * m_Vehicle->TimeElapsed();
double JitterThisTimeSlice = m_dWanderJitter * m_Vehicle->GetElapsedTime();

//first, add a small random vector to the target's position
m_vWanderTarget += D3DXVECTOR3(RandomClamped() * JitterThisTimeSlice,
1.0, RandomClamped() * JitterThisTimeSlice);

//reproject this new vector back on to a unit circle
//m_vWanderTarget.Normalize();
D3DXVec3Normalize(&m_vWanderTarget, &m_vWanderTarget);

//increase the length of the vector to the same as the radius
//of the wander circle

//move the target into a position WanderDist in front of the agent
D3DXVECTOR3 target = m_vWanderTarget + D3DXVECTOR3(m_dWanderDistance, 1.3, 1.0);

//project the target into world space
//D3DXVECTOR3 Target = PointToWorldSpace(target,
// ComponentMotion->Side(),
// ComponentMotion->m_vPos;

//TargetPos = Target;

TargetPos = target;

//and steer towards it
return target - ComponentMotion->m_vPos;
}//end Wander
////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////

[/source]

[color="#008000"] [color="#008000"]

• ### Game Developer Survey

We are looking for qualified game developers to participate in a 10-minute online survey. Qualified participants will be offered a \$15 incentive for your time and insights. Click here to start!

• 16
• 15
• 9
• 11
• 15