Thread: Need help with explosions

  1. #1
    Darkness Prevails Dark_Phoenix's Avatar
    Join Date
    Oct 2006
    Location
    Houston, Texas
    Posts
    174

    Need help with explosions

    I've finally started coding my first D3D game, a breakout clone. It's been going pretty smooth until now. I was thinking of having the ball explode when it goes past the bat, but I am having trouble making this work. I have a mesh called 'Ball' that I load from an .X file. I was thinking about creating a dynamic vertex buffer and filling it with the vertex data from the Ball mesh, that way I could have the vertices explode out and change colors. Not sure if I am on the right track, or even if it can work this way but here is what I have so far.

    I have these objects for the Ball and the dynamic buffer
    Code:
    CMesh               m_BallMesh;
    CVertexBuffer       m_ExplosionBuffer;
    My vertex buffer and custom vertex structure
    Code:
    bool CVertexBuffer::CreateBuffer(LPDIRECT3DDEVICE9 pDevice, 
                                     UINT numVertices, 
                                     DWORD FVF, 
                                     UINT VertexSize, 
                                     bool dynamic) { 
        Release(); 
        m_numVertices = numVertices; 
        m_FVF         = FVF; 
        m_VertexSize  = VertexSize; 
    
        D3DPOOL pool = dynamic ? D3DPOOL_DEFAULT : D3DPOOL_MANAGED; 
        DWORD usage  = dynamic ? D3DUSAGE_WRITEONLY | D3DUSAGE_DYNAMIC : D3DUSAGE_WRITEONLY; 
        if (FAILED(pDevice->CreateVertexBuffer(m_numVertices * m_VertexSize,
                                               usage, 
                                               m_FVF, 
                                               pool, 
                                               &m_pVB, 
                                               NULL ) ) ) { 
            SHOWERROR("CreateVertexBuffer failed.", __FILE__, __LINE__); 
            return false; 
        } 
        return true; 
    }
     
        typedef struct PositionNormalTextured { 
        public: 
            PositionNormalTextured() 
                : X(0), Y(0), Z(0), Nx(0), Ny(0), Nz(0), Tu(0), Tv(0) { } 
            PositionNormalTextured(float x, float y, float z, 
                                   float nx, float ny, float nz, 
                                   float tu, float tv) 
                : X(x), Y(y), Z(z), Nx(nx), Ny(ny), Nz(nz), Tu(tu), Tv(tv) { } 
            float X, Y, Z; 
            float Nx, Ny, Nz; 
            float Tu, Tv; 
        } PositionNormalTextured;
    And then I copy the vertex data from the mesh to the vertex buffer
    Code:
        // Create the buffer
        m_ExplosionBuffer.CreateBuffer(pDevice,
                                       m_BallMesh.GetMesh()->GetNumVertices(),
                                       D3DFVF_XYZ | D3DFVF_NORMAL | D3DFVF_TEX0,
                                       sizeof(cstmVertex::PositionNormalTextured),
                                       true);
        // Lock the mesh vertex buffer and get pointer to its memory
        void** pVB;
        m_BallMesh.GetMesh()->LockVertexBuffer(0, pVB);
        // Copy the buffer data to the ExplosionBuffer
        m_ExplosionBuffer.SetData(m_BallMesh.GetMesh()->GetNumVertices(),
                                  pVB,
                                  0);
        m_BallMesh.GetMesh()->UnlockVertexBuffer();
    But I get a segmentation fault when I get to the SetData function, which copies the data from pVB to m_ExplosionBuffer. I assumed that the size of m_ExplosionBuffer does not match the size of pVB, but everything looks ok to me.

    What am I missing here? Or am I even on the right track?
    Last edited by Dark_Phoenix; 01-13-2007 at 10:40 AM.
    Using Code::Blocks and Windows XP

    In every hero, there COULD be a villain!

  2. #2
    Darkness Prevails Dark_Phoenix's Avatar
    Join Date
    Oct 2006
    Location
    Houston, Texas
    Posts
    174
    OK< Never mind... I figured it out. I changed
    Code:
        // Lock the mesh vertex buffer and get pointer to its memory
        void** pVB;
        m_BallMesh.GetMesh()->LockVertexBuffer(0, pVB);
    to
    Code:
        // Lock the mesh vertex buffer and get pointer to its memory
        char* pVB;
        m_BallMesh.GetMesh()->LockVertexBuffer(0, (void**)&pVB);
    Using Code::Blocks and Windows XP

    In every hero, there COULD be a villain!

  3. #3
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    If you are using indexes in your meshes it would be easier to unlock both the index and vertex buffer. You know that 3 indices will always form a triangle and so you can decimate your mesh with this information. It does require two lock operations and two unlocks, but it makes things a lot easier.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Explosions
    By meehanmd in forum Game Programming
    Replies: 9
    Last Post: 04-12-2006, 10:25 AM
  2. Explosions
    By Fordy in forum A Brief History of Cprogramming.com
    Replies: 47
    Last Post: 07-07-2005, 04:36 PM