How to move a 3D object along X and Y axis by the keyboard?
-
I use Visual Studio 2012 and Microsoft DirectX SDK (June 2010). I created an applicatin with a 3d cube as an object. In the example that I use it is moved the point of view of the camera. In my case I would like to move the object by the keaboard along X and Y axis without moving the camera view. Could you please help? Here is my code: ... CubeDemo::CubeDemo(HINSTANCE hInstance, std::string winCaption, D3DDEVTYPE devType, DWORD requestedVP) : D3DApp(hInstance, winCaption, devType, requestedVP), MAX_SPEED(1500.0f), ACCELE(1000.0f) { // 5 units off the ground. mCameraHeight = 5.0f; buildVertexBuffer(); buildIndexBuffer(); onResetDevice(); InitAllVertexDeclarations(); } ... void CubeDemo::updateScene(float dt) { // One cube has 8 vertices and 12 triangles. gDInput->poll(); // Check input. if( gDInput->keyDown(DIK_W) ) { // Code for moving the 3D object along X axis??? } if( gDInput->keyDown(DIK_S) ) { // Code for moving the 3D object along Y axis??? } buildViewMtx(); } void CubeDemo::buildViewMtx() { D3DXVECTOR3 pos(10, 0, 10); D3DXVECTOR3 target(0.0f, 0.0f, 0.0f); D3DXVECTOR3 up(0.0f, 1.0f, 0.0f); D3DXMatrixLookAtLH(&mView, &pos, &target, &up); } void CubeDemo::drawScene() { HR(gd3dDevice->Clear(0, 0, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, 0xffffffff, 1.0f, 0)); HR(gd3dDevice->BeginScene()); HR(gd3dDevice->SetStreamSource(0, mVB, 0, sizeof(VertexPos))); HR(gd3dDevice->SetIndices(mIB)); HR(gd3dDevice->SetVertexDeclaration(VertexPos::Decl)); D3DXMATRIX W; D3DXMatrixIdentity(&W); HR(gd3dDevice->SetTransform(D3DTS_WORLD, &W)); HR(gd3dDevice->SetTransform(D3DTS_VIEW, &mView)); HR(gd3dDevice->SetTransform(D3DTS_PROJECTION, &mProj)); HR(gd3dDevice->SetRenderState(D3DRS_FILLMODE, D3DFILL_WIREFRAME)); HR(gd3dDevice->DrawIndexedPrimitive(D3DPT_TRIANGLELIST, 0, 0, 8, 0, 12)); HR(gd3dDevice->EndScene()); HR(gd3dDevice->Present(0, 0, 0, 0)); } Reagards
-
I use Visual Studio 2012 and Microsoft DirectX SDK (June 2010). I created an applicatin with a 3d cube as an object. In the example that I use it is moved the point of view of the camera. In my case I would like to move the object by the keaboard along X and Y axis without moving the camera view. Could you please help? Here is my code: ... CubeDemo::CubeDemo(HINSTANCE hInstance, std::string winCaption, D3DDEVTYPE devType, DWORD requestedVP) : D3DApp(hInstance, winCaption, devType, requestedVP), MAX_SPEED(1500.0f), ACCELE(1000.0f) { // 5 units off the ground. mCameraHeight = 5.0f; buildVertexBuffer(); buildIndexBuffer(); onResetDevice(); InitAllVertexDeclarations(); } ... void CubeDemo::updateScene(float dt) { // One cube has 8 vertices and 12 triangles. gDInput->poll(); // Check input. if( gDInput->keyDown(DIK_W) ) { // Code for moving the 3D object along X axis??? } if( gDInput->keyDown(DIK_S) ) { // Code for moving the 3D object along Y axis??? } buildViewMtx(); } void CubeDemo::buildViewMtx() { D3DXVECTOR3 pos(10, 0, 10); D3DXVECTOR3 target(0.0f, 0.0f, 0.0f); D3DXVECTOR3 up(0.0f, 1.0f, 0.0f); D3DXMatrixLookAtLH(&mView, &pos, &target, &up); } void CubeDemo::drawScene() { HR(gd3dDevice->Clear(0, 0, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, 0xffffffff, 1.0f, 0)); HR(gd3dDevice->BeginScene()); HR(gd3dDevice->SetStreamSource(0, mVB, 0, sizeof(VertexPos))); HR(gd3dDevice->SetIndices(mIB)); HR(gd3dDevice->SetVertexDeclaration(VertexPos::Decl)); D3DXMATRIX W; D3DXMatrixIdentity(&W); HR(gd3dDevice->SetTransform(D3DTS_WORLD, &W)); HR(gd3dDevice->SetTransform(D3DTS_VIEW, &mView)); HR(gd3dDevice->SetTransform(D3DTS_PROJECTION, &mProj)); HR(gd3dDevice->SetRenderState(D3DRS_FILLMODE, D3DFILL_WIREFRAME)); HR(gd3dDevice->DrawIndexedPrimitive(D3DPT_TRIANGLELIST, 0, 0, 8, 0, 12)); HR(gd3dDevice->EndScene()); HR(gd3dDevice->Present(0, 0, 0, 0)); } Reagards
-
I know but I need any example or tutorial easy to import in my code.
-
I know but I need any example or tutorial easy to import in my code.
-
Member 12268183 wrote:
I need any example or tutorial
Then you need to use Google to search for them.
What is the main purpose of this forum then? I thought that using forums people could reach the needed information faster. Unfortunately here the situation is different.
-
What is the main purpose of this forum then? I thought that using forums people could reach the needed information faster. Unfortunately here the situation is different.
Member 12268183 wrote:
What is the main purpose of this forum then
It is to help people with problems in the code that they write themselves. It is not a coding service, or a Google substitute. Since you do not have any code written, and I do not have a tutorial or sample code, then Google is the best place to look.
-
I use Visual Studio 2012 and Microsoft DirectX SDK (June 2010). I created an applicatin with a 3d cube as an object. In the example that I use it is moved the point of view of the camera. In my case I would like to move the object by the keaboard along X and Y axis without moving the camera view. Could you please help? Here is my code: ... CubeDemo::CubeDemo(HINSTANCE hInstance, std::string winCaption, D3DDEVTYPE devType, DWORD requestedVP) : D3DApp(hInstance, winCaption, devType, requestedVP), MAX_SPEED(1500.0f), ACCELE(1000.0f) { // 5 units off the ground. mCameraHeight = 5.0f; buildVertexBuffer(); buildIndexBuffer(); onResetDevice(); InitAllVertexDeclarations(); } ... void CubeDemo::updateScene(float dt) { // One cube has 8 vertices and 12 triangles. gDInput->poll(); // Check input. if( gDInput->keyDown(DIK_W) ) { // Code for moving the 3D object along X axis??? } if( gDInput->keyDown(DIK_S) ) { // Code for moving the 3D object along Y axis??? } buildViewMtx(); } void CubeDemo::buildViewMtx() { D3DXVECTOR3 pos(10, 0, 10); D3DXVECTOR3 target(0.0f, 0.0f, 0.0f); D3DXVECTOR3 up(0.0f, 1.0f, 0.0f); D3DXMatrixLookAtLH(&mView, &pos, &target, &up); } void CubeDemo::drawScene() { HR(gd3dDevice->Clear(0, 0, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, 0xffffffff, 1.0f, 0)); HR(gd3dDevice->BeginScene()); HR(gd3dDevice->SetStreamSource(0, mVB, 0, sizeof(VertexPos))); HR(gd3dDevice->SetIndices(mIB)); HR(gd3dDevice->SetVertexDeclaration(VertexPos::Decl)); D3DXMATRIX W; D3DXMatrixIdentity(&W); HR(gd3dDevice->SetTransform(D3DTS_WORLD, &W)); HR(gd3dDevice->SetTransform(D3DTS_VIEW, &mView)); HR(gd3dDevice->SetTransform(D3DTS_PROJECTION, &mProj)); HR(gd3dDevice->SetRenderState(D3DRS_FILLMODE, D3DFILL_WIREFRAME)); HR(gd3dDevice->DrawIndexedPrimitive(D3DPT_TRIANGLELIST, 0, 0, 8, 0, 12)); HR(gd3dDevice->EndScene()); HR(gd3dDevice->Present(0, 0, 0, 0)); } Reagards
You need to TRASNLATE the object's POSITION.
while (DIK\_W) for(i=0; i < iMax; i++) { D3DXVECTOR3 pos(10, i, 10);// This gets you up direction }do
Or something like that ...
The World as we think we know it Has a lot more to it than meets the eye. A Mad Scientist who has seen it for himself....