与直线相交的垂直射线

Vertical Ray with Line Intersection

我正在 java 制作 2D 游戏,我正在使用 2D 光线投射阴影。我使用此算法找到我在此处找到的射线和线之间的交点 How do you detect where two line segments intersect?

// Returns 1 if the lines intersect, otherwise 0. In addition, if the lines 
// intersect the intersection point may be stored in the floats i_x and i_y.
char get_line_intersection(float p0_x, float p0_y, float p1_x, float p1_y, 
float p2_x, float p2_y, float p3_x, float p3_y, float *i_x, float *i_y)
{
   float s1_x, s1_y, s2_x, s2_y;
   s1_x = p1_x - p0_x;     s1_y = p1_y - p0_y;
   s2_x = p3_x - p2_x;     s2_y = p3_y - p2_y;

   float s, t;
   s = (-s1_y * (p0_x - p2_x) + s1_x * (p0_y - p2_y)) / (-s2_x * s1_y + s1_x * s2_y);
   t = ( s2_x * (p0_y - p2_y) - s2_y * (p0_x - p2_x)) / (-s2_x * s1_y + s1_x * s2_y);

   if (s >= 0 && s <= 1 && t >= 0 && t<=1)
   {
       // Collision detected
       if (i_x != NULL)
           *i_x = p0_x + (t * s1_x);
       if (i_y != NULL)
           *i_y = p0_y + (t * s1_y);
       return 1;
   }

return 0; // No collision
}

问题是,如果射线和线都是垂直的,并且射线和线都在同一条线上,我需要得到射线击中线的点。例如,射线从 (10,10) 到 (10,30) 的交点和一条从 (10,20) 到 (10,40) 的线应该给我 (10,20) 但它 returns null相反。

这是我的java代码

public static Point2D.Double get_line_intersection(Line2D.Double ray, Line2D.Double line){
    double s1_x, s1_y, s2_x, s2_y;
    s1_x = ray.x2 - ray.x1;       s1_y = ray.y2 - ray.y1;
    s2_x = line.x2 - line.x1;     s2_y = line.y2 - line.y1;

    double s, t;
    s = (-s1_y * (ray.x1 - line.x1) + s1_x * (ray.y1 - line.y1)) / (-s2_x * s1_y + s1_x * s2_y);
    t = ( s2_x * (ray.y1 - line.y1) - s2_y * (ray.x1 - line.x1)) / (-s2_x * s1_y + s1_x * s2_y);


    if (s >= 0 && s<=1 && t >= 0)
    {
        // Collision detected
        Point2D.Double intersection = new Point2D.Double();
        intersection.x = ray.x1 + (t * s1_x);
        intersection.y = ray.y1 + (t * s1_y);
        return intersection;
    }

    return null;

}

我只删除了第一个 if 语句中的条件 t<=1,因为射线是无限的。

首先你必须检查你的射线和线段是否平行 - 在这种情况下叉积为零:

(-s2_x * s1_y + s1_x * s2_y) = 0

如果是这样,请考虑四个案例中的第一个和第二个案例,在您 link 的出色回答中进行了描述。你的例子是针对第一种情况(共线对象)