Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Contrary to what I found searching for the answer, I'm not trying to see whether a character is in view of another character. What I need to find out is the size of the field of view. This is because I'm using Mercury Particle Engine in a 3D space, and while MPE emitters take parameters ranging from (0,0) being the upper left corner of the screen to (graphics.PreferredBackBufferWidth, graphics.PreferredBackBufferHeight) being the lower right corner of the screen, the positioning of enemy ships use Vector3 values, where (0,0,0) would be the exact center of the screen. Since my ships don't move along the Z axis, this isn't a problem. What is a problem, however, is translating their coordinates in 3D space to the correct particle emitter positions when I need to show them exploding. This is what I'm currently using to create the perspective field of view:

projectionMatrix = Matrix.CreatePerspectiveFieldOfView(

And this is how I'm attempting to obtain the X and Y values for the particle emitter from the enemy ship position (however, the positions are not quite right with my math because I have the wrong constants):

x = ((float)graphics.PreferredBackBufferWidth/2) + (((float)graphics.PreferredBackBufferWidth/1700) * enemy[i].position.X);

y = ((float)graphics.PreferredBackBufferHeight / 2) + (((float)graphics.PreferredBackBufferHeight / 1100) * enemy[i].position.Y);

That being said, how do I calculate the proper constants from the information on hand?

share|improve this question

Couldn't you just project the positions with your matrix?


Vector4 v4 = new Vector4(enemy[i].position, 1);
Vector4 tmp = new Vector4();

tmp = Vector4.Transform(v4, projection); // x,y in range [-1,1]
float x = (tmp.X+1)/2;
float y = (tmp.Y+1)/2;
share|improve this answer
In the tests I did of this, it seems like it was producing the right Y values, but the X values were completely off. – Fibericon Jul 29 '12 at 13:14
I could be mistaken but I think GraphicsDevice.DisplayMode refers to the entire display, not your window, so when you use GraphicsDevice.DisplayMode.AspectRatioyour projection matrix might not fit your window/screen coordinates/object positions. Maybe try using GraphicsDevice.PresentationParameters.BackBufferWidth and -Height – melak47 Jul 29 '12 at 13:28
Aspect ratio isn't actually the resolution at all. Writing it to console gives me 1.6 – Fibericon Jul 29 '12 at 13:58
Aspect ratio should be width/height, 1920x1200 is 16:10 is 1.6 – melak47 Jul 29 '12 at 14:06
Replacing it with (float)(GraphicsDevice.PresentationParameters.BackBufferWidth) / (float)(GraphicsDevice.PresentationParameters.BackBufferHeight) gave me 1.667, and the same results with the emitter positioning. – Fibericon Jul 29 '12 at 15:00
up vote 0 down vote accepted

I ended up just using trial and error to get the right constant values.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.