- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我目前正在尝试实现 https://mollyrocket.com/849 中介绍的简化 GJK 算法进入我的 C++ 游戏。
但是,我在第二维和第三维遇到了奇怪的行为:算法有时(每秒调用多次时很常见)会卡在案例循环中。例如,调试消息一遍又一遍地将以下内容打印到 std::cout:
3ACxAB
4AB
3ABxAC
4ABD
4AD
如果您查看我的代码,您会发现这些行代表算法允许的情况。例如。 3ACxAB 表示单纯形当前是一个三角形,并且原点位于面的 voronoi 区域,在叉积 AC x AB 的方向(可以解释为如三角形“上方”或“下方”)。 4AB 表示单纯形是一个四面体,原点在边 AB 的 voronoi 区域。
A 始终是新添加的点。在代码中,A 始终是 simplex
的最大索引。 (^simplex[1]` 如果它是一条线,如果是三角形则为 2,如果是 a) 四面体则为 3。
即使经过几天的错误搜索(我找到了一些,但仍然存在一个或多个错误),该算法仍无法正常工作。
你看到代码有什么问题了吗?因为我和我的两个 friend 都没有。
PS:我没有从 Casey 的视频中复制任何计算(例如方向 vector 的叉积)。看完之后,我自己下定决心,所以这里可能存在潜在的问题,尤其是在三维方面,凯西故意没有说。
我的支持功能:
//hullA/B: convex hull of A resp. B; baseA/B: location of A/B
Vector3f gjkSupport(Vector3f direction,
std::vector<GLfloat> hullA, std::vector<GLfloat> baseA,
std::vector<GLfloat> hullB, std::vector<GLfloat> baseB) {
//Initialize
GLfloat maxDotP = -std::numeric_limits<GLfloat>::max();
Vector3f furthestPointA, furthestPointB;
//Get furthest point in given direction out of hullA by getting the maximum dot
//product of the direction vector and a hull vertex's position vector
for (GLuint i = 0; i < hullA.size(); i += 3) {
Vector3f current (hullA[i]+baseA[0], hullA[i+1]+baseA[1], hullA[i+2]+baseA[2]);
// * = dot product
GLfloat dotP = direction * current;
if (dotP > maxDotP) {
maxDotP = dotP;
furthestPointA = current;
}
}
maxDotP = -std::numeric_limits<GLfloat>::max();
//Get furthest point in negative of the given direction out of hullB
for (GLuint i = 0; i < hullB.size(); i += 3) {
Vector3f current (hullB[i]+baseB[0], hullB[i+1]+baseB[1], hullB[i+2]+baseB[2]);
GLfloat dotP = -direction * current;
if (dotP > maxDotP) {
maxDotP = dotP;
furthestPointB = current;
}
}
//Furthest Minkowski Difference point is difference of d*A[i]-(-d)*B[j]
return furthestPointA - furthestPointB;
}
我的单纯形函数:
bool gjkSimplex(std::vector<Vector3f> &simplex, Vector3f &direction) {
GLuint simplexSize = simplex.size();
std::cout << simplexSize;
switch (simplexSize) {
//If the simplex is a line segment
case 2:
//Point is closest feature
if ((simplex[0]-simplex[1])*-simplex[1] < 0) {
std::cout << "A";
simplex = {simplex[1]};
//direction = A0
direction = -simplex[1];
//Line is closest feature
} else {
std::cout << "AB";
//direction = AB x (A0 x AB)
// ^ = cross product
direction = (simplex[0]-simplex[1]) ^ ((-simplex[1]) ^ (simplex[0]-simplex[1]));
}
break;
//If the simplex is a triangle
case 3:
//Point is closest feature
if ((simplex[0]-simplex[2])*(-simplex[2]) < 0 && (simplex[1]-simplex[2])*(-simplex[2]) < 0) {
std::cout << "A";
//direction = A0
direction = -simplex[2];
simplex = {simplex[1]};
//Line to second-latest point is closest feature
} else if ((((simplex[0]-simplex[2])^(simplex[1]-simplex[2]))^(simplex[1]-simplex[2]))*-simplex[2] > 0) {
std::cout << "AB";
//direction = AB x (A0 x AB)
direction = (simplex[1]-simplex[2]) ^ ((-simplex[2]) ^ (simplex[1]-simplex[2]));
simplex = {simplex[1], simplex[2]};
//Line to oldest point is closest feature
} else if (((simplex[0]-simplex[2])^((simplex[0]-simplex[2])^(simplex[1]-simplex[2])))*-simplex[2] > 0) {
std::cout << "AC";
//direction = AC x (A0 x AC)
direction = (simplex[0]-simplex[2]) ^ ((-simplex[2]) ^ (simplex[0]-simplex[2]));
simplex = {simplex[0], simplex[2]};
//Face is closest feature
} else {
//Origin is in direction AC x AB
if (((simplex[1]-simplex[2]) ^ (simplex[0]-simplex[2])) * (-simplex[2]) < 0) {
std::cout << "ACxAB";
//direction = AC x AB
direction = (simplex[0]-simplex[2]) ^ (simplex[1]-simplex[2]);
//origin is in direction AB x AC (other side of the face)
} else {
std::cout << "ABxAC";
//direction = AB x AC
direction = (simplex[1]-simplex[2]) ^ (simplex[0]-simplex[2]);
simplex = {simplex[1], simplex[0], simplex[2]};
}
}
break;
//If the simplex is a tetrahedron
case 4:
//Newest point is closest feature
if ((simplex[0]-simplex[3])*(-simplex[3]) < 0 && (simplex[1]-simplex[3])*(-simplex[3]) < 0 &&
(simplex[2]-simplex[3])*(-simplex[3]) < 0) {
std::cout << "A";
//direction = A0
direction = -simplex[3];
simplex = {simplex[3]};
//Edge between newest and second-newest point is closest feature
} else if ((((simplex[2]-simplex[3]) ^ ((simplex[1]-simplex[3]) ^ (simplex[2]-simplex[3]))) * (-simplex[2]) < 0) &&
((((simplex[1]-simplex[3]) ^ (simplex[0]-simplex[3])) ^ (simplex[2]-simplex[3])) * (-simplex[2]) < 0)) {
std::cout << "AB";
//direction = AB x (A0 x AB)
direction = (simplex[2]-simplex[3]) ^ ((-simplex[3]) ^ (simplex[2]-simplex[3]));
simplex = {simplex[2], simplex[3]};
//Edge between newest and third-newest vertex is closest feature
} else if ((((simplex[1]-simplex[3]) ^ ((simplex[0]-simplex[3]) ^ (simplex[1]-simplex[3]))) * (-simplex[2]) < 0) &&
((((simplex[0]-simplex[3]) ^ (simplex[2]-simplex[3])) ^ (simplex[1]-simplex[3])) * (-simplex[2]) < 0)) {
std::cout << "AC";
//direction = AC x (A0 x AC)
direction = (simplex[1]-simplex[3]) ^ ((-simplex[3]) ^ (simplex[1]-simplex[3]));
simplex = {simplex[1], simplex[3]};
//Edge between newest and oldest point is closest feature
} else if ((((simplex[0]-simplex[3]) ^ ((simplex[2]-simplex[3]) ^ (simplex[0]-simplex[3]))) * (-simplex[2]) < 0) &&
((((simplex[2]-simplex[3]) ^ (simplex[1]-simplex[3])) ^ (simplex[0]-simplex[3])) * (-simplex[2]) < 0)) {
std::cout << "AD";
//direction = AD x (A0 x AD)
direction = (simplex[0]-simplex[3]) ^ ((-simplex[3]) ^ (simplex[0]-simplex[3]));
simplex = {simplex[0], simplex[3]};
//Face between the three newest points is closest feature
} else if (((simplex[1]-simplex[3]) ^ (simplex[2]-simplex[3])) * (-simplex[3]) > 0) {
std::cout << "ABC";
//direction = AC x AB (outer normal of face)
direction = (simplex[1]-simplex[3]) ^ (simplex[2]-simplex[3]);
simplex = {simplex[1], simplex[3], simplex[2]};
//Face between newest, second-newest and oldest point is closest feature
} else if (((simplex[2]-simplex[3]) ^ (simplex[0]-simplex[3])) * (-simplex[3]) > 0) {
std::cout << "ABD";
//direction = AB x AD
direction = (simplex[2]-simplex[3]) ^ (simplex[0]-simplex[3]);
simplex = {simplex[0], simplex[2], simplex[3]};
//Face between newest, second-oldest and oldest point is closest feature
} else if (((simplex[0]-simplex[3]) ^ (simplex[1]-simplex[3])) * (-simplex[3]) > 0) {
std::cout << "ACD";
//direction = AD x AC
direction = (simplex[0]-simplex[3]) ^ (simplex[1]-simplex[3]);
simplex = {simplex[0], simplex[3], simplex[1]};
//Origin is encased by simplex
} else {
//Collision detected
std::cout << "ABCD";
return true;
}
break;
default:
direction = {1,1,1};
simplex = {};
break;
}
std::cout << "\n";
return false;
};
GJK 主循环:
//Narrow Phase collision function using GJK
bool SolidObject::collidesWith(SolidObject *object) {
//Initialize by using an arbitrary direction
Vector3f direction (1,1,1);
std::vector<Vector3f> simplex;
Vector3f point = gjkSupport(direction,
this->meshes[0].getConvexHull(), this->base, object->meshes[0].getConvexHull(), object->base);
simplex = {point};
//Set direction to the negative of the resulting point
direction = -point;
bool originInSimplex = false;
while (!originInSimplex) {
//Get furthest point in new direction
point = gjkSupport(direction,
this->meshes[0].getConvexHull(), this->base, object->meshes[0].getConvexHull(), object->base);
//The furthest point in the negative direction is not in the opposing octant
// => no collision
if (point*direction < 0) {
return false;
}
//Add point to the simplex
simplex.push_back(point);
//Update simplex and direction, and return whether the simplex contains the origin
originInSimplex = gjkSimplex(simplex, direction);
}
std::cout << "\n";
return true;
}
最佳答案
在三角形的情况下:
//Point is closest feature
if ((simplex[0]-simplex[2])*(-simplex[2]) < 0 && (simplex[1]-simplex[2])*(-simplex[2]) < 0) {
std::cout << "A";
//direction = A0
direction = -simplex[2];
simplex = {simplex[1]};
}
应该是
simplex = {simplex[2]};
在四面体的情况下:
所有边检查都使用simplex[2]
执行点生成,但它们应该使用最新的点simplex[3]
。
我认为你的第一个边缘检查在第二个条件下使用了错误的脸,所以而不是
if ((((simplex[2]-simplex[3]) ^ ((simplex[1]-simplex[3]) ^ (simplex[2]-simplex[3]))) * (-simplex[2]) < 0) &&
((((simplex[1]-simplex[3]) ^ (simplex[0]-simplex[3])) ^ (simplex[2]-simplex[3])) * (-simplex[2]) < 0)) {
std::cout << "AB";
//direction = AB x (A0 x AB)
direction = (simplex[2]-simplex[3]) ^ ((-simplex[3]) ^ (simplex[2]-simplex[3]));
simplex = {simplex[2], simplex[3]};
}
应该是
if ((((simplex[2]-simplex[3]) ^ ((simplex[1]-simplex[3]) ^ (simplex[2]-simplex[3]))) * (-simplex[3]) < 0) &&
((((simplex[2]-simplex[3]) ^ (simplex[0]-simplex[3])) ^ (simplex[2]-simplex[3])) * (-simplex[3]) < 0)) {
std::cout << "AB";
//direction = AB x (A0 x AB)
direction = (simplex[2]-simplex[3]) ^ ((-simplex[3]) ^ (simplex[2]-simplex[3]));
simplex = {simplex[2], simplex[3]};
}
第二个边检查的第二个条件也一样,应该是:
((((simplex[1]-simplex[3]) ^ (simplex[2]-simplex[3])) ^ (simplex[1]-simplex[3])) * (-simplex[3]) < 0)
和第三次边缘检查的第二个条件:
((((simplex[0]-simplex[3]) ^ (simplex[1]-simplex[3])) ^ (simplex[0]-simplex[3])) * (-simplex[3]) < 0)
我还修复了三角形检查的输出单纯形。输入单纯形的最后一点应该始终是输出单纯形的最后一点。此外,点的顺序应与计算的方向一致。
这里是完整的固定函数:
bool gjkSimplex(std::vector<Vector3f> &simplex, Vector3f &direction) {
GLuint simplexSize = simplex.size();
std::cout << simplexSize;
switch (simplexSize) {
//If the simplex is a line segment
case 2:
//Point is closest feature
if ((simplex[0]-simplex[1])*-simplex[1] < 0) {
std::cout << "A";
//direction = A0
direction = -simplex[1];
simplex = {simplex[1]};
//Line is closest feature
} else {
std::cout << "AB";
//direction = AB x (A0 x AB)
// ^ = cross product
direction = (simplex[0]-simplex[1]) ^ ((-simplex[1]) ^ (simplex[0]-simplex[1]));
}
break;
//If the simplex is a triangle
case 3:
//Point is closest feature
if ((simplex[0]-simplex[2])*(-simplex[2]) < 0 && (simplex[1]-simplex[2])*(-simplex[2]) < 0) {
std::cout << "A";
//direction = A0
direction = -simplex[2];
simplex = {simplex[2]};
//Line to second-latest point is closest feature
} else if ((((simplex[0]-simplex[2])^(simplex[1]-simplex[2]))^(simplex[1]-simplex[2]))*-simplex[2] > 0) {
std::cout << "AB";
//direction = AB x (A0 x AB)
direction = (simplex[1]-simplex[2]) ^ ((-simplex[2]) ^ (simplex[1]-simplex[2]));
simplex = {simplex[1], simplex[2]};
//Line to oldest point is closest feature
} else if (((simplex[0]-simplex[2])^((simplex[0]-simplex[2])^(simplex[1]-simplex[2])))*-simplex[2] > 0) {
std::cout << "AC";
//direction = AC x (A0 x AC)
direction = (simplex[0]-simplex[2]) ^ ((-simplex[2]) ^ (simplex[0]-simplex[2]));
simplex = {simplex[0], simplex[2]};
//Face is closest feature
} else {
//Origin is in direction AC x AB
if (((simplex[1]-simplex[2]) ^ (simplex[0]-simplex[2])) * (-simplex[2]) < 0) {
std::cout << "ACxAB";
//direction = AC x AB
direction = (simplex[0]-simplex[2]) ^ (simplex[1]-simplex[2]);
//origin is in direction AB x AC (other side of the face)
} else {
std::cout << "ABxAC";
//direction = AB x AC
direction = (simplex[1]-simplex[2]) ^ (simplex[0]-simplex[2]);
simplex = {simplex[1], simplex[0], simplex[2]};
}
}
break;
//If the simplex is a tetrahedron
case 4:
//Newest point is closest feature
if ((simplex[0]-simplex[3])*(-simplex[3]) < 0 && (simplex[1]-simplex[3])*(-simplex[3]) < 0 &&
(simplex[2]-simplex[3])*(-simplex[3]) < 0) {
std::cout << "A";
//direction = A0
direction = -simplex[3];
simplex = {simplex[3]};
//Edge between newest and second-newest point is closest feature
} else if ((((simplex[2]-simplex[3]) ^ ((simplex[1]-simplex[3]) ^ (simplex[2]-simplex[3]))) * (-simplex[3]) < 0) &&
((((simplex[2]-simplex[3]) ^ (simplex[0]-simplex[3])) ^ (simplex[2]-simplex[3])) * (-simplex[3]) < 0)) {
std::cout << "AB";
//direction = AB x (A0 x AB)
direction = (simplex[2]-simplex[3]) ^ ((-simplex[3]) ^ (simplex[2]-simplex[3]));
simplex = {simplex[2], simplex[3]};
//Edge between newest and third-newest vertex is closest feature
} else if ((((simplex[1]-simplex[3]) ^ ((simplex[0]-simplex[3]) ^ (simplex[1]-simplex[3]))) * (-simplex[3]) < 0) &&
((((simplex[1]-simplex[3]) ^ (simplex[2]-simplex[3])) ^ (simplex[1]-simplex[3])) * (-simplex[3]) < 0)) {
std::cout << "AC";
//direction = AC x (A0 x AC)
direction = (simplex[1]-simplex[3]) ^ ((-simplex[3]) ^ (simplex[1]-simplex[3]));
simplex = {simplex[1], simplex[3]};
//Edge between newest and oldest point is closest feature
} else if ((((simplex[0]-simplex[3]) ^ ((simplex[2]-simplex[3]) ^ (simplex[0]-simplex[3]))) * (-simplex[3]) < 0) &&
((((simplex[0]-simplex[3]) ^ (simplex[1]-simplex[3])) ^ (simplex[0]-simplex[3])) * (-simplex[3]) < 0)) {
std::cout << "AD";
//direction = AD x (A0 x AD)
direction = (simplex[0]-simplex[3]) ^ ((-simplex[3]) ^ (simplex[0]-simplex[3]));
simplex = {simplex[0], simplex[3]};
//Face between the three newest points is closest feature
} else if (((simplex[1]-simplex[3]) ^ (simplex[2]-simplex[3])) * (-simplex[3]) > 0) {
std::cout << "ABC";
//direction = AC x AB (outer normal of face)
direction = (simplex[1]-simplex[3]) ^ (simplex[2]-simplex[3]);
simplex = {simplex[1], simplex[2], simplex[3]};
//Face between newest, second-newest and oldest point is closest feature
} else if (((simplex[2]-simplex[3]) ^ (simplex[0]-simplex[3])) * (-simplex[3]) > 0) {
std::cout << "ABD";
//direction = AB x AD
direction = (simplex[2]-simplex[3]) ^ (simplex[0]-simplex[3]);
simplex = {simplex[2], simplex[0], simplex[3]};
//Face between newest, second-oldest and oldest point is closest feature
} else if (((simplex[0]-simplex[3]) ^ (simplex[1]-simplex[3])) * (-simplex[3]) > 0) {
std::cout << "ACD";
//direction = AD x AC
direction = (simplex[0]-simplex[3]) ^ (simplex[1]-simplex[3]);
simplex = {simplex[0], simplex[1], simplex[3]};
//Origin is encased by simplex
} else {
//Collision detected
std::cout << "ABCD";
return true;
}
break;
default:
direction = {1,1,1};
simplex = {};
break;
}
std::cout << "\n";
return false;
};
关于c++ - GJK 算法陷入不同 Voronoi 区域案例的循环中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25750552/
在使用 GDI 进行图形处理的传统 Windows 程序中,您必须担心只绘制需要重绘的窗口区域;这是“更新矩形”,可以通过 PAINTSTRUCT.rcPaint 或通过调用 GetUpdateRec
我对 TFS 中的所有内容感到困惑。有人可以解释一下所有这些是如何组合在一起的吗? 团队项目合集 团队项目 团队 地区 迭代 来自 this page,我认为一个(团队)项目集合可以包含多个(团队)项
这对我来说根本没有意义。创建新的 API 网关时,您可以指定它是区域优化的还是边缘优化的。但话又说回来,当您为 API Gateway 创建自定义域名时,您可以在两者之间进行选择。 最糟糕的是,您可以
我有一个工作表,其中包含我想循环遍历的许多不同区域。我将进行一些计算,并认为它比在列中逐个单元格地更快/更有效。 我尝试了一些不同的东西,但不知道如何跳到下一个区域。我已经发表了一些评论,最有希望的是
最近迁移到 TFS 2010 后,我想知道对于区域而言,最佳或最广泛接受的定义或配置是什么? 我能在网上找到的唯一有用的文章是 this one并且是我认为是正确的。然而,这让我开始思考是否以下任何一
我在“北欧”有一个存储帐户,现在我想使用生命周期管理将冷 blob 移动到存档层。 但是我每次尝试都会遇到以下失败:无法为存储帐户“myStorageAccount”添加生命周期管理规则。错误:管理策
我正在开发一个项目,您在扫描仪中输入州的缩写,然后程序会告诉您该州位于美国的哪个地区。我认为我的大部分代码都是正确的,但我有以前从未使用过 switch。 我已经将我的工作改进为一个错误,即我的第一个
我正在尝试学习 AngularJS,我想我有一个架构问题。 如果我想开始开发一个应用程序,比方说,比如 youtube,我如何在“区域”(比如 Marionette)中组织页面?我的意思是:顶部导航栏
我正在制作一个动态内存分配器,当我释放其中的一部分时,我需要检查我传递给函数的指针实际上是否在该区域内。我有一个指向 malloc 区域开头的指针 typedef unsigned char byte
有时我想看看 到底在哪里页面上图片上的标签。 在 Javascript 中有没有办法改变颜色或隐藏与区域标签对应的图像部分? 最佳答案 也许您正在寻找类似 mapper.js 的内容.它允许您在鼠标
我有一个使用 D3 js 创建的时间序列图表。我想为特定时间间隔添加高亮区域,以显示在该特定时间发生的特定事件(会有不同类型的事件,因此每个高亮标记将根据其类型具有不同的颜色)。我希望这个突出显示区域
我正在尝试创建网站的密码保护区。我想通过检查 MySql 表中的用户名和密码来允许访问,然后启动一个 session 并允许在 session 处于事件状态时访问多个页面。如果有人尝试直接访问这些页面
在 ScrollView 中我添加了几个按钮。正如您在图片中看到的,2 个按钮完全可见,第三个按钮半可见。当我向右滚动看到第三个时;1-如果滚动条很短,则它会滚动回到第一个位置。2- 如果滚动足够则显
我正在开发我的第一个 spritekit 应用程序,并且有一个关于如何处理我想到的事情的快速问题。我正在制作一个我想要的棋盘游戏原型(prototype),并希望在 iOS map 中进行一些集思广益
在我的 C# 程序中,我收到这样的日期和时间:DateTime.Now 我得到:19/09/2010 20:10:30 因为我的地区是:希伯来语(以色列) 但是如果我将我的程序安装在区域为 Engli
有时我在 Visual Studio 的源文件中运行(在我的例子中是 2010),我看到很多区域,我必须点击 + 号才能一个一个地打开它们! 是否有任何快捷方式或菜单选项可以使这对我来说更容易,并且对
关闭。这个问题是opinion-based .它目前不接受答案。 想要改进这个问题? 更新问题,以便 editing this post 可以用事实和引用来回答它. 关闭 7 年前。 Improve
我正在寻找二维数据中的峰值区域(如果您愿意,灰度图像或二维景观,通过霍夫变换创建)。 峰值区域是指局部最大峰值,但不是单个点而是周围的一部分strong>contributing region 随之而
我希望能够将任何字符或字符串转换为形状或区域,以便我可以按照我喜欢的任何大小、样式、效果等来绘制该字符。 更具体地说,我将使用视差绘制它,以便它仅在特定角度清晰定义(这就是为什么我不能使用 html
我非常喜欢数学(或者你们大多数人会说的“数学”!),但我还没有达到知道这个问题答案的程度。我有一个主圆,它可以在显示器上的任何 x 和 y 处有一个中心点。其他圆圈将随意在显示器周围移动,但在任何给定
我是一名优秀的程序员,十分优秀!