Game Development Reference
In-Depth Information
The following code shows an intersect variation with improved accuracy.
private bool IntersectRayMesh(Entity entity, int x, int y)
PickingRay ray = ComputePickingRay(entity, x, y);
return entity.Mesh.Intersect(ray.Origin, ray.Direction);
Using Built-In D3DX Functionality
Reinventing the wheel is generally frowned upon, but a developer can always argue
that he would rather reinvent the wheel in some cases if it means he will walk away
understanding the mechanics of the solution at a lower level. This chapter has dis-
cussed the math and implementation behind converting a screen space coordinate
into world space, as well as computing a picking ray that can be used to perform
intersection tests against objects. I then went on to showing an improvement to
the intersection tests using built-in functionality of the Direct3D Mesh class. There
is actually enough built-in functionality with Managed Direct3D to implement a
full picking solution with only a few lines of code.
The Vector3 class has a method called Unproject that can be used to project a vec-
tor from screen space into world space. We can make two vectors that represent the
near and far clipping planes (Z = 0 and 1, respectively), unproject both of them,
and then subtract the near vector from the far vector to produce a picking ray suit-
able for Mesh.Intersect() .
The following code shows this.
private bool IntersectUnprojectMesh(Entity entity, int x, int y)
Vector3 near;
Vector3 far;
near = new Vector3(x, y, 0);
far = new Vector3(x, y, 1);
Matrix world = Matrix.Identity;
Search Nedrilad ::

Custom Search