public bool intersects( Ray ray )
{
// Only check if portal is open
if ( mOpen )
{
if ( mType == PORTAL_TYPE.PORTAL_TYPE_QUAD )
{
// since ogre doesn't have built in support for a quad, I'm going to first
// find the intersection point (if any) of the ray and the portal plane. Then
// using the intersection point, I take the cross product of each side of the portal
// (0,1,intersect), (1,2, intersect), (2,3, intersect), and (3,0,intersect). If
// all 4 cross products have vectors pointing in the same direction, then the
// intersection point is within the portal, otherwise it is outside.
IntersectResult result = ray.Intersects( mDerivedPlane );
if ( result.Hit )
{
// the ray intersects the plane, now walk around the edges
Vector3 isect = ray.GetPoint( result.Distance );
Vector3 cross, vect1, vect2;
Vector3 cross2, vect3, vect4;
vect1 = mDerivedCorners[ 1 ] - mDerivedCorners[ 0 ];
vect2 = isect - mDerivedCorners[ 0 ];
cross = vect1.Cross( vect2 );
vect3 = mDerivedCorners[ 2 ] - mDerivedCorners[ 1 ];
vect4 = isect - mDerivedCorners[ 1 ];
cross2 = vect3.Cross( vect4 );
if ( cross.Dot( cross2 ) < 0 )
{
return false;
}
vect1 = mDerivedCorners[ 3 ] - mDerivedCorners[ 2 ];
vect2 = isect - mDerivedCorners[ 2 ];
cross = vect1.Cross( vect2 );
if ( cross.Dot( cross2 ) < 0 )
{
return false;
}
vect1 = mDerivedCorners[ 0 ] - mDerivedCorners[ 3 ];
vect2 = isect - mDerivedCorners[ 3 ];
cross = vect1.Cross( vect2 );
if ( cross.Dot( cross2 ) < 0 )
{
return false;
}
// all cross products pointing same way, so intersect
// must be on the inside of the portal!
return true;
}
return false;
}
else if ( mType == PORTAL_TYPE.PORTAL_TYPE_AABB )
{
AxisAlignedBox aabb = new AxisAlignedBox( mDerivedCorners[ 0 ], mDerivedCorners[ 1 ] );
IntersectResult result = ray.Intersects( aabb );
return result.Hit;
}
else // sphere
{
IntersectResult result = ray.Intersects( mDerivedSphere );
return result.Hit;
}
}
return false;
}