public void OnCollisionEnded(CollisionEventArgs e)
 {
     if (CollisionEnded != null)
     {
         CollisionEnded(this, e);
     }
 }
Example #2
0
        /// <summary>
        /// Allows the game component to update itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        public override void Update(GameTime gameTime)
        {
            // performance
            if (m_mainTimer != null)
                m_mainTimer.Watch.Restart();

            if (m_findTimer != null)
                m_findTimer.Watch.Reset();

            if (m_resolveTimer != null)
                m_resolveTimer.Watch.Reset();

            ISceneManager sceneManager = Game.Services.GetService(typeof(ISceneManager)) as ISceneManager;
            if (sceneManager == null)
            {
                return;
            }
            foreach (CollisionDetectionElement e in m_colliders)
            {
                if (m_findTimer != null)
                    m_findTimer.Watch.Start();

                List<Collision> collisions = new List<Collision>();
                e.beginUpdate();
                // find possible colliders
                DefaultSelectionExecutor exe = new DefaultSelectionExecutor();
                sceneManager.getCollisions(e.BoundingBox,exe);
                foreach (SceneElement se in exe.Selected)
                {
                    CollisionDetectionElement collider = se.GameObject.getFirst(typeof(CollisionDetectionElement)) as CollisionDetectionElement;
                    if (collider != null && e != collider)
                    {
                        foreach (AbstractCollider ac in collider.Colliders)
                        {
                            foreach (AbstractCollider ac2 in e.Colliders)
                            {
                                if (((ac.CollidesWith & ac2.CollisionGroup) > 0) || ((ac.CollisionGroup & ac2.CollidesWith) > 0))
                                {
                                    Vector3 normal1, normal2;
                                    if ( ac != ac2 )
                                    {
                                        if (ac.intersects(ac2, out normal1, out normal2))
                                        {
                                            Collision c = new Collision(e, collider, normal2, normal1);
                                            collisions.Add(c);
                                        }
                                    }
                                }
                            }
                        }
                    }
                }

                if (m_findTimer != null)
                    m_findTimer.Watch.Stop();

                if (m_resolveTimer != null)
                    m_resolveTimer.Watch.Start();

                // resolve collisions
                foreach (Collision c in collisions)
                {
                    // report collision
                    CollisionEventArgs args1 = new CollisionEventArgs(c.Collider2);
                    CollisionEventArgs args2 = new CollisionEventArgs(c.Collider1);
                    if (c.Normal2.LengthSquared() > 0)
                    {
                        args1.HasCollisionNormal = true;
                        args1.CollisionNormal = c.Normal2;
                    }
                    if (c.Normal1.LengthSquared() > 0)
                    {
                        args2.HasCollisionNormal = true;
                        args2.CollisionNormal = c.Normal1;
                    }
                    c.Collider1.OnCollision(args1);
                    c.Collider2.OnCollision(args2);
                }
                e.endUpdate();

                if (m_resolveTimer != null)
                    m_resolveTimer.Watch.Stop();
            }

            if (m_mainTimer != null)
                m_mainTimer.Watch.Stop();

            base.Update(gameTime);
        }
 public void OnCollision(CollisionEventArgs e)
 {
     m_isIntersecting = true;
     if (Collision != null)
     {
         Collision(this, e);
     }
 }