gpt4 book ai didi

vector - 如何检测两个点向量(C++)中任意两点之间的碰撞?

转载 作者:行者123 更新时间:2023-12-01 11:14:14 24 4
gpt4 key购买 nike

我有这样一个类:

class Point
{
public:
int x;
int y;

bool operator==( Point& other )
{
return (x == other.x) && (y = other.y);
}
};

然后我有一个基本的 Sprite 类,它有一个点向量,以及我没有在下面显示的许多其他成员/方法:

class Sprite
{
std::vector<Point> points;
};

现在,如何使用 Point 类中的运算符 == 找到一个 Sprite 对象的向量中与另一个 Sprite 对象的向量中的任何其他点发生碰撞的点?我试过类似下面的方法,但效果不佳。 Size() 返回点向量中的点总数,Points() 返回点向量:

bool Sprite::CollidesWith( Sprite* other )
{
for ( int ixThis = 0; ixThis < Size(); ixThis++ ) {
for ( int ixOther = 0; ixOther < other->Size(); ixOther++ ) {
if ( points->at(ixThis) == other->Points()->at(ixOther) )
return true;
}
}

return false;
}

有什么想法吗?

最佳答案

您的operator== 不正确。

   bool operator==( Point& other )
{
return (x == other.x) && (y == other.y);
}

关于vector - 如何检测两个点向量(C++)中任意两点之间的碰撞?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13783329/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com