public CollidableBox(MyVector pos, float size, bool flipped)
            : base(new PhysicalProperties(0.4f,1300,0.6f,1f))
        {
            m_data.Position = pos;
            m_data.Acceleration = new MyVector(0, 0, 0);
            m_data.AngularAcceleration = new MyVector(0, 0, 0);
            m_data.AngularVelocity = new MyVector(0, 0, 0);
            m_data.Velocity = new MyVector(0, 0, 0);
            m_data.VelocityBody = new MyVector(0, 0, 0);
            m_data.Orientation = MyQuaternion.FromEulerAngles(0, 0, 0);
            m_data.Mass = size * size * size;
            m_data.Forces = new MyVector(0, 0, 0);
            m_data.Moments = new MyVector(0, 0, 0);

            float moment = 1 / 4f * m_data.Mass * (size * size);
            m_data.Inertia = new MyMatrix(moment, 0, 0, 0, moment, 0, 0, 0, moment);
            m_data.InertiaInverse = m_data.Inertia.Inverse;
            m_flipped = flipped;
            m_size = size;
            m_radius = 0.5f * size * (float)Math.Sqrt(2);
            //if (flipped)
            //    CreateMeshFlipped();
            //else
            //    CreateMesh();

            if (flipped)
                m_collisionMesh = CollisionMesh.FromFile(DefaultValues.MeshPath + "box1.cm", true, size / 70);
            else
                m_collisionMesh = CollisionMesh.FromFile(DefaultValues.MeshPath + "box1.cm", false, size / 70);
        }
        public KnightClass()
        {
            collisionData = ResourceCache.Instance.GetCollisionMesh("knightHitMesh2.cm");
            List<MyVector> tempPoints = new List<MyVector>();
            for (int i = 0; i < collisionData.m_hardPoints.Length; i++)
            {
                if (collisionData.m_hardPoints[i].Y < 0)
                    tempPoints.Add(collisionData.m_hardPoints[i]);
            }
            collisionData.m_hardPoints = tempPoints.ToArray();
            walkingCollisionData = ResourceCache.Instance.GetCollisionMesh("knightWalkingPoint.cm");
            //hitMesh = ResourceCache.Instance.GetCollisionMesh("knightHitMesh.cm");
            //            renderingData = ResourceCache.Instance.GetRenderingData("knightStanding.x");
            Matrix rot = Matrix.RotationY((float)Math.PI);
            renderingData = ResourceCache.Instance.GetRenderingData("knightStanding.x");
            renderingData.CustomTransform = rot;
            rdWithCrossbow = ResourceCache.Instance.GetRenderingData("knightWithCrossbow.x");
            rdWithCrossbow.CustomTransform = rot;
            rdWithGrenade = ResourceCache.Instance.GetRenderingData("knightWithGrenade.x");
            rdWithGrenade.CustomTransform = rot;
            deadRd = ResourceCache.Instance.GetRenderingData("knightDead.x");

            float r = renderingData.BoundingSphereRadius;

            Matrix tr = Matrix.Translation(0, -3, 0);
            deadRd.CustomTransform = tr;
        }
        public AssassinClass()
        {
            collisionData = ResourceCache.Instance.GetCollisionMesh("assassinHitMesh2.cm");
            List<MyVector> tempPoints = new List<MyVector>();
            for (int i = 0; i < collisionData.m_hardPoints.Length; i++)
            {
                if (collisionData.m_hardPoints[i].Y < 0)
                    tempPoints.Add(collisionData.m_hardPoints[i]);
            }
            collisionData.m_hardPoints = tempPoints.ToArray();
            walkingCollisionData = ResourceCache.Instance.GetCollisionMesh("assassinWalkingPoint.cm");
            //hitMesh = ResourceCache.Instance.GetCollisionMesh("assassinHitMesh.cm");

            //renderingData = ResourceCache.Instance.GetRenderingData("assassinWalkingMesh2.x");
            renderingData = ResourceCache.Instance.GetRenderingData("assassinWalkingMesh2.x");
            rdWithPipe = ResourceCache.Instance.GetRenderingData("assassinWithPipe.x");
            rdWithFireball = ResourceCache.Instance.GetRenderingData("assassinWithPipe.x");
            deadRd = ResourceCache.Instance.GetRenderingData("assassinDead.x");

            Matrix tr = Matrix.Translation(0, -4, 0);
            deadRd.CustomTransform = tr;
        }
 public bool IntersectMesh(out MyVector intersectionPoint, out MyVector intersectionNormal, out float collisionTime, MyVector startPoint,
     MyVector endPoint, float dt, CollisionMesh mesh)
 {
     bool status = mesh.IntersectPlane(out intersectionPoint, out intersectionNormal, out collisionTime, -startPoint, -endPoint, dt, this);
     intersectionPoint.Add(startPoint);
     intersectionNormal = -intersectionNormal;
     return status;
 }
        public bool IntersectMesh(out MyVector intersectionPoint, out MyVector intersectionNormal, out float collisionTime, MyVector startPoint, MyVector endPoint, float dt, CollisionMesh mesh)
        {
            intersectionPoint = new MyVector();
            intersectionNormal = new MyVector();
            MyVector translate = (endPoint - startPoint);
            collisionTime = 0;
            int nPoints = 0;

            MyVector rVelocity=new MyVector();
            MyVector rMove = endPoint - startPoint;
            MyVector iPoint;
            MyVector n1, n2, n3;
            float dist, speed, d1, d2, d3;

            for (int v = 0; v < mesh.m_hardPoints.Length; v++)
            {
                MyVector sPoint = startPoint + mesh.m_hardPoints[v];

                MyVector axis = mesh.MeshRotation;
                axis.Normalize();
                MyQuaternion rot = MyQuaternion.FromAxisAngle((dt * mesh.MeshRotation).Length, axis);

                //endPoint.Rotate(rot);
                //endPoint += relativeVelocity * dt;
                MyVector tmp=mesh.m_hardPoints[v];
                tmp.Rotate(rot);
                MyVector ePoint = startPoint + tmp + rMove;

                rVelocity = (ePoint - sPoint) / dt;
                translate = (ePoint - sPoint);
                for (int i = 0; i < m_faces.Length; i++)
                {
                    if ((ePoint - m_vertices[m_faces[i].v1]) * m_faces[i].n > 0)
                    {
                        //the line doesn't cross the triangle
                        continue;
                    }
                    dist = m_faces[i].n * (sPoint - m_vertices[m_faces[i].v1]);
                    speed = -dist / (m_faces[i].n * rVelocity);
                    if (speed < 0)
                    {
                        if (translate * (m_faces[i].n)<0)
                        {
                            if (dist < -0.5f)
                                continue;
                        }
                        else
                            continue;
                    }
                    iPoint = sPoint + speed * rVelocity;

                    //3 planes around triangle
                    //plane1
                    n1 = ((m_vertices[m_faces[i].v2] - m_vertices[m_faces[i].v1]) ^ m_faces[i].n).Normalize();
                    d1 = n1 * (m_vertices[m_faces[i].v1]);

                    //plane2
                    n2 = ((m_vertices[m_faces[i].v3] - m_vertices[m_faces[i].v2]) ^ m_faces[i].n).Normalize();
                    d2 = n2 * (m_vertices[m_faces[i].v2]);

                    //plane3
                    n3 = ((m_vertices[m_faces[i].v1] - m_vertices[m_faces[i].v3]) ^ m_faces[i].n).Normalize();
                    d3 = n3 * (m_vertices[m_faces[i].v3]);

                    float x1 = n1 * iPoint - d1;
                    float x2 = n2 * iPoint - d2;
                    float x3 = n3 * iPoint - d3;

                    if (x1 <= 0 && x2 <= 0 && x3 <= 0)
                    {

                        if (nPoints == 0)
                        {
                            nPoints = 1;

                            intersectionPoint = iPoint;
                            intersectionNormal = m_faces[i].n;
                            collisionTime = speed;
                        }
                        else if (speed < collisionTime)
                        {
                            nPoints = 1;

                            intersectionPoint = iPoint;
                            intersectionNormal = m_faces[i].n;
                            collisionTime = speed;
                        }
                        else if (speed == collisionTime && nPoints > 0)
                        {
                            nPoints++;
                            intersectionPoint.Add(iPoint);
                            intersectionNormal.Add(m_faces[i].n);
                        }
                    }
                }

            }

            //second
            //for (int v = 0; v < m_hardPoints.Length; v++)
            //{
            //    //MyVector sPoint = -startPoint + m_hardPoints[v];
            //    //MyVector ePoint = -endPoint + m_hardPoints[v];
            //    MyVector sPoint = -startPoint + m_hardPoints[v];

            //    MyVector axis = -mesh.MeshRotation;
            //    axis.Normalize();
            //    MyQuaternion rot = MyQuaternion.FromAxisAngle((dt * mesh.MeshRotation).Length, axis);

            //    //endPoint.Rotate(rot);
            //    //endPoint += relativeVelocity * dt;
            //    MyVector tmp = m_hardPoints[v];
            //    tmp.Rotate(rot);
            //    MyVector ePoint = -startPoint + tmp - rMove;

            //    rVelocity = (ePoint - sPoint) / dt;
            //    translate = (ePoint - sPoint);
            //    for (int i = 0; i < mesh.m_faces.Length; i++)
            //    {

            //        if ((ePoint - mesh.m_vertices[mesh.m_faces[i].v1]) * mesh.m_faces[i].n > 0)
            //        {
            //            //the line doesn't cross the triangle
            //            continue;
            //        }
            //        dist = mesh.m_faces[i].n * (sPoint - mesh.m_vertices[mesh.m_faces[i].v1]);
            //        speed = -dist / (mesh.m_faces[i].n * (-rVelocity));
            //        if (speed < 0)
            //        {
            //            if (translate * (mesh.m_faces[i].n) < 0)
            //            {
            //                if (speed < -0.04)
            //                    continue;
            //            }
            //            else
            //                continue;
            //        }
            //        iPoint = sPoint + speed * (-rVelocity);

            //        //3 planes around triangle
            //        //plane1
            //        n1 = ((mesh.m_vertices[mesh.m_faces[i].v2] - mesh.m_vertices[mesh.m_faces[i].v1]) ^ mesh.m_faces[i].n).Normalize();
            //        d1 = n1 * (mesh.m_vertices[mesh.m_faces[i].v1]);

            //        //plane2
            //        n2 = ((mesh.m_vertices[mesh.m_faces[i].v3] - mesh.m_vertices[mesh.m_faces[i].v2]) ^ mesh.m_faces[i].n).Normalize();
            //        d2 = n2 * (mesh.m_vertices[mesh.m_faces[i].v2]);

            //        //plane3
            //        n3 = ((mesh.m_vertices[mesh.m_faces[i].v1] - mesh.m_vertices[mesh.m_faces[i].v3]) ^ mesh.m_faces[i].n).Normalize();
            //        d3 = n3 * (mesh.m_vertices[mesh.m_faces[i].v3]);

            //        float x1 = n1 * iPoint - d1;
            //        float x2 = n2 * iPoint - d2;
            //        float x3 = n3 * iPoint - d3;
            //        if (x1 <= 0 && x2 <= 0 && x3 <= 0)
            //        {

            //            if (nPoints == 0)
            //            {
            //                nPoints = 1;

            //                intersectionPoint = iPoint + startPoint;
            //                intersectionNormal = -mesh.m_faces[i].n;
            //                collisionTime = speed;
            //            }
            //            else if (speed < collisionTime)
            //            {
            //                nPoints = 1;

            //                intersectionPoint = iPoint + startPoint;
            //                intersectionNormal = -mesh.m_faces[i].n;
            //                collisionTime = speed;
            //            }
            //            else if (speed == collisionTime && nPoints > 0)
            //            {
            //                nPoints++;
            //                intersectionPoint.Add(iPoint + startPoint);
            //                intersectionNormal.Add(-mesh.m_faces[i].n);
            //            }
            //        }
            //    }
            //}

            if (nPoints > 1)
            {
                intersectionPoint.Divide(nPoints);
                intersectionNormal.Normalize();
            }

            return (nPoints > 0);
        }
        public object Clone()
        {
            CollisionMesh newMesh = new CollisionMesh();
            newMesh.boundingShpereRadius = boundingShpereRadius;
            newMesh.m_vertices = new MyVector[m_vertices.Length];
            for (int i = 0; i < m_vertices.Length; i++)
            {
                newMesh.m_vertices[i] = m_vertices[i];
            }
            newMesh.m_faces = new CFace[m_faces.Length];
            for (int i = 0; i < m_faces.Length; i++)
            {
                newMesh.m_faces[i] = m_faces[i];
            }
            newMesh.m_edges = new CEdge[m_edges.Length];
            for (int i = 0; i < m_edges.Length; i++)
            {
                newMesh.m_edges[i] = m_edges[i];
            }
            newMesh.m_hardPoints = new MyVector[m_hardPoints.Length];
            for (int i = 0; i < m_hardPoints.Length; i++)
            {
                newMesh.m_hardPoints[i] = m_hardPoints[i];
            }

            newMesh.MeshRotation = this.MeshRotation;
            return newMesh;
        }
        /// <summary>
        /// Loads collision mesh data from file
        /// </summary>
        /// <param name="path">Path to file</param>   
        /// <param name="flip">Specifies if the mesh should be flipped</param>
        /// <param name="scale">Mesh vertex data will be multiplied by this</param>
        /// <returns>A new CollisionMesh object cobtaining file's data</returns>
        public static CollisionMesh FromFile(string path, bool flip, float scale)
        {
            BinaryReader file = new BinaryReader(new FileStream(path, FileMode.Open));
            CollisionMesh mesh = new CollisionMesh();
            mesh.m_vertices = new MyVector[file.ReadInt32()];
            for (int i = 0; i < mesh.m_vertices.Length; i++)
            {
                mesh.m_vertices[i] = new MyVector();
                mesh.m_vertices[i].X = scale * file.ReadSingle();
                mesh.m_vertices[i].Y = scale * file.ReadSingle();
                mesh.m_vertices[i].Z = scale * file.ReadSingle();
            }
            mesh.boundingShpereRadius = 0;

            for (int i = 0; i < mesh.m_vertices.Length; i++)
            {
                if (mesh.m_vertices[i].Length > mesh.boundingShpereRadius)
                    mesh.boundingShpereRadius = mesh.m_vertices[i].Length;
            }

            mesh.m_faces = new CFace[file.ReadInt32()];
            for (int i = 0; i < mesh.m_faces.Length; i++)
            {

                if (!flip)
                {
                    mesh.m_faces[i].v1 = (uint)file.ReadInt32() - 1;
                    mesh.m_faces[i].v2 = (uint)file.ReadInt32() - 1;
                    mesh.m_faces[i].v3 = (uint)file.ReadInt32() - 1;
                }
                else
                {
                    mesh.m_faces[i].v3 = (uint)file.ReadInt32() - 1;
                    mesh.m_faces[i].v2 = (uint)file.ReadInt32() - 1;
                    mesh.m_faces[i].v1 = (uint)file.ReadInt32() - 1;
                }
                mesh.m_faces[i].n = ((mesh.m_vertices[mesh.m_faces[i].v2] - mesh.m_vertices[mesh.m_faces[i].v1]) ^
                        (mesh.m_vertices[mesh.m_faces[i].v3] - mesh.m_vertices[mesh.m_faces[i].v1])).Normalize();

            }

            try
            {
                mesh.m_edges = new CEdge[file.ReadInt32()];
                for (int i = 0; i < mesh.m_edges.Length; i++)
                {
                    mesh.m_edges[i] = new CEdge((uint)file.ReadInt32() - 1, (uint)file.ReadInt32() - 1);
                }
            }
            catch
            {
                mesh.m_edges = new CEdge[0];
            }

            file.Close();
            mesh.m_hardPoints = (MyVector[])mesh.m_vertices.Clone();
            return mesh;
        }
        //public void FromMesh(Mesh mesh)
        //{
        //    int[] adjacency=new int[mesh.NumberFaces*3];
        //    mesh.GenerateAdjacency(1,adjacency);
        //    Mesh m=mesh.Optimize(MeshFlags.SimplifyFace,adjacency);
        //    CustomVertex.PositionOnly[] vdata = (CustomVertex.PositionOnly[])m.LockVertexBuffer(typeof(CustomVertex.PositionOnly), LockFlags.None, m.NumberVertices);
        //    int[] ind = (int[])m.LockIndexBuffer(typeof(int), LockFlags.None, mesh.NumberFaces * 3);
        //    m_vertices = new MyVector[vdata.Length];
        //    for(int i=0;i<m_vertices.Length;i++)
        //    {
        //        m_vertices[i].X = vdata[i].X;
        //        m_vertices[i].Y = vdata[i].Y;
        //        m_vertices[i].Z = vdata[i].Z;
        //    }
        //}
        /// <summary>
        /// Generates collision mesh from arrays
        /// </summary>
        /// <param name="vertices">An Array of vertices</param>
        /// <param name="faces">3 zero based indices per face</param>
        public static CollisionMesh FromArrays(MyVector[] vertices, uint[] faces)
        {
            CollisionMesh mesh = new CollisionMesh();
            mesh.m_vertices = (MyVector[])vertices.Clone();
            mesh.m_faces = new CFace[faces.Length / 3];
            List<CEdge> edges = new List<CEdge>();

            mesh.boundingShpereRadius = 0;

            for (int i = 0; i < vertices.Length; i++)
            {
                if (vertices[i].Length > mesh.boundingShpereRadius)
                    mesh.boundingShpereRadius = vertices[i].Length;
            }
            for (int i = 0; i < faces.Length; i += 3)
            {
                mesh.m_faces[i / 3].v1 = faces[i];
                mesh.m_faces[i / 3].v2 = faces[i + 1];
                mesh.m_faces[i / 3].v3 = faces[i + 2];
                mesh.m_faces[i / 3].n = ((mesh.m_vertices[faces[i + 1]] - mesh.m_vertices[faces[i]]) ^
                    (mesh.m_vertices[faces[i + 2]] - mesh.m_vertices[faces[i]])).Normalize();

                bool be1 = false, be2 = false, be3 = false;
                CEdge e1, e2, e3;
                e1 = new CEdge();
                e1.v1 = faces[i];
                e1.v2 = faces[i + 1];

                e2 = new CEdge();
                e2.v1 = faces[i + 1];
                e2.v2 = faces[i + 2];

                e3 = new CEdge();
                e3.v1 = faces[i + 2];
                e3.v2 = faces[i];
                for (int j = 0; j < edges.Count; j++)
                {
                    if (!be1 && edges[j].CompareTo(e1) == 0)
                        be1 = true;

                    if (!be2 && edges[j].CompareTo(e2) == 0)
                        be2 = true;

                    if (!be3 && edges[j].CompareTo(e3) == 0)
                        be3 = true;
                }
                if (!be1)
                    edges.Add(e1);

                if (!be2)
                    edges.Add(e2);

                if (!be3)
                    edges.Add(e3);

            }

            mesh.m_edges = edges.ToArray();
            mesh.m_hardPoints = (MyVector[])vertices.Clone();
            return mesh;
        }
        private void CreateMesh()
        {
            float d = m_size / 2;
            MyVector[] verts = new MyVector[8];
            verts[0] = new MyVector(-d, -d, -d);
            verts[1] = new MyVector(-d, -d, d);
            verts[2] = new MyVector(d, -d, d);
            verts[3] = new MyVector(d, -d, -d);
            verts[4] = new MyVector(d, d, -d);
            verts[5] = new MyVector(-d, d, -d);
            verts[6] = new MyVector(-d, d, d);
            verts[7] = new MyVector(d, d, d);

            uint[] ind ={ 0, 2, 1, 0, 3, 2, 5, 6, 7, 5, 7, 4, 1, 7, 6, 1, 2,
                7, 2, 3, 4, 2, 4, 7, 3, 5, 4, 3, 0, 5, 0, 1, 6, 0, 6, 5 };

            m_collisionMesh = CollisionMesh.FromArrays(verts, ind);
        }