开发者

2D Rectangle collision detection system (that works in a real game)

I have been developing a game in my spare time for the past few months. One sticking point that I have reimplemented over and over and not got 100% working is the collision detection. My system (posted below) mostly works however random things seem to happen from time to time, like the player being pushed outside the bounds of the level etc. Tutorials on the matter I have come across seem to offer basic workings, i.e, you have to k开发者_运维百科now an object will be in relation to the object in question, but in a real game you wouldn't really know this. Here is my implementation but what I am after is if anyone knows of a good solid system for achieving what I am doing here.

Note that item comes from a collection of IItem which exposes the Rectangle and a few other bits for each item.

    public void Move(float xAdditional, float yAdditional)
    {
        X += xAdditional;
        Y += yAdditional;

        foreach (var item in Level.Items)
        {
            if (item != this)
            {
                if (item.Boundary.Intersects(this.Boundary))
                {
                    if (item.Boundary.Top > this.Boundary.Top) //we have collided with an object below us.
                    {
                        Y = item.Boundary.Top - (this.Boundary.Height/2);
                    }
                    if(item.Boundary.Bottom < this.Boundary.Bottom)//we have collided with an object above us.
                    {
                        Y = item.Boundary.Bottom + (this.Boundary.Height/2);
                    }
                    if(item.Boundary.Left > this.Boundary.Left) //We have collided with an object to the right.
                    {
                        X = item.Boundary.Left - (this.Boundary.Width/2);
                    }
                    if(item.Boundary.Right < this.Boundary.Right)// We have collided with an object to the left;
                    {
                        X = item.Boundary.Right + (this.Boundary.Width/2);
                    }
                }
            }

        }


    }


The final solution was to drop my own solution and implement Farseer.

Thanks

After some time with it I then opted for physics2d.net which I have found much more usable from a developers point of view.


First of all, Collision Detection depends heavily on the object geometry. There are multiple proven to work solutions cast into linear algebra math, but they are dependent on the geometries you try to use. Rather than implementing an algorithm using your object's properties try to implement a mathematic equation directly and then use the result to determine if your collision happened or not. (like using vectors t = a-b, if |t|>0 collision, else not).

If you have a simple geometry like rectangles or circles you should take a look into 'Mathematics for 3d game programming & computer graphics' by Eric Lengyel.

Also keep in mind that you can achieve collision detection with other methods like sprite collision. Another possibility is to use a higher level framework doing this work for you (e.g. OSG)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜