关于碰撞检测系统中的类结构的建议
Advice on class structure in a collision detection system
C++是我广泛使用的第一个使用面向对象的语言,所以我对这个想法仍然有点陌生。我正在尝试将我正在处理的游戏库从 Go(它使用接口而不是真正的 OOP 系统)移植到 C++。
我有一个使用四种类型的碰撞系统:点、边界、线和多边形。我想做的是让所有这些可以抽象到一个"碰撞体"类中,并有一个能够接受两个碰撞体对象并测试碰撞的函数。它看起来像这样:
bool Collides(Collider obj1, Collider obj2);
最初,我认为我可以为每个碰撞类型提供方法来测试给定另一种类型的碰撞(即方法OnPoint,OnBounding,OnLine和OnPolygon),然后让"Collider"成为需要所有这些方法的虚拟类,但后来我意识到这在C++是不可能的,因为它会使类相互依赖进行编译(对吧?
我有点不知所措,不知道我还能做什么。我的设计理念是白日梦吗?
你想要
的是一个函数,它不仅在第一个参数上调度,而且在第二个参数上调度,即双调度。这在C++中不直接支持,但可以模拟。
class Point;
class Line;
class Collider {
public:
virtual bool Collides(Collider const& x) const = 0;
protected:
virtual bool Collides(Point const& p) const = 0;
virtual bool Collides(Line const& l) const = 0;
friend class Point;
friend class Line;
};
class Point: public Collider {
public:
virtual bool Collides(Collider const& x) const {
// here, the type of this is now Point
// now, find out what x is and call the overload on this type
return x.Collides(*this);
}
protected:
virtual bool Collides(Point const& p) const {
return false;
// we now know that we are a point and checking vs. another point
}
virtual bool Collides(Line const& l) const {
return false;
// we now know that we are a point and checking vs. a line
}
};
class Line: public Collider {
public:
virtual bool Collides(Collider const& x) const {
// here, the type of this is now Line
return x.Collides(*this);
}
protected:
virtual bool Collides(Point const& p) const {
return false;
// we now know that we are a line and checking vs. a point
}
virtual bool Collides(Line const& l) const {
return false;
// we now know that we are a line and checking vs. another line
}
};
现在,检查两个对象将执行两个运行时调度:
Collider* p = new Point();
Collider* l = new Line();
p->Collides(*l);
// first dynamically dispatches on p to call Point::Collides
// in Collides, this (which is p) now has the static Point.
// now do the second dispatch on l and use *this as the parametet
// to find the overload.
例如,这用于访客设计模式。如果您的对象集是固定的,但您希望对它们执行的操作将更改或扩展,则 Visitor 是一个不错的选择。
相关文章:
- 我可以检测和更改 gcc/g++ 中结构的当前数据对齐设置吗?
- "configure"脚本如何检测我的系统是否支持某些头文件或 cpp 功能?
- C++ / Qt:如何检测主机名或IP地址何时引用当前系统?
- 在node-gip binding.gyp文件中,如何根据系统结构(32位、64位)包含不同的库文件
- 如何读取/查询文件系统和文件结构
- 以编程方式在运行时检测 CPU 体系结构
- 如何检测两个文件条目是否引用与Qt相同的物理文件系统?
- 代表软件包安装和系统依赖关系的最佳数据结构
- 如何将向量添加到结构中以创建一个库存系统,在该系统中,我可以仅使用一个结构向系统添加多种不同的葡萄酒
- C++ boost::文件系统如何检测路径是文件还是目录
- 系统调用检测和监测
- 正在检测来自我的英特尔系统的 CPU 和内核信息
- 使用C 17可以检测结构/类是否具有任何基础
- UNIX类似文件系统的目录结构
- 检测系统是否可以运行 amd64 可执行文件
- 在一行中检测系统字节序
- Qt::如何识别/检测系统是否被Linux中的其他系统远程访问
- 检测系统重启并启动应用程序
- Waitforsingleobject用于检测系统命令退出
- 关于碰撞检测系统中的类结构的建议