zomgistania

Main page | About | Articles | Previous Posts | Archives

Friday, May 05, 2006

Picking up stuff from 3D-space with mouse rays

Okay so I finally have it figured out thanks to the nice people at microsoft.public.win32.programmer.directx.managed newsgroup.

I'll just post the code here too in case someone else might someday run into a similar problem, and to be honest, there are no good examples of doing this unless you're using a Mesh object.


//Checks if point P is inside the plane
internal bool PointIntersects(Point p, Device d, out IntersectInformation intersectInfo)
{
//Create near and far vectors from the Point location
Vector3 near = new Vector3(p.X, p.Y, 0);
Vector3 far = new Vector3(p.X, p.Y, 1);

//Unproject the vectors into world coordinates
near.Unproject(d.Viewport,
d.Transform.Projection,
d.Transform.View, Matrix.Translation(location));
far.Unproject(d.Viewport,
d.Transform.Projection,
d.Transform.View, Matrix.Translation(location));

//Calculate the direction of the ray from the vectors
Vector3 dir = Vector3.Subtract(far, near);

//Set up the locations of the vertices
//in the first triangle in the quad
Vector3 loc1 = location; //Bottom left
Vector3 loc2 = new Vector3(location.X,
location.Y + size.Height, location.Z); //top left
Vector3 loc3 = new Vector3(location.X+size.Width,
location.Y, location.Z); //bottom right


//check for intersection
bool intersection = Geometry.IntersectTri(loc1, loc2, loc3,
near, dir, out intersectInfo);

//If we didn't find an intersection in the first triangle,
//check the second
if (!intersection)
{
//Set loc1 to the coordinate of the top right vertex
loc1 = new Vector3(location.X + size.Width,
location.Y + size.Height, location.Z);

//Check the other triangle
intersection = Geometry.IntersectTri(loc2, loc1, loc3,
near, dir, out intersectInfo);
}

return intersection;
}


I used that inside my Plane class which is used for rendering 2D-like quads.

1 Comments:

  • near and far are both on a straight
    line from the camera origin

    so you can drop the near from this and get the same result (ie: a direction vector from the camera)

    Vector3 dir = new Vector3(p.X, p.Y, 1);

    //Unproject the vectors into world coordinates
    dir.Unproject(d.Viewport,
    d.Transform.Projection,
    d.Transform.View, Matrix.Translation(location));

    same result

    By Anonymous Anonymous, at 4:31 PM  

Post a Comment

<< Home