本文整理汇总了C#中Vertices.GetEdge方法的典型用法代码示例。如果您正苦于以下问题:C# Vertices.GetEdge方法的具体用法?C# Vertices.GetEdge怎么用?C# Vertices.GetEdge使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Vertices
的用法示例。
在下文中一共展示了Vertices.GetEdge方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: PolygonCollision
/// <summary>
/// Check if polygon A is going to collide with polygon B.
/// The last parameter is the *relative* velocity
/// of the polygons (i.e. velocityA - velocityB)
/// </summary>
/// <param name="polygonA">The polygon A.</param>
/// <param name="polygonB">The polygon B.</param>
/// <param name="velocity">The velocity.</param>
/// <returns></returns>
internal PolygonCollisionResult PolygonCollision(Vertices polygonA, Vertices polygonB, Vector2 velocity)
{
PolygonCollisionResult result = new PolygonCollisionResult();
result.Intersect = true;
result.WillIntersect = true;
int edgeCountA = polygonA.Count;
int edgeCountB = polygonB.Count;
float minIntervalDistance = float.PositiveInfinity;
Vector2 translationAxis = new Vector2();
Vector2 edge;
// Loop through all the edges of both polygons
for (int edgeIndex = 0; edgeIndex < edgeCountA + edgeCountB; edgeIndex++)
{
if (edgeIndex < edgeCountA)
{
edge = polygonA.GetEdge(edgeIndex);
}
else
{
edge = polygonB.GetEdge(edgeIndex - edgeCountA);
}
// ===== 1. Find if the polygons are currently intersecting =====
// Find the axis perpendicular to the current edge
Vector2 axis = new Vector2(-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
float intervalDistance = IntervalDistance(minA, maxA, minB, maxB);
if (intervalDistance > 0)
result.Intersect = false;
// ===== 2. Now find if the polygons *will* intersect =====
// Project the velocity on the current axis
//float velocityProjection = Vector2.Dot(axis, velocity);
// Get the projection of polygon A during the movement
//if (velocityProjection < 0) {
// minA += velocityProjection;
//} else {
// maxA += velocityProjection;
//}
// Do the same test as above for the new projection
//float intervalDistance = IntervalDistance(minA, maxA, minB, maxB);
//if (intervalDistance > 0) result.WillIntersect = false;
// If the polygons are not intersecting and won't intersect, exit the loop
//if (!result.Intersect && !result.WillIntersect) break;
if (!result.Intersect) break;
// Check if the current interval distance is the minimum one. If so store
// the interval distance and the current distance.
// This will be used to calculate the minimum translation vector
intervalDistance = Math.Abs(intervalDistance);
if (intervalDistance < minIntervalDistance)
{
minIntervalDistance = intervalDistance;
translationAxis = axis;
Vector2 d = polygonA.GetCentroid() - polygonB.GetCentroid();
if (Vector2.Dot(d, translationAxis) < 0)
translationAxis = -translationAxis;
}
}
// The minimum translation vector
// can be used to push the polygons appart.
if (result.WillIntersect)
result.MinimumTranslationVector =
translationAxis * minIntervalDistance;
return result;
}