Ejemplo n.º 1
0
        public void Collided(Collision collision, World world, Particles particles)
        {
            var go = collision.GameObject;

            if (go is Rocket || go is PlasmaBullet)
            {
                for (int h = 0; h < 100; h++)
                    particles.Emit(ParticleManager.Explosion, (go.Position + Position) / 2f, Chaos.GetVector2InCircle(3f));
                if (go is Rocket)
                    HP -= 7;
                else
                    HP -= 3;
                if (HP <= 0)
                    IsDestroyed = true;
            }

            if (go is Asteroid)
            {
                if (collision.Time > 0)
                    Response.Apply(this, collision);
                for (int i = 0; i < collision.GameObject.Collisions.Count; i++)
                    if (collision.GameObject.Collisions[i].GameObject == this)
                    {
                        CollisionPool.Instance.PutObject(collision.GameObject.Collisions[i]);
                        collision.GameObject.Collisions[i] = null;
                    }
                collision.GameObject.Collisions.RemoveAll(c => c == null);
            }
        }
Ejemplo n.º 2
0
        public static void Apply(GameObject go, Collision collision)
        {
            var go1 = go;
            var go2 = collision.GameObject;

            if (collision.Time > 0)
            {
                go1.Position += go1.Speed * collision.Time;
                go2.Position += go2.Speed * collision.Time;
            }

            // ось столкновения и нормаль к ней
            var nrm = collision.Delta + go2.Speed * collision.Time - go1.Speed * collision.Time;
            var tan = new Vector2(nrm.Y, -nrm.X);
            nrm.Normalize();
            tan.Normalize();

            // проекция скорости на ось столкновения (нормальная скорость)
            float go1nrm = Vector2.Dot(go1.Speed, nrm);
            float go2nrm = Vector2.Dot(go2.Speed, nrm);

            // перераспределяем импульс между нормальными скоростями в соответствии с массами
            float go1rsp = ((go1.Mass - go2.Mass) * go1nrm + 2f * go2.Mass * go2nrm) / (go1.Mass + go2.Mass);
            float go2rsp = ((go2.Mass - go1.Mass) * go2nrm + 2f * go1.Mass * go1nrm) / (go1.Mass + go2.Mass);

            // проекция скорости на нормаль к оси столкновения (тангенциальная скорость)
            float go1tan = Vector2.Dot(go1.Speed, tan);
            float go2tan = Vector2.Dot(go2.Speed, tan);

            go1.Speed = nrm * go1rsp + tan * go1tan;
            go2.Speed = nrm * go2rsp + tan * go2tan;

            if (collision.Time > 0)
            {
                go1.Position -= go1.Speed * collision.Time;
                go2.Position -= go2.Speed * collision.Time;
            }

            //float gp = Maf.Sqrt(collision.DepthSquared);
            //go1.Position -= gp * go2.Mass / (go1.Mass + go2.Mass) * nrm;
            //go2.Position += gp * go1.Mass / (go1.Mass + go2.Mass) * nrm;
        }
Ejemplo n.º 3
0
 private void Collided(Collision col, World world, Particles particles)
 {
     if (col.GameObject is SpeedBonus)
     {
         _speedBonusTime = 1000;
     }
     if (col.GameObject is Medkit)
         HP = MaxHP;
     if (col.GameObject is PlasmaBullet)
         HP -= 3;
     if (col.GameObject is Rocket)
         HP -= 7;
     if (HP <= 0)
     {
         var t = this.GetType();
         Game.WinShip(t);
         for (int i = 0; i < _world.Count; i++)
         {
             var ship = _world[i] as Ship;
             if (ship != null && ship.HP < 0)
             {
                 ship.HP = ship.MaxHP;
                 ship.Position = Chaos.GetVector2InRectangle(Game.WorldWidth, Game.WorldHeight);
             }
         }
     }
 }
Ejemplo n.º 4
0
 private void Collided(Collision col, World world, Particles particles)
 {
     if (col.GameObject is Ship)
         IsDestroyed = true;
 }