游戏开发论坛

 找回密码
 立即注册
搜索
查看: 1258|回复: 0

关于pick的问题,新手请教,请高手帮忙看下,谢谢了

[复制链接]

2

主题

4

帖子

8

积分

新手上路

Rank: 1

积分
8
发表于 2009-5-24 16:39:00 | 显示全部楼层 |阅读模式
我的鼠标拾取怎么也不好用, 就是目标朝着一个方向移动,无论鼠标点击什么地方 [em15] 实现的代码片段如下:
struct Ray
{
        D3DXVECTOR3 _originPt;
        D3DXVECTOR3 _vDirection;
};
void Hero::GetRey(Input &mouse, HWND hWnd, DXCCamera &camera, StaticMesh *Mesh)
{
        if( MBUTTON == mouse.GetMouseButtonState( ))
        {
                POINT p;
                GetCursorPos(&p);
                ScreenToClient(hWnd, &p);

                Ray ray = mouse.CalculateRay(hWnd, p);
                D3DXMATRIX invMat;
                D3DXMatrixInverse(&invMat,NULL,&m_matWorld);
                D3DXVec3TransformCoord(&ray._originPt,&ray._originPt,&invMat);
                D3DXVec3TransformNormal(&ray._vDirection,&ray._vDirection,&invMat);

                BOOL bHit;
                DWORD dwFaceIndex;
                FLOAT fU;
                FLOAT fV;
                D3DXIntersect( Mesh->GetMesh( ), camera.GetVameraPos( ), &ray._vDirection,
                          &bHit, &dwFaceIndex, &fU, &fV, NULL, NULL, NULL);
                if( bHit )
                {
                        Mesh->GetPickVertex(&m_vMoveToPt, dwFaceIndex, fU, fV);
                        m_vMoveToPt = m_vMoveToPt - m_PosPt;
                        D3DXVec3Normalize(&m_vMoveToPt, &m_vMoveToPt);
                        m_bISAutoMove = TRUE;
                }
        }
}

Ray Input::CalculateRay(HWND hWnd, POINT clientPt )
{
        DWORD clientWidth,clientHeight;
        RECT rc;
        GetClientRect( hWnd ,&rc);
        clientWidth = rc.right - rc.left;
        clientHeight = rc.bottom - rc.top;

        D3DXMATRIX matProj,matView;
        m_pD3DDevice->GetTransform(D3DTS_PROJECTION,&matProj);
        m_ray._vDirection.x= (2.0f*clientPt.x/clientWidth-1.0f)/matProj._11;
        m_ray._vDirection.y= (2.0f*clientPt.y/clientHeight-1.0f)/matProj._22;
        m_ray._vDirection.z = 1.0f;

        m_pD3DDevice->GetTransform(D3DTS_VIEW,&matView);
        D3DXMatrixInverse(&matView, NULL, &matView );

        D3DXVec3TransformCoord(&m_ray._originPt, &m_ray._originPt, &matView);
        D3DXVec3TransformNormal(&m_ray._vDirection, &m_ray._vDirection, &matView);

        D3DXVec3Normalize(&m_ray._vDirection, &m_ray._vDirection);

        return m_ray;
}
[em15]
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2025-12-20 07:52

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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