Sign in to follow this  
Spa8nky

Creating the 6 clipping planes of a cube correctly.

Recommended Posts

Spa8nky    230
Hello there, I have a plane class that defines a plane as follows:
        /// <summary>
        /// If we have a Point available that we know lies on the Plane.
        /// We can compute the distance directly from this Point and the plane normal using: 
        /// distance (d) = (Vector3.Dot(Point, Normal))
        /// </summary>
        /// <param name="normal">Plane Normal</param>
        /// <param name="point">Point that lies on Plane</param>
        public CD_Plane(Vector3 normal, Vector3 point)
        {
            Normal = Vector3.Normalize(normal);
            D = Vector3.Dot(Normal, point);

.......

When creating the 6 clipping planes of a cube I am currently doing the following:
            position = centre;
            normal = normal_Surface;

            Vector3 tangent = Vector3.Cross(normal, Vector3.UnitZ);

            if (tangent.LengthSquared() < 0.0001f)
            {
                tangent = Vector3.Cross(normal, Vector3.UnitY);
            }

            Vector3 binormal = Vector3.Cross(normal, tangent);

            //=============================================
            //------- [Calculate Boundary Planes] ---------
            //=============================================
            float d = Vector3.Dot(centre, tangent);

            plane_Left = new CD_Plane(tangent, tangent * (width * 0.5f + d));
            plane_Right = new CD_Plane(-tangent, -tangent * (width * 0.5f - d));

            d = Vector3.Dot(centre, binormal);

            plane_Bottom = new CD_Plane(-binormal, -binormal * (height * 0.5f - d));
            plane_Top = new CD_Plane(binormal, binormal * (height * 0.5f + d));

            d = Vector3.Dot(centre, normal);

            plane_Back = new CD_Plane(-normal, -normal * (depth * 0.5f - d));
            plane_Front = new CD_Plane(normal, normal * (depth * 0.5f + d));

The problem is that if the projection onto the axis for the plane is 0, then the plane is not in the right location based on the centre. For example if the centre of the cube is (5, 0, 0) and the tangent is (1, 0, 0) then d would be 5. Therefore a plane created using n = tangent and the distance along n [tangent * (width * 0.5f + d)] would be correct. If the tangent was (0, 1, 0) then d would be 0 and the distance along n is no longer correct. How can I obtain the correct distance along n in all cases for the 6 dividing planes? Thank you.

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