Beispiel #1
0
	/// ------------------------------------------------ //
	/// [4/18/2014 jianglei.kinly]
	/// 扇形
	/// ------------------------------------------------ //
	xObjectSet XGameMap::CaptureObjectBySector( const XVector3& vCenter, const XVector3& vDir, xgc_uint32 dwRadius, xgc_uint16 wAngle, const std::function< xgc_bool( xObject ) > &fnFilter/* = xgc_nullptr*/ )
	{
		// 计算旋转向量
		XVector3 vRotate = vDir.RotateZ( DEG2RAD( wAngle / 2 ) );
		xgc_real32 fDotValue = vDir.DotProduct( vRotate );
		auto fn = [&]( xObject hObject )->xgc_bool {
			if( xgc_nullptr == fnFilter || fnFilter( hObject ) )
			{
				XGameObject* pObj = ObjectCast<XGameObject>( hObject );
				XGC_ASSERT_RETURN( pObj, false );

				XVector3 vDistance = pObj->GetPosition() - vCenter;
				if( vDistance.SqurLength() > dwRadius * dwRadius * 1.0f )
					return false;

				if( vDir.DotProduct( vDistance ) < fDotValue )
					return false;

				return true;
			}

			return false;
		};

		iRect rc(
			WorldToArea( vCenter.x - dwRadius, vCenter.y - dwRadius ),
			WorldToArea( vCenter.x + dwRadius, vCenter.y + dwRadius ) );
		return FetchObject( rc, fn );
	}
Beispiel #2
0
	/// ------------------------------------------------ //
	/// [4/25/2014 jianglei.kinly]
	/// 方向矩形(subCenter)
	/// ------------------------------------------------ //
	xObjectSet XGameMap::CaptureObjectByRectangle( const XVector3& vCenter, const XVector3& vDir, xgc_uint32 dwRadiusMax, xgc_uint32 dwRadiusMin, const std::function< xgc_bool( xObject ) > &fnFilter/* = xgc_nullptr*/ )
	{
		/*
		0,0
		-------------------------------x
		|
		|        ------
		|       /    /
		|      /    /
		|     ---|--
		|       subCenter
		|    斜线是dir的方向,dwRadiusMax
		|    横线是dwRadiusMin
		y
		*/
		// 取中心点坐标
		XVector3 newDirect = vDir * ( dwRadiusMax * 1.0f );
		XVector3 newCenter = vCenter + newDirect;

		// 取对角线直径
		xgc_uint32 dwDiameter = xgc_uint32( XMath::Sqrt( XMath::Pow( 1.0f * dwRadiusMax, 2.0f ) + XMath::Pow( 1.0f * dwRadiusMin, 2.0f ) ) );

		auto fn = [&]( xObject hObject )->xgc_bool {
			if( xgc_nullptr == fnFilter || fnFilter( hObject ) )
			{
				XGameObject* pObj = ObjectCast<XGameObject>( hObject );
				XGC_ASSERT_RETURN( pObj, false );

				// 取我和目标连线的向量
				XVector3 subVec = pObj->GetPosition() - vCenter;
				// 判断夹角(我的朝向点乘我和目标连线的向量 > 0 代表夹角在0-90范围内)
				auto fTemp = vDir.DotProduct( subVec );
				if( fTemp < 0 )
					return false;
				// 找两个投影,判断是否在方向矩形内
				XVector3 vecy = vDir * fTemp;
				XVector3 vecx = subVec - vecy;
				return !( vecy.Length() > dwRadiusMax || vecx.Length() > dwRadiusMin / 2 );
			}

			return false;
		};

		iRect rc(
			WorldToArea( vCenter.x - dwDiameter / 2, vCenter.y - dwDiameter / 2 ),
			WorldToArea( vCenter.x + dwDiameter / 2, vCenter.y + dwDiameter / 2 ) );
		return FetchObject( rc, fn );
	}