コード例 #1
0
        public NavMesh(GraphicsDevice device, datx02_rally.GameLogic.Curve curve, int resolution, float width, Vector3 terrainScale)
        {
            this.device = device;

            #region Vertices

            //vertices = new NavMeshTriangleVertex[2 * resolution];
            vertices = new VertexPositionColor[2 * resolution];
            {
                int i = 0;
                var curveRasterization = new CurveRasterization(curve, resolution).Points;
                foreach (var point in curveRasterization)
                {
                    vertices[i++] = new VertexPositionColor(10 * Vector3.Up + point.Position - (terrainScale * width * point.Side), Color.White);
                    vertices[i++] = new VertexPositionColor(10 * Vector3.Up + point.Position + (terrainScale * width * point.Side), Color.White);
                }
            }

            #endregion

            #region Indicies

            int[] indices = new int[6 * resolution];

            int[] offsets = { 0, 1, 2, 2, 1, 0 };
            for (int i = 0; i < indices.Length; i++)
            {
                int v = i / 3 + offsets[i % 6];
                if (v >= vertices.Length)
                {
                    v -= vertices.Length;
                }
                indices[i] = v;
            }

            #endregion

            #region Triangles

            triangles = new NavMeshTriangle[2 * resolution];

            for (int i = 0; i < indices.Length; i += 3)
            {
                triangles[i / 3] = new NavMeshTriangle(vertices[indices[i]].Position,
                                                       vertices[indices[i + 1]].Position,
                                                       vertices[indices[i + 2]].Position);
            }


            // Smoothen out normals
            for (int i = 1; i < triangles.Length; i += 2)
            {
                int j = i - 1, c = i - 2, d = j - 2;
                if (c < 0)
                {
                    c += triangles.Length;
                    d += triangles.Length;
                }

                Vector3 normal = Vector3.Lerp(triangles[i].normal, triangles[j].normal, .5f);
                Vector3 others = Vector3.Lerp(triangles[c].normal, triangles[d].normal, .5f);
                normal = Vector3.Lerp(normal, others, .6f);
                triangles[i].normal = triangles[j].normal = normal;
            }

            #endregion

            #region Buffers

            indexBuffer = new IndexBuffer(device, typeof(int), indices.Length, BufferUsage.None);
            indexBuffer.SetData(indices);
            indexBuffer.SetData(indices, 0, 6);

            vertexbuffer = new VertexBuffer(device,
                                            typeof(VertexPositionColor), vertices.Length,
                                            BufferUsage.None);
            vertexbuffer.SetData(vertices);
            vertexbuffer.SetData(vertices, 0, 6);

            #endregion

            Effect = new BasicEffect(device);
        }
コード例 #2
0
ファイル: GamePlayView.cs プロジェクト: MintL/datx02-rally
        private bool CollisionCheck(NavMeshTriangle triangle)
        {
            var downray = new Ray(Car.Position, Vector3.Down);
            var upray = new Ray(Car.Position, Vector3.Up);

            float? d1 = downray.Intersects(triangle.trianglePlane),
                d2 = upray.Intersects(triangle.trianglePlane);

            if (d1.HasValue || d2.HasValue)
            {
                float d = d1.HasValue ? d1.Value : d2.Value;
                Ray ray = d1.HasValue ? downray : upray;

                var point = ray.Position + d * ray.Direction;

                bool onTriangle = PointInTriangle(triangle.vertices[0],
                    triangle.vertices[1],
                    triangle.vertices[2],
                    point);

                if (onTriangle)
                {
                    Car.Position = point;
                    Car.Normal = triangle.normal;
                }

                return onTriangle;
            }
            return false;
        }
コード例 #3
0
ファイル: NavMesh.cs プロジェクト: MintL/datx02-rally
        public NavMesh(GraphicsDevice device, datx02_rally.GameLogic.Curve curve, int resolution, float width, Vector3 terrainScale)
        {
            this.device = device;

            #region Vertices

            //vertices = new NavMeshTriangleVertex[2 * resolution];
            vertices = new VertexPositionColor[2 * resolution];
            {
                int i = 0;
                var curveRasterization = new CurveRasterization(curve, resolution).Points;
                foreach (var point in curveRasterization)
                {
                    vertices[i++] = new VertexPositionColor(10 * Vector3.Up + point.Position - (terrainScale * width * point.Side), Color.White);
                    vertices[i++] = new VertexPositionColor(10 * Vector3.Up + point.Position + (terrainScale * width * point.Side), Color.White);
                }
            }

            #endregion

            #region Indicies

            int[] indices = new int[6 * resolution];

            int[] offsets = { 0, 1, 2, 2, 1, 0 };
            for (int i = 0; i < indices.Length; i++)
            {
                int v = i / 3 + offsets[i % 6];
                if (v >= vertices.Length)
                    v -= vertices.Length;
                indices[i] = v;
            }

            #endregion

            #region Triangles

            triangles = new NavMeshTriangle[2 * resolution];

            for (int i = 0; i < indices.Length; i += 3)
            {
                triangles[i / 3] = new NavMeshTriangle(vertices[indices[i]].Position,
                    vertices[indices[i + 1]].Position,
                    vertices[indices[i + 2]].Position);
            }

            // Smoothen out normals
            for (int i = 1; i < triangles.Length; i += 2)
            {
                int j = i - 1, c = i - 2, d = j - 2;
                if (c < 0)
                {
                    c += triangles.Length;
                    d += triangles.Length;
                }

                Vector3 normal = Vector3.Lerp(triangles[i].normal, triangles[j].normal, .5f);
                Vector3 others = Vector3.Lerp(triangles[c].normal, triangles[d].normal, .5f);
                normal = Vector3.Lerp(normal, others, .6f);
                triangles[i].normal = triangles[j].normal = normal;
            }

            #endregion

            #region Buffers

            indexBuffer = new IndexBuffer(device, typeof(int), indices.Length, BufferUsage.None);
            indexBuffer.SetData(indices);
            indexBuffer.SetData(indices, 0, 6);

            vertexbuffer = new VertexBuffer(device,
                    typeof(VertexPositionColor), vertices.Length,
                    BufferUsage.None);
            vertexbuffer.SetData(vertices);
            vertexbuffer.SetData(vertices, 0, 6);

            #endregion

            Effect = new BasicEffect(device);
        }