当前位置: 首页>>代码示例>>C#>>正文


C# Collision.OnStopColliding方法代码示例

本文整理汇总了C#中Collision.OnStopColliding方法的典型用法代码示例。如果您正苦于以下问题:C# Collision.OnStopColliding方法的具体用法?C# Collision.OnStopColliding怎么用?C# Collision.OnStopColliding使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在Collision的用法示例。


在下文中一共展示了Collision.OnStopColliding方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: OnCollision

        public virtual void OnCollision(Collision collision)
        {
            if (!collision.SecondObject.Collidable || !Parent.Collidable || !Parent.CanCollideWith(collision.SecondObject))
                return;

            VectorRectangle bounds = Parent.Bounds;
            VectorRectangle lastFrame = Parent.LastFrameBounds;
            Vector2 size = bounds.Size;

            float vX, vY, aX, aY;
            vX = vY = aX = aY = 1;

            if (
                (collision.IsBottom() || collision.IsTop())
                && !collision.IsLeft()
                && !collision.IsRight()
                )
            {
                vY = -collision.SecondObject.Elasticity;

                float newY;

                if (collision.IsBottom())
                {
                    newY = collision.SecondObject.Position.Y - size.Y;
                }
                else
                {
                    newY = collision.SecondObject.Position.Y + collision.SecondObject.Size.Y;
                }
                Parent.Position = new Vector2(Parent.Position.X, newY);
            }

            if (
                (collision.IsLeft() || collision.IsRight())
                && !collision.IsTop()
                && !collision.IsBottom()
                )
            {
                vX = 0;

                float newX;

                if (collision.IsLeft())
                {
                    newX = collision.SecondObject.Position.X + collision.SecondObject.Size.X;
                }
                else
                {
                    newX = collision.SecondObject.Position.X - size.X;
                }
                Parent.Position = new Vector2(newX, Parent.Position.Y);
            }

            if (collision.TouchingSidesCount == 2)
            {
                float width = collision.Intersect.Width, height = collision.Intersect.Height;

                if (lastFrame.IntersectsVertical(collision.SecondObject.Bounds) && !lastFrame.IntersectsHorizontal(collision.SecondObject.Bounds))    // TODO also previous frame
                {
                    height = 0;
                    vX = 0;
                    collision.OnStopColliding(Collision.Side.Top);
                    collision.OnStopColliding(Collision.Side.Bottom);
                    Console.WriteLine("Stopping horizontal");
                }
                else if (lastFrame.IntersectsHorizontal(collision.SecondObject.Bounds))
                {
                    width = 0;
                    vY = 0;
                    collision.OnStopColliding(Collision.Side.Left);
                    collision.OnStopColliding(Collision.Side.Right);
                    Console.WriteLine("Stopping vertical");
                }
                else
                {
                    Parent.OnDecollision(collision.SecondObject);
                }

                if (collision.IsRight())
                    width *= -1;
                if (collision.IsBottom())
                    height *= -1;

                Parent.Position += new Vector2(width, height);
            }

            Parent.Velocity *= new Vector2(vX, vY);
            Parent.Acceleration *= new Vector2(aX, aY);

            //TODO: Make _parent.Velocity.Y equal to Zero when value drops below sertain value to decrease hovering
            //above the platform he is standing on.
            if (Parent.Velocity.Y < 0 && Parent.Velocity.Y > -6)
                Parent.Velocity = new Vector2(Parent.Velocity.X, 0);
            //END OF TODO

            //Console.WriteLine("Collision");
        }
开发者ID:steynh,项目名称:GamingMinor,代码行数:98,代码来源:MovableBehaviour.cs


注:本文中的Collision.OnStopColliding方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。