Reputation: 715
I am learning DirectX 11 and have gotten myself to a point where I have a square displayed. My 4 vertices are defined as:
VertexPos vertices[] =
{
XMFLOAT3(-0.2f, 0.2f, 0.2f),
XMFLOAT3(0.2f, 0.2f, 0.2f),
XMFLOAT3(-0.2f, -0.2f, 0.2f),
XMFLOAT3(0.2f, -0.2f, 0.2f)
};
They then go through the necessary stages to render to the screen (like the hello world of DirectX programming). I have combined some code from a direct Input demo and would like to be able to move the square around using the arrow keys. I have so far:
void SquareMove::Update( float dt )
{
keyboardDevice_->GetDeviceState(sizeof(keyboardKeys_), (LPVOID)&keyboardKeys_);
// Button down event.
if (KEYDOWN(prevKeyboardKeys_, DIK_DOWN) && !KEYDOWN(keyboardKeys_, DIK_DOWN))
{
PostQuitMessage(0);
}
(That was to test that my eg. down arrow callback works). I am now at a loss how to actually implement the steps necessary to move my square. I understand it has to do with a D3DXMatrixTranslation, but am struggling to see how they all convolute together to perform the necessary operations. Thanks
Upvotes: 0
Views: 3154
Reputation: 715
Just for completeness this is how I managed to do it. In the render function I added the following
XMMATRIX m_Translation = XMMatrixTranslation(0.0f, fY, 0.0f);
XMMATRIX mvp = world*vpMatrix_*m_Translation;
(Previously it was just world*viewPortMatrix
)
where fY was controlled via button down events
if (KEYDOWN(prevKeyboardKeys_, DIK_DOWN) && !KEYDOWN(keyboardKeys_, DIK_DOWN))
{
fY -= 0.1f;
}
And when running the application my object moves!
Upvotes: 0
Reputation:
I am not quite clear if you actually understand the effect of the translation matrix or not. There are many great tutorials to guide you through for the implementation details, so I would just share what is interesting to me - the way I understood rendering first.
In short you have to learn to visualize the effect of mathematics on your object. Your square's vertices are currently in what is called "model" and "local" space. I.e. they are just giving information actually on the shape and size of the model (related to the 0,0,0 world coordinates). Now you have to position it in your "world". To do so you have to move ("translate") each of the vertices of your object to the new place keeping the size and shape you just defined in your model space - to translate each of them with the same length and in the same direction, in order words with the same vector, to apply the same calculation to each of the vertices.
In rendering transformation of object is achieved through matrices. Each matrix has values at certain positions which when multiplied with the coordinates of the object will in some way change it. Their consequent application on the object's coordinates (via multiplication) applies consequent transformations - an object may first be rotated - this will rotate it around the center of your world, i.e. around (0,0,0), then translated, then rotated again...this is just multiplication of 3 matrices.
The specific implementation may be found at plenty of places: http://www.directxtutorial.com/Lesson.aspx?lessonid=9-4-5
Upvotes: 2