碰撞检测实现 [英] Collision Detection Implementation

查看:77
本文介绍了碰撞检测实现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个碰撞检测类,它可以通过查找中心之间的距离以及该距离是否足够小而发生碰撞(参见碰撞检测错误)。我的问题是试图使它真正起作用,并且使椭圆碰撞。如有必要,我将解释更多。
Thx

I have a collision detection class that works by finding the distance between the centres and whether that distance is small enough to be a collision (see Collision Detection error). My problem is trying to make this actually work, with ellipses colliding. I will explain more if necessary. Thx

推荐答案

当图像重叠时,实现每像素碰撞检测的最佳方法是您可以了解更多在以下链接中

The best way would to implement per pixel collision detection when the images are overlapping you can read more about this in the following links

http://www.codeproject.com/KB/game/collision3.aspx

C#中的每像素碰撞问题

我也针对这样的问题几年前的一个项目,当我需要检测两个圆是否在我使用以下代码的地方重叠

I also did a problem like this for a project a few years ago when I needed to detect if two circles overlapped where i used the following code

    public static bool Intersect(Rectangle rectangle1, Rectangle rectangle2)
    {
        if (((rectangle1.X < (rectangle2.X + rectangle2.Width)) && (rectangle2.X < (rectangle1.X + rectangle1.Width))) && (rectangle1.Y < (rectangle2.Y + rectangle2.Height)) && (rectangle2.Y < (rectangle1.Y + rectangle1.Height)))
        {
            Vector2 rect1Centre = new Vector2(rectangle1.X + rectangle1.Width / 2, rectangle1.Y + rectangle1.Height / 2);
            Vector2 rect2Centre = new Vector2(rectangle2.X + rectangle2.Width / 2, rectangle2.Y + rectangle1.Height / 2);
            double radius1 = ((rectangle1.Width / 2) + (rectangle1.Height / 2)) / 2;
            double radius2 = ((rectangle2.Width / 2) + (rectangle2.Height / 2)) / 2;

            double widthTri = rect1Centre.X - rect2Centre.X;
            double heightTri = rect1Centre.Y - rect2Centre.Y;
            double distance = Math.Sqrt(Math.Pow(widthTri, 2) + Math.Pow(heightTri, 2));

            if (distance <= (radius1 + radius2))
                return true;
        }
        return false;
    }

代码不是很好,但是我是在做我的第一个XNA游戏的时候写的

Not very nice code but I wrote it doing my first XNA game

这篇关于碰撞检测实现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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