Exemplo n.º 1
0
        /// <summary>
        /// New raycast code; Optimized
        /// </summary>
        public void RayCast2()
        {
            //Get viewport's FOV, determine fov's start and end angles relative to the viewpoint's angles
            //Interpolate between the start and end angles, casting a ray each interval
            int fovDegrees = 75;

            Angle2D minAng = this.mViewpoint.mAngles; minAng.Add((float)-(fovDegrees / 2));
            Angle2D maxAng = this.mViewpoint.mAngles; maxAng.Add((float)(fovDegrees / 2));

            //Column Buffer loop
            for (int x = 0; x < mViewportBounds.Width; x++)
            {
                float scalar = x / mViewportBounds.Width;
                Angle2D ang = new Angle2D(0) { x = minAng.x + (minAng.x - maxAng.x) * scalar, y = minAng.y + (minAng.y - maxAng.y) * scalar };

                //bool geomDone = false;

                Vector2 startPos = mViewpoint.mPos;
                Vector2 endPos = startPos + ang.ToVector2() * 100;

                Vector2 curPos = startPos;

                World.base_geom_entity geom = null;

                for (float sc = 0.0f; sc < 1; sc += 0.01f)
                {
                    curPos = startPos + ((startPos - endPos) * sc);

                    geom = Engine.mWorld.mGeometry.Find(i => i.mBounds.intersectsRadius(curPos, 0.01f));

                    if (geom != null)
                    {
                        int dist = (int)(startPos.DistTo(curPos));

                        if (dist <= 0)
                            break;

                        int drawHeight = (int)(this.mViewportBounds.Height / dist);

                        Point pDrawStart = new Point(x, (this.mViewportBounds.Height / 2) - (drawHeight / 2));
                        Point pDrawEnd = new Point(x, (this.mViewportBounds.Height / 2) + (drawHeight / 2));

                        mViewportSurface.Draw(new Line(pDrawStart, pDrawEnd), Color.Red);
                    }
                    else
                    {
                        continue;
                    }
                }
            }
        }