# How to calculate faces for collision?

Hi there,

I have a little assignment which involves collision in a 3D world. I have to create a little simulation of a “pool-table-game”. I’ve already managed it for the balls to collide ( using vectors of course) onto each other.
I’m not allowed to use any of the unity3D physics components. I have to write the “simulation” myself.

The problem is however that I do not understand how the mesh.vertices work in this matter. How do I know which vertex belongs to what face? To me this isn’t very clear, neither in the documentation.
I know how to calculate the normals( I want this to do myself as well ) if I know which vertices to use. But I can not figure out how the faces are calculated.

Afterwards I want to apply Barycentric calculations to see if the ball is intersecting with the faces of my mesh model. Performance is not the key here, as long it will work. I’m not sure if that will work?

Any tips on how to manually find the faces and what technique i could use?

To get the connection between the vertices, use the Mesh.triangles array

http://unity3d.com/support/documentation/ScriptReference/Mesh-triangles.html

The size of this array is always a multiple of 3, since for each polygon there are three vertex indices.

So

``````Mesh.triangles[ 0 ]
Mesh.triangles[ 1 ]
Mesh.triangles[ 2 ]
``````

are the indices of the first face, and

``````Mesh.triangles[ 3 ]
Mesh.triangles[ 4 ]
Mesh.triangles[ 5 ]
``````

the indices of the second, etc.

To get the 3 Vectors that make up the first face you would do the following

``````Vector3 v1 = Mesh.vertices[ Mesh.triangles[ 0 ] ];
Vector3 v2 = Mesh.vertices[ Mesh.triangles[ 1 ] ];
Vector3 v3 = Mesh.vertices[ Mesh.triangles[ 2 ] ];
``````

I havent tested this, but I can imaginge the vertices in Mesh.vertices only contain the positions of the vertices when no rotation / translation is applied. To get from these root positions to the current position of the vertex, you can use

http://unity3d.com/support/documentation/ScriptReference/Transform.TransformPoint.html

or

http://unity3d.com/support/documentation/ScriptReference/Transform.InverseTransformPoint.html