如何防止一个对象穿过另一个对象? [英] how do I prevent an object from going through another object?

查看:140
本文介绍了如何防止一个对象穿过另一个对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我需要有关对象冲突的帮助.我有两个图片框,我控制蓝色的一个,红色是一个对象.我有它,所以当我与红色的图片框碰撞时,蓝色的图片框变为黑色,显示出碰撞.但是对于一款游戏,我该如何防止蓝色的图片框通过红色的图片框?

这是我的代码:

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.

推荐答案

这实际上很简单.
您的代码在某种意义上已经是错误的,因为它不是在检测运动本身,而是在经过的计时器上检测到碰撞.
如果只想防止移动到某个位置,则可以在不更改控件位置的情况下首先计算新位置.
然后,您检查是否存在碰撞,如果没有碰撞,则不要移动对象.如果没有碰撞,则移动对象.
容易.
我假设您只是想阻止运动,而又不想创建Sergey所谈论的复杂事物,无论如何,您的运动都是基于击键.这不是突破吗?
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 ?


请参阅我对问题的评论.首先,您需要了解物理引擎的一些基本原理.即使您想要高度简化的内容,您也需要知道该问题以便能够正确指定它.您可以从这里开始:
http://en.wikipedia.org/wiki/Collision [ http://en.wikipedia.org/wiki/Physics_engine [ http://en.wikipedia.org/wiki/Numerical_simulation [
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


这篇关于如何防止一个对象穿过另一个对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆