Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/299.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
2D碰撞检测不工作XNA C#_C#_Xna_2d - Fatal编程技术网

2D碰撞检测不工作XNA C#

2D碰撞检测不工作XNA C#,c#,xna,2d,C#,Xna,2d,我是XNA和C#的新手(我是两天前开始的,所以请原谅我的草率编程),我在制作一个简单的游戏时遇到了问题。我似乎在任何在线教程中都找不到解决方案。我试图利用矩形碰撞检测,但我无法让它正常工作。角色完全掉落在地板上,似乎在任何地方都没有碰撞。我在这里找不到我的逻辑错误。下面我发布了一些关于碰撞检测的代码。如果需要,我可以发布更多。提前感谢您的帮助 Level.cs //The method below is called in the initial LoadContent method in Ga

我是XNA和C#的新手(我是两天前开始的,所以请原谅我的草率编程),我在制作一个简单的游戏时遇到了问题。我似乎在任何在线教程中都找不到解决方案。我试图利用矩形碰撞检测,但我无法让它正常工作。角色完全掉落在地板上,似乎在任何地方都没有碰撞。我在这里找不到我的逻辑错误。下面我发布了一些关于碰撞检测的代码。如果需要,我可以发布更多。提前感谢您的帮助

Level.cs

//The method below is called in the initial LoadContent method in Game1.cs. 'LoadBlocks()' is supposed to read in a text file and setup the level's "blocks" (the floor or platforms in the game). 

public void LoadBlocks(int level){
    if (level == 0)
        {
            fileName = "filepath";
        }

        System.IO.StreamReader file = new System.IO.StreamReader(fileName);
        while ((line = file.ReadLine()) != null)
        {
            for (int i = 0; i < 35; i++)
            {
                rectBlock = new Rectangle((int)positionBlock.X, (int)positionBlock.Y, width / 30, height / 30);

                if (line.Substring(i, 1).Equals(","))
                {
                    positionBlock.X += (width / 100) * 24;
                }
                if (line.Substring(i, 1).Equals("#"))
                {  //block -> (bool isPassable, Texture2D texture, Rectangle rectangle, Vector2 position)
                    block = new Block(false, textureBlock, rectBlock, positionBlock);
                    blocks.Add(block);
                    positionBlock.X += (width / 100) * 24;
                }
            }


            positionBlock.Y += (height / 100) * 8;
            positionBlock.X = 0;
        }
    }

//This method below updates the character 'bob' position and velocity.
 public void UpdatePlayer()
    {
        bob.position += bob.velocity;
        bob.rectangle = new Rectangle((int)bob.position.X, (int)bob.position.Y, width / 30, height / 30);

        float i = 1;
        bob.velocity.Y += 0.15f * i;
        foreach (Block block in blocks)
        {
            if (bob.isOnTopOf(bob.rectangle, block.rectangle))
            {
                bob.velocity.Y = 0f;
                bob.hasJumped = false;
            }
        }
    }
//在Game1.cs的初始LoadContent方法中调用以下方法LoadBlocks()应该读入文本文件并设置关卡的“块”(游戏中的地板或平台)。
公共无效加载块(整数级){
如果(级别==0)
{
fileName=“filepath”;
}
System.IO.StreamReader file=新的System.IO.StreamReader(文件名);
而((line=file.ReadLine())!=null)
{
对于(int i=0;i<35;i++)
{
rectBlock=新矩形((int)positionBlock.X,(int)positionBlock.Y,宽度/30,高度/30);
if(行子字符串(i,1).等于(“,”)
{
位置块X+=(宽度/100)*24;
}
if(line.Substring(i,1).Equals(“#”)
{//block->(布尔可访问,纹理2D纹理,矩形,向量2位置)
块=新块(false、textureBlock、rectBlock、positionBlock);
块。添加(块);
位置块X+=(宽度/100)*24;
}
}
位置块Y+=(高度/100)*8;
位置块X=0;
}
}
//下面的此方法更新角色“bob”的位置和速度。
public void UpdatePlayer()
{
摆锤位置+=摆锤速度;
bob.rectangle=新矩形((int)bob.position.X,(int)bob.position.Y,宽度/30,高度/30);
浮点数i=1;
摆锤速度Y+=0.15f*i;
foreach(块中的块)
{
if(bob.isOnTopOf(bob.rectangle,block.rectangle))
{
bob.velocity.Y=0f;
bob.hasshopped=false;
}
}
}
Character.cs

//Here is my whole Character class for 'bob'

public class Character
{
    int height = GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Height;
    int width = GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Width;

    int health;
    String name;
    bool gender;
    Texture2D texture;
    public Vector2 position;
    public Vector2 velocity;
    public bool hasJumped;
    public Rectangle rectangle;

    public Character(int newHealth, String newName, bool newGender, Texture2D newTexture, Vector2 newPosition)
    {
        health = newHealth;
        gender = newGender;
        name = newName;
        texture = newTexture;
        position = newPosition;
        hasJumped = true;
        rectangle = new Rectangle(0,0, width/30,height/30);
        velocity = Vector2.Zero;
    }

    public bool isOnTopOf(Rectangle r1, Rectangle r2)
    {
        const int penetrationMargin = 5;

        return (r1.Bottom >= r2.Top - penetrationMargin &&
           r1.Bottom <= r2.Top &&
           r1.Right >= r2.Left + 5 &&
           r1.Left <= r2.Right - 5);

    }


    public void Draw(SpriteBatch spriteBatch)
    {
        spriteBatch.Draw(texture, rectangle,null, Color.White,0,position,SpriteEffects.None,0);
    }

}
//这是我为“bob”设计的整个字符类
公共阶级性质
{
int height=GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.height;
int width=GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.width;
国际卫生组织;
字符串名;
布尔性别;
纹理2D纹理;
公共向量2位置;
公共矢量2速度;
公众的注意力已经跳跃;
公共矩形;
公共字符(int-newHealth、String-newName、bool-newGender、Texture2D-newexture、Vector2-newPosition)
{
健康=新健康;
性别=新性别;
name=newName;
纹理=新纹理;
位置=新位置;
hasshopped=true;
矩形=新矩形(0,0,宽度/30,高度/30);
速度=矢量2.0;
}
公共bool isOnTopOf(矩形r1,矩形r2)
{
穿透裕度常数=5;
返回(r1.底部>=r2.顶部-贯穿件余量&&
r1.底部=r2.左侧+5&&

r1.Left您的交叉点测试条件似乎不正确,至少是这样。代码可能还有其他问题(有几个问题只是简单地看一下),但让我们关注一下:

r1.Bottom >= r2.Top - penetrationMargin &&
r1.Bottom <= r2.Top &&
r1.Right >= r2.Left + 5 &&
r1.Left <= r2.Right - 5;
这已经不可能了。r1.底部不能同时位于下方和上方

此外,即使所有的比较都是正确的,您也在使用AND everywhere(&&),这意味着只有当所有4个条件都为真时,该条件才为真。基本上,您不是在测试交叉点,而是在测试容器,即,您正在测试r1是否完全在r2内。交叉点测试将使用OR(| |)not和(&&&)加入条件


但是,同样,尽管出于教育目的,您可能希望自己编写代码,但从工程角度来看,最好的解决方案是使用库提供的内容,这里是矩形.Intersects。

您的交叉测试条件似乎不正确,至少是这样。代码可能还有其他问题(有几个只是简单地看一下),但让我们关注一下:

r1.Bottom >= r2.Top - penetrationMargin &&
r1.Bottom <= r2.Top &&
r1.Right >= r2.Left + 5 &&
r1.Left <= r2.Right - 5;
这已经不可能了。r1.底部不能同时位于下方和上方

此外,即使所有的比较都是正确的,您也在使用AND everywhere(&&),这意味着只有当所有4个条件都为真时,该条件才为真。基本上,您不是在测试交叉点,而是在测试容器,即,您正在测试r1是否完全在r2内。交叉点测试将使用OR(| |)not和(&&&)加入条件


但是,同样,尽管出于教育目的,您可能希望自己编写代码,但从工程角度来看,最好的解决方案是使用库提供的内容,这里是矩形。相交。

我找到了答案,但我不确定这到底是为什么。我的绘图方法有问题。我最初有:

spriteBatch.Draw(texture, rectangle, null, Color.White, 0, position, SpriteEffects.None, 0); 
作为我的方法,但后来我将此更改为

spriteBatch.Draw(texture, rectangle, Color.White);

现在一切都正常了。

我想出来了,但我不知道为什么会这样。我的绘图方法有点问题。我原来有:

spriteBatch.Draw(texture, rectangle, null, Color.White, 0, position, SpriteEffects.None, 0); 
作为我的方法,但后来我将此更改为

spriteBatch.Draw(texture, rectangle, Color.White);

现在一切都好了。

谢谢你的帮助。这种方法简单多了。谢谢你的帮助。这种方法简单多了。