游戏开发论坛

 找回密码
 立即注册
搜索
查看: 2130|回复: 3

关于碰撞检测的问题

[复制链接]

15

主题

31

帖子

35

积分

注册会员

Rank: 2

积分
35
发表于 2004-3-28 07:26:00 | 显示全部楼层 |阅读模式



  我在我的d3d程序中加上了一个 CD3DMesh* 模型,按照DXSDK中pick例子的方法,检测出摄像机与模型的位置关

系。

具体实现如下:


struct D3DVERTEX
{
    D3DXVECTOR3 p;
    D3DXVECTOR3 n;  //定点法线?
    FLOAT       tu, tv;
};

创建:
CD3DMesh->Create(d3dDevice,_T("*.x") )

SetFVF( d3dDevice, D3DFVF_XYZ | D3DFVF_NORMAL | D3DFVF_TEX1 );

  pd3dDevice->CreateVertexBuffer( dwNumVertices*sizeof(D3DVERTEX),            

D3DUSAGE_WRITEONLY,D3DFVF_XYZ | D3DFVF_NORMAL | D3DFVF_TEX1,D3DPOOL_MANAGED,

&LPDIRECT3DVERTEXBUFFER9, NULL ) )


渲染:(每帧执行)

Pick() //pick例子中函数
{
...
   LPD3DXBASEMESH          pMesh = jizn4zhu4_1->GetLocalMesh();
        LPDIRECT3DVERTEXBUFFER9 pVB;
        LPDIRECT3DINDEXBUFFER9  pIB;

        pMesh->GetVertexBuffer( &pVB );
        pMesh->GetIndexBuffer( &pIB );

        WORD*      pIndices;
        D3DVERTEX*    pVertices;

        pIB->Lock( 0, 0, (void**)&pIndices, 0 );
        pVB->Lock( 0, 0, (void**)&pVertices, 0 );

        if( m_bUseD3DX )
        {
            // When calling D3DXIntersect, one can get just the closest intersection and not
            // need to work with a D3DXBUFFER.  Or, to get all intersections between the ray and
            // the mesh, one can use a D3DXBUFFER to receive all intersections.  We show both
            // methods.
            if( m_bClosestOnly )
            {
                // Collect only the closest intersection
                BOOL bHit;
                DWORD dwFace;
                FLOAT fBary1, fBary2, fDist;
                D3DXIntersect(pMesh, &vPickRayOrig, &vPickRayDir, &bHit, &dwFace, &fBary1, &fBary2,

&fDist,
                    NULL, NULL);
                if( bHit )
                {
                    m_dwNumIntersections = 1;
                    m_IntersectionArray[0].dwFace = dwFace;
                    m_IntersectionArray[0].fBary1 = fBary1;
                    m_IntersectionArray[0].fBary2 = fBary2;
                    m_IntersectionArray[0].fDist = fDist;
                }
                else
                {
                    m_dwNumIntersections = 0;
                }
            }
            else
            {
                // Collect all intersections
                BOOL bHit;
                LPD3DXBUFFER pBuffer = NULL;
                D3DXINTERSECTINFO* pIntersectInfoArray;
                if( FAILED( hr = D3DXIntersect(pMesh, &vPickRayOrig, &vPickRayDir, &bHit, NULL,

NULL, NULL, NULL,
                    &pBuffer, &m_dwNumIntersections) ) )
                {
                    return hr;
                }
                if( m_dwNumIntersections > 0 )
                {
                    pIntersectInfoArray = (D3DXINTERSECTINFO*)pBuffer->GetBufferPointer();
                    if( m_dwNumIntersections > MAX_INTERSECTIONS )
                        m_dwNumIntersections = MAX_INTERSECTIONS;
                    for( DWORD iIntersection = 0; iIntersection < m_dwNumIntersections;

iIntersection++ )
                    {
                        m_IntersectionArray[iIntersection].dwFace = pIntersectInfoArray

[iIntersection].FaceIndex;
                        m_IntersectionArray[iIntersection].fBary1 = pIntersectInfoArray

[iIntersection].U;
                        m_IntersectionArray[iIntersection].fBary2 = pIntersectInfoArray

[iIntersection].V;
                        m_IntersectionArray[iIntersection].fDist = pIntersectInfoArray

[iIntersection].Dist;
                    }
                }
                SAFE_RELEASE( pBuffer );
            }

        }
        else
        {
            // Not using D3DX
            DWORD dwNumFaces = jizn4zhu4_1->GetLocalMesh()->GetNumFaces();
            FLOAT fBary1, fBary2;
            FLOAT fDist;
            for( DWORD i=0; i<dwNumFaces; i++ )
            {
                D3DXVECTOR3 v0 = pVertices[pIndices[3*i+0]].p;
                D3DXVECTOR3 v1 = pVertices[pIndices[3*i+1]].p;
                D3DXVECTOR3 v2 = pVertices[pIndices[3*i+2]].p;

                // Check if the pick ray passes through this point
                if( IntersectTriangle( vPickRayOrig, vPickRayDir, v0, v1, v2,
                                       &fDist, &fBary1, &fBary2 ) )
                {
                    if( !m_bClosestOnly || m_dwNumIntersections == 0 || fDist <

m_IntersectionArray[0].fDist )
                    {
                        if( m_bClosestOnly )
                            m_dwNumIntersections = 0;
                        m_IntersectionArray[m_dwNumIntersections].dwFace = i;
                        m_IntersectionArray[m_dwNumIntersections].fBary1 = fBary1;
                        m_IntersectionArray[m_dwNumIntersections].fBary2 = fBary2;
                        m_IntersectionArray[m_dwNumIntersections].fDist = fDist;
                        m_dwNumIntersections++;
                        if( m_dwNumIntersections == MAX_INTERSECTIONS )
                            break;
                    }
                }
            }
        }

        // Now, for each intersection, add a triangle to m_pVB and compute texture coordinates
        if( m_dwNumIntersections > 0 )
        {
            D3DVERTEX* v;
            D3DVERTEX* vThisTri;
            WORD* iThisTri;
            D3DVERTEX  v1, v2, v3;
            INTERSECTION* pIntersection;

            fff->Lock( 0, 0, (void**)&v, 0 );

            for( DWORD iIntersection = 0; iIntersection < m_dwNumIntersections; iIntersection++ )
            {
                pIntersection = &m_IntersectionArray[iIntersection];

                vThisTri = &v[iIntersection * 3];
                iThisTri = &pIndices[3*pIntersection->dwFace];
                // get vertices hit
                vThisTri[0] = pVertices[iThisTri[0]];
                vThisTri[1] = pVertices[iThisTri[1]];
                vThisTri[2] = pVertices[iThisTri[2]];

                                //!!!!!!!!!!!!!!!!!!!!!!!
                                //这是被选中的三角形面法线角度吗?
                                //!!!!!!!!!!!!!!!!!!!!!!!
                                vThisTri[1].n.x;
                                vThisTri[1].n.y;
                                vThisTri[1].n.z;
//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!


                // If all you want is the vertices hit, then you are done.  In this sample, we
                // want to show how to infer texture coordinates as well, using the BaryCentric
                // coordinates supplied by D3DXIntersect
                FLOAT dtu1 = vThisTri[1].tu - vThisTri[0].tu;
                FLOAT dtu2 = vThisTri[2].tu - vThisTri[0].tu;
                FLOAT dtv1 = vThisTri[1].tv - vThisTri[0].tv;
                FLOAT dtv2 = vThisTri[2].tv - vThisTri[0].tv;
                pIntersection->tu = vThisTri[0].tu + pIntersection->fBary1 * dtu1 + pIntersection-

>fBary2 * dtu2;
                pIntersection->tv = vThisTri[0].tv + pIntersection->fBary1 * dtv1 + pIntersection-

>fBary2 * dtv2;
            }
            fff->Unlock();
        }

        pVB->Unlock();
        pIB->Unlock();

        pVB->Release();
        pIB->Release();

...
}

CD3DMesh->Render(d3dDevice);


我想问vThisTri[1].n是被选中的三角形面法线角度吗?
[em4]

5

主题

255

帖子

255

积分

中级会员

Rank: 3Rank: 3

积分
255
发表于 2004-3-29 02:57:00 | 显示全部楼层

Re:关于碰撞检测的问题

累死了,还没看完

54

主题

2918

帖子

3765

积分

论坛元老

Rank: 8Rank: 8

积分
3765
QQ
发表于 2004-3-29 07:01:00 | 显示全部楼层

Re:关于碰撞检测的问题

看别人的代码我就晕、、、
[em4]

1

主题

13

帖子

13

积分

新手上路

Rank: 1

积分
13
发表于 2004-3-29 17:14:00 | 显示全部楼层

Re:关于碰撞检测的问题

注释太少了,不好看,楼主多加点注释啊
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

作品发布|文章投稿|广告合作|关于本站|游戏开发论坛 ( 闽ICP备17032699号-3 )

GMT+8, 2025-5-16 08:42

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表