c# - XNA 中两个纹理之间的碰撞检测

标签 c# windows-8 xna windows-store-apps monogame

我正在使用 XNA 的 monogame 实现为 Windows 商店制作游戏,其中,我正在应用轻弹手势在屏幕上移动对象。

这是我在Update 方法中编写的用于轻弹和更新对象的位置和速度的代码

            while (TouchPanel.IsGestureAvailable)
            {
                GestureSample gesture = TouchPanel.ReadGesture();
                if (gesture.GestureType == GestureType.Flick)
                    velocity += gesture.Delta;
                if (gesture.GestureType == GestureType.Hold)
                    position = gesture.Position;
            }

             if (velocity != Vector2.Zero)
             {
                float elapsedSeconds = (float)gameTime.ElapsedGameTime.TotalSeconds;
                position += velocity * elapsedSeconds;
                float newMagnitude = velocity.Length() - DECELERATION * elapsedSeconds;
                velocity.Normalize();
                velocity *= Math.Max(0, newMagnitude);
            }
            UpdateSprite(gameTime, ref position, ref velocity);

        void UpdateSprite(GameTime gameTime, ref Vector2 spritePosition, ref Vector2 spriteSpeed)
        {
        // Move the sprite by speed, scaled by elapsed time.
        spritePosition += spriteSpeed * (float)gameTime.ElapsedGameTime.TotalSeconds;
        int MaxX = GraphicsDevice.Viewport.Width - cat.Width;
        int MinX = 0;
        int MaxY = 500 - cat.Height;
        int MinY = 0;
        // Check for bounce.
        if (spritePosition.X > MaxX - 10)
        {
            if (spritePosition.Y <= 200 || spritePosition.Y >= 300)
            {
                spriteSpeed.X *= -1;
                spritePosition.X = MaxX;
            }
            else
            {
                PositionCount = 0;
            }
        }
        else if (spritePosition.X < MinX + 10)
        {
            if (spritePosition.Y <= 200 || spritePosition.Y >= 300)
            {
                spriteSpeed.X *= -1;
                spritePosition.X = MinX;
            }
            else
            {
                PositionCount = 0;
            }
        }
        if (spritePosition.Y > MaxY)
        {
            spriteSpeed.Y *= -1;
            spritePosition.Y = MaxY;
        }
        else if (spritePosition.Y < MinY)
        {
            spriteSpeed.Y *= -1;
            spritePosition.Y = MinY;
        }
    }

对象的轻弹与此代码完美配合。但我正在寻找的是,当用户轻弹对象时,如果轻弹对象与任何其他对象发生碰撞,那么第二个对象也应该移动,就像轻弹撞针与它们碰撞时 carom 硬币的移动方式一样。

我曾尝试为碰撞和移动添加逻辑但徒劳无功。

我们将不胜感激任何形式的帮助。

非常感谢。

最佳答案

使用Farseer Physics Engine .它是基于 Box2D 的流行 C# 物理引擎,并且与 MonoGame 兼容。它有很多代码示例和不错的文档。

关于c# - XNA 中两个纹理之间的碰撞检测,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20989246/

相关文章:

c# - : Defining a class with a method that gets an object of the class type?是什么意思

javascript - 如何调试 Metro 风格应用程序中 eval 代码中的异常

xna - 游戏 AI - 行为树

c# - Verlet Integration 正在炸毁我的物理引擎

c# - 在 blazor 中添加动态组件列表

c# - 服务在 StackOverflowException 上继续

c# - 如何阻止 Mongodb .NET 驱动程序抛出 System.FormatException?

vb.net - WinRT - 读取文件的访问被拒绝

c# - : the attachable property triggers was not found in style in a Grid Application如何解决

c# - Xbox360 上的 XNA 和 Linq