本文整理汇总了C++中Polygon2d::ComputeCenter方法的典型用法代码示例。如果您正苦于以下问题:C++ Polygon2d::ComputeCenter方法的具体用法?C++ Polygon2d::ComputeCenter怎么用?C++ Polygon2d::ComputeCenter使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Polygon2d
的用法示例。
在下文中一共展示了Polygon2d::ComputeCenter方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: PolygonCollisionTest
CollisionResult GD_API PolygonCollisionTest(Polygon2d& p1,
Polygon2d& p2,
bool ignoreTouchingEdges) {
if (p1.vertices.size() < 3 || p2.vertices.size() < 3) {
CollisionResult result;
result.collision = false;
result.move_axis.x = 0.0f;
result.move_axis.y = 0.0f;
return result;
}
p1.ComputeEdges();
p2.ComputeEdges();
sf::Vector2f edge;
sf::Vector2f move_axis(0, 0);
sf::Vector2f mtd(0, 0);
float min_dist = FLT_MAX;
CollisionResult result;
// Iterate over all the edges composing the polygons
for (std::size_t i = 0; i < p1.vertices.size() + p2.vertices.size(); i++) {
if (i < p1.vertices.size()) // or <=
{
edge = p1.edges[i];
} else {
edge = p2.edges[i - p1.vertices.size()];
}
sf::Vector2f axis(
-edge.y, edge.x); // Get the axis to which polygons will be projected
normalise(axis);
float minA = 0;
float minB = 0;
float maxA = 0;
float maxB = 0;
project(axis, p1, minA, maxA);
project(axis, p2, minB, maxB);
float dist = distance(minA, maxA, minB, maxB);
if (dist > 0.0f || (dist == 0.0 && ignoreTouchingEdges)) {
// If the projections on the axis do not overlap, then
// there is no collision
result.collision = false;
result.move_axis.x = 0.0f;
result.move_axis.y = 0.0f;
return result;
}
float absDist = std::abs(dist);
if (absDist < min_dist) {
min_dist = absDist;
move_axis = axis;
}
}
result.collision = true;
sf::Vector2f d = p1.ComputeCenter() - p2.ComputeCenter();
if (dotProduct(d, move_axis) < 0.0f) move_axis = -move_axis;
result.move_axis = move_axis * min_dist;
return result;
}