# Compressing a transformation to a vector.

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

## Recommended Posts

Sorry if this has been asked before, but I got most frustrated looking for the answer. Frankly, I can't suss my camera out. Here's the long and the short of it: The short of it is that I think I want to know how to apply a matrix transformation to a vector position. I'm not sure, though. If anyone can be bothered, this is what I'm doing. Please tell me if I'm a complete no-brainer! My camera is encapsulated in a class that has two D3DXVECTOR3 members, location and target, and two D3DXMATRIX members, viewMatrix and projectionMatrix. When I construct it, I fill the projection matrix as usual, and the vectors are filled by arguments:
Camera::Camera(D3DXVECTOR3 aLocation, D3DXVECTOR3 aTarget) :
location(aLocation),
target(aTarget)
{
D3DXMatrixPerspectiveFovLH(&projectionMatrix, D3DX_PI/4, 640.0f/480.0f, 0.01f, 50.0f);
Engine::getInstance().getDevice()->SetTransform(D3DTS_PROJECTION, &projectionMatrix);
apply();
}

There are magic numbers in there, but that's not important. The apply() method calls this function:
void Camera::apply()
{
D3DXMatrixLookAtLH(&viewMatrix, &location, &target, &D3DXVECTOR3(0,1,0));
Engine::getInstance().getDevice()->SetTransform(D3DTS_VIEW, &viewMatrix);
}

This is the reason my location and target are held as vectors; the D3DXMatrixLookAtLH() function uses them. To rotate the camera, I do the usual. Move the whole contraption so the target is the origin, apply rotations, move back. This results in a matrix that contains: 1. a translation 2. the first rotation 3. the second rotation 4. another translation
void Camera::orbit(float radiansX, float radiansY)
{
D3DXMATRIX total;

//move the contraption to origin
D3DXMatrixTranslation(&total, -target.x, -target.y, -target.z);

//get the axis for longitudinal rotation
D3DXVECTOR3 axisOfRotation, vecToCamera;
D3DXVec3Normalize(&vecToCamera, &(location - target));
D3DXVec3Cross(&axisOfRotation, &vecToCamera, &(D3DXVECTOR3(0,1,0)));

D3DXMATRIX rotatelat, rotatelong, rettranslate;

//apply latitude/longitude type rotations

//move back
D3DXMatrixTranslation(&rettranslate, target.x, target.y, target.z);
D3DXMatrixMultiply(&total, &rotatelat, &rotatelong);
D3DXMatrixMultiply(&total, &total, &rettranslate);

//total now contains all four transformations
apply();
}

Yeah, so I'm a right newbie. That's what you guys are here for, right? :)

##### Share on other sites
Hmmm... when you call apply() I think you're resetting all the transformations you've made with the call
D3DXMatrixPerspectiveFovLH(&projectionMatrix, D3DX_PI/4,                                640.0f/480.0f, 0.01f, 50.0f);

But I'm not 100% sure if that's right - you should just be able to apply the matrix without that.

And you should include source code in [source] tags so it doesn't mess up the borders of the page, for those out there who run los-res (me! [wink])

Mushu - trying to help those he doesn't know, with things he doesn't know.
Why won't he just go away? An question the universe may never have an answer to...
[/source]

##### Share on other sites
OMG I'm such a dork.

I just found, after 20 minutes of trawling the docs, the function D3DXVec3TransformNormal().

It takes, for anyone who searches and finds this thread, a reference to the output vector, a reference to the input vector, and a reference to the matrix that represents the translation in that order.

None of the examples I used even hinted that this existed!

Oh well! Sorry :)

##### Share on other sites
You might wanna look at D3DXVec3TransformCoord instead, depending on your needs. Also, make sure to take a look at the Graphics:Reference:D3DX Reference:Functions:Math area in the docs. There are a lot of useful functions in there.

• ### 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!

• 13
• 14
• 40
• 63