D
D
Dmitry Kovalev2021-02-20 20:32:19
C++ / C#
Dmitry Kovalev, 2021-02-20 20:32:19

How to correctly implement check of contact of coordinates in PictureBoxes?

What is the point: There is a movable object (character) and an object that he can "absorb" (bullet). For this to happen, it is necessary that they coincide in coordinates, that is, one object was on top of another.
Problem: Coordinates are a single point in the upper left corner of an object, which is independent of its size and other characteristics. And in order for the coordinates to touch each other - the person who controls the character needs to spend over9999 hours on it.
I spent about an hour or two trying to fix it, but nothing came of it, unfortunately.
The code:

private void eatBullet()
        {
            if (character.Location == bullet.Location)
            {
                _characterSizeX += 5;
                _characterSizeY += 5;
            }
        }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
ayazer, 2021-02-20
@PriestFromRL

judging by the winforms tag - your objects already have the coordinate of the upper right corner + dimensions (i.e. height and width). In fact - you need to check not that 2 coordinates coincide, but that 2 rectangles (around the character and around the bullet) intersect.
alternatively, you can take https://github.com/MonoGame/MonoGame/blob/7b2d69d6... and tweak it for yourself. the only difference is that instead of a Rectangle you will have some kind of PictureBox (honestly, I haven’t touched winforms for 10 years, I don’t remember what it’s called there)
UPD:

public bool bulletBorderCheck(){

   return bullet.Location.X < character.Location.X + character.Width
      && character.Location.X < bullet.Location.X + bullet.Width
      && bullet.Location.Y < character.Location.Y < character.Height
      && character.Location.Y < bullet.Location.Y + bullet.Height

}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question