//given a plane and a ray this function determins how far along the ray //an interestion occurs. Returns negative if the ray is parallel public static double DistanceToRayPlaneIntersection(Vector2D RayOrigin, Vector2D RayHeading, Vector2D PlanePoint, //any point on the plane Vector2D PlaneNormal) { double d = -PlaneNormal.Dot(PlanePoint); double numer = PlaneNormal.Dot(RayOrigin) + d; double denom = PlaneNormal.Dot(RayHeading); // normal is parallel to vector if ((denom < 0.000001) && (denom > -0.000001)) { return(-1.0); } return(-(numer / denom)); }
//------------------ isSecondInFOVOfFirst ------------------------------------- // // returns true if the target position is in the field of view of the entity // positioned at posFirst facing in facingFirst //----------------------------------------------------------------------------- public static bool isSecondInFOVOfFirst(Vector2D posFirst, Vector2D facingFirst, Vector2D posSecond, double fov) { Vector2D toTarget = Vec2DNormalize(posSecond - posFirst); return(facingFirst.Dot(toTarget) >= Math.Cos(fov / 2.0)); }
//----------------------------- DoRayCircleIntersect -------------------------- public static bool DoRayCircleIntersect(Vector2D RayOrigin, Vector2D RayHeading, Vector2D CircleOrigin, double radius) { Vector2D ToCircle = CircleOrigin - RayOrigin; double length = ToCircle.Length(); double v = ToCircle.Dot(RayHeading); double d = radius * radius - (length * length - v * v); // If there was no intersection, return -1 return(d < 0.0); }
//--------------------- PointToLocalSpace -------------------------------- // //------------------------------------------------------------------------ public static Vector2D PointToLocalSpace(Vector2D point, Vector2D AgentHeading, Vector2D AgentSide, Vector2D AgentPosition) { //make a copy of the point Vector2D TransPoint = new Vector2D(point.X, point.Y); //create a transformation matrix C2DMatrix matTransform = new C2DMatrix(); double Tx = -AgentPosition.Dot(AgentHeading); double Ty = -AgentPosition.Dot(AgentSide); //create the transformation matrix matTransform._11(AgentHeading.X); matTransform._12(AgentSide.X); matTransform._21(AgentHeading.Y); matTransform._22(AgentSide.Y); matTransform._31(Tx); matTransform._32(Ty); //now transform the vertices matTransform.TransformVector2D(TransPoint); return(TransPoint); }
//-------------------------- GetRayCircleIntersec ----------------------------- public static double GetRayCircleIntersect(Vector2D RayOrigin, Vector2D RayHeading, Vector2D CircleOrigin, double radius) { Vector2D ToCircle = CircleOrigin - RayOrigin; double length = ToCircle.Length(); double v = ToCircle.Dot(RayHeading); double d = radius * radius - (length * length - v * v); // If there was no intersection, return -1 if (d < 0.0) { return(-1.0); } // Return the distance to the [first] intersecting point return(v - Math.Sqrt(d)); }
public static Enum_Span_Type WhereIsPoint(Vector2D point, Vector2D PointOnPlane, //any point on the plane Vector2D PlaneNormal) { Vector2D dir = PointOnPlane - point; double d = dir.Dot(PlaneNormal); if (d < -0.000001) { return(Enum_Span_Type.plane_front); } else if (d > 0.000001) { return(Enum_Span_Type.plane_backside); } return(Enum_Span_Type.on_plane); }
//------------------ isSecondInFOVOfFirst ------------------------------------- // // returns true if the target position is in the field of view of the entity // positioned at posFirst facing in facingFirst //----------------------------------------------------------------------------- public static bool isSecondInFOVOfFirst(Vector2D posFirst, Vector2D facingFirst, Vector2D posSecond, double fov) { Vector2D toTarget = Vec2DNormalize(posSecond - posFirst); return facingFirst.Dot(toTarget) >= Math.Cos(fov / 2.0); }
//given a plane and a ray this function determins how far along the ray //an interestion occurs. Returns negative if the ray is parallel public static double DistanceToRayPlaneIntersection(Vector2D RayOrigin, Vector2D RayHeading, Vector2D PlanePoint, //any point on the plane Vector2D PlaneNormal) { double d = -PlaneNormal.Dot(PlanePoint); double numer = PlaneNormal.Dot(RayOrigin) + d; double denom = PlaneNormal.Dot(RayHeading); // normal is parallel to vector if ((denom < 0.000001) && (denom > -0.000001)) { return (-1.0); } return -(numer / denom); }
//--------------------- PointToLocalSpace -------------------------------- // //------------------------------------------------------------------------ public static Vector2D PointToLocalSpace(Vector2D point, Vector2D AgentHeading, Vector2D AgentSide, Vector2D AgentPosition) { //make a copy of the point Vector2D TransPoint = new Vector2D(point.X, point.Y); //create a transformation matrix C2DMatrix matTransform = new C2DMatrix(); double Tx = -AgentPosition.Dot(AgentHeading); double Ty = -AgentPosition.Dot(AgentSide); //create the transformation matrix matTransform._11(AgentHeading.X); matTransform._12(AgentSide.X); matTransform._21(AgentHeading.Y); matTransform._22(AgentSide.Y); matTransform._31(Tx); matTransform._32(Ty); //now transform the vertices matTransform.TransformVector2D( TransPoint); return TransPoint; }