c# - 在带有 C# 的 XNA 游戏中实现碰撞检测的最简单方法是什么?

标签 c# xna

我基本上是在做 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/

相关文章:

c# - C# 中的编码(marshal)处理 - 将指针传递给结构 ("double ref"的引用?)

c# - 在 Visual Studio 2015 中重命名时导致 "unresolvable conflict(s)"的原因是什么?

c# - ObjectIDGenerator 实现

c# - 多线程或 GPU 计算

c# - 在 xna 中使用 Rasterizer Scissoring 时出错

xna - 如何开始在 Zune 上进行游戏编程

c# - 使用 C# 中的字典,将某些自定义类属性用作同一类中另一个属性(形成为值)的唯一键

c# 远程 powershell 创建 AD/Exchange 用户帐户并修改

c# - 如何将 List<string> 分配给 JavaScript 数组或可枚举对象

c# - XNA中如何正确卸载图形资源?