Vector UTIL_GetMouseAim( C_HL2WarsPlayer *pPlayer, int x, int y ) { Vector forward; // Remap x and y into -1 to 1 normalized space float xf, yf; xf = ( 2.0f * x / (float)(ScreenWidth()-1) ) - 1.0f; yf = ( 2.0f * y / (float)(ScreenHeight()-1) ) - 1.0f; // Flip y axis yf = -yf; const VMatrix &worldToScreen = engine->WorldToScreenMatrix(); VMatrix screenToWorld; MatrixInverseGeneral( worldToScreen, screenToWorld ); // Create two points at the normalized mouse x, y pos and at the near and far z planes (0 and 1 depth) Vector v1, v2; v1.Init( xf, yf, 0.0f ); v2.Init( xf, yf, 1.0f ); Vector o2; // Transform the two points by the screen to world matrix screenToWorld.V3Mul( v1, pPlayer->Weapon_ShootPosition() ); // ray start origin screenToWorld.V3Mul( v2, o2 ); // ray end origin VectorSubtract( o2, pPlayer->Weapon_ShootPosition(), forward ); forward.NormalizeInPlace(); return forward; }
void CEngineTool::CreatePickingRay( const CViewSetup &viewSetup, int x, int y, Vector& org, Vector& forward ) { // Remap x and y into -1 to 1 normalized space float xf, yf; xf = ( 2.0f * (float)x / (float)viewSetup.width ) - 1.0f; yf = ( 2.0f * (float)y / (float)viewSetup.height ) - 1.0f; // Flip y axis yf = -yf; VMatrix worldToScreen; GetWorldToScreenMatrixForView( viewSetup, &worldToScreen ); VMatrix screenToWorld; MatrixInverseGeneral( worldToScreen, screenToWorld ); // Create two points at the normalized mouse x, y pos and at the near and far z planes (0 and 1 depth) Vector v1, v2; v1.Init( xf, yf, 0.0f ); v2.Init( xf, yf, 1.0f ); Vector o2; // Transform the two points by the screen to world matrix screenToWorld.V3Mul( v1, org ); // ray start origin screenToWorld.V3Mul( v2, o2 ); // ray end origin VectorSubtract( o2, org, forward ); forward.NormalizeInPlace(); }