12,403,983 members (69,181 online)
Rate this:
See more:
Hi, I need help with object collisions. I have two picture boxes, I control the blue one and the red is an object. I have it so that when I collide with the red picturebox the blue picturebox changes to black showing a collision. But for a game how can I prevent the blue picturebox from going through the red one?

here is my code:

```     private void Form1_KeyDown(object sender, KeyEventArgs e)
{
int x = pictureBox1.Location.X;
int y = pictureBox1.Location.Y;

if (e.KeyCode == Keys.W) y -= 5;
{
if (pictureBox1.Bounds.IntersectsWith(pictureBox2.Bounds) == false)
{
pictureBox1.Location = new Point(x, y);
}
if (e.KeyCode == Keys.S) y += 5;
{
if (pictureBox1.Bounds.IntersectsWith(pictureBox2.Bounds) == false)
{
pictureBox1.Location = new Point(x, y);
}
if (e.KeyCode == Keys.A) x -= 5;
{
if (pictureBox1.Bounds.IntersectsWith(pictureBox2.Bounds) == false)
{
pictureBox1.Location = new Point(x, y);
}
if (e.KeyCode == Keys.D) x += 5;
{
if (pictureBox1.Bounds.IntersectsWith(pictureBox2.Bounds) == false)
{
pictureBox1.Location = new Point(x, y);
}
}
}
}
}
}
}
}
```

Thanks for the help.
Posted 13-Sep-12 6:57am
Updated 13-Sep-12 9:56am
v4
Sergey Alexandrovich Kryukov 13-Sep-12 13:03pm

"Prevent" may mean very different things. This is physics. You may simulate perfectly elastic collision, perfectly inelastic, and something in the middle. You can take into account passing appropriate part of energy into other degrees of freedom like rotation depending on the point of impact and friction, and fake it somehow. This is a whole science you need to learn first, and then you need to learn the techniques of modeling "physics" as it is being developed in gaming, with all simplifications, approximation and faking tricks involved.
--SA

Rate this:

## Solution 2

It is actually pretty simple.
Your code is already wrong in the sense that it is not detecting a collision upon the movement itself but on a timer that elapses.
If you just want to prevent the movement to a certain position, you calculate the new position first without changing the controls position just yet.
You then check for the collision and if there is a collision you just don't move the object. If there is no collision you move the object.
Easy.
I am assuming you just want to block the movement and don't want to create the complicated stuff Sergey is talking about, your movement is based on keystrokes anyway. This is not breakout is it ?
MR. AngelMendez 13-Sep-12 14:25pm

no just a simple 2D top-down movement based game. I see what you mean though, let me see if I can implement what you are saying and I'll respond my results.
MR. AngelMendez 13-Sep-12 14:35pm

back with the results. It seems like it works... to good actually, now I can't move the object away from the red picturebox when I collide with it.
Philip Stuyck 13-Sep-12 14:54pm

As I said, you need to check "before" you move. You don't move if the next position would be a collision. You should then be able to move away without a problem.
MR. AngelMendez 13-Sep-12 15:14pm

hmmm I did noticed that when I move to the object my picturebox kind of gets inside of the object a little bit so maybe thats why it still gets stuck.
MR. AngelMendez 13-Sep-12 15:20pm

I updated my code with your implement
Philip Stuyck 13-Sep-12 15:29pm

yes, I see and it is not correct.Do it like this: you take the picturebox bounds rectangle that you are going to move. You check the keystrokes and modify the rectangle to the new position. You check if that rectangle intersects with the object. If it intersects, you dont touch the picturebox, if it does not intersect, then and only then you modify the location of the picturebox.
In your code you are checking the location before you calculated the new position, so you are basically checking the current position and not the position you want to move to.
You are getting closer, just a little more coding, you'll get it.
MR. AngelMendez 13-Sep-12 15:57pm

I tried this and no success, other than that I have no clue on what to do next.
Philip Stuyck 13-Sep-12 16:35pm

Rect r = pictureBox.Bounds.
if (e.KeyCode == Keys.W) r.top -= 5;
if (r.intersectsWith(...)
see the difference ?
Bottom line, take a copy of the bound rectangle and manipulate that copy, use that for your check and only then change the position.
Philip Stuyck 14-Sep-12 9:28am

MR. AngelMendez 17-Sep-12 8:00am

sorry I haven't been on but I tried what you did but I get an error at r.Top saying "Error Property or indexer 'System.Drawing.Rectangle.Top' cannot be assigned to -- it is read only"
Philip Stuyck 17-Sep-12 9:56am

Point is you need a rectangle that is moved to the top, to the bottom whatever the movement is. Check the api of the rectangle. It might have an X and Y property that are modifyable, or just create a new one based on the old one but with a different x or y and same width and height.
MR. AngelMendez 18-Sep-12 17:29pm

thanks I'll see what I can come up with
Rate this:

## Solution 1

Please see my comment to the question. First of all, you need to understand some basic principles of Physics Engines. Even it you want something highly simplified, you need to know the matter to be able to specify it correctly. You can start here:
http://en.wikipedia.org/wiki/Collision[^],
http://en.wikipedia.org/wiki/Physics_engine[^],
http://en.wikipedia.org/wiki/Numerical_simulation[^].

Good luck,
—SA
v2

Top Experts
Last 24hrsThis month
 OriginalGriff 313 Richard MacCutchan 247 Karthik Bangalore 210 Suvendu Shekhar Giri 185 ppolymorphe 145
 OriginalGriff 7,386 ppolymorphe 3,175 Karthik Bangalore 3,127 Richard MacCutchan 2,342 F-ES Sitecore 2,262