Simple way to calculate point of intersection between two polygons in C#
-
I've got two polygons defined as a list of Vectors, I've managed to write routines to transform and intersect these two polygons (seen below Frame 1). Using line-intersection I can figure out whether these collide, and have written a working Collide() function. This is to be used in a variable step timed game, and therefore (as shown below) in Frame 1 the right polygon is not colliding, it's perfectly normal for on Frame 2 for the polygons to be right inside each other, with the right polygon having moved to the left. My question is, what is the best way to figure out the moment of intersection? In the example, let's assume in Frame 1 the right polygon is at X = 300, Frame 2 it moved -100 and is now at 200, and that's all I know by the time Frame 2 comes about, it was at 300, now it's at 200. What I want to know is when did it actually collide, at what X value, here it was probably about 250. I'm preferably looking for a C# source code solution to this problem. Maybe there's a better way of approaching this for games?
-
Answer:
I would use the separating axis theorem, as outlined here: http://www.metanetsoftware.com/technique/tutorialA.html http://en.wikipedia.org/wiki/Separating_axis_theorem Then I would http://www.gamasutra.com/view/feature/3383/simple_intersection_tests_for_games.php or use http://www.metanetsoftware.com/technique/tutorialA.html#section5 if needed. GMan here on StackOverflow wrote a sample implementation over at http://www.gpwiki.org/index.php/VB%3aTutorials%3aBuilding_A_Physics_Engine%3aBasic_Intersection_Detection. This may all be overkill for your use-case, but it handles polygons of any order. Of course, for simple bounding boxes it can be done much more efficiently through other means.
Rob at Stack Overflow Visit the source
Other answers
I'm no mathematician either, but one possible though crude solution would be to run a mini simulation. Let us call the moving polygon M and the stationary polygon S (though there is no requirement for S to actually be stationary, the approach should work just the same regardless). Let us also call the two frames you have F1 for the earlier and F2 for the later, as per your diagram. If you were to translate polygon M back towards its position in F1 in very small increments until such time that they are no longer intersecting, then you would have a location for M at which it 'just' intersects, i.e. the previous location before they stop intersecting in this simulation. The intersection in this 'just' intersecting location should be very small — small enough that you could treat it as a point. Let us call this polygon of intersection I. To treat I as a point you could choose the vertex of it that is nearest the centre point of M in F1: that vertex has the best chance of being outside of S at time of collision. (There are lots of other possibilities for interpreting I as a point that you could experiment with too that may have better results.) Obviously this approach has some drawbacks: The simulation will be slower for greater speeds of M as the distance between its locations in F1 and F2 will be greater, more simulation steps will need to be run. (You could address this by having a fixed number of simulation cycles irrespective of speed of M but that would mean the accuracy of the result would be different for faster and slower moving bodies.) The 'step' size in the simulation will have to be sufficiently small to get the accuracy you require but smaller step sizes will obviously have a larger calculation cost. Personally, without the necessary mathematical intuition, I would go with this simple approach first and try to find a mathematical solution as an optimization later.
Paul Ruane
If you have the ability to determine whether the two polygons overlap, one idea might be to use a modified binary search to detect where the two hit. Start by subdividing the time interval in half and seeing if the two polygons intersected at the midpoint. If so, recursively search the first half of the range; if not, search the second half. If you specify some tolerance level at which you no longer care about small distances (for example, at the level of a pixel), then the runtime of this approach is O(log D / K), where D is the distance between the polygons and K is the cutoff threshold. If you know what point is going to ultimately enter the second polygon, you should be able to detect the collision very quickly this way. Hope this helps!
templatetypedef
For a rather generic solution, and assuming ... no polygons are intersecting at time = 0 at least one polygon is intersecting another polygon at time = t and you're happy to use a C# clipping library (eg http://www.angusj.com/delphi/clipper.php) then use a binary approach to deriving the time of intersection by... double tInterval = t; double tCurrent = 0; int direction = +1; while (tInterval > MinInterval) { tInterval = tInterval/2; tCurrent += (tInterval * direction); MovePolygons(tCurrent); if (PolygonsIntersect) direction = +1; else direction = -1; }
Angus Johnson
Well - you may see that it's allways a point of one of the polygons that hits the side of the other first (or another point - but thats after all almost the same) - a possible solution would be to calculate the distance of the points from the other lines in the move-direction. But I think this would end beeing rather slow. I guess normaly the distances between frames are so small that it's not importand to really know excactly where it hit first - some small intersections will not be visible and after all the things will rebound or explode anyway - don't they? :)
Carsten König
Related Q & A:
- what is transverse intersection of Whitney stratifications?Best solution by Mathoverflow
- How to get all intersections between two simple polygons in O(n+k?Best solution by Computational Science
- What is the best way to calculate a date difference?Best solution by Stack Overflow
- How to learn C# (moving from C++ to C#?Best solution by stackoverflow.com
- What is the difference between two way and one way security systems in car alarms?Best solution by Yahoo! Answers
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.