zomgistania

Main page | About | Articles | Previous Posts | Archives

Tuesday, June 20, 2006

Finding the location of a point in a certain Z depth

I've been trying to solve a problem for my GUI code...

If I unproject certain coordinates to the 3D space, I get a 3D Vector. Now, how to find out what that point would be in, let's say -5 Z instead of 0 Z?

I was kind of lost for a while about how the point could be found, but now I've finally found an answer!


//Let's say we want point 100 100

Vector3 point = new Vector3(100, 100, 0);

 

//Create near and far vectors

Vector3 near = new Vector3(point.X, point.Y, 0);

Vector3 far = new Vector3(point.X, point.Y, 1);

 

//Unproject them

near.Unproject(device.Viewport, device.Transform.Projection,

    device.Transform.View, device.Transform.World);

far.Unproject(device.Viewport, device.Transform.Projection,

    device.Transform.View, device.Transform.World);

 

//Create a plane at the z-depth (-5) we want to know the position           

Plane zplane = Plane.FromPointNormal(

    new Vector3(0, 0, -5), new Vector3(0, 0, 1));

 

//Intersect the line going from near point to far

//to get the result

Vector3 resultPoint = Plane.IntersectLine(zplane, near, far);



The result obviously depends on the viewport, projection and such of your device.

Ah well, now I can finally start thinking about the GUI placement.

Getting the GUI in some kind of shape would allow me to do lots of new things, like unit detail sheets, building new units (which was in with a very crap looking GUI), Attacking etc. so it's pretty vital to get it working properly!

And for the sake of mentioning other things, I've recently felt like reinstalling Sim City 4... doing that might be dangerous since I could forget anything about coding and just build a better city for my sims to live in... :]

It also looks like I might actually get something meaningful to do for this summer... I visited the company I do freelance-coding for yesterday and we had some talks about starting working on a Flash-project for a client. Would take the rest of the summer to work on that, and I'd get paid too, unlike when coding my own things ;)... Also, as I don't know Flash very well, I could also learn a bit of that though I will be mainly working on the ActionScript side of things.

Labels:

0 Comments:

Post a Comment

<< Home