D3DXMatrixRotationY Query

Started by
13 comments, last by Kram 16 years, 1 month ago
Quote:Original post by Evil Steve
Why? You want to set the new position of the cube to the position that was clicked, so you'll want to set the X and Y values to that position.

If you mean you want the cube to move over several frames, then yes - you'll need the old position, the new position, and the interpolation between then (0..1), and then just set the position to oldPos * (1-time) + newPos * time or similar.


Hmm, ok then, maybe I'm thinking about this incorrectly, but when I create the cube, the X, Y and Z vectors are around the -3...3 range, and the cube is centered on the screen. When I click the mouse directly next to the cube, I get X and Y's of say 200, 225 which is the X and Y offsets from the top left corner of my window. What am I doing wrong?
Advertisement
The coordinates of the mouse click you're getting (which probably come from the Windows API one way or another) are in a different coordinate space than what you're using for your graphics. The mouse click is in screen space, you have to convert it to world space. This can be done with D3DXVec3Unproject, if you'd like.
Quote:Original post by MJP
The coordinates of the mouse click you're getting (which probably come from the Windows API one way or another) are in a different coordinate space than what you're using for your graphics. The mouse click is in screen space, you have to convert it to world space. This can be done with D3DXVec3Unproject, if you'd like.


Ah I see, thats what I needed to know! I'll have a go at that method tonight when I get home from work.

Also, I just wanted to clarify, that changing the X and Y coords when the user clicks the mouse. Is the X and Y "changing" done in the D3DXMatrixRotationX and D3DXMatrixRotationY methods? I thought these were to offset the object from the origin? If I am correct, then you cant just plug in the mouse's X and Y's can you?
Ok so I am getting there!

Below is my Render method and a helper method to get the converted mouse coords

void GameEntity::Render(IDirect3DDevice9* p_d3dDevice){	// select which vertex format we are using    p_d3dDevice->SetFVF(CUSTOMFVF);    // set the view transform    D3DXMATRIX matView;    // the view transform matrix    D3DXMatrixLookAtLH(&matView,    &D3DXVECTOR3 (0.0f, 8.0f, 25.0f),					// the camera position    &D3DXVECTOR3 (0.0f, 0.0f, 0.0f),					// the look-at position    &D3DXVECTOR3 (0.0f, 1.0f, 0.0f));					// the up direction    p_d3dDevice->SetTransform(D3DTS_VIEW, &matView);    // set the world transform    static float index = 0.0f; index += 0.03f;				// an ever-increasing float value    D3DXMATRIX matRotateY;									// a matrix to store the rotation    D3DXMatrixRotationY(&matRotateY, index);				// the rotation matrix	    // set the projection transform    D3DXMATRIX matProjection;    // the projection transform matrix    D3DXMatrixPerspectiveFovLH(&matProjection,                               D3DXToRadian(45),    // the horizontal field of view                               (FLOAT)640 / (FLOAT)480, // aspect ratio                               1.0f,    // the near view-plane                               500.0f);    // the far view-plane    p_d3dDevice->SetTransform(D3DTS_PROJECTION, &matProjection);		D3DXVECTOR3* transformedPoints = 0;	transformedPoints = this->SetupLocalMousePoints(p_d3dDevice);	if (transformedPoints)	{		m_xOffset = transformedPoints->x;		m_yOffset = transformedPoints->y;	}	//translate the X and Y of needed	D3DXMATRIX matTranslate;	D3DXMatrixTranslation(&matTranslate, m_xOffset, m_yOffset, m_zOffset);	//ALWAYS, Rotate before Translate    p_d3dDevice->SetTransform(D3DTS_WORLD, &(matRotateY * matTranslate));  // set the world transform    // select the vertex buffer to display    p_d3dDevice->SetStreamSource(0, this->GetVertexBuffer(), 0, sizeof(CUSTOMVERTEX));    // set the texture    p_d3dDevice->SetTexture(0, m_triangleTexture);    // draw the textured square    p_d3dDevice->DrawPrimitive(D3DPT_TRIANGLESTRIP, 0, 2);    p_d3dDevice->DrawPrimitive(D3DPT_TRIANGLESTRIP, 4, 2);    p_d3dDevice->DrawPrimitive(D3DPT_TRIANGLESTRIP, 8, 2);    p_d3dDevice->DrawPrimitive(D3DPT_TRIANGLESTRIP, 12, 2);    p_d3dDevice->DrawPrimitive(D3DPT_TRIANGLESTRIP, 16, 2);    p_d3dDevice->DrawPrimitive(D3DPT_TRIANGLESTRIP, 20, 2); }D3DXVECTOR3* GameEntity::SetupLocalMousePoints(IDirect3DDevice9* p_d3dDevice){	if (m_mousePoint)	{		D3DXVECTOR3 result;		D3DXVECTOR3 screenPoint;		D3DVIEWPORT9 viewport;		D3DXMATRIX matProjection;		D3DXMATRIX matView;		D3DXMATRIX matTranslate;		screenPoint.x = m_mousePoint->x;		screenPoint.y = m_mousePoint->y;		screenPoint.z = 0.0f;		p_d3dDevice->GetViewport(&viewport);		p_d3dDevice->GetTransform( D3DTS_PROJECTION, &matProjection );		p_d3dDevice->GetTransform( D3DTS_VIEW, &matView );		p_d3dDevice->GetTransform( D3DTS_WORLD, &matTranslate );		//transform the mouse clicked location		D3DXVec3Unproject( &result, &screenPoint, &viewport, &matProjection, &matView, &matTranslate );		m_mousePoint = 0;		return &result;	}	return NULL;}


This somewhat works, I think. I say, I think, because I am getting values converted into my world space, but I dont think they are correct.

If I understand how D3DXVec3Unproject works, I need to basically setup the VIEW, PROJECTION and WORLD Transforms first, then convert the users mouse click into the current world space.

I think I am doing this, but if I click in the very top left corner, my X and Y values are like X: -23, Y: 8. I was kind of expecting more like a Y value of +25 or something??

Anyways, with that aside, once I have my X and Y values, is the code (as listed above):

D3DXVECTOR3* transformedPoints = 0;transformedPoints = this->SetupLocalMousePoints(p_d3dDevice);if (transformedPoints){    m_xOffset = transformedPoints->x;    m_yOffset = transformedPoints->y;}//translate the X and Y of neededD3DXMATRIX matTranslate;D3DXMatrixTranslation(&matTranslate, m_xOffset, m_yOffset, m_zOffset);


Correct? If I want to simply move the cube to the clicked location?

Thanks!
Mark
sorry to bump, but I totally stuck here, any assistance with getting the correct X and Y from D3DXVec3Unproject would be greatly appreciated!

Thanks
Mark

This topic is closed to new replies.

Advertisement