Re: [hlcoders] Screen To World (from mouse cursor to world coords)
-- [ Picked text/plain from multipart/alternative ] Wow, absolute awesomeness! It worked first time like a charm! This should be added to the SDK in the future, it should of been in there already. Thanks Micheal and Yahn. On 7/30/07, Michael Kramer <[EMAIL PROTECTED]> wrote: > > -- > [ Picked text/plain from multipart/alternative ] > Direct Copy paste from another email with same problem, > > From Yahn at Valve: > Note that the client .dll can request the last worldToScreen from the > engine through the engine->GetWorldToScreenMatrix API. You might have > to do this differently if the view model code hoses that matrix, though, > or you could cache the matrix during the rendering of regular entities > and use it later when you need it. > > This gives you and origin an direction for what's under the mouse and > then you just trace a ray from that origin in the forward direction and > see what it hits. > > Yahn > > > // x and y are the mouse position relative to the viewport/engine view > which is w wide and h high > void CreatePickingRay( float x, float y, int w, int h, Vector& org, > Vector& forward, const Vmatrix& worldToScreen ) > { >// Remap x and y into -1 to 1 normalized space >float xf, yf; >xf = ( 2.0f * x / (float)(w-1) ) - 1.0f; >yf = ( 2.0f * y / (float)(h-1) ) - 1.0f; > >// Flip y axis >yf = -yf; > >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(); > -- > > ___ > To unsubscribe, edit your list preferences, or view the list archives, > please visit: > http://list.valvesoftware.com/mailman/listinfo/hlcoders > > -- ___ To unsubscribe, edit your list preferences, or view the list archives, please visit: http://list.valvesoftware.com/mailman/listinfo/hlcoders
Re: [hlcoders] Screen To World (from mouse cursor to world coords)
-- [ Picked text/plain from multipart/alternative ] Direct Copy paste from another email with same problem, >From Yahn at Valve: Note that the client .dll can request the last worldToScreen from the engine through the engine->GetWorldToScreenMatrix API. You might have to do this differently if the view model code hoses that matrix, though, or you could cache the matrix during the rendering of regular entities and use it later when you need it. This gives you and origin an direction for what's under the mouse and then you just trace a ray from that origin in the forward direction and see what it hits. Yahn // x and y are the mouse position relative to the viewport/engine view which is w wide and h high void CreatePickingRay( float x, float y, int w, int h, Vector& org, Vector& forward, const Vmatrix& worldToScreen ) { // Remap x and y into -1 to 1 normalized space float xf, yf; xf = ( 2.0f * x / (float)(w-1) ) - 1.0f; yf = ( 2.0f * y / (float)(h-1) ) - 1.0f; // Flip y axis yf = -yf; 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(); -- ___ To unsubscribe, edit your list preferences, or view the list archives, please visit: http://list.valvesoftware.com/mailman/listinfo/hlcoders
[hlcoders] Screen To World (from mouse cursor to world coords)
-- [ Picked text/plain from multipart/alternative ] I've been trying to get the world coordinates from the screen coords with no luck. The function ScreenTransform if you switch around the screen/point vectors they take and inverse the WorldToScreen matrix, it SHOULD be possible to get the world origin values. However, I can't get it to work. From a top down view of the map, I can almost match the world coordinates with screen, but when I rotate my view around the values are way off. Any of you guys had luck with this? You'd use this mostly in RTS type games So I'm sure I wouldn't be the only one who could use this knowledge! -- ___ To unsubscribe, edit your list preferences, or view the list archives, please visit: http://list.valvesoftware.com/mailman/listinfo/hlcoders