Jump to content
  • Advertisement
Sign in to follow this  
Weird Al

Compressing a transformation to a vector.

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

If you intended to correct an error in the post then please contact us.

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) :
	D3DXMatrixPerspectiveFovLH(&projectionMatrix, D3DX_PI/4, 640.0f/480.0f, 0.01f, 50.0f);
	Engine::getInstance().getDevice()->SetTransform(D3DTS_PROJECTION, &projectionMatrix);
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
	D3DXMatrixRotationAxis(&rotatelong, &axisOfRotation, radiansY);
	D3DXMatrixRotationY(&rotatelat, radiansX);

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

//total now contains all four transformations
Yeah, so I'm a right newbie. That's what you guys are here for, right? :)

Share this post

Link to post
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...

Share this post

Link to post
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 this post

Link to post
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.

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!