Ejemplo n.º 1
0
        private void GameEnvironment_Collision(object sender, GameEnvironment.CollisionEventArgs e)
        {
            foreach (CollisionInformation collisionInformation in (from collisionInfo in e.CollisionInformation
                                                                   where object.Equals(this.AssociatedObject, collisionInfo.CollidingElement)
                                                                   select collisionInfo))
            {
                double vx = this.MovableBehavior.Velocity.X;
                double vy = this.MovableBehavior.Velocity.Y;
                Vector collisionNormal = collisionInformation.Normal;

                // move the object out of the collision region
                Point newPosition = new Point(this.MovableBehavior.X, this.MovableBehavior.Y);
                if (collisionNormal.X < 0)
                {
                    newPosition.X -= 2 * vx;
                }
                if (collisionNormal.Y < 0)
                {
                    newPosition.Y -= 2 * vy;
                }
                this.MovableBehavior.SetPosition(newPosition);

                // deflect the velocity around the normal of collision
                double dot = vx * collisionNormal.X + vy * collisionNormal.Y;
                vx -= (2 * dot * collisionNormal.X);
                vy -= (2 * dot * collisionNormal.Y);

                this.MovableBehavior.Velocity = new Vector(vx, vy);
                this.Direction = Math.Atan2(vy, vx) * 180 / Math.PI;
            }
        }
 private void OnGameEnvironmentCollision(object sender, GameEnvironment.CollisionEventArgs e)
 {
     if (e.CollisionInformation.Any(collisionInfo => object.ReferenceEquals(this.Source, collisionInfo.CollidingElement) || object.ReferenceEquals(this.Source, collisionInfo.HitElement)))
     {
         this.InvokeActions(EventArgs.Empty);
     }
 }