'How can I copy the Matrix/Transform values of a System.Windows.Shapes.Rectangle and apply them to a System.Windows.Rect in a WPF canvas?
I'm trying to detect collisions between 2 Rectangles. It works, however one of the rectangles is tilted and rotated and the Rect that I'm creating from it doesn't have these values, it just has the Left,Top,Width and Height, how can I apply the values of a System.Windows.Shapes.Rectangle to the System.Windows.Rect that I'm creating programatically? Thanks in advance :)
Here is an idea of the code that I'm using below:
internal static bool CheckIfCharacterCanMove(Rectangle Character, string RoomName)
{
if (RoomName == "PlayerRoom")
{
#region Collision creation
Rect PlayerHitBox = new Rect(Canvas.GetLeft(Character), Canvas.GetTop(Character), Character.Width, Character.Height);
Rect Wall1HitBox = new Rect(Canvas.GetLeft(PageController.PlayerRoom.Wall1), Canvas.GetTop(PageController.PlayerRoom.Wall1), PageController.PlayerRoom.Wall1.Width, PageController.PlayerRoom.Wall1.Height);
Rect Wall2HitBox = new Rect(Canvas.GetLeft(PageController.PlayerRoom.Wall2), Canvas.GetTop(PageController.PlayerRoom.Wall2), PageController.PlayerRoom.Wall2.Width, PageController.PlayerRoom.Wall2.Height);
Rect Wall3HitBox = new Rect(Canvas.GetLeft(PageController.PlayerRoom.Wall3), Canvas.GetTop(PageController.PlayerRoom.Wall3), PageController.PlayerRoom.Wall3.Width, PageController.PlayerRoom.Wall3.Height);
Rect Wall4HitBox = new Rect(Canvas.GetLeft(PageController.PlayerRoom.Wall4), Canvas.GetTop(PageController.PlayerRoom.Wall4), PageController.PlayerRoom.Wall4.Width, PageController.PlayerRoom.Wall4.Height);
#endregion
#region Collision detection
if (PlayerHitBox.IntersectsWith(Wall1HitBox))
{
Console.WriteLine("Collision detected with Wall1");
Canvas.SetLeft(Character, Canvas.GetLeft(PageController.PlayerRoom.Wall1) + 20);
return false;
}
if (PlayerHitBox.IntersectsWith(Wall2HitBox))
{
Console.WriteLine("Collision detected with Wall2");
Canvas.SetTop(Character, Canvas.GetTop(PageController.PlayerRoom.Wall2) + 20);
return false;
}
if (PlayerHitBox.IntersectsWith(Wall3HitBox))
{
Console.WriteLine("Collision detected with Wall3");
Canvas.SetLeft(Character, Canvas.GetLeft(PageController.PlayerRoom.Wall3) - 210);
return false;
}
if (PlayerHitBox.IntersectsWith(Wall4HitBox))
{
Console.WriteLine("Collision detected with Wall4");
Canvas.SetTop(Character, Canvas.GetTop(PageController.PlayerRoom.Wall4) - 210);
return false;
}
Console.WriteLine("No collision detected");
return true; //returns true because no collision was detected so the objet can move
#endregion
}
else
{
return true;//Just here to serve the compiler, should be ignored.
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
