bool KG3DScenePvsEditor::GetPlaceObjectPos(D3DXVECTOR3& vPos, KG3DSceneOutputWnd* pWnd) { ASSERT(pWnd); if (!pWnd || !m_pPvs) return false; RECT rectWnd; POINT ptCursor; GetWindowRect(pWnd->m_hWnd, &rectWnd); GetCursorPos(&ptCursor); if (!PtInRect(&rectWnd, ptCursor)) return false; D3DXVECTOR3 v1; D3DXVECTOR3 v2; D3DXVECTOR3 dir; const D3DXPLANE plane = D3DXPLANE(0.0f, 1.0f, 0.0f, 0.f); pWnd->GetPickRay(&v1, &dir, NULL); v2 = v1 + (dir * 100000.0f); D3DXPlaneIntersectLine(&vPos, &plane, &v1, &v2); D3DXVECTOR3 vSrc[1] = { v1 }; D3DXVECTOR3 vDst[1] = { v2 }; float fDis = 0.f; if (m_pPvs->GetCameraNearRayIntersect(vDst, vSrc, 1, &fDis)) vPos = vDst[0] - dir * fDis; return true; }
//============================================================================== // Brief : 衝突判定押し戻し壁擦り処理(点と面) // Return : bool : 衝突判定結果(true : 面の裏側) // Arg : D3DXVECTOR3* pPoint : 対象点 // Arg : D3DXVECTOR3* pVel : 対象点速度 // Arg : D3DXPLANE* pPlane : 対象面 //============================================================================== bool CCollision::IsCollidedPushAlong(D3DXVECTOR3* pPoint, D3DXVECTOR3* pVel, D3DXPLANE* pPlane) { D3DXVECTOR3 m_posPoint = *pPoint + *pVel; // 判定座標 D3DXVECTOR3 vecNormal; // 面の法線 float fDist; // 対象間の距離 // 判定 fDist = D3DXPlaneDotCoord(pPlane, &m_posPoint); if(fDist <= 0.0f) { // 押し戻し // m_posPoint = *pPoint; // m_posPoint.y -= 1.0f; D3DXPlaneIntersectLine(pPoint, pPlane, pPoint, &m_posPoint); // 速度設定 vecNormal.x = pPlane->a; vecNormal.y = pPlane->b; vecNormal.z = pPlane->c; *pVel += -D3DXVec3Dot(pVel, &vecNormal) * vecNormal; // 当たっている return true; } // 当たっていない return false; }
bool TerrainRenderablePlane::RayHit(HippoRay* ray,D3DXVECTOR3* insertPoint) { auto end=ray->GetEndPoint(100000.f); auto* insertpoint=D3DXPlaneIntersectLine(insertPoint,&m_phy_shape,&ray->m_OrgPos,&end); return insertpoint!=0; }
//----------------------------------------------------------------------------// void Direct3D10RenderTarget::unprojectPoint(const GeometryBuffer& buff, const Vector2& p_in, Vector2& p_out) const { if (!d_matrixValid) updateMatrix(); const Direct3D10GeometryBuffer& gb = static_cast<const Direct3D10GeometryBuffer&>(buff); D3D10_VIEWPORT vp; setupViewport(vp); D3DXVECTOR3 in_vec; in_vec.z = 0.0f; // project points to create a plane orientated with GeometryBuffer's data D3DXVECTOR3 p1; D3DXVECTOR3 p2; D3DXVECTOR3 p3; in_vec.x = 0; in_vec.y = 0; D3DXVec3Project(&p1, &in_vec, &vp, &d_matrix, 0, gb.getMatrix()); in_vec.x = 1; in_vec.y = 0; D3DXVec3Project(&p2, &in_vec, &vp, &d_matrix, 0, gb.getMatrix()); in_vec.x = 0; in_vec.y = 1; D3DXVec3Project(&p3, &in_vec, &vp, &d_matrix, 0, gb.getMatrix()); // create plane from projected points D3DXPLANE surface_plane; D3DXPlaneFromPoints(&surface_plane, &p1, &p2, &p3); // unproject ends of ray in_vec.x = vp.Width * 0.5f; in_vec.y = vp.Height * 0.5f; in_vec.z = -d_viewDistance; D3DXVECTOR3 t1; D3DXVec3Unproject(&t1, &in_vec, &vp, &d_matrix, 0, gb.getMatrix()); in_vec.x = p_in.d_x; in_vec.y = p_in.d_y; in_vec.z = 0.0f; D3DXVECTOR3 t2; D3DXVec3Unproject(&t2, &in_vec, &vp, &d_matrix, 0, gb.getMatrix()); // get intersection of ray and plane D3DXVECTOR3 intersect; D3DXPlaneIntersectLine(&intersect, &surface_plane, &t1, &t2); p_out.d_x = intersect.x; p_out.d_y = intersect.y; }
Ray OBB::GetContactPoint(Ray &ray) { D3DXMATRIX p, r, world, invWorld; D3DXMatrixTranslation(&p, m_pos.x, m_pos.y, m_pos.z); D3DXMatrixRotationQuaternion(&r, &m_rot); D3DXMatrixMultiply(&world, &r, &p); D3DXMatrixInverse(&invWorld, NULL, &world); D3DXVECTOR3 org, dir; D3DXVec3TransformCoord(&org, &ray.m_org, &invWorld); D3DXVec3TransformNormal(&dir, &ray.m_dir, &invWorld); D3DXPLANE planes[] = {D3DXPLANE(0.0f, 0.0f, -1.0f, -m_size.z), D3DXPLANE(0.0f, 0.0f, 1.0f, -m_size.z), D3DXPLANE(0.0f, -1.0f, 0.0f, -m_size.y), D3DXPLANE(0.0f, 1.0f, 0.0f, -m_size.y), D3DXPLANE(-1.0f, 0.0f, 0.0f, -m_size.x), D3DXPLANE(1.0f, 0.0f, 0.0f, -m_size.x) }; D3DXVECTOR3 result, normal; int numPlanes = 0; int numIntersections = 0; for (int i=0; i<6; i++) { float d = org.x * planes[i].a + org.y * planes[i].b + org.z * planes[i].c; if (d > -planes[i].d) { D3DXVECTOR3 r; if (D3DXPlaneIntersectLine(&r, &planes[i], &org, &(org + dir * 1000.0f)) != NULL) { numPlanes++; if (abs(r.x) <= m_size.x && abs(r.y) <= m_size.y && abs(r.z) <= m_size.z) { D3DXVec3TransformCoord(&r, &r, &world); result = r; normal = D3DXVECTOR3(planes[i].a, planes[i].b, planes[i].c); numIntersections++; } } } } if (numIntersections == 0) { //Warning! OBB No Intersections! return Ray(ray.m_org, -ray.m_dir); } D3DXVec3Normalize(&normal, &normal); D3DXVec3TransformNormal(&normal, &normal, &world); return Ray(result, normal); }
bool KG3DScenePvsEditor::GetPlacePortalPos(D3DXVECTOR3& vPos, KG3DSceneOutputWnd* pWnd, KG3DPvsPortal* ptl) { ASSERT(pWnd); if (!pWnd || !m_pPvs || !ptl) return false; RECT rectWnd; POINT ptCursor; GetWindowRect(pWnd->m_hWnd, &rectWnd); GetCursorPos(&ptCursor); if (!PtInRect(&rectWnd, ptCursor)) return false; D3DXVECTOR3 v1; D3DXVECTOR3 v2; D3DXVECTOR3 dir; const D3DXPLANE plane = D3DXPLANE(0.0f, 1.0f, 0.0f, 0.f); pWnd->GetPickRay(&v1, &dir, NULL); v2 = v1 + (dir * 100000.0f); D3DXPlaneIntersectLine(&vPos, &plane, &v1, &v2); D3DXVECTOR3 vScal; ptl->GetScaling(&vScal); vScal *= 0.5f; D3DXVECTOR3 vSrc[5] = { v1, v1 - pWnd->GetCamera().GetCameraRight() * vScal.x + pWnd->GetCamera().GetCameraUp() * vScal.y, v1 + pWnd->GetCamera().GetCameraRight() * vScal.x + pWnd->GetCamera().GetCameraUp() * vScal.y, v1 + pWnd->GetCamera().GetCameraRight() * vScal.x - pWnd->GetCamera().GetCameraUp() * vScal.y, v1 - pWnd->GetCamera().GetCameraRight() * vScal.x - pWnd->GetCamera().GetCameraUp() * vScal.y }; D3DXVECTOR3 vDst[5] = { v2, v2 - pWnd->GetCamera().GetCameraRight() * vScal.x + pWnd->GetCamera().GetCameraUp() * vScal.y, v2 + pWnd->GetCamera().GetCameraRight() * vScal.x + pWnd->GetCamera().GetCameraUp() * vScal.y, v2 + pWnd->GetCamera().GetCameraRight() * vScal.x - pWnd->GetCamera().GetCameraUp() * vScal.y, v2 - pWnd->GetCamera().GetCameraRight() * vScal.x - pWnd->GetCamera().GetCameraUp() * vScal.y }; float fDis = 0.f; if (m_pPvs->GetCameraNearRayIntersect(vDst, vSrc, 5, &fDis)) vPos = v2 - dir * fDis; return true; }
// Project the mouse cursor from screen space to object space void EditExt::ProjectScreenToWorld(D3DXVECTOR3* pOut, float screenX, float screenY, float worldZ) { D3DXVECTOR3 lineBegin, lineEnd; // Unproject the near and far points given by the screen X,Y coords D3DXVECTOR3 screenSpace(screenX, screenY, 0.0f); D3DXVec3Unproject(&lineBegin, &screenSpace, &viewport, &projection_matrix, &view_matrix, &worldMatrix); screenSpace.z = 1.0f; D3DXVec3Unproject(&lineEnd, &screenSpace, &viewport, &projection_matrix, &view_matrix, &worldMatrix); // Using a plane intersection, we can determine the object space coordinates of the screen space coords // at a certain Z depth, intersecting the line given above. orig.z = worldZ; D3DXPlaneFromPointNormal(&plane, &orig, &normal); D3DXPlaneIntersectLine(pOut, &plane, &lineBegin, &lineEnd); }
BOOL KG3DBaseCoordImp::IntersectPlaneAndFindPoint( KG3DCOORD WhichCoord , const D3DXVECTOR3& Center , const D3DXVECTOR3& vSrc , const D3DXVECTOR3& vDir , D3DXVECTOR3& vInter ) { _ASSERTE(WhichCoord >= KG3DCOORD_FIRST_AXIS && WhichCoord <= KG3DCOORD_INTEGRATION); D3DXPLANE planeUse; if(WhichCoord >= KG3DCOORD_FIRST_AXIS && WhichCoord < KG3DCOORD_FIRST_AXIS + KG3DCOORD_AXIS_COUNT) { //如果选中的是线,那么找过这条线和发射线垂直的面,然后求交点 const D3DXVECTOR3& vAxis = D3DXVec3GetNormalOfPlane(WhichCoord - KG3DCOORD_FIRST_AXIS); D3DXVECTOR3 vUp; D3DXVec3Cross(&vUp, &vAxis, &vDir); D3DXVECTOR3 vNormalOfBestPlane; D3DXVec3Cross(&vNormalOfBestPlane, &vUp, &vAxis); D3DXVec3Normalize(&vNormalOfBestPlane, &vNormalOfBestPlane); D3DXPlaneFromPointNormal(&planeUse, &Center, &vNormalOfBestPlane); } else if(WhichCoord >= KG3DCOORD_FIRST_PLANE && WhichCoord < KG3DCOORD_FIRST_PLANE + KG3DCOORD_PLANE_COUNT) { const D3DXVECTOR3& NormalUse = D3DXVec3GetNormalOfPlane(WhichCoord - KG3DCOORD_FIRST_PLANE); D3DXPlaneFromPointNormal(&planeUse, &Center, &NormalUse); } else if (WhichCoord == KG3DCOORD_INTEGRATION) { const D3DXPLANE planeIntergration = GetPlaneIntergration(); const D3DXVECTOR3& NormalUse = (const D3DXVECTOR3&)planeIntergration; D3DXPlaneFromPointNormal(&planeUse, &Center, &NormalUse); } else { return FALSE; } const FLOAT someLargeLength = 100000.f; return NULL != D3DXPlaneIntersectLine(&vInter, &planeUse, &vSrc, &(vSrc + vDir * someLargeLength)); }
BOOL cCollision::IntersectLinePlane( cLine& Line,cPlane& TargetPlane,D3DXVECTOR3* pCrossPos/*=NULL*/ ,D3DXVECTOR3* pReflectionVec/*=NULL*/) { D3DXVECTOR3 posCross; D3DXPLANE tempPlane(TargetPlane.GetNormal().x,TargetPlane.GetNormal().y,TargetPlane.GetNormal().z,TargetPlane.GetDistance()); //평면 교점 구하기 if ( NULL == D3DXPlaneIntersectLine(&posCross,&tempPlane,&Line.GetStart(),&D3DXVECTOR3(Line.GetDirection()+Line.GetStart())) ) return FALSE; //반사벡터 구하기 if (pReflectionVec!=NULL) { D3DXVECTOR3 CrossNormal,vecLineStartToCross; vecLineStartToCross = posCross - Line.GetStart(); *pReflectionVec = (2.0f * TargetPlane.GetNormal()) + vecLineStartToCross; } return TRUE; }
//============================================================================== // Brief : 衝突判定押し戻し壁擦り速度保存処理(点と球状地形) // Return : bool : 衝突判定結果(true : 面の裏側) // Arg : D3DXVECTOR3* pPoint : 対象点 // Arg : D3DXVECTOR3* pVel : 対象点速度 // Arg : D3DXPLANE* pPlane : 対象面 //============================================================================== bool CCollision::IsCollidedPushAlongConservationSphereField(D3DXVECTOR3* pPoint, D3DXVECTOR3* pVel, D3DXPLANE* pPlane) { D3DXVECTOR3 m_posPoint = *pPoint + *pVel; // 判定座標 D3DXVECTOR3 vecNormal; // 面の法線 D3DXVECTOR3 vecAlong; // 壁擦り速度ベクトル float fDist; // 対象間の距離 float fMagVel; // 速度の大きさ float fMagVelAlong; // 壁擦り速度の大きさ // 判定 fDist = D3DXPlaneDotCoord(pPlane, &m_posPoint); if(fDist <= 0.0f) { // 押し戻し m_posPoint = 2.0f * *pPoint; m_posPoint.y -= 1.0f; D3DXPlaneIntersectLine(pPoint, pPlane, pPoint, &m_posPoint); // 速度設定 fMagVel = D3DXVec3Length(pVel); vecNormal.x = pPlane->a; vecNormal.y = pPlane->b; vecNormal.z = pPlane->c; vecAlong = *pVel + (-D3DXVec3Dot(pVel, &vecNormal) * vecNormal); fMagVelAlong = D3DXVec3Length(&vecAlong); if(fMagVelAlong >= 0.001f) { vecAlong *= fMagVel / D3DXVec3Length(&vecAlong); } else { vecAlong = -*pVel; } *pVel = vecAlong; // 当たっている return true; } // 当たっていない return false; }
CollideInfo * CBoundBox::CheckCollide( D3DXVECTOR3 *vSrcPos, CBoundBox *pBoundBox ) { m_pCollideInfo->m_bCollided = false; m_pCollideInfo->m_pPlane = NULL; static float length,currLength; static int index; length = 0.f; currLength = 0.f; index = 0; D3DXVECTOR3 *vSrcPoints = pBoundBox->GetTrueVertices(); for( int i = 0; i < 8; i++ ) { if( DeterminePointInBox( vSrcPoints ) ) { m_pCollideInfo->m_bCollided = true; break; } vSrcPoints++; } if( m_pCollideInfo->m_bCollided ) { D3DXVECTOR3 vOut; for( int i = 0; i < 4; i++ ) { D3DXPlaneIntersectLine( &vOut, m_pPlaneSides[i], vSrcPos, &m_vCenterPoint ); currLength = D3DXVec3Length( &( vOut - *vSrcPos) ); if( i == 0 ) length = currLength; if( currLength < length ){ length = currLength; index = i; } } m_pCollideInfo->m_pPlane = m_pPlaneSides[index]; } return m_pCollideInfo; }
//============================================================================== // Brief : 衝突判定押し戻し停止処理(点と面) // Return : bool : 衝突判定結果(true : 面の裏側) // Arg : D3DXVECTOR3* pPoint : 対象点 // Arg : D3DXVECTOR3* pVel : 対象点速度 // Arg : D3DXPLANE* pPlane : 対象面 //============================================================================== bool CCollision::IsCollidedPushStop(D3DXVECTOR3* pPoint, D3DXVECTOR3* pVel, D3DXPLANE* pPlane) { D3DXVECTOR3 m_posPoint = *pPoint + *pVel; // 判定座標 float fDist; // 対象間の距離 // 判定 fDist = D3DXPlaneDotCoord(pPlane, &m_posPoint); if(fDist <= 0.0f) { // 押し戻し // m_posPoint = *pPoint; // m_posPoint.y -= 1.0f; D3DXPlaneIntersectLine(pPoint, pPlane, pPoint, &m_posPoint); // 当たっている return true; } // 当たっていない return false; }
void FieldSector::generateHeights() { if(m_heights) return; m_heights = new D3DXFLOAT16[g_grassSectorSize * g_grassSectorSize]; TerrainVertex *terrainVertices = core.game->getWorld()->getTerrainManager()->m_vertices; int terrainWidth = core.game->getWorld()->getTerrainManager()->m_size; float coordinatesModifier = g_grassTileSize / core.game->getWorld()->getTerrainManager()->m_hmGeoStep; for(int y = 0; y < g_grassSectorSize; y++) { for(int x = 0; x < g_grassSectorSize; x++) { float terrainGrassProportion = g_grassTileSize / core.game->getWorld()->getTerrainManager()->m_hmGeoStep; //terrainGrassProportion = 0.5f; /// filling heightmap with data up D3DXPLANE plane; int X = x + m_posX; int Y = y + m_posY; Vec3 p1 = terrainVertices[(int)(Y*terrainGrassProportion)*terrainWidth+(int)(X*terrainGrassProportion)].pos; Vec3 p2 = terrainVertices[(int)((Y*terrainGrassProportion)+1)*terrainWidth+(int)(X*terrainGrassProportion)].pos; Vec3 p3 = terrainVertices[(int)(Y*terrainGrassProportion)*terrainWidth+(int)(X*terrainGrassProportion)+1].pos; Vec3 p4 = terrainVertices[(int)(Y*terrainGrassProportion+1)*terrainWidth+(int)(X*terrainGrassProportion)+1].pos; Vec3 resultPos; Vec3 upPoint = Vec3((X)*g_grassTileSize, 2000, (Y)*g_grassTileSize); Vec3 downPoint = Vec3((X)*g_grassTileSize, -2000, (Y)*g_grassTileSize); f32 pU, pV, pDist; if(D3DXIntersectTri(&p1, &p2, &p3, &upPoint, &Vec3(0,-1,0), &pU, &pV, &pDist)) D3DXPlaneFromPoints(&plane, &p1, &p2, &p3); else if(D3DXIntersectTri(&p2, &p3, &p4, &upPoint, &Vec3(0,-1,0), &pU, &pV, &pDist)) D3DXPlaneFromPoints(&plane, &p2, &p3, &p4); D3DXPlaneIntersectLine(&resultPos, &plane, &upPoint, &downPoint); D3DXFloat32To16Array(&m_heights[y * g_grassSectorSize + x], &resultPos.y, 1); } } }
/** * CABT::splitTriangle * @date Modified Apr 18, 2006 */ void CABT::splitTriangle(CMesh::SVertex* pVertex, SPlane& oPlane, std::vector<CMesh::SVertex>& vFront, std::vector<CMesh::SVertex>& vBack) { unsigned int nResA, nResB; CMesh::SVertex vA, vB; vA = pVertex[2]; nResA = getPointClassification(vA.vPosition, oPlane); std::vector<CMesh::SVertex> vFrontFaces, vBackFaces; // Check triangle edges for(unsigned int i = 0; i < 3; ++i) { // Get the next triangle point vB = pVertex[i]; nResB = getPointClassification(vB.vPosition, oPlane); if(nResB == PT_FRONT) { if(nResA == PT_BACK) { // Find intersection D3DXVECTOR3 vIntersect; D3DXPLANE oDXPlane; D3DXPlaneIntersectLine(&vIntersect, D3DXPlaneFromPointNormal(&oDXPlane, &oPlane.point, &oPlane.normal), &vA.vPosition, &vB.vPosition); // Calculate interpolation factor D3DXVECTOR3 vNew, vWhole; D3DXVec3Subtract(&vNew, &vA.vPosition, &vIntersect); D3DXVec3Subtract(&vWhole, &vA.vPosition, &vB.vPosition); float fFactor = D3DXVec3Length(&vNew) / D3DXVec3Length(&vWhole); // Interpolate vertex data. CMesh::SVertex vNewVertex; D3DXCOLOR oColor; vNewVertex.vPosition = vIntersect; D3DXVec3Lerp(&vNewVertex.vNormal, &vA.vNormal, &vB.vNormal, fFactor); D3DXVec2Lerp(&vNewVertex.vTexCoord0, &vA.vTexCoord0, &vB.vTexCoord0, fFactor); D3DXColorLerp(&oColor, &D3DXCOLOR(vA.Color), &D3DXCOLOR(vB.Color), fFactor); vNewVertex.Color = oColor; vFrontFaces.push_back(vNewVertex); vBackFaces.push_back(vNewVertex); } vFrontFaces.push_back(vB); } else if(nResB == PT_BACK) { if(nResA == PT_FRONT) { // Find intersection D3DXVECTOR3 vIntersect; D3DXPLANE oDXPlane; D3DXPlaneIntersectLine(&vIntersect, D3DXPlaneFromPointNormal(&oDXPlane, &oPlane.point, &oPlane.normal), &vA.vPosition, &vB.vPosition); // Calculate interpolation factor D3DXVECTOR3 vNew, vWhole; D3DXVec3Subtract(&vNew, &vA.vPosition, &vIntersect); D3DXVec3Subtract(&vWhole, &vA.vPosition, &vB.vPosition); float fFactor = D3DXVec3Length(&vNew) / D3DXVec3Length(&vWhole); // Interpolate vertex data. CMesh::SVertex vNewVertex; D3DXCOLOR oColor; vNewVertex.vPosition = vIntersect; D3DXVec3Lerp(&vNewVertex.vNormal, &vA.vNormal, &vB.vNormal, fFactor); D3DXVec2Lerp(&vNewVertex.vTexCoord0, &vA.vTexCoord0, &vB.vTexCoord0, fFactor); D3DXColorLerp(&oColor, &D3DXCOLOR(vA.Color), &D3DXCOLOR(vB.Color), fFactor); vNewVertex.Color = oColor; vFrontFaces.push_back(vNewVertex); vBackFaces.push_back(vNewVertex); } vBackFaces.push_back(vB); } else { vFrontFaces.push_back(vB); vBackFaces.push_back(vB); } // Next Edge vA = vB; nResA = nResB; } // Make a triangle list out of the the vertices created. for(unsigned int i = 1; i < vFrontFaces.size() - 1; ++i) { vFront.push_back(vFrontFaces[0]); vFront.push_back(vFrontFaces[i]); vFront.push_back(vFrontFaces[i+1]); } for(unsigned int i = 1; i < vBackFaces.size() - 1; ++i) { vBack.push_back(vBackFaces[0]); vBack.push_back(vBackFaces[i]); vBack.push_back(vBackFaces[i+1]); } }
HRESULT KG3DRotationCoordinateOld::FrameMove() { HRESULT hResult = E_FAIL; HRESULT hRetCode = E_FAIL; IEKG3DSceneOutputWnd *piCurOutputWnd = NULL; D3DXVECTOR3 vCoordX; D3DXVECTOR3 vCoordY; D3DXVECTOR3 vCoordZ; switch (m_dwMode) { case KG3DCOORD_WORLD: { /* D3DXMATRIX matEntity = m_EntityList.GetWorldMatrix(); D3DXMatrixIdentity(&m_matCoord); m_matCoord._41 = matEntity._41; m_matCoord._42 = matEntity._42; m_matCoord._43 = matEntity._43;*/ D3DXVECTOR3 vCenter(0,0,0); KSF::GetSelectionCenter(m_pAttachScene->GetSelectionTool(), vCenter); D3DXMatrixTranslation(&m_matCoord, vCenter.x, vCenter.y, vCenter.z); } break; case KG3DCOORD_LOCAL : { //m_matCoord = m_EntityList.GetWorldMatrix(); D3DXVECTOR3 vCenter(0,0,0); KSF::GetSelectionCenter(m_pAttachScene->GetSelectionTool(), vCenter); D3DXMatrixTranslation(&m_matCoord, vCenter.x, vCenter.y, vCenter.z); D3DXVECTOR3 t; D3DXVECTOR3 s; D3DXMATRIX q; D3DXMATRIX m; MatrixExract(s, t, q, &m_matCoord); D3DXMatrixTranslation(&m, t.x, t.y, t.z); m_matCoord = q * m; } break; default : ASSERT(false); break; } vCoordX = D3DXVECTOR3(m_matCoord._11, m_matCoord._12, m_matCoord._13); vCoordY = D3DXVECTOR3(m_matCoord._21, m_matCoord._22, m_matCoord._23); vCoordZ = D3DXVECTOR3(m_matCoord._31, m_matCoord._32, m_matCoord._33); if (!m_nMoveFlag && m_pAttachScene && m_pCoordMesh && /*m_EntityList.GetSize()*/0 != m_pAttachScene->GetSelectionTool().GetSelectionCount()) { D3DXVECTOR3 vOrg; D3DXVECTOR3 vDir; hRetCode = m_pAttachScene->GetCurOutputWnd(&piCurOutputWnd); KGLOG_COM_PROCESS_ERROR(hRetCode); piCurOutputWnd->GetPickRay(&vOrg, &vDir, NULL); D3DXVec3Normalize(&vDir, &vDir); m_dwCurrSelCoord = GetSelCoord(vOrg, vDir); } if (m_nMoveFlag && m_pAttachScene && m_pCoordMesh && /*m_EntityList.GetSize()*/0 != m_pAttachScene->GetSelectionTool().GetSelectionCount()) { D3DXVECTOR3 vModelPosition; D3DXVECTOR3 vOrg; D3DXVECTOR3 vDir; D3DXVECTOR3 vCurrOrig; D3DXMATRIX matWorldMatrix; hRetCode = m_pAttachScene->GetCurOutputWnd(&piCurOutputWnd); KG_PROCESS_ERROR(piCurOutputWnd); piCurOutputWnd->GetPickRay(&vOrg, &vDir, NULL); matWorldMatrix = m_matEntityWorld; vModelPosition.x = matWorldMatrix._41; vModelPosition.y = matWorldMatrix._42; vModelPosition.z = matWorldMatrix._43; D3DXVECTOR3 vCurrCross; KG_PROCESS_ERROR( D3DXPlaneIntersectLine( &vCurrCross, &m_currSelPane, &vOrg, &(vOrg + vDir * 10000000.0f) ) ); D3DXVECTOR3 vCurrDir; D3DXVECTOR3 vPrevDir; D3DXVec3Normalize(&vCurrDir, &(vCurrCross - vModelPosition)); D3DXVec3Normalize(&vPrevDir, &(m_vPrevCross - vModelPosition)); D3DXVECTOR3 vCross; FLOAT fDot = D3DXVec3Dot(&vCurrDir, &vPrevDir); if (fDot > 1.0f) fDot = 1.0f; D3DXVec3Cross(&vCross, &vCurrDir, &vPrevDir); FLOAT fAngel = 0.0f; D3DXVec3Normalize(&vCross, &vCross); D3DXVec3Normalize(&m_currSelNormal, &m_currSelNormal); if (fabs(D3DXVec3Dot(&vCross, &m_currSelNormal) - 1) <= 0.05) fAngel = -acosf(fDot); else fAngel = acosf(fDot); D3DXMATRIX mat; switch (m_dwCurrSelCoord) { case 0 : D3DXMatrixRotationAxis(&mat, &vCoordY, fAngel); m_fAngelY += fAngel; break; case 1 : D3DXMatrixRotationAxis(&mat, &vCoordX, fAngel); m_fAngelX += fAngel; break; case 2 : D3DXMatrixRotationAxis(&mat, &vCoordZ, fAngel); m_fAngelZ += fAngel; break; default : break; } //m_EntityList.Rotation(mat); KSF::SelectionRotationEntity(m_pAttachScene->GetSelectionTool(), mat); m_vPrevCross = vCurrCross; } IEKG3DSceneEditorBase* pEditorBase = NULL; m_pAttachScene->GetSceneEditorBase(&pEditorBase); if (pEditorBase) pEditorBase->SetRtsActive(m_dwCurrSelCoord != 0xffffffff); hResult = S_OK; Exit0: return hResult; }
HRESULT KG3DRotationCoordinateOld::RotateBegin() { //m_matEntityWorld = m_EntityList.GetWorldMatrix(); { D3DXVECTOR3 vCenter(0,0,0); KSF::GetSelectionCenter(m_pAttachScene->GetSelectionTool(), vCenter); D3DXMatrixTranslation(&m_matEntityWorld, vCenter.x, vCenter.y, vCenter.z); } m_vBeginCross.x = m_matEntityWorld._41; m_vBeginCross.y = m_matEntityWorld._42; m_vBeginCross.z = m_matEntityWorld._43; m_vPrevCross = m_vBeginCross; HRESULT hResult = E_FAIL; HRESULT hRetCode = E_FAIL; D3DXVECTOR3 vOrg; D3DXVECTOR3 vDir; D3DXMATRIX matWorldInv; D3DXVECTOR3 vCrossXZ; D3DXVECTOR3 vCrossYZ; D3DXVECTOR3 vCrossXY; D3DXPLANE planeXZ; D3DXPLANE planeXY; D3DXPLANE planeYZ; D3DXVECTOR3 vPoint = D3DXVECTOR3(m_matCoord._41, m_matCoord._42, m_matCoord._43); D3DXVECTOR3 vNorXZ = D3DXVECTOR3(m_matCoord._21, m_matCoord._22, m_matCoord._23); D3DXVECTOR3 vNorXY = D3DXVECTOR3(m_matCoord._31, m_matCoord._32, m_matCoord._33); D3DXVECTOR3 vNorYZ = D3DXVECTOR3(m_matCoord._11, m_matCoord._12, m_matCoord._13); D3DXVECTOR3 vCross; IEKG3DSceneOutputWnd *piCurOutputWnd = NULL; KG_PROCESS_ERROR(m_dwCurrSelCoord != 0xFFFFFFFF); //KG_PROCESS_ERROR(m_pAttachScene); //KG_PROCESS_ERROR(m_EntityList.GetSize()); _ASSERTE(NULL != m_pAttachScene); KG_PROCESS_ERROR(0 != m_pAttachScene->GetSelectionTool().GetSelectionCount()); hRetCode = m_pAttachScene->GetCurOutputWnd(&piCurOutputWnd); KGLOG_COM_PROCESS_ERROR(hRetCode); piCurOutputWnd->GetPickRay(&vOrg, &vDir, NULL); D3DXPlaneFromPointNormal( &planeXZ, &vPoint, &vNorXZ ); D3DXPlaneFromPointNormal( &planeXY, &vPoint, &vNorXY ); D3DXPlaneFromPointNormal( &planeYZ, &vPoint, &vNorYZ ); D3DXPlaneNormalize(&planeYZ, &planeYZ); D3DXPlaneNormalize(&planeXZ, &planeXZ); D3DXPlaneNormalize(&planeXY, &planeXY); switch (m_dwCurrSelCoord) { case 0 : // y KG_PROCESS_ERROR( D3DXPlaneIntersectLine( &vCross, &planeXZ, &vOrg, &(vOrg + vDir * 10000000.0f) ) ); m_vPrevCross = vCross; m_currSelPane = planeXZ; m_currSelNormal = vNorXZ; break; case 1 : // x KG_PROCESS_ERROR( D3DXPlaneIntersectLine( &vCross, &planeYZ, &vOrg, &(vOrg + vDir * 10000000.0f) ) ); m_vPrevCross = vCross; m_currSelPane = planeYZ; m_currSelNormal = vNorYZ; break; case 2 : // z KG_PROCESS_ERROR( D3DXPlaneIntersectLine( &vCross, &planeXY, &vOrg, &(vOrg + vDir * 10000000.0f) ) ); m_vPrevCross = vCross; m_currSelPane = planeXY; m_currSelNormal = vNorXY; break; default : ASSERT(FALSE); break; } m_vBeginCross = m_vPrevCross; m_fAngelX = 0.0f; m_fAngelY = 0.0f; m_fAngelZ = 0.0f; m_nMoveFlag = TRUE; hResult = S_OK; Exit0: return hResult; }
void Character::Move( Real FrameTime, WorldManager* SM ) { //declarations Real Slope; Vector3D SlopeNormal; D3DXPLANE TestPlane; //Move the object down from gravity. TargetVelocity.y -= (GRAVITY/2) * FrameTime; if(TargetVelocity.y < -GRAVITY) TargetVelocity.y = -GRAVITY; //find the objects current altitude. Real GetHeightValue = SM->GetHeightValue(Location); //Move the object based on the velocity if(fabs(TargetVelocity.x) > 1 || fabs(TargetVelocity.z) > 1) { Location.x += TargetVelocity.x * FrameTime; Location.z += TargetVelocity.z * FrameTime; } if(fabs(TargetVelocity.y) > .2f) Location.y += TargetVelocity.y * FrameTime; Real NewHeight = SM->GetHeightValue(Location, &Slope, &SlopeNormal, &TestPlane); //adjust the objects height if it's falling through //the ground if(Location.y< NewHeight) { if(Slope > .8f) { Real UpSpeed = (NewHeight-Location.y)*12; Location.y+= UpSpeed*FrameTime; if(Location.y > NewHeight) Location.y = NewHeight; TargetVelocity.y = 0; } else { //Adjust the objects height if it's falling. Take into //account slope and "slide" the object if neccesary. //first, create a ray to test with D3DXVECTOR3 p1, p2, result; p1.x = Location.x; p1.y = Location.y; p1.z = Location.z; p2.x = Location.x + SlopeNormal.x * 30; p2.y = Location.y + SlopeNormal.y * 30; p2.z = Location.z + SlopeNormal.z * 30; //Next, find where our ray intersects the plane D3DXPlaneIntersectLine(&result, &TestPlane, &p1, &p2); //Now, place the player at that intersection point Location.x = result.x; Location.y = result.y; Location.z = result.z; TargetVelocity.y += FRICTION/1.2f * FrameTime; } } }
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * Summary: Responds to key presses Parameters: [in] xDelta - Change in mouse x-axis since last frame [in] yDelta - Change in mouse y-axis since last frame [in] zDelta - Change in mouse z-axis since last frame [in] pMouseButtons - Mouse button states [in] pPressedKeys - Keyboard keys that are pressed and not locked [in] elapsedTime - Time elapsed since last frame * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ void terrain3d::ProcessInput( long xDelta, long yDelta, long zDelta, BOOL* pMouseButtons, BOOL* pPressedKeys, float elapsedTime ) { float cameraSpeed = 30.0f; if ( pMouseButtons[1] ) { D3DVIEWPORT9 viewport; m_pFramework->m_pGraphics->GetDevice()->GetViewport(&viewport); D3DXVECTOR3 inP1( (float) m_pFramework->m_mouse.GetX(), (float) m_pFramework->m_mouse.GetY(), 0.1f); D3DXVECTOR3 outP1; D3DXVECTOR3 outP2; D3DXMATRIX viewMatrix; m_pFramework->m_pGraphics->GetDevice()->GetTransform( D3DTS_VIEW, &viewMatrix); D3DXMATRIX projMatrix; m_pFramework->m_pGraphics->GetDevice()->GetTransform( D3DTS_PROJECTION, &projMatrix); D3DXMATRIX worldMatrix; m_pFramework->m_pGraphics->GetDevice()->GetTransform( D3DTS_WORLD, &worldMatrix); outP1 = *m_camera.GetPosition(); inP1.z = viewport.MaxZ; D3DXVec3Unproject(&outP2, &inP1, &viewport, /*&m_camera.m_projection*/ /*m_camera.GetProjectionMatrix()*/ &projMatrix, /*&m_camera.m_view*//*m_camera.GetViewMatrix()*/&viewMatrix, /*&worldMatrix*/ /*m_terrain.GetTransform()*/&worldMatrix); //printf("DEBUG: %f, %f, %f\n", outP2.x, outP2.y, outP2.z); D3DXVECTOR3 intersection; D3DXPLANE p(0.0f, 1.0f, 0.0f, 0.0f); D3DXPlaneIntersectLine(&intersection, &p, &outP1, &outP2); //printf("DEBUG: %f, %f\n", intersection.x * 100.0f, intersection.z * 100.0f); waypoint[0] = intersection.z * 100.0f; waypoint[1] = intersection.x * 100.0f; } if ( pMouseButtons[0] ) { m_camera.Yaw( xDelta * elapsedTime * 0.1f); m_camera.Pitch( yDelta * elapsedTime * 0.1f ); updated = true; } if ( pPressedKeys[DIK_I] ) { m_camera.MoveForward( cameraSpeed * elapsedTime ); updated = true; } if ( pPressedKeys[DIK_J] ) { m_camera.Strafe( -cameraSpeed * elapsedTime ); updated = true; } if ( pPressedKeys[DIK_K] ) { m_camera.MoveForward( -cameraSpeed * elapsedTime ); updated = true; } if ( pPressedKeys[DIK_L] ) { m_camera.Strafe( cameraSpeed * elapsedTime ); updated = true; } if ( pPressedKeys[DIK_Q] ) { m_terrain.ScaleRel( 0.0f, -0.1f * elapsedTime, 0.0f ); updated = true; } if ( pPressedKeys[DIK_E] ) { m_terrain.ScaleRel( 0.0f , 0.1f * elapsedTime, 0.0f ); updated = true; } if ( pPressedKeys[DIK_F5] ) { m_pFramework->LockKey( DIK_F5 ); if ( m_pFramework != NULL ) { m_pFramework->ToggleFullscreen(); } } if ( pPressedKeys[DIK_F6] ) { m_pFramework->LockKey( DIK_F6 ); if ( m_pFramework != NULL ) { m_pFramework->ToggleWireframe(); updated = true; } } }