Thread: Moving Objects in D3D?

  1. #1
    Registered User
    Join Date
    Jan 2007
    Posts
    3

    Moving Objects in D3D?

    So I got a set of Vertices like this:

    Code:
    struct Vertex{
     float x,y,z;
     float nx,ny,nz;
     DWORD color;
    };
    
    /* ... */
    
    
    Vertex Object[]={
     {-0.5f,0.5f,0.2f, // ...};
    };  // Well, some Set of Vertices which
     // let the DrawPrimitive() function
     // render an Object (f.e. a Cube)
    
    /* When I want the Vertices to be
     rendered I first must create
     a VertexBuffer, and copy
     the Vertices to the VertexBuffer */
    
    /* ... */
    
    /* Once the Vertices are in the Vertex
     Buffer DrawPrimitive can render 
     the Set Of Vertices */
    So here is my question:
    When I want the Object to change its Position, will I have to lock() the Vertex Buffer again, copy the new - valid - Values of the Vertices to the Vertex Buffer - and then unlock() it?
    I don't want the camera or the entire world matrix to move or rotate, but only the Set Of Vertices I copied to the VertexBuffer (this 'Set Of Vertices' could be a cube f.e.).

    Is this an appropriate way to move Objects (which are stored in Vertex Buffers)? I am not sure, once I got 20 Objects in 20 different Vertex Buffers this means I must call lock() 20 times and also unlock() 20 times. Can D3D handle so many calls to the
    pD3DVertexBuffer->Lock() functions during one frame (f.e. when the game is running at 40 fps)?

  2. #2
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    This is a highly inappropriate way of moving objects. It takes forever and will totally ruin your performance.
    The vertices of an object are always relative to object-space, i.e. usually your object is centered at (0,0,0). To move an object, you change the world transformation matrix.

    (So yes, you have to do exactly what you don't want to do. But that's the correct way.)
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  3. #3
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    You must understand the concepts of 3D engines before attempting to use them. You need a world transformation matrix as has been stated already.

    Code:
    D3DXMATRIX matTrans;
    
    D3DXMatrixTranslation(&matTrans,dx,dy,dz);
    
    m_pDevice->SetTransform(D3DTS_WORLD,&matTrans);
    
    m_pDevice->DrawPrimitive(....)
    If you are using your own vertex buffer you must call DrawPrimitiveUP() and/or DrawIndexedPrimitiveUP().

  4. #4
    Registered User
    Join Date
    Jan 2007
    Posts
    3

    many calls to drawprimitive

    So I will need to call DrawPrimitive for every single object (Vertex Buffer) like
    that:

    Code:
    LPDIRECT3DVERTEXBUFFER object[5]; // initialized with Sets Of Vertices
    
    /* ... */
    
    D3DXMATRIX matrix_for_calculations;
    
    /* Drawing object1 */
    D3DXMatrixTranslation(&matrix_for_calculation, x_posObject1, 
      y_posObject1, z_posObject1);
    pd3dDevice->SetTransform(D3DTS_WORLD,&matrix_for_calculations);
    
    // This means the Stream must be set everytime again for every single object??
    pd3dDevice->SetStreamSource(0,object[1],0,sizeof(basic_Vertex));
    
    #define ALL_TRIANGLES_OF_OBJECT1 3
    DrawPrimitive(D3DPT_TRIANGLESTRIP,0,ALL_TRIANGLES_OF_OBJECT1);
    
    /* And this procedure is necessary for EVERY object (that is again calculating the Translation_Rotation Matrix, Setting the Stream, calling Draw Primitive... until every Object is drawn at its new Position)??
    I guess this must be even more slowly than locking the VertexBuffers, copy the Objects' new positions to the Vertex Buffer, and finally draw them.
    5 calls to SetTransform plus 5 calls to MatrixTranslation(or RotationX_Y_Z) are executed more quickly than 5 calls to Lock() + 5 memcopy()?
    Are you sure?
    
    */
    Thanks for your tips!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Moving objects with mouse in 3D.
    By psychopath in forum Game Programming
    Replies: 15
    Last Post: 07-10-2011, 04:20 PM
  2. moving objects in a window
    By axr0284 in forum Windows Programming
    Replies: 1
    Last Post: 02-26-2006, 07:23 PM
  3. moving objects
    By condorx in forum Game Programming
    Replies: 2
    Last Post: 02-22-2002, 02:05 PM
  4. moving objects in d3d...
    By Unregistered in forum Game Programming
    Replies: 1
    Last Post: 01-09-2002, 04:07 PM
  5. Moving Objects on the Screen
    By BigSter in forum C++ Programming
    Replies: 6
    Last Post: 12-02-2001, 10:37 PM