Quantcast
Channel: Windows Presentation Foundation (WPF) forum
Viewing all articles
Browse latest Browse all 18858

Intersect Rectangles in Different Containers WPF

$
0
0

Hello all. I am hoping that someone can help me understand how to do this. I am trying to perform hit testing but it is not working and I think the problem is that the objects are in different containers. Here is my code.

<Grid x:Name="MainGrid"><Canvas x:Name="MyCanvas" HorizontalAlignment="Left" VerticalAlignment="Top" OpacityMask="#FF1B1A1A" Background="Black" Width="1200" Height="800"><Viewbox x:Name="ViewB" Height="170" Canvas.Left="128" Stretch="Fill" Canvas.Top="25" Width="100" Margin="1"><Grid Height="170" Width="100"><Image x:Name="Rocket" Source="Resources/alien-spaceship.png" Margin="0,0,0,70"/><Image x:Name="picThrust" Source="Resources/rocket-146104_640.png" Margin="0,70,0,0"/></Grid></Viewbox><Rectangle x:Name="LandingPad" Fill="#FFF4F4F5" Height="100" Canvas.Left="151" Stroke="Black" Canvas.Top="690" Width="100"/></Canvas></Grid>

Basically I want to check when the bottom of the Image Rocket collides with the top of the Rectangle Landing Pad.

Here is my C# code.

private bool CheckIfLanded(Image Ship, Rectangle Pad)
        {
            Rect Rect1 = new Rect();
            Rect Rect2=new Rect();

            Rect1.X = Ship.Margin.Left;
            Rect1.Y = Ship.Margin.Top;
            Rect1.Width = Ship.Width;
            Rect1.Height = Ship.Height;
            Rect2.X = Ship.Margin.Left;
            Rect2.Y = Ship.Margin.Top;
            Rect2.Width = Ship.Width;
            Rect2.Height = Ship.Height;
            Rect1.Intersect(Rect2);

            if (Rect1.IsEmpty == false)
            {
                return true;
            }else
            {
                return false;
            }

        }

Am I on the right track that the problem is because the objects are in different containers? Can someone show me how to handle this situation and help me to understand it?

Thanks,

David


Viewing all articles
Browse latest Browse all 18858

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>