Game Development Reference
In-Depth Information
For jigLib.JTriangleMesh colliders, if a scene object is associated with a
JTriangleMesh collider, then you can select a triangle/polygon in a scene
object. Let's look at the constructor of JTriangleMesh :
JTriangleMesh=function(skin, maxTrianglesPerCell,
minCellSize)
this.jigLibObj=new jigLib.JTriangleMesh(null, 100, 0.1);
this.jigLibObj.createMesh(triangles.verts,
triangles.faces);
The constructor of the JTriangleMesh collider takes a hint of the size of an object
by taking maxTrianglesPerCell and mincellSize parameters. This class has a
function called createMesh which takes triangles.verts and triangles.faces
as parameters. These parameters refer to the vertices and indices in the mesh. So
basically, a JTriangleMesh collider knows the exact shape and size of the scene
object, but it is very slow. It basically checks each triangle in the mesh for collision.
Even though it is precise, it certainly is computationally expensive. However, we
can use the JTriangleMesh.prototype.segmentIntersect=function(out,
seg, state) function of the class to get the exact triangle that our ray/segment
intersected. This method of finding an intersection is precise but slow.
We can use any type of collider in our scene for picking, but remember that we have
to choose between precision and performance.
Implementing picking using ray casting
Let's now start implementing what we have discussed. The algorithm we will apply
is as follows:
1.
Create a rigid body for each scene object.
2.
Calculate the click coordinates of the canvas, convert these coordinates to
normalized device coordinates, and create a vector.
3.
"Unproject" the vector using the camera projection matrix.
4.
Create a ray segment using the vector.
5.
Check for an intersection.
6.
If an intersection is found, then change the color of the selected object.
 
Search WWH ::




Custom Search