简体   繁体   English

C#中的基本冲突代码-未检测到冲突

[英]Basic Collision Code in C#- Collision not Detecting

I'm having some problems with a fairly basic platforming game I'm attempting to make. 我在尝试制作一个相当基本的平台游戏时遇到一些问题。 I'm trying to write collision code so that the player can land on individuals random platforms which come from a list, but despite various things I've tried, the boolean I'm using to control whether the character is experiencing gravity or not isn't reaching a value of false when the character steps off a platform, so they can float through the air in either direction. 我正在尝试编写碰撞代码,以便玩家可以在列表中的各个随机平台上着陆,但是尽管我尝试了各种方法,但我用来控制角色是否正在经历重力的布尔值不是当角色离开平台时不会达到false的值,因此角色可以沿任一方向在空中漂浮。 They also sink into the platform, but I'd like to make them stand on top. 它们也沉入平台中,但我想使它们站在顶部。

The player class takes a position vector, a velocity vector, a direction string and a boolean for whether it's landed as parameters. 播放器类采用位置矢量,速度矢量,方向字符串和布尔值作为是否登陆的参数。 The code in the update method is as follows, but I'll also put in the code that determines whether an object is on top. update方法中的代码如下,但我还将放入确定对象是否位于顶部的代码。

character.position += character.velocity;

 if (character.landed == false)
                    character.velocity.Y = character.velocity.Y + 0.15f;
                else character.velocity.Y = 0;
            foreach (Platform p in Platforms)
            {
                for (int i = 0; i < p.size; i++)
                {
                    if ((isOnTopOf(character.position, charRSprite, new Vector2(p.xPos + (32 * i), p.yPos), grassBlock) == true)
                        || (isOnTopOf(character.position, charLSprite, new Vector2(p.xPos + (32 * i), p.yPos), grassBlock) == true))
                    {
                        character.landed = true;
                    }
                    if ((isOnTopOf(character.position, charRSprite, new Vector2(p.xPos + (32 * i), p.yPos), grassBlock) == true) 
                        && character.position.X + 3f > p.xPos + (32*p.size)
                      || (isOnTopOf(character.position, charLSprite, new Vector2(p.xPos + (32 * i), p.yPos), grassBlock) == true)
                        && character.position.X -3f < p.xPos)
                    {
                        character.landed = false;
                    }

                } 

            }
            if (InputHandler.KeyPressed(Keys.Left))
            {
                character.velocity.X = -3f;
                character.direction = "Left";
            }
            if (InputHandler.KeyPressed(Keys.Right))
            {
                character.velocity.X = +3f;
                character.direction = "Right";
            }
            if ((InputHandler.KeyPressed(Keys.Space) || InputHandler.KeyPressed(Keys.W)) && character.landed == true)
            {
                character.position.Y = character.position.Y - 10f;
                character.velocity.Y = -7.5f;
                character.landed = false;
            }
            if (character.position.Y > 3000)
            {
                GameRef.Exit();
            }
        }

The collision boolean is as follows: 碰撞布尔值如下:

    public bool isOnTopOf(Vector2 item1Position, Texture2D item1Sprite, Vector2 item2Position, Texture2D item2Sprite)
    {
        area1.X = (int)item1Position.X;
        area1.Y = (int)item1Position.Y;
        area2.X = (int)item2Position.X;
        area2.Y = (int)item2Position.Y;
        area1.Width = item1Sprite.Width;
        area1.Height = item1Sprite.Height;
        area2.Width = item2Sprite.Width;
        area2.Height = item2Sprite.Height;

        return (area1.Bottom >= area2.Top - 3 && area1.Bottom <= area2.Top + 3 && area1.Right >= area2.Left - 3 
            && area1.Left <= area2.Right + 3);
    }

I'd really appreciate any help that won't require me to remove all of the code I've done so far, but will allow me to slightly adapt it. 我非常感谢您提供的帮助,这些帮助不需要我删除到目前为止已经完成的所有代码,但是可以让我稍加修改。 I'm under time constraints... Also, the following code (included in 1st code segment) was a part which I added to stop the character from walking air off the platform, which for some reason didn't return true. 我受到时间限制...此外,以下代码(包含在第一个代码段中)是我添加的部分,以阻止角色从平台上空走,由于某种原因,该代码未返回true。

if ((isOnTopOf(character.position, charRSprite, new Vector2(p.xPos + (32 * i), p.yPos), grassBlock) == true) 
                    && character.position.X + 3f > p.xPos + (32*p.size)
                  || (isOnTopOf(character.position, charLSprite, new Vector2(p.xPos + (32 * i), p.yPos), grassBlock) == true)
                    && character.position.X -3f < p.xPos)
                {
                    character.landed = false;
                }

循环浏览每个平台,并选择与玩家的速度/位置最近的平台进行碰撞。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM