NickMcCrea

Hi all,

I'll get right to the point. My camera is defined using the following code...

cameraworld = Matrix.CreateTranslation(xplane, yplane, 0) * Matrix.CreateScale(zoom) * Matrix.CreateRotationZ(angle);

view = cameraworld * Matrix.CreateLookAt(new Vector3(0, 0, 100), Vector3.Zero, Vector3.Up);

projection = Matrix.CreatePerspectiveFieldOfView(MathHelper.ToRadians(45),(float)graphics.GraphicsDevice.Viewport.Width / (float)graphics.GraphicsDevice.Viewport.Height,1.0f, 1000f);

effect.View = view;

effect.Projection = projection;

I've also got code to allow me to place objects in the game world, after unprojecting the mouse points and finding the point of intersection with my XY plane. Only problem is, it doesn't work after I translate the camera, controlled via keyboard input into the xplane/yplane variables above. The mouse unprojection seems to take no account of any translation. Scaling and rotation works just fine.

Relevant mouse code here - I've left out the ray generation etc for brevity, but :

mState = Mouse.GetState();

int mouseX = mState.X;

int mouseY = mState.Y;

Vector3 nearsource = new Vector3((float)mouseX, (float)mouseY, 0f);

Vector3 farsource = new Vector3((float)mouseX, (float)mouseY, 1f);

Matrix world = Matrix.CreateTranslation(0,0,0);

Vector3 nearPoint = graphics.GraphicsDevice.Viewport.Unproject(nearsource, camera.effect.Projection, camera.effect.View, world);

Vector3 farPoint = graphics.GraphicsDevice.Viewport.Unproject(farsource, camera.effect.Projection, camera.effect.View, world);

What is going wrong here with the unprojection Why does my code seem to ignore translation, when rotation and scaling is accounted for In other words, if I translate the camera position, new objects are placed when I click as though no translation had taken place. Yet scale and rotation works as intended...



Re: XNA Game Studio Express 3D picking with the mouse combined with a moving camera...

Derek Nedelman

The first thing I'd look at is the way you're composing cameraworld in your first line of code. I would put the CreateTranslation() last.





Re: XNA Game Studio Express 3D picking with the mouse combined with a moving camera...

NickMcCrea

Doesn't solve the problem, I'm afraid...the unproject still takes no account of camera translation.



Re: XNA Game Studio Express 3D picking with the mouse combined with a moving camera...

dczraptor

You'll need to set the World Matrix to something else. Right now, I think you're just detected when the camera is centered at 0,0,0. From the MSDN article:

Using Viewport.Unproject determine points in world space on the near and far clip plane. For the point on the near plane pass a source vector with x and y set to the mouse position and z set to 0. For the point on the far plane pass a source vector with x and y set to the mouse position and z set to 1. For both points pass Unproject the current projection matrix and view matrix and a translation matrix for the point (0,0,0).

I can't be sure, because i haven't actually tried it. I swear there used to be a different method to do this on the article. I wonder what happened to it; I know that method worked.






Re: XNA Game Studio Express 3D picking with the mouse combined with a moving camera...

Kyle_W

Nick,

This sounds very similar to two threads from svxtc. Are you guys working on the same project Anyway, you may find something on those threads helpful:

http://forums.microsoft.com/MSDN/ShowPost.aspx PostID=1221214&SiteID=1&mode=1

http://forums.microsoft.com/MSDN/ShowPost.aspx PostID=1220805&SiteID=1

Kyle





Re: XNA Game Studio Express 3D picking with the mouse combined with a moving camera...

svxtc

Thats pretty funny Kyle_W. We are not on the same project but I am also monitoring this page for anything that might help.



Re: XNA Game Studio Express 3D picking with the mouse combined with a moving camera...

NickMcCrea

I think I know svxtc's exact problem, but it's different from mine...I think he's doing the unproject of the mouse position, and then using the farPoint of his Ray to set the X, and Y coordinates of his 3D object, not the exact point of the intersect with his plane of movement. That's why he gets more inaccurate the further from the origin...I'll try and draw an ASCI diagram :)

My problem is I think somehow about how I generate my camera translation. I'm very accurate in mouse unprojection, and it perfectly takes account of camera rotation and scaling (so I can keep the mouse still, move the camera in these ways, and my 3D object still moves as I expect it to, i.e. the mouse has 'moved' in 3D space even though I've not moved it. Translation just doesn't work.

He's using two vectors, nearPoint and farPoint, say, to draw a Ray from nearPoint in the direction of farPoint.

x nearPoint
|<------ angle theta
| \
| \ distance Dsmall
| \
____ \x Intersect with XY plane
| \
| \ Dbig
| \
| \
________ \_x farPoint


If, as I suspect, he's doing the ray /bounding box intersection to detect a mouse ray/ 3d object collision, he's then setting the position of his object to be (FarPoint.X, farPoint.Y, 0). This gets more inaccurate the further away from the origin you are, because angle theta is getting bigger.

You need to set the position of the your 3D object to be the X,Y coords of the point of intersect, not the XY of farPoint. So you need to know distance Dsmall, which you can work out using the ratios of the two right angled triangles in the diagram above (you should have all the info you need with the coordinates of farPoint and nearPoint).

At least, I think that's his problem :).





Re: XNA Game Studio Express 3D picking with the mouse combined with a moving camera...

NickMcCrea

dczraptor, that was the first thing I tried. I tried setting the world matrix for the unproject to the current camera xy position....

Matrix world = Matrix.CreateTranslation(camPos)

Seems to have absolutely zero effect. It's a doozie, all right. I must be doing something daft and simple...




Re: XNA Game Studio Express 3D picking with the mouse combined with a moving camera...

NickMcCrea

Any ideas anyone I've rewritten my whole camera class with no results...

Why does the mouse Ray/bounding box intersection take no account of camera translation I've got a camera facing down the Z axis that I can zoom (using a scaling matrix), rotate around Z and translate, I'm not mucking about with anything else - so imagine a simple RTS, face-the-ground camera...It works exactly as I intend it.

The 3D picking works exactly as I intend - EXCEPT after a translation of the camera, at which point the point I'm 'picking' remains untranslated. I've tried everything I can think of, but nothing seems to work.






Re: XNA Game Studio Express 3D picking with the mouse combined with a moving camera...

Kyle_W

Nick,

When you used the camera world matrix in the unprojection there MUST have been something that was different. Could it be that the translation happened in the opposite direction Another thought is that maybe you need to apply the world translation to the nearPoint and farPoint after the unprojection. As a last resort you could try building the sample unproject project from the docs and see if you can even get that sample to work with a translated camera. If you can then you at least know that the sample project is valid and you can begin dissecting it to find differences with your project.





Re: XNA Game Studio Express 3D picking with the mouse combined with a moving camera...

NickMcCrea

Fixed it, thank Christ, with some help. We had to re-implement the camera from the ground up, and then translate the intersection point after the unproject. Was never too far away from the right answer, I think, but just had to go through it logically step by step.

Thanks Kyle_W