0

I'm trying to build a simple RPG using the C# Windows Forms (as I had recently stumbled upon a fun tutorial demonstrating this ability. I have two items:

A 'Character' object that has been placed IN game: In-game character--GUI

I also have a code-generated Draw Object, a tree--built in-game:

public void MainFormPaint(object sender, PaintEventArgs e)
        {
            //Drawing a tree, to create transparency
            Image Tree_2 = Image.FromFile("[Directory to PNG].png");
            Tree_2.Tag = "Tree";
            e.Graphics.DrawImage(Tree_2,50,50,200,200);
        }

...which generates this: Code-generated Tree Object

Seeing as I cannot detect the object by some means similar to:

Character.Bounds.Intersectswith([insert_my_picture].Bounds);

this leaves me kind of baffled, and I'm not sure what to do. I want to detect this collision, so that I can stop movement. However, I'm not sure how to check for an 'empty spot' next to me or any object for that matter that's code-generated. It's important to note that this image is code-generated to maintain the graphic's transparency (as apparently there are problems placing objects in the form and maintaining transparency with overlaying objects).

Thank you for your help!

  • There are *many* tutorials over different kinds of collision systems, as this comes up in most every game to different extents. Try a web search and get a cup of coffee.. maybe "pixel perfect" would also be a useful keyword, although usually one wants to establish additional bounding regions and separate the view/physics systems. – user2864740 Aug 23 '18 at 03:23
  • Thank you for that. I've found this [link](https://stackoverflow.com/questions/16832680/per-pixel-collision-code), however, I wouldn't know how to implement everything except the argument [Rectangle rectangleA]. Is there an 'other' argument that can be passed in regarding a picture? – EskinubisPrime Aug 23 '18 at 04:49

1 Answers1

1

As the graphic was drawn at runtime, I was needing to generate a Rectangle:

Rectangle firstTree = new Rectangle();

...in the public variables area, and then created it on paint event.

public void MainFormPaint(object sender, PaintEventArgs e)
    {
        e.Graphics.DrawImage(Tree_Obj,50,50,200,200);
        firstTree.X = 50;
        firstTree.Y = 50;
        firstTree.Width = 200;
        firstTree.Height=200;
    }

The full, encapsulating bounds were only for trial. Problem solved!