Exemplo n.º 1
0
        public SpaceShip(CollisionDetection cd, Vector3 position)
        {
            _boundingCube    = cd.BoudingCube;
            _oldKeyState     = Keyboard.GetState();
            _model           = cd.Content.Load <Model>("Models\\ShipModel");
            _hullModel       = cd.Content.Load <Model>("Models\\ShipHull");
            _modelTransforms = new Matrix[_model.Bones.Count];
            _hullTransforms  = new Matrix[_hullModel.Bones.Count];
            _position        = position;
            _direction       = //Vector3.Zero;
                               new Vector3(
                ((float)cd.Random.NextDouble() - 0.5f) * Speed,
                ((float)cd.Random.NextDouble() - 0.5f) * Speed,
                ((float)cd.Random.NextDouble() - 0.5f) * Speed);

            //initial rotation
            _rotation = new Rotation(cd.Random);

            #region creating bounding ball
            {
                var meshPart = _model.Meshes[0].MeshParts[0];
                var vpnt     = new VertexPositionNormalTexture[meshPart.VertexBuffer.VertexCount];
                meshPart.VertexBuffer.GetData <VertexPositionNormalTexture>(vpnt);
                var vertices = new Vector3[vpnt.Length];
                for (int i = 0; i < vpnt.Length; i++)
                {
                    vertices[i] = Vector3.Transform(vpnt[i].Position, Scale);
                }
                CollisionSphere = new BoundingBall(cd, vertices, this);
            }
            #endregion

            #region make hullobject for GJK
            //loop through each mesh of hull
            ShipHulls = new List <Hull>();
            foreach (var hullmesh in _hullModel.Meshes)
            {
                List <Vector3> hull_vertices = new List <Vector3>();
                //now get the vertices and make a hull object and add it to shiphull list
                foreach (var mparts in hullmesh.MeshParts)
                {
                    int vertexStride = mparts.VertexBuffer.VertexDeclaration.VertexStride;
                    var vpnt_hull    = new VertexPositionNormalTexture[mparts.NumVertices];
                    mparts.VertexBuffer.GetData <VertexPositionNormalTexture>(vpnt_hull);
                    for (int k = 0; k < mparts.NumVertices; k++)
                    {
                        hull_vertices.Add(vpnt_hull[k].Position);
                    }
                }
                //how that i have all the vertices in a hull
                //let me add that to ship hull with index number
                ShipHulls.Add(new Hull(hull_vertices, Size, hullmesh.ParentBone.Index, _rotation));
            }
            #endregion

            #region rearrange hulls
            Shuffle(ShipHulls);
            #endregion
        }
Exemplo n.º 2
0
            public bool Collides(BoundingBall bv)
            {
                // For a normalized plane (|p.n| = 1), evaluating the plane equation
                // for a point gives the signed distance of the point to the plane
                float distance = Vector3.Dot(bv.Center, _normal) - _distanceFromOrigin;

                // If sphere center within +/-radius from plane, plane intersects sphere
                return(Math.Abs(distance) <= bv.Radius);
            }
Exemplo n.º 3
0
 public bool Collides(BoundingBall bv)
 {
     foreach (var boundary in _boundaries)
     {
         if (boundary.Collides(bv))
         {
             return(true);
         }
     }
     return(false);
 }
Exemplo n.º 4
0
        public bool Intersects(BoundingBall that)
        {
            // We compute the distanace squred to avoid the expensive squred root calculation
            Vector3 distance        = this.Center - that.Center;
            float   distaceSquared  = Vector3.Dot(distance, distance);
            float   radiiSumSquared = this.Radius + that.Radius;

            // Need the square of the radius since we use the squre of the distance
            radiiSumSquared *= radiiSumSquared;
            // Checking bounding volumes for collision
            return(distaceSquared <= radiiSumSquared);
        }