How can I determine what type of element is at a given coordinate on a canvas?
I am making tetris and have polygons dropping down the canvas. I wish to check position (50,50) for example, and see if that point contains a polygon or not.
Basically I want to do collision detection, and can't find an easy way to do it since my polygons are not all rectangular.
Loading

VulpesRank #55 · 29.2K reputationPosted Jul 12, 2011, 2:20 AM EDT
As you'll see the vertices of the polygon need to be passed as an array of Points.
Jaganathan BantheswaranRank #170 · 14.1K reputationPosted Jul 12, 2011, 2:29 AM EDT
There is a Method called IntersectsWith in System.Drawing which will say you that whether a rect 1 intersects the rect 2.
See the example.
public Rect GetBounds(FrameworkElement of, FrameworkElement from)
{
// Might throw an exception if of and from are not in the same visual tree
GeneralTransform transform = of.TransformToVisual(from);
return transform.TransformBounds(new Rect(0, 0, of.ActualWidth, of.ActualHeight));
}
Vehicle IsBotCollided(IEnumerable
{
//currentBounds is of type Rect, which contains the 4 points of the rectangle (even when rotated)
var currentBounds = GetBounds(BotBody, BattleArena);
//Then I check if the current bounds intersect with each of the other Bots` bounds that are also in the Arena
foreach (Vehicle vehicle in blist)
{
if(GetBounds(vehicle.BotBody, BattleArena).IntersectsWith(currentBounds))
{
return vehicle;
}
}
return null;
}
David Kay12 reputationPosted Jul 12, 2011, 2:55 AM EDT
Jaganathan BantheswaranRank #170 · 14.1K reputationPosted Jul 12, 2011, 3:07 AM EDT
David Kay12 reputationPosted Jul 12, 2011, 10:24 PM EDT
Jaganathan BantheswaranRank #170 · 14.1K reputationPosted Jul 13, 2011, 3:57 AM EDT
Sam HobbsRank #107 · 20.5K reputationPosted Jul 13, 2011, 12:45 PM EDT
I am not sure that will help, but I think it is worth looking at.