0

I have a background image on a Direct3D canvas. I am drawing a circle at a specific position on the background. After the canvas is zoomed/panned with a camera (using world/view/projection), the background is updated.

Now the problem is, I can't find the previous position on the image to redraw the circle. I'm using the following function (DirectX 11.1) to find the coordinate. But it's not working.

XMVECTOR Camera::Unproject(Windows::Foundation::Rect viewPort, Windows::Foundation::Point location)
{
XMFLOAT3 worldPosition(location.X, location.Y, 0.0f);
XMVECTOR worldVector = XMLoadFloat3(&worldPosition);
return XMVector3Unproject(worldVector, viewPort.X, viewPort.Y, viewPort.Width, viewPort.Height, 0.0f, 1.0f, this->Projection(), this->View(), this->World());   
}

I even tried with XMMatrixIdentity as the world. But no success. Can anyone please help me?

Liton
  • 1,398
  • 2
  • 14
  • 27
  • Why do you need to redraw the circle? as you said, the background will updated based on camera move, thus the circle on the background will update(zoom in/out) automatically. – zdd Oct 17 '12 at 03:28
  • @zdd The color and texture of the circle need to be changed with new zoom factor. – Liton Oct 17 '12 at 04:19
  • then just draw it as where it is before and change the color and texture, since you are moving the camera, the camera only change your view, it does not change the orientation of the circle. you don't need to calculate the new center for it. – zdd Oct 17 '12 at 05:51

1 Answers1

1

This is a non-trivial problem with the information you provided.

A 2D-point on the screen cannot be mapped to one 3D-point in the scene. Every 2D-point maps to a whole ray. Therefore, if you draw a circle on a specific 2D-position, the circle can be placed on various positions in the scene. That's probably why you get some strange looking results.

If you move the camera, the ray will probably become "more visible". I.e. it will not map to a single 2D-point, but a 2D-ray. And your circle is placed somewhere on this ray. You see, there is no way to get the new position of the circle with the provided information. You can only say that it is somewhere on a certain ray.

But perhaps you can specify the circle's position. Maybe it is placed on an object, on a virtual plane or something like that. Then this problem is solveable.

First, you have to find the 3D-position of the circle. Use XMVector3Unproject twice. Once with a z-coordinate of 0 and once with 1. This will give you a point very close to the camera and one that is far away. This is your ray. You then have to compute the position of the circle on that ray. E.g. by intersecting the ray with a plane. This 3D-position can be projected back onto the screen (XMVector3Project) with the new camera parameters and defines the new circle position.

And yes, you should probably define the Identity matrix as the world matrix.

Nico Schertler
  • 32,049
  • 4
  • 39
  • 70