# Screen to World Ray

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

## Recommended Posts

Hi guys,

I'm trying to raycast from the center of the screen towards anything in front of the camera.

My goal is that I make the player able to shoot enemies according to the "+" symbol in the center of the screen.

How can I get D3DXVECTOR3 of the center of the screen?

Any help would be appreciated.

[attachment=12578:ray.jpg] Edited by Medo3337

##### Share on other sites
Use D3DXIntersect() maybe?

##### Share on other sites
I use Bullet physics raytest, how do I get the 3D position that appear exactly infront of the camera (at the center of the screen)?

I'm trying to get the 3D position of the symbol "+" so I can raycast from it. Edited by Medo3337

##### Share on other sites
Assuming that in the coordinate system you are using Y is up, X is to the right and Z is forward, the camera forward vector is (X=0, Y=0, Z=1) in view space. You just multiply this by the view-to-world matrix to get the vector in world space. Remember to set W to 0 since this is a direction rather than a point. The origin for the ray would be (X=0, Y=0, Z=0) in view space. To get that into world space you do the same but with W as 1. Then you can just scale the direction vector with something large like 10 000 to get a really long ray vector. If I remember correctly, these two vectors (ie. a ray origin and direction) is what Bullet expects.

##### Share on other sites
@GuyWithBeard: I have the point of the cursor which is x, y, from this point how can I get the x, y, z in the 3D world? the idea is the same like picking 3D mesh, you get the 3D world from the cursor x, y cursor point and raycast from this position to for example 1000.0f forward (1000.0f is the distance). Edited by Medo3337

##### Share on other sites
Is your crosshair in the middle of the screen (or to be more exact in the middle of the view)?. In that case you don't need to work with 2D coordinates at all. The ray will originate from the view space origin (where your camera is) and go straight forward in the direction the camera is facing.

##### Share on other sites
Here is what I want to do, I am trying to do two things:
1. Allow the player to shoot the enemy raycast(centerOfScreenWorld.x, centerOfScreenWorld.y, centerOfScreenWorld.z, 1000.0f); // 1000.0f = distance
How can I get centerOfScreenWorld?

2. This is something completely different (picking mesh), it should be something like pickMesh(cursorPoint.x, cursorPoint.y, 500.0f); // 500.0f = distance
pickMesh should return information about the picked mesh (in other word mesh that was hit by the ray), How can I raycasting from cursorPoint to the distance? Edited by Medo3337

##### Share on other sites
1. The center of the screen = the center of the near plane. This would be (X=0, Y=0, Z=NearPlaneDistance) in view space. Use what I told you before to get into world space.

Update: actually that's not right. The "center of the screen" would probably be the camera position, not the near plane. My bad.

2. http://halogenica.net/ray-casting-and-picking-using-bullet-physics/ That article should give you all you need.

##### Share on other sites
Nabbed this from dxtut
 // get the current transform matrices D3DXMATRIX matProjection, matView, matWorld, matInverse; d3ddev->GetTransform(D3DTS_PROJECTION, &matProjection); d3ddev->GetTransform(D3DTS_VIEW, &matView); d3ddev->GetTransform(D3DTS_WORLD, &matWorld); // use the mouse coordinates to get the mouse angle GetCursorPos(&MousePos); float xAngle = (((2.0f * MousePos.x) / SCREEN_WIDTH) - 1.0f) / matProjection(0, 0); float yAngle = (((-2.0f * MousePos.y) / SCREEN_HEIGHT) + 1.0f) / matProjection(1, 1); D3DXVECTOR3 origin, direction; origin = D3DXVECTOR3(0.0f, 0.0f, 0.0f); direction = D3DXVECTOR3(xAngle, yAngle, 1.0f); // find the inverse matrix D3DXMatrixInverse(&matInverse, NULL, &(matWorld * matView)); // convert origin and direction into model space D3DXVec3TransformCoord(&origin, &origin, &matInverse); D3DXVec3TransformNormal(&direction, &direction, &matInverse); D3DXVec3Normalize(&direction, &direction); 

##### Share on other sites
Focusing on number 1 (player shooting raycasting) I tried doing something close to what you said, but it's not working as expected, can you show an example?

1. 1
Rutin
49
2. 2
3. 3
4. 4
5. 5

• 11
• 16
• 9
• 10
• 13
• ### Forum Statistics

• Total Topics
633003
• Total Posts
3009846
• ### Who's Online (See full list)

There are no registered users currently online

×

## Important Information

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!