Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
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
Edited 13-Sep-12 9:56am
v4
Comments
Sergey Alexandrovich Kryukov at 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: bad
good
Please Sign up or sign in to vote.

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 ?
  Permalink  
Comments
MR. AngelMendez at 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 at 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 at 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 at 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 at 13-Sep-12 15:20pm
   
I updated my code with your implement
Philip Stuyck at 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 at 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 at 13-Sep-12 16:35pm
   
I can easily spot your mistakes, read your code and read my explanation. 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 at 14-Sep-12 9:28am
   
If it works please mark my answer
MR. AngelMendez at 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 at 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 at 18-Sep-12 17:29pm
   
thanks I'll see what I can come up with
Rate this: bad
good
Please Sign up or sign in to vote.

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
  Permalink  
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Your Filters
Interested
Ignored
     
0 Shai Vashdi 1,518
1 OriginalGriff 478
2 Manas Bhardwaj 394
3 Tadit Dash 310
4 Damith Weerasinghe 260
0 Sergey Alexandrovich Kryukov 9,575
1 OriginalGriff 5,856
2 Peter Leow 4,405
3 Maciej Los 3,540
4 Abhinav S 3,513


Advertise | Privacy | Mobile
Web03 | 2.8.140415.2 | Last Updated 13 Sep 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Use
Layout: fixed | fluid