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?


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;
  • \$\begingroup\$ In the tests I did of this, it seems like it was producing the right Y values, but the X values were completely off. \$\endgroup\$ – Fibericon Jul 29 '12 at 13:14
  • \$\begingroup\$ 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 \$\endgroup\$ – user13213 Jul 29 '12 at 13:28
  • \$\begingroup\$ Aspect ratio isn't actually the resolution at all. Writing it to console gives me 1.6 \$\endgroup\$ – Fibericon Jul 29 '12 at 13:58
  • \$\begingroup\$ Aspect ratio should be width/height, 1920x1200 is 16:10 is 1.6 \$\endgroup\$ – user13213 Jul 29 '12 at 14:06
  • \$\begingroup\$ Replacing it with (float)(GraphicsDevice.PresentationParameters.BackBufferWidth) / (float)(GraphicsDevice.PresentationParameters.BackBufferHeight) gave me 1.667, and the same results with the emitter positioning. \$\endgroup\$ – Fibericon Jul 29 '12 at 15:00

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


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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