开发者

How can I translate the mouse coordinates to 3D coordinates, with a overhead camera, in XNA 4.0?

So I am making a game in XNA 4.0 and I am having an issue with translating the coordinates from the mouse to the 3D world. I have used the Viewport.Unproject() method, and it almost works. The issue is that my projection is a "field of view" so the distance away from the center axises i开发者_如何学Gos exponential. If I change the projection to be a standard perspective than my 3D objects are deformed. Is there a mathematical fix for using the field of view with the translated data from the mouse coordinates?

I am currently doing some trig. to calculate the width and height of the far plane, based on the camera position, the width & height of the viewport, and the fieldOfView angle. Is there a better method? This is almost dead on.


i have the same problem, i just didn't understand the technical explanation of it until i read your question. What i did is Translate the origin of mouse coordinates & then multiple or divide it by a factor so that small mouse movements could be magnified on a larger scale which is far behind,

Tough i didn't understand my problem is of perspective & field of view, that is the mouse is moving on screen or camera lens, and the scale i am trying to move is far behind and larger, still i did the maths to make it work. just because i saw the exponential scaling difference, is simple

This is what i did

translated-X =  { mouse-X + (screenwidth/2) } / 2.7;
translated-Y =  {-mouse-Y + (screenHeight/2)} / 2.8;

and method with current values Translates well for my current settings, which are

  • camera is at (0,0,260)
  • for every item i have coordinates (x,y,0),so they're lying at X&Y plane
  • my screen resolution,PreferredBackBufferWidthxHeight is 1024 x 768

only for the above three conditions my equation works.

but if change any of the above values i will have to adjust the equations according to the scenarios, so these hard-coded values 2.7 & 2.8 will have to variables & will have to be calculated according to the position of Camera & Screen Resolution

this 2.7 & 2.8 holds the scale till a certain point, now

The Generic Formula for Translation, i could formulate that, by technique called Numerical Analysis, i am good at that but for that i need data that i can not get by myself, i need to find out the camera distance, perspective settings at which certain resolution is fully in the field of view.

That is, if my resolution is 1024x768, from how much distance of camera while looking at origin, we actually get to see 1024 points around x-axis and 768 points around y-axis. similarly i need at-least two more readings for higher resolutions.

And i cant do get these values because my stupid old monitor supports at max 1024x768, i cant go any higher. If you can get me camera distance for multiple resolutions, i will formulate a relation and come up with a generic formula.


http://create.msdn.com/en-US/education/catalog/sample/picking_triangle

there you go. Cursor is Implemented in this Official Code Sample from Microsoft. The project is not specifically for cursor but the solution is with in

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜