示例#1
0
        private bool LoadBrushLump(Q3BSPDirEntry dir, BinaryReader fileReader)
        {
            int brushCount = dir.Length / Q3BSPConstants.sizeBrush;

            brushes = new Q3BSPBrush[brushCount];
            for (int i = 0; i < brushCount; i++)
            {
                brushes[i] = Q3BSPBrush.FromStream(fileReader);
            }

            bspLogger.WriteLine("No of brushes: " + brushCount);
            return(true);
        }
示例#2
0
        private void CheckBrush(ref Q3BSPBrush brush, ref Q3BSPCollisionData cd)
        {
            float startFraction = -1.0f;
            float endFraction   = 1.0f;
            bool  startsOut     = false;
            bool  endsOut       = false;

            for (int i = 0; i < brush.NumberOfSides; i++)
            {
                Q3BSPBrushSide brushSide = brushSides[brush.StartBrushSide + i];
                Plane          plane     = planes[brushSide.PlaneIndex];

                float startDistance = 0, endDistance = 0;

                if (cd.type == Q3BSPCollisionType.Ray)
                {
                    startDistance = Vector3.Dot(cd.startPosition, plane.Normal) - plane.D;
                    endDistance   = Vector3.Dot(cd.endPosition, plane.Normal) - plane.D;
                }

                else if (cd.type == Q3BSPCollisionType.Sphere)
                {
                    startDistance = Vector3.Dot(cd.startPosition, plane.Normal) - (plane.D + cd.sphereRadius);
                    endDistance   = Vector3.Dot(cd.endPosition, plane.Normal) - (plane.D + cd.sphereRadius);
                }

                else if (cd.type == Q3BSPCollisionType.Box)
                {
                    Vector3 offset = new Vector3();
                    if (plane.Normal.X < 0)
                    {
                        offset.X = cd.boxMaximums.X;
                    }
                    else
                    {
                        offset.X = cd.boxMinimums.X;
                    }

                    if (plane.Normal.Y < 0)
                    {
                        offset.Y = cd.boxMaximums.Y;
                    }
                    else
                    {
                        offset.Y = cd.boxMinimums.Y;
                    }

                    if (plane.Normal.Z < 0)
                    {
                        offset.Z = cd.boxMaximums.Z;
                    }
                    else
                    {
                        offset.Z = cd.boxMinimums.Z;
                    }

                    startDistance = (cd.startPosition.X + offset.X) * plane.Normal.X +
                                    (cd.startPosition.Y + offset.Y) * plane.Normal.Y +
                                    (cd.startPosition.Z + offset.Z) * plane.Normal.Z -
                                    plane.D;

                    endDistance = (cd.endPosition.X + offset.X) * plane.Normal.X +
                                  (cd.endPosition.Y + offset.Y) * plane.Normal.Y +
                                  (cd.endPosition.Z + offset.Z) * plane.Normal.Z -
                                  plane.D;
                }

                if (startDistance > 0)
                {
                    startsOut = true;
                }
                if (endDistance > 0)
                {
                    endsOut = true;
                }

                if (startDistance > 0 && endDistance > 0)
                {
                    return;
                }

                if (startDistance <= 0 && endDistance <= 0)
                {
                    continue;
                }

                if (startDistance > endDistance)
                {
                    float fraction = (startDistance - Q3BSPConstants.Epsilon) / (startDistance - endDistance);
                    if (fraction > startFraction)
                    {
                        startFraction = fraction;
                    }
                }
                else
                {
                    float fraction = (startDistance + Q3BSPConstants.Epsilon) / (startDistance - endDistance);
                    if (fraction < endFraction)
                    {
                        endFraction = fraction;
                    }
                }
            }

            if (false == startsOut)
            {
                cd.startOutside = false;
                if (false == endsOut)
                {
                    cd.inSolid = true;
                }

                return;
            }

            if (startFraction < endFraction)
            {
                if (startFraction > -1.0f && startFraction < cd.ratio)
                {
                    if (startFraction < 0)
                    {
                        startFraction = 0;
                    }
                    cd.ratio = startFraction;
                }
            }
        }
示例#3
0
        private void WalkNode(int nodeIndex, float startRatio, float endRatio, Vector3 startPosition, Vector3 endPosition, ref Q3BSPCollisionData cd)
        {
            // Is this a leaf?
            if (0 > nodeIndex)
            {
                Q3BSPLeaf leaf = leafs[-(nodeIndex + 1)];
                for (int i = 0; i < leaf.LeafBrushCount; i++)
                {
                    Q3BSPBrush brush = brushes[leafBrushes[leaf.StartLeafBrush + i]];
                    if (0 < brush.NumberOfSides &&
                        1 == (textureData[brush.TextureIndex].Contents & 1))
                    {
                        CheckBrush(ref brush, ref cd);
                    }
                }

                return;
            }

            // This is a node
            Q3BSPNode thisNode      = nodes[nodeIndex];
            Plane     thisPlane     = planes[thisNode.Plane];
            float     startDistance = Vector3.Dot(startPosition, thisPlane.Normal) - thisPlane.D;
            float     endDistance   = Vector3.Dot(endPosition, thisPlane.Normal) - thisPlane.D;
            float     offset        = 0;

            // Set offset for sphere-based collision
            if (cd.type == Q3BSPCollisionType.Sphere)
            {
                offset = cd.sphereRadius;
            }

            // Set offest for box-based collision
            if (cd.type == Q3BSPCollisionType.Box)
            {
                offset = Math.Abs(cd.boxExtents.X * thisPlane.Normal.X) + Math.Abs(cd.boxExtents.Y * thisPlane.Normal.Y) + Math.Abs(cd.boxExtents.Z * thisPlane.Normal.Z);
            }

            if (startDistance >= offset && endDistance >= offset)
            {
                // Both points are in front
                WalkNode(thisNode.Left, startRatio, endRatio, startPosition, endPosition, ref cd);
            }
            else if (startDistance < -offset && endDistance < -offset)
            {
                WalkNode(thisNode.Right, startRatio, endRatio, startPosition, endPosition, ref cd);
            }
            else
            {
                // The line spans the splitting plane
                int     side           = 0;
                float   fraction1      = 0.0f;
                float   fraction2      = 0.0f;
                float   middleFraction = 0.0f;
                Vector3 middlePosition = new Vector3();

                if (startDistance < endDistance)
                {
                    side = 1;
                    float inverseDistance = 1.0f / (startDistance - endDistance);
                    fraction1 = (startDistance - offset + Q3BSPConstants.Epsilon) * inverseDistance;
                    fraction2 = (startDistance + offset + Q3BSPConstants.Epsilon) * inverseDistance;
                }
                else if (endDistance < startDistance)
                {
                    side = 0;
                    float inverseDistance = 1.0f / (startDistance - endDistance);
                    fraction1 = (startDistance + offset + Q3BSPConstants.Epsilon) * inverseDistance;
                    fraction2 = (startDistance - offset - Q3BSPConstants.Epsilon) * inverseDistance;
                }
                else
                {
                    side      = 0;
                    fraction1 = 1.0f;
                    fraction2 = 0.0f;
                }

                if (fraction1 < 0.0f)
                {
                    fraction1 = 0.0f;
                }
                else if (fraction1 > 1.0f)
                {
                    fraction1 = 1.0f;
                }
                if (fraction2 < 0.0f)
                {
                    fraction2 = 0.0f;
                }
                else if (fraction2 > 1.0f)
                {
                    fraction2 = 1.0f;
                }

                middleFraction = startRatio + (endRatio - startRatio) * fraction1;
                middlePosition = startPosition + fraction1 * (endPosition - startPosition);

                int side1;
                int side2;
                if (0 == side)
                {
                    side1 = thisNode.Left;
                    side2 = thisNode.Right;
                }
                else
                {
                    side1 = thisNode.Right;
                    side2 = thisNode.Left;
                }

                WalkNode(side1, startRatio, middleFraction, startPosition, middlePosition, ref cd);

                middleFraction = startRatio + (endRatio - startRatio) * fraction2;
                middlePosition = startPosition + fraction2 * (endPosition - startPosition);

                WalkNode(side2, middleFraction, endRatio, middlePosition, endPosition, ref cd);
            }
        }