Skip to content
This repository has been archived by the owner on Mar 30, 2019. It is now read-only.

Collision.RayIntersectsRay treats Rays as lines #1077

Open
mludlum opened this issue Sep 10, 2018 · 2 comments
Open

Collision.RayIntersectsRay treats Rays as lines #1077

mludlum opened this issue Sep 10, 2018 · 2 comments

Comments

@mludlum
Copy link

mludlum commented Sep 10, 2018

When finding the intersection point:

float s = dets / denominator;
float t = dett / denominator;

//The points of intersection.
Vector3 point1 = ray1.Position + (s * ray1.Direction);
Vector3 point2 = ray2.Position + (t * ray2.Direction);

both s and t should be non-negative numbers because rays have a starting point.

I had to add this code after computing s and t.

            if (s < 0 || t < 0)
            {
                point = Vector3.Zero;
                return false;
            }

Also, I added the else in this section when the Rays are parallel but not on top of each other:

            //Lines are parallel.
            if (MathUtil.IsZero(denominator))
            {
                //Lines are parallel and on top of each other.
                if (MathUtil.NearEqual(ray2.Position.X, ray1.Position.X) &&
                    MathUtil.NearEqual(ray2.Position.Y, ray1.Position.Y) &&
                    MathUtil.NearEqual(ray2.Position.Z, ray1.Position.Z))
                {
                    point = Vector3.Zero;
                    return true;
                }
                else
                {
                    point = Vector3.Zero;
                    return false;
                }
            }
@h1cks
Copy link

h1cks commented Mar 30, 2019

Submitted PR for this fix. Could you eyeball the changes to make sure they are correct. I've done my once over and seems to be ok.

@h1cks
Copy link

h1cks commented Mar 30, 2019

#1139 - contains your change also.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants