After I accidentally solved that problem and was able to reproduce fix for it, I wrote another testing application - for those who had that privilege to see it: yes the SimpleTest - this time I had working 3D concept, simple square with editable corners. Application was able to track mouse cursor, project ray through space and search for possible vertex intersections, applied when found first one in vertex array. Once found, this vertex was highlighted and it was possible to change its Y-coordinate (height) by simple pressing left or right mouse button. Simple and nice concept.
It would be extremely easy to create sime, using simple VertexBuffer and IndexBuffer to define map-grid vertices (and some 3D-points representing grid nodes). Yes, I was lazy and had no time (school) to do that. So no progress was done.
Rays in space
I think it will cause no harm if I provide some help to those who do not know 3D-space analytic geometry. I am going to talk about projecting rays and searching for intersections with points in space. It is rather simple, although I will start with some theory.
Having 3D world is one thing; having it rendered into 2D is another thing. Obviously, if we put our cursor on this transformed 2D output, we will never be able to reproduce where exactly our cursor points. However we can reproduce ray between 2 points; one in near plane and one in far plane (near and far planes define distances from camera where we can see - things beyond these are not rendered). So we have this ray that defines where our cursor points. Nice.
We could do some math with Matrices to get these points, but DirectX provides some functionality to do this. Following code example should describe how to have it done.
Once we know points, beginning and end of ray, we only have to search for points laying on it. To do this, we have to know little of analytic geometry - ray equation. Ray equation has many forms, but we are interested in vector-expressed form, which basicly looks like this:// Z-coordinate specifies plane in this case // 0 is near plane, 1 is far plane Vector3 pointA = new Vector3(mouse.X, mouse.Y, 0.0f); Vector3 pointB = new Vector3(mouse.X, mouse.Y, 1.0f); // Method Vector3.Unproject gives us points space, on near and far plane, as we wanted pointA.Unproject(device.Viewport, device.Transform.Projection, device.Transform.View, device.Transform.World); pointB.Unproject(device.Viewport, device.Transform.Projection, device.Transform.View, device.Transform.World);
If
cx
, cy
and cz
are equal - point is part of the ray, thus point we are searching for. This is nice, however there is little problem - in real situation, these number will never be equal. They will always differ a little - it is up to you, how big difference you wish to tolerate.Yes, one more thing - ray direction vector is ray origin subtracted from ray end. Easy, is not it?
A question mark simply wouldn't suffice in this situation. All I can say is keep it up, you grow more and more smart and I understand less and less of what you're saying with each new project. But hey, it takes nothing less than a genious to create a working silkroad modifier right? :p
ReplyDeleteGood job.
i came from Eckoro's blog and let me tell you that work is just amazing.
ReplyDeletekeep it up please.
regards.