我基本上是在做 Pong 克隆,我想添加碰撞。我希望它的工作方式是,每个 Racket 都被分成 4 个部分(不是视觉上的),球会根据它击中的部分做出不同的 react 。
就碰撞而言,我只知道矩形碰撞,基本上就是整个 Racket 。我不知道怎么做,所以桨有 4 个可能的碰撞点, react 不同。
最佳答案
只需将 Racket 分成 4 block ,并保持为一 block :)
Collide 如果 rects 没有碰撞,方法返回 -1,或者碰撞的第一个索引。
CollideIndex 方法返回碰撞矩形的 IEnumerable 索引。 ;)
Paddle p;
World w;
Ball b;
Rect[] paddleSections = new Rect[]
{
p.Section[0],
p.Section[1],
p.Section[2],
p.Section[3],
}
Rect[] worldSections = new Rect[]
{
new Rect(p.Top, p.Left, p.Right, p.Bottom) // just simple world, for example
}
Rect[] ballSections = new Rect[]
{
new Rect(p.Top, p.Left, p.Right, p.Bottom) // just one ball piece, for example
}
Rect[] bricksSection = GetBrickRects();
int index = 0;
if ((index = Collide(ballSections, worldSections) != -1)
{
foreach(int collideIndex in CollideIndex(ballSection[index], worldSection)
{
// worldSection[collideIndex] intersects ballSections[index]
...
// something goes up here
}
}
针对球、墙壁、砖 block 调用您最喜欢的算法。
别忘了给我发一份你的游戏!
关于c# - 在带有 C# 的 XNA 游戏中实现碰撞检测的最简单方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4731388/