Beispiel #1
0
        /// <summary>
        /// Constructor.
        /// use quad tree.
        /// </summary>
        /// <param name="vertices">model's vertices</param>
        /// <param name="buildQuadTreeDepth">quad tree depth count</param>
        public CollideModel(Vector3[] vertices, int buildQuadTreeDepth) : base()
        {
            this.vertices = vertices;

            //  Creates vector array by triangle's vertices count
            this.normal = new Vector3[this.vertices.Length / 3];

            //  Creates normal vector by each triangle
            for (int i = 0; i < this.normal.Length; i++)
            {
                Vector3 v1 = this.vertices[i * 3];
                Vector3 v2 = this.vertices[i * 3 + 1];
                Vector3 v3 = this.vertices[i * 3 + 2];

                this.normal[i] = Vector3.Normalize(Vector3.Cross(v3 - v1, v2 - v1));
            }

            //  builds quad tree.
            if (buildQuadTreeDepth > 0)
            {
                this.quadTree = new QuadTree();
                this.quadTree.Build(this.Vertices, buildQuadTreeDepth);
            }
        }
Beispiel #2
0
        /// <summary>
        /// Set to new transform matrix.
        /// </summary>
        public override void Transform(Matrix matrix)
        {
            if (this.QuadTree != null)
            {
                int nodeDepth = this.QuadTree.DepthLevel;

                Matrix newTransform = matrix;

                //  if changed matrix, re-build quad tree and collison vertices
                if (this.TransformMatrix != newTransform)
                {
                    for (int i=0; i < this.vertices.Length; i++)
                    {
                        this.vertices[i] = 
                                    Vector3.Transform(this.vertices[i], newTransform);
                    }

                    for (int i = 0; i < this.normal.Length; i++)
                    {
                        this.normal[i] = 
                                    Vector3.Transform(this.normal[i], newTransform);
                    }

                    //  re-build quad tree.
                    this.quadTree = null;
                    this.quadTree = new QuadTree();
                    this.quadTree.Build(this.Vertices, nodeDepth);
                }
            }

            base.Transform(matrix);
        }