Here's some source code that should help you. It does the opposite of
what you need, but it might help anyways.
Notes:
- This code came from Yahn
- This code depends on v_angles (so this works on the client only)
//-----------------------------------------------------------------------------
// Purpose:
// Given a field of view and mouse/screen positions as well as the current
// render origin and angles, returns a unit vector through the mouse position
// that can be used to trace into the world under the mouse click pixel.
// Input : fov -
// mousex -
// mousey -
// screenwidth -
// screenheight -
// vecRenderAngles -
// c_x -
// vpn -
// vup -
// 360.0 -
//-----------------------------------------------------------------------------
void CreatePickingRay( int mousex, int mousey, Vector& outVecPickingRay )
{
float dx, dy;
float c_x, c_y;
float dist;
Vector vpn, vup, vright;
//char gDebugMessage[256];
float fovDegrees = gHUD.m_iFOV;
vec3_t vecRenderAngles;
VectorCopy(v_angles, vecRenderAngles);
//vec3_t theForward, theRight, theUp;
//AngleVectors(v_angles, theForward, theRight, theUp);
//ASSERT(v_angles.x == vecRenderAngles.x);
//ASSERT(v_angles.y == vecRenderAngles.y);
//ASSERT(v_angles.z == vecRenderAngles.z);
c_x = ScreenWidth/2;
c_y = ScreenHeight/2;
dx = (float)mousex - c_x;
// Invert Y
dy = c_y - (float)mousey;
//sprintf(gDebugMessage, "inMouseX/Y: %d/%d, dx/dy = %d/%d", (int)mousex,
(int)mousey, (int)dx, (int)dy);
//CenterPrint(gDebugMessage);
// Convert view plane distance
dist = c_x tan( M_PI * fovDegrees 360.0 );
// Decompose view angles
AngleVectors( vecRenderAngles, vpn, vright, vup );
// Offset forward by view plane distance, and then by pixel offsets
outVecPickingRay = vpn * dist + vright * ( dx ) + vup * ( dy );
//sprintf(gDebugMessage, "outVecPickingRay: %.0f, %.0f, %.0f",
outVecPickingRay.x, outVecPickingRay.y, outVecPickingRay.z);
//CenterPrint(gDebugMessage);
// Convert to unit vector
VectorNormalize( outVecPickingRay );
}
Charlie Cleveland
Game programmer and designer
http://www.natural-selection.org
[EMAIL PROTECTED]
_______________________________________________
To unsubscribe, edit your list preferences, or view the list archives, please visit:
http://list.valvesoftware.com/mailman/listinfo/hlcoders