TA的每日心情![](source/plugin/dsu_paulsign/img/emot/kx.gif) | 开心 昨天 15:13 |
---|
签到天数: 153 天 [LV.7]常住居民III
管理员
中国膜结构网www.mjgou.com
- 积分
- 2147483647
![QQ](static/image/common/qq_big.gif)
|
发表于 2024-6-29 16:35:28
|
显示全部楼层
- nfp 临界多边形算法移动碰撞法的基本步骤如下: 首先根据多边形A和B当前时刻的靠接状态,得到B下一步的移动方向,在该移动方向上,计算出A和B之间的最小碰撞距离,从而得到移动距离,根据移动方向和移动距离,将B移动到新的位置,然后重复以上过程,直至绕完一圈,回到初始位置。 完整c++代码 static vector<AcGePoint2d> NoFitPolygon(vector<AcGePoint2d> py1, vector<AcGePoint2d> py2)
- 实现NFP(Non-Fitting Polygon)临界多边形算法涉及多个步骤,包括计算多边形的凸包、确定接触点、计算最小碰撞距离等。下面是一个简化版的C++伪代码示例,用于演示如何使用NFP算法来处理两个多边形A和B的碰撞检测和响应:
- Cpp
- #include <vector>
- #include <iostream>
- struct AcGePoint2d {
- double x;
- double y;
- };
- // 简化版本的NFP算法实现
- std::vector<AcGePoint2d> NoFitPolygon(std::vector<AcGePoint2d> poly1, std::vector<AcGePoint2d> poly2) {
- std::vector<AcGePoint2d> result;
- // 假设poly1和poly2已经经过预处理,确保它们是凸的且在原点附近
- while (true) {
- // 计算poly1和poly2之间的最小距离和相应的法线
- AcGePoint2d normal = calculateNormal(poly1, poly2);
- double minDistance = calculateMinDistance(poly1, poly2, normal);
- if (minDistance > 0) {
- // 如果最小距离大于0,则poly1和poly2之间存在碰撞
- // 根据碰撞响应调整poly2的位置
- movePolygon(poly2, -minDistance * normal);
- } else {
- // 如果最小距离小于等于0,则poly1和poly2没有碰撞
- break;
- }
- // 检查是否回到了初始位置
- if (isBackToStart(poly1, poly2)) {
- break;
- }
- }
- return result; // 返回结果,这里可能需要返回一些关于NFP的信息,如碰撞点或碰撞次数
- }
- AcGePoint2d calculateNormal(std::vector<AcGePoint2d> poly1, std::vector<AcGePoint2d> poly2) {
- // 计算poly1和poly2之间的最小距离的法线
- // 这里省略了具体的实现细节
- AcGePoint2d normal;
- return normal;
- }
- double calculateMinDistance(std::vector<AcGePoint2d> poly1, std::vector<AcGePoint2d> poly2, AcGePoint2d normal) {
- // 计算poly1和poly2之间的最小距离
- // 这里省略了具体的实现细节
- double minDistance = 0;
- return minDistance;
- }
- void movePolygon(std::vector<AcGePoint2d>& poly, AcGePoint2d direction) {
- // 根据方向移动多边形
- for (auto& point : poly) {
- point.x += direction.x;
- point.y += direction.y;
- }
- }
- bool isBackToStart(std::vector<AcGePoint2d> poly1, std::vector<AcGePoint2d> poly2) {
- // 检查是否回到了初始位置
- // 这里省略了具体的实现细节
- bool backToStart = false;
- return backToStart;
- }
- 请注意,上述代码中的calculateNormal、calculateMinDistance、movePolygon和isBackToStart函数都需要具体实现。此外,实际应用中可能还需要考虑多边形旋转、速度等因素。
- 由于NFP算法的具体实现较为复杂,涉及到几何计算、线性代数等多个领域的知识,上述代码仅提供了一个框架性的示例,具体实现时需要根据实际情况进行详细设计和编程。
- double computeCollisionDistance(vector<AcGePoint2d>& poly1, vector<AcGePoint2d>& poly2) {
- // 遍历poly2边界,与poly1边界对比
- // ...
- } 完整的C++代码 给出
- 计算两个多边形碰撞距离的过程通常涉及找到能够分离这两个多边形的轴,这个轴可以是其中一个多边形的边的法线。下面是一个基于分离轴定理(Separating Axis Theorem, SAT)的简化版C++函数,用于计算两个多边形之间的最小碰撞距离。请注意,为了简化问题,我们假设输入的多边形是凸的。
- Cpp
- #include <vector>
- #include <algorithm>
- #include <cmath>
- struct AcGePoint2d {
- double x;
- double y;
- };
- // 向量点乘
- double dotProduct(const AcGePoint2d& v1, const AcGePoint2d& v2) {
- return v1.x * v2.x + v1.y * v2.y;
- }
- // 向量叉乘
- double crossProduct(const AcGePoint2d& v1, const AcGePoint2d& v2) {
- return v1.x * v2.y - v1.y * v2.x;
- }
- // 计算向量的长度
- double vectorLength(const AcGePoint2d& v) {
- return std::sqrt(v.x * v.x + v.y * v.y);
- }
- // 计算向量的单位化
- AcGePoint2d normalizeVector(const AcGePoint2d& v) {
- double length = vectorLength(v);
- return {v.x / length, v.y / length};
- }
- // 计算两个多边形之间的最小碰撞距离
- double computeCollisionDistance(std::vector<AcGePoint2d>& poly1, std::vector<AcGePoint2d>& poly2) {
- double minDistance = std::numeric_limits<double>::max();
- AcGePoint2d normal;
- // 对于poly1的每条边
- for (size_t i = 0; i < poly1.size(); ++i) {
- size_t nextIndex = (i + 1) % poly1.size();
- AcGePoint2d edge = {poly1[nextIndex].x - poly1[i].x, poly1[nextIndex].y - poly1[i].y};
- AcGePoint2d edgeNormal = normalizeVector({-edge.y, edge.x});
- double minProjPoly1 = dotProduct(edgeNormal, poly1[i]);
- double maxProjPoly1 = minProjPoly1;
- for (size_t j = 0; j < poly1.size(); ++j) {
- double proj = dotProduct(edgeNormal, poly1[j]);
- minProjPoly1 = std::min(minProjPoly1, proj);
- maxProjPoly1 = std::max(maxProjPoly1, proj);
- }
- double minProjPoly2 = dotProduct(edgeNormal, poly2[0]);
- double maxProjPoly2 = minProjPoly2;
- for (size_t j = 0; j < poly2.size(); ++j) {
- double proj = dotProduct(edgeNormal, poly2[j]);
- minProjPoly2 = std::min(minProjPoly2, proj);
- maxProjPoly2 = std::max(maxProjPoly2, proj);
- }
- if (maxProjPoly1 < minProjPoly2 || maxProjPoly2 < minProjPoly1) {
- // 如果两个多边形在这个轴上的投影不重叠,则没有碰撞
- return std::numeric_limits<double>::max();
- }
- double overlap = std::max(0.0, std::min(maxProjPoly1, maxProjPoly2) - std::max(minProjPoly1, minProjPoly2));
- if (overlap < minDistance) {
- minDistance = overlap;
- normal = edgeNormal;
- }
- }
- // 对于poly2的每条边,重复上述过程
- for (size_t i = 0; i < poly2.size(); ++i) {
- size_t nextIndex = (i + 1) % poly2.size();
- AcGePoint2d edge = {poly2[nextIndex].x - poly2[i].x, poly2[nextIndex].y - poly2[i].y};
- AcGePoint2d edgeNormal = normalizeVector({-edge.y, edge.x});
- double minProjPoly1 = dotProduct(edgeNormal, poly1[0]);
- double maxProjPoly1 = minProjPoly1;
- for (size_t j = 0; j < poly1.size(); ++j) {
- double proj = dotProduct(edgeNormal, poly1[j]);
- minProjPoly1 = std::min(minProjPoly1, proj);
- maxProjPoly1 = std::max(maxProjPoly1, proj);
- }
- double minProjPoly2 = dotProduct(edgeNormal, poly2[i]);
- double maxProjPoly2 = minProjPoly2;
- for (size_t j = 0; j < poly2.size(); ++j) {
- double proj = dotProduct(edgeNormal, poly2[j]);
- minProjPoly2 = std::min(minProjPoly2, proj);
- maxProjPoly2 = std::max(maxProjPoly2, proj);
- }
- if (maxProjPoly1 < minProjPoly2 || maxProjPoly2 < minProjPoly1) {
- return std::numeric_limits<double>::max();
- }
- double overlap = std::max(0.0, std::min(maxProjPoly1, maxProjPoly2) - std::max(minProjPoly1, minProjPoly2));
- if (overlap < minDistance) {
- minDistance = overlap;
- normal = edgeNormal;
- }
- }
- return minDistance;
- }
- 这段代码实现了通过遍历每个多边形的所有边,并检查它们的法线方向是否能作为分离轴来判断两个多边形是否有碰撞,以及如果碰撞发生,计算最小碰撞距离。请注意,这个函数返回的是两个多边形之间的最小重叠距离,如果返回值为正,则表示两个多边形有碰撞;如果返回值为std::numeric_limits<double>::max(),则表示没有碰撞。
- // 计算多边形之间的最小碰撞距离和碰撞方向 double CalculateCollisionDistanceAndDirection(const std::vector<AcGePoint2d>& polyA, const std::vector<AcGePoint2d>& polyB, Vector2d& collisionDirection) { // 实现计算最小碰撞距离和碰撞方向的逻辑 // 这里需要使用到多边形顶点之间的距离计算、向量运算等 // ... return minDistance; // 返回最小碰撞距离 }完整的C++代码
- 在计算两个多边形之间的最小碰撞距离及其方向时,我们可以采用分离轴定理(Separating Axis Theorem, SAT)。以下是基于SAT的C++代码实现,它不仅计算了最小碰撞距离,还返回了碰撞方向。我们使用Vector2d作为collisionDirection的类型,这可以是一个自定义的结构体或者类来表示二维向量。
- 首先,我们需要定义Vector2d类型:
- Cpp
- struct Vector2d {
- double x, y;
- Vector2d(double x = 0.0, double y = 0.0) : x(x), y(y) {}
- Vector2d operator-(const Vector2d& other) const {
- return Vector2d(x - other.x, y - other.y);
- }
- double dot(const Vector2d& other) const {
- return x * other.x + y * other.y;
- }
- Vector2d normalized() const {
- double len = sqrt(x * x + y * y);
- return Vector2d(x / len, y / len);
- }
- };
- 接下来是主要的函数实现:
- Cpp
- double CalculateCollisionDistanceAndDirection(
- const std::vector<AcGePoint2d>& polyA,
- const std::vector<AcGePoint2d>& polyB,
- Vector2d& collisionDirection)
- {
- double minOverlap = std::numeric_limits<double>::max();
- Vector2d bestAxis;
- // 遍历polyA的边,计算其法线并作为分离轴
- for (size_t i = 0; i < polyA.size(); ++i) {
- size_t nextIndex = (i + 1) % polyA.size();
- Vector2d edge = Vector2d(polyA[nextIndex].x - polyA[i].x, polyA[nextIndex].y - polyA[i].y);
- Vector2d axis = edge.normalized().rotated90CCW();
- double minA = std::numeric_limits<double>::max();
- double maxA = std::numeric_limits<double>::lowest();
- for (const auto& p : polyA) {
- double projection = Vector2d(p.x, p.y).dot(axis);
- minA = std::min(minA, projection);
- maxA = std::max(maxA, projection);
- }
- double minB = std::numeric_limits<double>::max();
- double maxB = std::numeric_limits<double>::lowest();
- for (const auto& p : polyB) {
- double projection = Vector2d(p.x, p.y).dot(axis);
- minB = std::min(minB, projection);
- maxB = std::max(maxB, projection);
- }
- if (maxA < minB || maxB < minA) {
- return std::numeric_limits<double>::max(); // 不相交
- }
- double overlap = std::max(0.0, std::min(maxA, maxB) - std::max(minA, minB));
- if (overlap < minOverlap) {
- minOverlap = overlap;
- bestAxis = axis;
- }
- }
- // 遍历polyB的边,计算其法线并作为分离轴
- for (size_t i = 0; i < polyB.size(); ++i) {
- size_t nextIndex = (i + 1) % polyB.size();
- Vector2d edge = Vector2d(polyB[nextIndex].x - polyB[i].x, polyB[nextIndex].y - polyB[i].y);
- Vector2d axis = edge.normalized().rotated90CCW();
- double minA = std::numeric_limits<double>::max();
- double maxA = std::numeric_limits<double>::lowest();
- for (const auto& p : polyA) {
- double projection = Vector2d(p.x, p.y).dot(axis);
- minA = std::min(minA, projection);
- maxA = std::max(maxA, projection);
- }
- double minB = std::numeric_limits<double>::max();
- double maxB = std::numeric_limits<double>::lowest();
- for (const auto& p : polyB) {
- double projection = Vector2d(p.x, p.y).dot(axis);
- minB = std::min(minB, projection);
- maxB = std::max(maxB, projection);
- }
- if (maxA < minB || maxB < minA) {
- return std::numeric_limits<double>::max(); // 不相交
- }
- double overlap = std::max(0.0, std::min(maxA, maxB) - std::max(minA, minB));
- if (overlap < minOverlap) {
- minOverlap = overlap;
- bestAxis = axis;
- }
- }
- collisionDirection = bestAxis;
- return minOverlap;
- }
- 注意,上面的代码中,axis.rotated90CCW()函数用于计算一个向量逆时针旋转90度后的向量,这可以通过简单的坐标变换实现,例如:
- Cpp
- Vector2d Vector2d::rotated90CCW() const {
- return Vector2d(-y, x);
- }
- 这个函数实现了计算两个多边形之间的最小碰撞距离和碰撞方向的核心逻辑,其中collisionDirection参数用于输出碰撞方向。如果两个多边形不相交,则函数会返回一个非常大的数(通过std::numeric_limits<double>::max()表示),表示没有碰撞发生。
复制代码 |
|