这有点复杂:AutoPolygon给你一堆三角形 - PhysicsBody :: createPolygon需要一个凸多边形顺时针缠绕...所以这些是两个不同的东西。顶点数甚至可能是有限的。我认为Box2d对于1多边形的最大数量是8.
如果您想尝试此操作,则必须合并三角形以形成多边形。一个选项可以是从一个三角形开始,只要整个事物保持凸起就可以增加更多。如果不能添加更多的三角形,请重新开始一个新的多边形。将所有多边形作为PhysicsShapes添加到物理主体以形成一个复合对象。
我建议,因为
- Autopolygon被渲染优化你不走这条道路 - 不是最适合的 物理学 - 这是有区别的。自动多边形跟踪的多边形将始终比原始精灵大 - 否则,您会看到渲染伪像。
- 你必须接近对生成多边形
- 跟踪形状在应用程序中没有控制会增加你的启动时间
- 三角网格和物理轮廓是2件不同的事情
我会尝试一些不同方法:离线生成碰撞形状。这给你一堆优势:
- 你可以在可视化编辑器中生成和调整多边形,例如,通过使用PhysicsEditor
- 加载的准备多边形的方式更快
- 您可以设置类似质量等其他参数
- 的解决方案是战斗证明和开箱
,但如果你想知道多边形如何相交。你可以看看这段代码。
// Calculate the projection of a polygon on an axis
// and returns it as a [min, max] interval
public void ProjectPolygon(Vector axis, Polygon polygon, ref float min, ref float max) {
// To project a point on an axis use the dot product
float dotProduct = axis.DotProduct(polygon.Points[0]);
min = dotProduct;
max = dotProduct;
for (int i = 0; i < polygon.Points.Count; i++) {
flaot d = polygon.Points[i].DotProduct(axis);
if (d < min) {
min = dotProduct;
} else {
if (dotProduct> max) {
max = dotProduct;
}
}
}
}
// Calculate the distance between [minA, maxA] and [minB, maxB]
// The distance will be negative if the intervals overlap
public float IntervalDistance(float minA, float maxA, float minB, float maxB) {
if (minA < minB) {
return minB - maxA;
} else {
return minA - maxB;
}
}
// Check if polygon A is going to collide with polygon B.
public boolean PolygonCollision(Polygon polygonA, Polygon polygonB) {
boolean result = true;
int edgeCountA = polygonA.Edges.Count;
int edgeCountB = polygonB.Edges.Count;
float minIntervalDistance = float.PositiveInfinity;
Vector edge;
// Loop through all the edges of both polygons
for (int edgeIndex = 0; edgeIndex < edgeCountA + edgeCountB; edgeIndex++) {
if (edgeIndex < edgeCountA) {
edge = polygonA.Edges[edgeIndex];
} else {
edge = polygonB.Edges[edgeIndex - edgeCountA];
}
// ===== Find if the polygons are currently intersecting =====
// Find the axis perpendicular to the current edge
Vector axis = new Vector(-edge.Y, edge.X);
axis.Normalize();
// Find the projection of the polygon on the current axis
float minA = 0; float minB = 0; float maxA = 0; float maxB = 0;
ProjectPolygon(axis, polygonA, ref minA, ref maxA);
ProjectPolygon(axis, polygonB, ref minB, ref maxB);
// Check if the polygon projections are currentlty intersecting
if (IntervalDistance(minA, maxA, minB, maxB) > 0)
result = false;
return result;
}
}
该功能可以采用这种方式
boolean result = PolygonCollision(polygonA, polygonB);
据我所知,现在是不可能的。我在论坛上看过一些他们想要在AutoPolygons之间添加碰撞检测的地方。现在我想你必须使用物理。 – Makalele