在XNA中,两个纹理之间的碰撞检测

4

我正在使用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;
        }
    }

这段代码已经可以完美实现物体的轻弹效果。但我希望在用户轻弹物体后,如果该物体与其他物体发生碰撞,则第二个物体也应该移动,就像桌球中的硬币一样,当击球手碰到它们时它们会移动。

我已经尝试过碰撞和移动的逻辑,但都没有成功。

非常感谢任何形式的帮助。

非常感谢。


根据您所需的真实感,您可能希望利用2D物理引擎,这将轻松允许多个动态复杂碰撞。http://box2d.org是一个著名的物理框架,易于使用,我自己也使用过几次。 - Eisenhorn
很抱歉,但您提供的链接是空白的。 - A.K.
现在应该可以工作了 :) - Eisenhorn
1
你所询问的更多是关于碰撞响应,而不是碰撞检测。如果你的所有对象都是圆形,那么是比较简单的。然而,如果你的对象是其他任何形状,并且你想要一种真实感,那么你将需要编写一个物理引擎。 - jgallant
1个回答

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

是的,我已经开始阅读相关资料了。但是确定需要物理引擎吗?我的意思是,我不能不使用引擎完成这项工作吗?(抱歉,我有些疑虑) - A.K.
1
你不一定需要物理引擎,但是碰撞检测可能是一个复杂的话题,物理引擎可以帮助处理这方面的问题。或者,你可以开始搜索“2D碰撞检测”的教程,例如:http://gamedevelopment.tutsplus.com/tutorials/create-custom-2d-physics-engine-aabb-circle-impulse-resolution--gamedev-6331。 - craftworkgames

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接