Jump to content
  • Advertisement
Sign in to follow this  

[Solved] RayPicking Direction Problem C++

This topic is 2861 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi everyone,
I'm starting a new game (that I'm making during my free time, I'd like a Doom1-like)
and my RayPicking has a problem.

I want a FPS camera.
The ray (that I use to check collisions) can't use the mouse screen position because I use
SetCursorPos( GetSystemMetrics( SM_CXSCREEN ) / 2 , GetSystemMetrics( SM_CYSCREEN ) / 2 );

Before that, I used that code : (and it worked till I rotate the view )

void Class1::LaunchTheRayBullet()

D3DVIEWPORT8 viewport;
POINT ptCursor;
GetCursorPos( &ptCursor );
ScreenToClient( g_hWnd, &ptCursor );
mousepos = D3DXVECTOR3(ptCursor.x,ptCursor.y,0);
g_pd3dDevice->GetViewport(&viewport); // OK

v.x = ( ( ( 2.0f * ptCursor.x ) / m_d3dsdBackBuffer.Width ) - 1 ) / m_matProj._11;
v.y = -( ( ( 2.0f * ptCursor.y ) / m_d3dsdBackBuffer.Height ) - 1 ) / m_matProj._22;
v.z = 1.0f;
D3DXMatrixInverse( &m, NULL, &m_matView ); // Get the inverse view matrix

// Transform the screen space pick ray into 3D space
vPickRayDir.x = v.x*m._11 + v.y*m._21 + v.z*m._31;
vPickRayDir.y = v.x*m._12 + v.y*m._22 + v.z*m._32;
vPickRayDir.z = v.x*m._13 + v.y*m._23 + v.z*m._33;


vPickRayOrig.x = m._41; // Perfect (eye/camera position)
vPickRayOrig.y = m._42;
vPickRayOrig.z = m._43;

// calc origin as intersection with near frustum

// And then check collision (I know how to do that with meshes)


My problem : when I move the mouse, the direction doesn't stay where the mouse is pointing at.
I'd like the ray direction to stay on the mouse. (in the center of the screen).

Do you have any suggestion for my matrix/cursor problem?

Share this post

Link to post
Share on other sites
I don't quite understand what's your problem. You mean you have FPS camera and don't know how to raycast from the center of the screen? Well that's simple, just subtract Eye position from LookAt position (Ray = LookAt - Eye) and you'll have ray.

Share this post

Link to post
Share on other sites
Oh thanks Ripiz!

I can't believe it was that simple, in fact the coordinates where the camera looks are just in the
D3DXVECTOR3 g_vLook(0.0f, 0.0f, 1.0f);
It didn't occur to me to look what was that g_vLook vector.

A thousand thanks!

PS : I'm happy biggrin.png

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!