smernesto wrote:HI,
anyone has an example for using the code?
I need to know what entity the ray hit.
RaycastFromPoint as displayed on the wiki returns you true if an object
was hit by the ray specified by the parameters 'point' and 'normal' (latter
would be better called 'direction'). In the parameter list are also two (Mogre version) results, 'result' (better 'position') and 'resNormal', which in case true has been returned contain the coordinate where the ray intersects the mesh,
and the normal vector for the surface at that point. To get the name of
the entity that was hit you would need to add a third result parameter,
and set it thus:
Code: Select all
if (ncf > -1)
{
closest_result = ray.GetPoint(closest_distance);
// if you don't need the normal, comment this out; you'll save some CPU cycles.
Vector3 v1 = vertices[indices[ncf]] - vertices[indices[ncf + 1]];
Vector3 v2 = vertices[indices[ncf + 2]] - vertices[indices[ncf + 1]];
vNormal = v1.CrossProduct(v2);
name = pentity.Name; // THIS HERE NEW LINE
}
My version does this and more, I could clean it up a bit and update the Wiki page. Soon
The general Raycast test using this function would be something like:
Code: Select all
Vector3 point = Vector3.ZERO;
Vector3 direction = -Vector3.UNIT_Z;
Vector3 pos = Vector3.ZERO;
Vector3 norm = Vector3.ZERO;
String name = "";
if (RaycastFromPoint(point, direction, ref pos, ref norm, ref name))
{ // an object was hit by the ray
Console.WriteLine(name + " was hit at " + pos.ToString() + ". Normal: " + norm.ToString());
} else
Console.WriteLine("No object was hit by the ray");