/// <summary>
        /// Recursive function to get all elements intersecting a given bounding box
        /// </summary>
        public void GetElements(
            CollisionBox b,
            List <CollisionTreeElem> e,
            uint recurseId)
        {
            // if selection box does not intersect node box, return
            if (b.BoxIntersect(box) == false)
            {
                return;
            }

            // if any elements in this node add them to selection list
            if (elems != null)
            {
                foreach (CollisionTreeElem elem in elems)
                {
                    // elements can be repeated in many nodes
                    // only add element to selection list if not already
                    // added by another node in this same recursion
                    if (elem.lastRecurseId < recurseId)
                    {
                        // if selection box intersect the element box
                        if (elem.box.BoxIntersect(b))
                        {
                            // add element to selection list
                            e.Add(elem);
                        }
                        // set this recuse id to prevent duplicate results
                        elem.lastRecurseId = recurseId;
                    }
                }
            }

            // if not a leaf node, recurso to all children
            if (children != null)
            {
                foreach (CollisionTreeNode n in children)
                {
                    n.GetElements(b, e, recurseId);
                }
            }
        }
        public void GetElements(CollisionBox b, List<CollisionTreeElem> e, uint recurse_id)
        {
            if (b.BoxIntersect(box) == false)
                return;

            if (elems != null)
            {
                foreach (CollisionTreeElem elem in elems)
                {
                    if (elem.last_recurse_id < recurse_id)
                    {
                        if (elem.box.BoxIntersect(b))
                            e.Add(elem);
                        elem.last_recurse_id = recurse_id;
                    }
                }
            }

            if (childs != null)
            {
                foreach (CollisionTreeNode n in childs)
                    n.GetElements(b, e, recurse_id);
            }
        }
Example #3
0
        /// <summary>
        /// Recursive function to get all elements intersecting a given bounding box
        /// </summary>
        public void GetElements(
            CollisionBox b,
            List<CollisionTreeElem> e,
            uint recurseId)
        {
            // if selection box does not intersect node box, return
            if (b.BoxIntersect(box) == false)
                return;

            // if any elements in this node add them to selection list
            if (elems != null)
            {
                foreach (CollisionTreeElem elem in elems)
                {
                    // elements can be repeated in many nodes
                    // only add element to selection list if not already
                    // added by another node in this same recursion
                    if (elem.lastRecurseId < recurseId)
                    {
                        // if selection box intersect the element box
                        if (elem.box.BoxIntersect(b))
                            // add element to selection list
                            e.Add(elem);
                        // set this recuse id to prevent duplicate results
                        elem.lastRecurseId = recurseId;
                    }
                }
            }

            // if not a leaf node, recurso to all children
            if (children != null)
            {
                foreach (CollisionTreeNode n in children)
                    n.GetElements(b, e, recurseId);
            }
        }
Example #4
0
        /// <summary>
        /// Check collisions for dynamic objects
        /// </summary>
        public virtual void CheckCollisions(GameTime gameTime)
        {
            GameEntityList deadObjects = new GameEntityList();

            CollisionBox cameraBox = new CollisionBox(this.camera.box);

            cameraBox.min += this.camera.world.Translation;
            cameraBox.max += this.camera.world.Translation;
            cameraBox.min.Y -= this.camera.head_height + this.camera.step_height;
            cameraBox.max.Y += this.camera.head_height + this.camera.step_height;

            ///Dynamic objects with player
            foreach (GameEntity ge in this.sceneObjects.Values)
            {
                if (cameraBox.BoxIntersect(ge.Box))
                {
                    if (ge is ICatchable)
                    {
                        ((ICatchable)ge).Attach(this.player);

                        deadObjects.Add(ge.ModelMesh.Name, ge);
                        ge.ModelMesh.Tag = null;
                    }
                    else if (ge is IAttachable)
                    {
                        if (ge is GameObjectEntity)
                            this.player.Add((GameObjectEntity)ge);
                    }
                }

                ge.Update(gameTime, this.collisionMesh);
            }

            foreach (GameEntity ge in deadObjects.Values)
            {
                this.sceneObjects.Remove(ge.ModelMesh.Name);
            }

            deadObjects.Clear();

            ///Enemies with player
            foreach (Enemy e in this.enemies.Values)
            {
                if (e.ActualAnimationState != GameEntityAnimationState.Die)
                {
                    if (cameraBox.BoxIntersect(e.Box))
                    {
                        this.SendMessage(player, e, GameEntityMessageType.Hit, gameTime);
                    }
                }
                e.Update(gameTime, this.collisionMesh);
            }

            ///Bullets with enemies, static objects...

            foreach (Bullet b in this.bullets)
            {
                Vector3 position = b.Position;
                Vector3 speed = b.Speed;

                Vector3 new_pos = position + speed * (float)gameTime.ElapsedGameTime.TotalSeconds;
                this.collisionMesh.PointMove(position, new_pos, 1.0f, 1.0f, 3, out position);

                //these are discartable, just to pass to the PointIntersect method as out parameters
                float intersect_distance;
                Vector3 intersect_position = new Vector3();
                Vector3 intersection_normal = new Vector3();

                //collision of bullets with static scenario (walls, etc.)
                if (this.collisionMesh.PointIntersect(position, new_pos, out intersect_distance, out intersect_position, out intersection_normal))
                {
                    b.Dead = true;
                }

                //collision of bullets with enemies
                foreach (Enemy e in this.enemies.Values)
                {
                    if (e.Box.PointInside(b.Position))
                    {
                        b.Dead = true;
                        SendMessage(b, e, GameEntityMessageType.Damage, gameTime);
                        if (e.Health >= 10)
                            SendMessage(b, e, GameEntityMessageType.Hit, gameTime);
                    }
                }

                b.Position = position;
                b.Speed = speed;
            }
        }
Example #5
0
        /// <summary>
        /// Update game for given elapsed time
        /// </summary>
        public void Update(float elapsedTime)
        {
            // update player 1
            players[0].Update(elapsedTime, levelCollision, levelSpawns);

            // if in multiplayer mode
            if (gameMode == GameMode.MultiPlayer)
            {
                // update player 2
                players[1].Update(elapsedTime, levelCollision, levelSpawns);

                // if both players are alive
                if (players[0].IsAlive && players[1].IsAlive)
                {
                    // test collision between players
                    Vector3 position1 = players[0].Position;
                    Vector3 position2 = players[1].Position;
                    CollisionBox player1Box = new CollisionBox(
                                                    position1 + players[0].box.min,
                                                    position1 + players[0].box.max);
                    CollisionBox player2Box = new CollisionBox(
                                                    position2 + players[1].box.min,
                                                    position2 + players[1].box.max);
                    // if player boxes intersect
                    if (player1Box.BoxIntersect(player2Box))
                    {
                        // compute push direction
                        Vector3 direction = Vector3.Normalize(position2 - position1);

                        // push players in oposide directions
                        direction *= GameOptions.ShipCollidePush;
                        players[0].AddImpulseForce(-direction);
                        players[1].AddImpulseForce(direction);

                        // play ship collide sound
                        PlaySound("ship_collide");
                    }
                }
            }

            // update animated sprites
            animatedSprite.Update(elapsedTime);

            // update animated projectiles
            projectile.Update(elapsedTime);

            // update particle systems
            particle.Update(elapsedTime);

            // update powerups
            powerup.Update(elapsedTime);

            // delete any finished 3D sounds
            cueSoundsDelete.Clear();
            foreach (Cue cue in cueSounds)
                if (cue.IsStopped)
                    cueSoundsDelete.Add(cue);
            foreach (Cue cue in cueSoundsDelete)
            {
                cueSounds.Remove(cue);
                cue.Dispose();
            }

            // if gamepad vibreate enabled
            if (GameOptions.UseGamepadVibrate)
            {
                // check vibration for each player
                for (int i = 0; i < GameOptions.MaxPlayers; i++)
                {
                    float leftMotorAmount = 0;
                    float rightMotorAmount = 0;

                    // if left vibration
                    if (vibrationTime[i] > 0)
                    {
                        leftMotorAmount = GameOptions.VibrationIntensity *
                            Math.Min(1.0f,
                            vibrationTime[i] / GameOptions.VibrationFadeout);
                        vibrationTime[i] = Math.Max(0.0f,
                            vibrationTime[i] - elapsedTime);
                    }
                    else
                        // if right vibration
                        if (vibrationTime[i] < 0)
                        {
                            rightMotorAmount = GameOptions.VibrationIntensity *
                                Math.Min(1.0f,
                                -vibrationTime[i] / GameOptions.VibrationFadeout);
                            vibrationTime[i] = Math.Min(0.0f,
                                    vibrationTime[i] + elapsedTime);
                        }

                    // set vibration values
                    GamePad.SetVibration((PlayerIndex)i, leftMotorAmount,
                        rightMotorAmount);
                }
            }
        }