public EntityIntersection TestIntersection(Ray iray, Quaternion parentrot)
{
// In this case we're using a sphere with a radius of the largest dimension of the prim
// TODO: Change to take shape into account
EntityIntersection result = new EntityIntersection();
Vector3 vAbsolutePosition = AbsolutePosition;
Vector3 vScale = Scale;
Vector3 rOrigin = iray.Origin;
Vector3 rDirection = iray.Direction;
//rDirection = rDirection.Normalize();
// Buidling the first part of the Quadratic equation
Vector3 r2ndDirection = rDirection*rDirection;
float itestPart1 = r2ndDirection.X + r2ndDirection.Y + r2ndDirection.Z;
// Buidling the second part of the Quadratic equation
Vector3 tmVal2 = rOrigin - vAbsolutePosition;
Vector3 r2Direction = rDirection*2.0f;
Vector3 tmVal3 = r2Direction*tmVal2;
float itestPart2 = tmVal3.X + tmVal3.Y + tmVal3.Z;
// Buidling the third part of the Quadratic equation
Vector3 tmVal4 = rOrigin*rOrigin;
Vector3 tmVal5 = vAbsolutePosition*vAbsolutePosition;
Vector3 tmVal6 = vAbsolutePosition*rOrigin;
// Set Radius to the largest dimension of the prim
float radius = 0f;
if (vScale.X > radius)
radius = vScale.X;
if (vScale.Y > radius)
radius = vScale.Y;
if (vScale.Z > radius)
radius = vScale.Z;
// the second part of this is the default prim size
// once we factor in the aabb of the prim we're adding we can
// change this to;
// radius = (radius / 2) - 0.01f;
//
radius = (radius / 2) + (0.5f / 2) - 0.1f;
//radius = radius;
float itestPart3 = tmVal4.X + tmVal4.Y + tmVal4.Z + tmVal5.X + tmVal5.Y + tmVal5.Z -
(2.0f*(tmVal6.X + tmVal6.Y + tmVal6.Z + (radius*radius)));
// Yuk Quadradrics.. Solve first
float rootsqr = (itestPart2*itestPart2) - (4.0f*itestPart1*itestPart3);
if (rootsqr < 0.0f)
{
// No intersection
return result;
}
float root = ((-itestPart2) - (float) Math.Sqrt((double) rootsqr))/(itestPart1*2.0f);
if (root < 0.0f)
{
// perform second quadratic root solution
root = ((-itestPart2) + (float) Math.Sqrt((double) rootsqr))/(itestPart1*2.0f);
// is there any intersection?
if (root < 0.0f)
{
// nope, no intersection
return result;
}
}
// We got an intersection. putting together an EntityIntersection object with the
// intersection information
Vector3 ipoint =
new Vector3(iray.Origin.X + (iray.Direction.X*root), iray.Origin.Y + (iray.Direction.Y*root),
iray.Origin.Z + (iray.Direction.Z*root));
result.HitTF = true;
result.ipoint = ipoint;
// Normal is calculated by the difference and then normalizing the result
Vector3 normalpart = ipoint - vAbsolutePosition;
result.normal = normalpart / normalpart.Length();
// It's funny how the Vector3 object has a Distance function, but the Axiom.Math object doesn't.
// I can write a function to do it.. but I like the fact that this one is Static.
Vector3 distanceConvert1 = new Vector3(iray.Origin.X, iray.Origin.Y, iray.Origin.Z);
Vector3 distanceConvert2 = new Vector3(ipoint.X, ipoint.Y, ipoint.Z);
float distance = (float) Util.GetDistanceTo(distanceConvert1, distanceConvert2);
result.distance = distance;
return result;
}