Sign in to follow this  

How do I get the contact point for a 1D ray/slab test?

Recommended Posts

If I have a 2D ray slab test I can normally get the contact point by using the following equation: contact.point = ray_Origin + tb_Enter * ray_Direction; However when a ray is 1D then I no longer have a second dimension for the point:
        private static bool RaySlabIntersect(float slab_Min, float slab_Max, float ray_Origin, float ray_End, float tb_Enter, float tb_Exit,
            ref Contact2D contact)
            float ray_Direction = ray_End - ray_Origin;

            // If ray is parallel to the slab
            if (Math.Abs(ray_Direction) < float.Epsilon)
                // Ray is parallel to slab, but NOT inside the slab
                if (ray_Origin < slab_Min || ray_Origin > slab_Max)
                    return false;
                // Compute intersection t value of ray with near and far plane of slab
                float ood = 1.0f / ray_Direction;
                float ts_Enter = (slab_Min - ray_Origin) * ood;
                float ts_Exit = (slab_Max - ray_Origin) * ood;

                // Make "ts_Enter" be intersection with near plane, "ts_Exit" with far plane
                if (ts_Enter > ts_Exit)
                    Math_Ext.Swap(ref ts_Enter, ref ts_Exit);

                // Compute the intersection of slab intersection intervals
                tb_Enter = Math.Max(tb_Enter, ts_Enter);
                tb_Exit = Math.Min(tb_Exit, ts_Exit);

                // Exit with no collision as soon as slab intersection becomes empty
                if (tb_Enter > tb_Exit)
                    return false;

            // How do I assign contact location?
            //contact.point = ray_Origin + tb_Enter * ray_Direction;

            return true;

How do I obtain the contact location in 2D for the above test? Do I use the current axis being tested in some way? Thanks.

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this