Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ssis/2.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
C# Can';t使用Rectangle.Intersects()正确检测碰撞_C#_Xna_Collision Detection - Fatal编程技术网

C# Can';t使用Rectangle.Intersects()正确检测碰撞

C# Can';t使用Rectangle.Intersects()正确检测碰撞,c#,xna,collision-detection,C#,Xna,Collision Detection,我正在使用一个单一的精灵表图像作为我的突破游戏的主要纹理。图像如下: 我的代码有点混乱,因为我使用一个点来表示元素大小及其在图纸上的位置,一个向量来表示其在视口中的位置,以及一个表示元素本身的矩形来从同一纹理创建两个元素 Texture2D sheet; Point paddleSize = new Point(112, 24); Point paddleSheetPosition = new Point(0, 240); Vector2 paddleViewportPosition; Re

我正在使用一个单一的精灵表图像作为我的突破游戏的主要纹理。图像如下:

我的代码有点混乱,因为我使用一个
点来表示元素大小及其在图纸上的位置,一个
向量来表示其在视口中的位置,以及一个表示元素本身的
矩形来从同一纹理创建两个元素

Texture2D sheet;

Point paddleSize = new Point(112, 24);
Point paddleSheetPosition = new Point(0, 240);
Vector2 paddleViewportPosition;
Rectangle paddleRectangle;

Point ballSize = new Point(24, 24);
Point ballSheetPosition = new Point(160, 240);
Vector2 ballViewportPosition;
Rectangle ballRectangle;
Vector2 ballVelocity;
我的初始化也有点混乱,但它按预期工作:

paddleViewportPosition = new Vector2((GraphicsDevice.Viewport.Bounds.Width - paddleSize.X) / 2, GraphicsDevice.Viewport.Bounds.Height - (paddleSize.Y * 2));
paddleRectangle = new Rectangle(paddleSheetPosition.X, paddleSheetPosition.Y, paddleSize.X, paddleSize.Y);

Random random = new Random();
ballViewportPosition = new Vector2(random.Next(GraphicsDevice.Viewport.Bounds.Width), random.Next(GraphicsDevice.Viewport.Bounds.Top, GraphicsDevice.Viewport.Bounds.Height / 2));
ballRectangle = new Rectangle(ballSheetPosition.X, ballSheetPosition.Y, ballSize.X, ballSize.Y);
ballVelocity = new Vector2(3f, 3f);
图纸:

spriteBatch.Draw(sheet, paddleViewportPosition, paddleRectangle, Color.White);
spriteBatch.Draw(sheet, ballViewportPosition, ballRectangle, Color.White);
问题是我无法使用以下代码正确检测碰撞:

if(ballRectangle.Intersects(paddleRectangle))
{
    ballVelocity.Y = -ballVelocity.Y;
}

我做错了什么?

您正在测试基于“精灵表”纹理的碰撞。这些矩形(
pailerectangle
ballRectangle
)是根据纹理坐标定义的,即这些精灵在图纸上的位置。测试这些矩形的碰撞是没有意义的

您需要使用屏幕坐标进行碰撞,也就是说,您需要使用屏幕位置定义的不同矩形:


我有两个不同的矩形(见编辑后的文章)。我很确定初始化过程中有问题…@DanielRibeiro我已经做了一个编辑来澄清。尝试使用整个代码块进行冲突。你的初始化没问题。我知道Source矩形很重要。有没有更简单的方法来实现精灵表问题,而不必为每个对象创建两个矩形?@DanielRibero我不这么认为,事实上,我相信你这样做是正确的(预期的)方法。问题中的代码(冲突位除外)正常。或者你在我的回答中是在谈论这两个额外的矩形?不,我只是一个怪胎,当涉及到最佳实践和重复代码时。既然你的解决方案非常有效,我现在就开始讨论。非常感谢!我很高兴文档仅仅在一个简单的方法签名上澄清了这么多!
Rectangle paddleViewportRectangle = new Rectangle(paddleViewportPosition.X, 
                                              paddleViewportPosition.Y, 
                                              paddleSize.X, 
                                              paddleSize.Y);

Rectangle ballViewportRectangle = new Rectangle(ballViewportPosition.X,
                                                ballViewportPosition.Y,
                                                ballSize.X,
                                                ballSize.Y);

if(ballViewportRectangle.Intersects(paddleViewportRectangle))
{
    ballVelocity.Y = -ballVelocity.Y;
}