简体   繁体   English

C#矩形碰撞检测无法正常运行

[英]C# Rectangle Collision Detection not working as it is supposed to

Hy everyone, so I am making a little game in C# / XNA and I have this problem with collision. 大家好,我正在用C#/ XNA做一个小游戏,但我遇到了碰撞问题。

Basiclly what I do in my code is I make a normal player Movement and instead of just checking if for example the Up key is pressed, it also checks if player CanGoTop == true; 基本上,我在代码中所做的就是使玩家正常运动,而不仅仅是检查是否按下了Up键,还检查玩家CanGoTop == true;

if (keyState.IsKeyDown(Keys.Up) && CanGoTop == true) 
{
bla bla bla
}

I do this for left, right, up, down, and if a bollean is false, player will not move (There is an else statment after those 4 statments of movemnt that sets velocity,x and .y to 0) 我是针对左,右,上,下执行此操作的,如果布尔型错误,则玩家将不会移动(在movemnt的这4个语句之后,还有另一个语句将velocity,x和.y设置为0)

Okay now I made a method inside my Player class, and you will get the idea what it is just by simply looking at it, I dont think that I have to explain what is going on here: 好的,现在我在Player类中创建了一个方法,您只需看一下就可以了解它的含义,我认为我不必解释这里发生的事情:

        public void RectangleInteraction(Rectangle anotherRectangle)
    {
        if (playerRectangle.Bottom <= anotherRectangle.Top &&
            playerRectangle.Top >= (anotherRectangle.Top - playerRectangle.Height) &&
            playerRectangle.Left <= anotherRectangle.Right &&
            playerRectangle.Right >= anotherRectangle.Left)
        {
            CanGoBot = false;
        }
        else CanGoBot = true;


        if (playerRectangle.Top >= anotherRectangle.Bottom &&
            playerRectangle.Bottom <= (anotherRectangle.Bottom + playerRectangle.Height) &&
            playerRectangle.Left <= anotherRectangle.Right &&
            playerRectangle.Right >= anotherRectangle.Left)
        {
            CanGoTop = false;
        }
        else CanGoTop = true;


        if (playerRectangle.Top <= anotherRectangle.Bottom &&
            playerRectangle.Bottom >= anotherRectangle.Top &&
            playerRectangle.Right <= anotherRectangle.Left &&
            playerRectangle.Left >= (anotherRectangle.Left - playerRectangle.Width))
        {
            CanGoRight = false;
        }
        else CanGoRight = true;


        if (playerRectangle.Top <= anotherRectangle.Bottom &&
            playerRectangle.Bottom >= anotherRectangle.Top &&
            playerRectangle.Left >= anotherRectangle.Right &&
            playerRectangle.Right <= (anotherRectangle.Right + playerRectangle.Width))
        {
            CanGoLeft = false;
        }
        else CanGoLeft = true;

    }

and then In my Game1.cs Update() method I use my player instance to call that method there, and now something thats bugging me a lot, It works for CanGoLeft but for other 3 bools, it doesn't. 然后在我的Game1.cs Update()方法中,使用播放器实例在该位置调用该方法,现在有些事情困扰了我很多, 它适用于CanGoLeft,但对于其他3个布尔值却无效。

I really don't know why, here are my 4 screenshoots with InGameMsgs to help me out, and well checking those Messages with my code tells me that CollisionLogic is good, but something else is wrong. 我真的不知道为什么,这是我对InGameMsgs的4个屏幕快照,可以帮助我解决问题,并且用我的代码很好地检查这些消息可以告诉我CollisionLogic是好的,但是还有其他错误。 Why does only CanGoLeft work then ? 为什么只有CanGoLeft可以工作呢? :/ :/

Thanks in advanced for your time and help. 在此先感谢您的时间和帮助。

CanGoLeft CanGoBot

Your logic seems broken at several points. 您的逻辑似乎在几个方面都被打破了。 You should use the Rectangle 's method like Offset and Intersects to check if the target Rectangle s would colide. 您应该使用Rectangle的方法(例如OffsetIntersects来检查目标Rectangle是否会碰撞。 I think this comes close to your intention (assuming distance is the value by which you're player moves): 我认为这很接近您的意图(假设distance是您玩家移动的值):

public void RectangleInteraction(Rectangle anotherRectangle)
{
    Rectangle down = playerRectangle; down.Offset(0, distance);
    Rectangle up = playerRectangle; up.Offset(0, -distance);
    Rectangle left = playerRectangle; left.Offset(-distance, 0);
    Rectangle right = playerRectangle; right.Offset(distance, 0);

    ConGoBot = !down.Intersects(anotherRectanlge); 
    ConGoTop = !up.Intersects(anotherRectanlge); 
    ConGoLeft = !left.Intersects(anotherRectanlge); 
    ConGoRight = !right.Intersects(anotherRectanlge); 
}

(This is assuming you're using Microsoft.Xna.Framework.Rectangle , there are other Rectangle structs in .net framework, but your question was tagged ) (这是假设您使用的是Microsoft.Xna.Framework.Rectangle ,.net框架中还有其他Rectangle结构,但是您的问题被标记为

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

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