多边形碰撞算法

原文地址:http://www.codeproject.com/Articles/15573/2D-Polygon-Collision-Detection

==================================================

// 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)public PolygonCollisionResult PolygonCollision(Polygon polygonA, Polygon polygonB, Vector velocity) { PolygonCollisionResult result = new PolygonCollisionResult(); result.Intersect = true; result.WillIntersect = true; int edgeCountA = polygonA.Edges.Count; int edgeCountB = polygonB.Edges.Count; float minIntervalDistance = float.PositiveInfinity; Vector translationAxis = new Vector(); Vector edge; // Loop through all the edges of both polygonsfor (int edgeIndex = 0; edgeIndex < edgeCountA + edgeCountB; edgeIndex++) { if (edgeIndex < edgeCountA) { edge = polygonA.Edges[edgeIndex]; } else { edge = polygonB.Edges[edgeIndex - edgeCountA]; } // ===== 1. 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 axisfloat 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 intersectingif (IntervalDistance(minA, maxA, minB, maxB) > 0)\ result.Intersect = false; // ===== 2. Now find if the polygons *will* intersect =====// Project the velocity on the current axisfloat velocityProjection = axis.DotProduct(velocity); // Get the projection of polygon A during the movementif (velocityProjection < 0) { minA += velocityProjection; } else { maxA += velocityProjection; } // Do the same test as above for the new projectionfloat intervalDistance = IntervalDistance(minA, maxA, minB, maxB); if (intervalDistance > 0) result.WillIntersect = false; // If the polygons are not intersecting and won't intersect, exit the loopif (!result.Intersect && !result.WillIntersect) 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; Vector d = polygonA.Center - polygonB.Center; if (d.DotProduct(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;}==========================================================================Vector polygonATranslation = new Vector();PolygonCollisionResult r = PolygonCollision(polygonA, polygonB, velocity);if (r.WillIntersect) { // Move the polygon by its velocity, then move// the polygons appart using the Minimum Translation Vector polygonATranslation = velocity + r.MinimumTranslationVector;} else { // Just move the polygon by its velocity polygonATranslation = velocity;}polygonA.Offset(polygonATranslation);

转载于:https://www.cnblogs.com/cly84920/archive/2012/04/02/4426534.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值