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
Sam HobbsPosted Jul 13, 2011, 4:45 PM
I am not sure that will help, but I think it is worth looking at.
Jaganathan BantheswaranPosted Jul 13, 2011, 7:57 AM
David KayPosted Jul 13, 2011, 2:24 AM
Jaganathan BantheswaranPosted Jul 12, 2011, 7:07 AM
David KayPosted Jul 12, 2011, 6:55 AM
Jaganathan BantheswaranPosted Jul 12, 2011, 6:29 AM
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;
}
VulpesPosted Jul 12, 2011, 6:20 AM
As you'll see the vertices of the polygon need to be passed as an array of Points.