简体   繁体   中英

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. 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.

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.

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;
                }

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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