Click here to Skip to main content
15,887,683 members
Please Sign up or sign in to vote.
3.00/5 (4 votes)
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:

C#
     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
Updated 13-Sep-12 9:56am
v4
Comments
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

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 ?
 
Share this answer
 
Comments
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
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
 
Share this answer
 
v2

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900