Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7178143
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T16:51:41+00:00 2026-05-28T16:51:41+00:00

I’m creating my first non-console game in Visual C#. I have a player which

  • 0

I’m creating my first non-console game in Visual C#.
I have a player which is a picturebox, and obstacles which are also pictureboxes.
Now when I create an obstacle (picturebox) at a random position, I would like to check if it already touches an other obstacle.

Here’s what I have now:

Picturebox obstacles = new Picturebox[20];
for (int i = 0; i < obstacles.Length; i++)
{
    DateTime date = DateTime.Now;
    Random randomNumber = new Random(date.Second * (date.Minute / 2) ^ 2 + date.Hour * 123 + (i ^ 9 + i / 2));
    obstacles[i] = new PictureBox();
    obstacles[i].Image = Properties.Resources.es;
    obstacles[i].Size = new Size(25, 50);
    obstacles[i].Location = new Point(randomNumber.Next(640 - obstacles[i].Image.Width), randomNumber.Next(topBar.Height, 480 - obstacles[i].Image.Height));
    if (IsTouching(obstacles[i], player))
    {
        i--;
    }
    else
    {
        bool tmp = true;
        for (int j = 0; j < obstacles.Length; j++)
        {
            if (obstacles[j] != null && j != i)
            {
                if (IsTouching(obstacles[j], obstacles[i]))
                {
                    tmp = false;
                    break;
                }
            }
        }
        if (tmp)
        {
            Controls.Add(obstacles[i]);
        }
        else
        {
            i--;
        }
    }
}

So that’s my way, but I know it’s not really effective, so any better ideas, cause it takes a while (~5 seconds) to create those obstacles.
And here’s my IsTouching method, which also kinda sucks, anyone have better ideas?

private bool IsTouching(PictureBox obj1, PictureBox obj2)
{
    Point[] obj1Points = new Point[(obj1.Width * obj1.Height) - ((obj1.Width - 2) * (obj1.Height - 2))];
    int count = 0;
    for (int x = obj1.Left + 1; x < obj1.Left + obj1.Width - 1; x++)
    {
        obj1Points[count] = new Point(x, obj1.Top);
        obj1Points[count + 1] = new Point(x, obj1.Top + obj1.Height);
        count += 2;
    }
    for (int y = obj1.Top; y < obj1.Top + obj1.Height; y++)
    {
        obj1Points[count] = new Point(obj1.Left, y);
        obj1Points[count + 1] = new Point(obj1.Left + obj1.Width, y);
        count += 2;
    }

    Point[] obj2Points = new Point[(obj2.Width * obj2.Height) - ((obj2.Width - 2) * (obj2.Height - 2))];
    count = 0;
    for (int x = obj2.Left + 1; x < obj2.Left + obj2.Width - 1; x++)
    {
        obj2Points[count] = new Point(x, obj2.Top);
        obj2Points[count + 1] = new Point(x, obj2.Top + obj2.Height);
        count += 2;
    }
    for (int y = obj2.Top; y < obj2.Top + obj2.Height; y++)
    {
        obj2Points[count] = new Point(obj2.Left, y);
        obj2Points[count + 1] = new Point(obj2.Left + obj2.Width, y);
        count += 2;
    }

    for (int obj2Point = 0; obj2Point < obj2Points.Length; obj2Point++)
    {
        for (int obj1Point = 0; obj1Point < obj1Points.Length; obj1Point++)
        {
            if (obj2Points[obj2Point].X == obj1Points[obj1Point].X && obj2Points[obj2Point].Y == obj1Points[obj1Point].Y)
            {
                return true;
            }
        }
    }
    return false;
}

What it does: Checks if the given two parameters edges touch each other. So basically just a collision-detection, anyone have any ideas, cause I’m kinda new at this stuff?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T16:51:43+00:00Added an answer on May 28, 2026 at 4:51 pm

    If we assume that all the obstacles are solid (i.e. 2 obstacles touch if the other is inside the other), you can use the following method:

    private bool IsTouching(PictureBox p1, PictureBox p2)
    {
        if (p1.Location.X + p1.Width < p2.Location.X) 
            return false;
        if (p2.Location.X + p2.Width < p1.Location.X)
            return false;
        if (p1.Location.Y + p1.Height < p2.Location.Y)
            return false;
        if (p2.Location.Y + p2.Height < p1.Location.Y)
            return false;
        return true;
    }
    

    I tested your current IsTouching method and found out that it fails in some corner cases, such as in this one (it claims they are not touching although they are). My method works in these corner cases too.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a jquery bug and I've been looking for hours now, I can't
this is what i have right now Drawing an RSS feed into the php,
I have a text area in my form which accepts all possible characters from
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.