0

I am creating a little platforming game and I want to compare the bounds of one player rectangle with many rectangle elements that are my platforms. I know in WinForms I can just use a PictureBox .bounds.intersectswith() method.

in my xaml I define my controls like this inside a gridpane called program.

 <Rectangle Name="player" Tag="player"  HorizontalAlignment="Left" Height="61" Stroke="Black" VerticalAlignment="Top" Width="75" Canvas.Top="332" Canvas.Left="25">
            <Rectangle.Fill>
                <ImageBrush ImageSource="player.png"/>
            </Rectangle.Fill>
        </Rectangle>

        <Rectangle Tag="platform"  HorizontalAlignment="Left" Height="82" Stroke="Black" VerticalAlignment="Top" Width="100" Canvas.Top="398">
            <Rectangle.Fill>
                <ImageBrush ImageSource="purplebrick.png"/>
            </Rectangle.Fill>
        </Rectangle>

and then I want to compare the bounds of the player with the about 50 platforms.

 foreach (Rectangle x in LogicalTreeHelper.GetChildren(program))
        {
            if (x is Rectangle && x.Tag.ToString() == "platform")
            { 
                 if(player.intersectswiht(x) // problem area

iv been using this question Bounds.Intersect for WPF as a reference but do I need to name every rectangle element and create an actual reference to be able to use its intersect with function?

or should I just compare every elements dimensions (canvas.top ect)but that would only give me the edges value not a range for if they overlap correct?

  • When you create the platform rectangles, you could add them to a `List` and just compare against that, but you aren't showing how you are creating them... Are you really doing each one in XAML? Or are you loading a level and creating it in code? – Ron Beyer May 08 '18 at 21:24
  • im doing each one in XAML currently is that a bad decision? – wintersummer May 08 '18 at 21:27
  • You can do it that way, but really seems tedious. The issue though is that the WPF `Rectangle` is a visual control, unlike the GDI `Rectangle` which is a layout/helper class, which is closer to [`System.Windows.Rect`](https://msdn.microsoft.com/en-us/library/system.windows.rect.aspx) for WPF. Take a look at [`Rect.IntersectsWith`](https://msdn.microsoft.com/en-us/library/system.windows.rect.intersectswith.aspx) – Ron Beyer May 08 '18 at 21:34
  • rect.intersectswith is the function I was planning on using but is the only way to access it to have distinct rectangle references instead of my many xaml rectangles with a tag? Do i need to replace them with a nonvisual control rectangle? Id like to hack together a way just using the visual controls – wintersummer May 08 '18 at 21:38
  • If you loop through your rectangles and add them to a `List myPlatforms` (or something similiar) you can do this in code: `if myPlatforms.Any(p => IntersectsRect(player, p))) { ... }` and then the function `IntersectsRect` takes the `player` and `p` (platform) rectangle and uses `Rect.IntersectsWith` returning true if it does, that would work, if you get what I'm pushing at... – Ron Beyer May 08 '18 at 21:41
  • ok that was an option i was debating just making a rectangle list like that. Maybe im just not thinking straight but I cant think of how you would assign the visual control to the rectangle list tho. something like `foreach (Rectangle x in LogicalTreeHelper.GetChildren(program)){ myplatforms.add(new rectangle(x)) }` or for the new rectangle function have to assign every value doing of the visual control? im not sure what the equivalent coordinates would be – wintersummer May 08 '18 at 21:49
  • That is the problem with using WPF for this, it can change depending on DPI or screen resolution. Really you should be using something designed to do this, like [Unity3d Platformer](https://unity3d.com/learn/tutorials/topics/2d-game-creation/creating-basic-platformer-game) – Ron Beyer May 08 '18 at 21:51
  • yeah I'm very much recognizing that. is there any solution you could think of that could get this platformer running? just compare the platforms canvas.topproperty possibly? – wintersummer May 08 '18 at 22:00
  • Iv got this mostly working but how would I distinguish between what platform in the myPlatforms is the player intersecting with? – wintersummer May 10 '18 at 01:08

0 Answers0