-(CC3Vector4)intersectMeshNode:(CC3MeshNode*)node:(CC3Vector)position:(CC3Vector)direction
{
bool success;
CC3VertexLocations* vertices = ((CC3VertexArrayMeshModel*)node.meshModel).vertexLocations;
for(int i=0; i<vertices.elementCount; i=i+3)
{
CC3Vector pos1 = (CC3Vector)([node vertexLocationAt:i]);
pos1 = CC3VectorScale(pos1,node.scale);
CC3Vector pos2 = (CC3Vector)([node vertexLocationAt:i+1]);
pos2 = CC3VectorScale(pos2,node.scale);
CC3Vector pos3 = (CC3Vector)([node vertexLocationAt:i+2]);
pos3 = CC3VectorScale(pos3,node.scale);
CC3Vector4 ret = RayIntersectionTriangle(position,direction,pos1,pos2,pos3,&success);
if(success)return ret;
}
return CC3Vector4Make(0,0,0,0);
}
And the RayIntersectionTriangle-Function:
CC3Vector4 RayIntersectionTriangle(CC3Vector p,
CC3Vector d,
CC3Vector v0,
CC3Vector v1,
CC3Vector v2,
bool* success)
{
float a,f,u,v,t;
CC3Vector e1 = CC3VectorDifference(v1,v0);
CC3Vector e2 = CC3VectorDifference(v2,v0);
CC3Vector h = CC3VectorCross(d,e2);
a = CC3VectorDot(e1,h);
if (a > -0.00001 && a < 0.00001)
{
*success = NO;
return CC3Vector4Make(0,0,0,0);
}
f = 1/a;
CC3Vector s = CC3VectorDifference(p,v0);
u = f * (CC3VectorDot(s,h));
if (u < 0.0 || u > 1.0)
{
*success = NO;
return CC3Vector4Make(0,0,0,0);
}
CC3Vector q = CC3VectorCross(s,e1);
v = f * (CC3VectorDot(d,q));
if (v < 0.0 || u + v > 1.0)
{
*success = NO;
return CC3Vector4Make(0,0,0,0);
}
t = f * (CC3VectorDot(e2,q));
*success = YES;
return CC3Vector4Make(t,u,v,0);
}
Have fun!
-----
Edit: It is important to use a non-sorted triangle-list in the COLLADA->POD-Exporter:
Primitive Type = "Triangle list" (disable "Indexed")
Triangle Sort = "None" (disable "Sort vertices")
That's why:
1. Face = Vertices{0,1,2}
2. Face = Vertices{3,4,5}
3. Face = Vertices{6,7,8}
...