在C#/ XNA中使用通用函数进行碰撞检测 [英] Using Generic Functions for collision detection in C# / XNA

查看:123
本文介绍了在C#/ XNA中使用通用函数进行碰撞检测的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



Sphere
Cube
Plane

我在c#/ XNA中制作物理引擎, p>

全部来源于

GameObject

I将所有对象存储在一个GameObjects列表中,我想遍历这个列表,并能够调用一个CheckCollision函数,该函数将为每对对象执行正确的函数。



例如

go是一个Sphere,

是一个球体

  if(CheckCollision(go,go2))
{
// do stuff
}

bool CheckCollision(Sphere one,Sphere two)
{
//检查球体到球体
}

bool CheckCollision(球体,平面)
{
//检查领域飞机
}

喜欢它只是去检查正确的功能,而不必使用检查。



谢谢。

解决方案

你可以使用虚拟调度:

 抽象类GameObject 
{
抽象布尔CheckCollision(GameObject other);
抽象布尔CheckCollision(Sphere其他);
抽象布尔CheckCollision(Cube其他);
抽象布尔CheckCollision(平面其他);
}

class Sphere:GameObject
{
override bool CheckCollision(GameObject other){return other.CheckCollision(this); }
覆盖bool CheckCollision(Sphere other){/ * ...实现... * /}
覆盖bool CheckCollision(Cube other){/ * ... implementation ... * /}
覆盖bool CheckCollision(Plane other){/ * ... implementation ... * /}
}

class Cube:GameObject
{
override bool CheckCollision(GameObject other){return other.CheckCollision(this); }
覆盖bool CheckCollision(Sphere other){/ * ...实现... * /}
覆盖bool CheckCollision(Cube other){/ * ... implementation ... * /}
覆盖bool CheckCollision(Plane other){/ * ...实现... * /}
}

类平面:GameObject
{
覆盖bool CheckCollision(GameObject other){return other.CheckCollision(this); }
覆盖bool CheckCollision(Sphere other){/ * ...实现... * /}
覆盖bool CheckCollision(Cube other){/ * ... implementation ... * /}
覆盖bool CheckCollision(Plane other){/ * ... implementation ... * /}
}

编辑



请考虑当发生这种情况时会发生什么情况:

  GameObject go1 = new Sphere(); 
GameObject go2 = new Cube();
bool collision = go1.CheckCollision(go2);




  • 调用进入抽象的GameObject.CheckCollision(GameObject)方法
  • 虚拟调度意味着我们去Sphere.CheckCollision(GameObject)Sphere.CheckCollision(GameObject)调用抽象的GameObject.CheckCollision (Sphere)方法。

  • 虚拟调度意味着我们去Cube.CheckCollision(Sphere)!


    因此,如果语句是必要的,那么不需要检查

    编辑2



    请参阅Eric Lippert在 https:// stackoverflow上的答案。 COM /一个/三十八万五千八百四十四分之二百三十六万七千九百八十一;第一种选择 - 访问者模式 - 基本上是上面概述的方法。 Eric在 https://stackoverflow.com/a/9069976/385844 上的其他答案也讨论了这个问题。


    I am making a physics engine in c# / XNA, I have three basic objects...

    Sphere Cube Plane

    that are all derived from

    GameObject

    I store all my objects in a list of GameObjects and I would like to loop through this list and be able to call a CheckCollision function that will go to the correct function for each pair of objects

    eg

    go is a Sphere,

    go2 is a Sphere

    if(CheckCollision(go, go2))
    {
      //do stuff
    }
    
    bool CheckCollision(Sphere one, Sphere two)
    {
      //Check Sphere to Sphere
    }
    
    bool CheckCollision(Sphere sphere, Plane plane)
    {
      //Check Sphere to Plane
    }
    

    and I would like it to just go to the correct function withour having to use if checks.

    Thank you.

    解决方案

    You could use virtual dispatch:

    abstract class GameObject
    {
        abstract bool CheckCollision (GameObject other);
        abstract bool CheckCollision (Sphere other);
        abstract bool CheckCollision (Cube other);
        abstract bool CheckCollision (Plane other);
    }
    
    class Sphere : GameObject
    {
        override bool CheckCollision (GameObject other) { return other.CheckCollision(this); }
        override bool CheckCollision (Sphere other) { /* ... implementation ... */ }
        override bool CheckCollision (Cube other) { /* ... implementation ... */ }
        override bool CheckCollision (Plane other) { /* ... implementation ... */ }
    }
    
    class Cube : GameObject
    {
        override bool CheckCollision (GameObject other) { return other.CheckCollision(this); }
        override bool CheckCollision (Sphere other) { /* ... implementation ... */ }
        override bool CheckCollision (Cube other) { /* ... implementation ... */ }
        override bool CheckCollision (Plane other) { /* ... implementation ... */ }
    }
    
    class Plane : GameObject
    {
        override bool CheckCollision (GameObject other) { return other.CheckCollision(this); }
        override bool CheckCollision (Sphere other) { /* ... implementation ... */ }
        override bool CheckCollision (Cube other) { /* ... implementation ... */ }
        override bool CheckCollision (Plane other) { /* ... implementation ... */ }
    }
    

    EDIT

    Consider what happens when you have this:

    GameObject go1 = new Sphere();
    GameObject go2 = new Cube();
    bool collision = go1.CheckCollision(go2);
    

    • The call goes to the abstract GameObject.CheckCollision(GameObject) method on the sphere.
    • Virtual dispatch means that we go to Sphere.CheckCollision(GameObject)
    • Sphere.CheckCollision(GameObject) calls the abstract GameObject.CheckCollision(Sphere) method on the cube.
    • Virtual dispatch means that we go to Cube.CheckCollision(Sphere)!

    Therefore, no type-checking if statements are necessary.

    EDIT 2

    See Eric Lippert's answer at https://stackoverflow.com/a/2367981/385844; the first option -- the visitor pattern -- is essentially the approach outlined above. Eric's other answer at https://stackoverflow.com/a/9069976/385844 also discusses this issue.

    这篇关于在C#/ XNA中使用通用函数进行碰撞检测的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆