Jump to content
  • Advertisement
Sign in to follow this  
FoDDa

C# DirectX - World Coordinates to Screen Coodinates for sprite overlay

This topic is 3632 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

Aloha all I'm having trouble finding a good method for converting world space coordinates into screen space coordinates. My situation is this: When a players projectile hits an object, I want a cloud of smoke (my sprite) to appear on screen at the point of intersection. I don't need fancy scaling code or anything, as my game has a 2.5D overhead view point, and I already know my intersection point. I just need to convert it (a vector3) to it's on screen coordinates. I've been browsing the net most of the evening, and most of the guides I found were for raytracing a point from screen space to world space. Any others were overcomplicated, or contradicted with other "useful" articles I had found. I did find a useful article on the D3DXVec3Project function, but I don't believe I can use it in c#. I also found some info on multiplying my world position by my wvp matrix, but it wasn't clear on the actual maths involved. Can anybody help me out? Cheers all [Edited by - FoDDa on July 10, 2008 8:32:17 AM]

Share this post


Link to post
Share on other sites
Advertisement
It's pretty simple:


Vector2 WorldSpaceToScreenSpace(Vector3 worldSpacePos)
{
Vector4 clipSpacePos = Vector4.Transform(new Vector4(worldSpacePos, 1.0f), viewProjMatrix);
Vector2 screenSpacePos;
screenSpacePos.x = (clipSpacePos.x / clipSpacePos.w) * 0.5f + 0.5f;
screenSpacePos.y = 1.0f - ((clipSpacePos.y / clipSpacePos.w) * 0.5f + 0.5f);
screenSpacePos.x *= viewPortWidth;
screenSpacePos.y *= viewPortHeight;
return screenSpacePos;
}





[Edited by - MJP on July 10, 2008 2:02:11 PM]

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.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!