Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Managed C++/CLI
  4. How to move a 3D object along X and Y axis by the keyboard?

How to move a 3D object along X and Y axis by the keyboard?

Scheduled Pinned Locked Moved Managed C++/CLI
tutorialcsharpvisual-studiographicsgame-dev
7 Posts 3 Posters 3 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Member 12268183
    wrote on last edited by
    #1

    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

    L T 2 Replies Last reply
    0
    • M Member 12268183

      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

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      You move an object by repainting it at successive different points of the view screen.

      M 1 Reply Last reply
      0
      • L Lost User

        You move an object by repainting it at successive different points of the view screen.

        M Offline
        M Offline
        Member 12268183
        wrote on last edited by
        #3

        I know but I need any example or tutorial easy to import in my code.

        L 1 Reply Last reply
        0
        • M Member 12268183

          I know but I need any example or tutorial easy to import in my code.

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          Member 12268183 wrote:

          I need any example or tutorial

          Then you need to use Google to search for them.

          M 1 Reply Last reply
          0
          • L Lost User

            Member 12268183 wrote:

            I need any example or tutorial

            Then you need to use Google to search for them.

            M Offline
            M Offline
            Member 12268183
            wrote on last edited by
            #5

            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.

            L 1 Reply Last reply
            0
            • M Member 12268183

              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.

              L Offline
              L Offline
              Lost User
              wrote on last edited by
              #6

              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.

              1 Reply Last reply
              0
              • M Member 12268183

                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

                T Offline
                T Offline
                The_Inventor
                wrote on last edited by
                #7

                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....

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups