new BroadPhase (Sweep and Prune)

简介:

I did a quick look through your most recent code and was kind of shocked to find out you still didn’t have a more efficient Broad Phase. So I decided to write one for your engine during my break. I tried to make it simple instead of optimized for ease of understanding. 

public  class SweepAndPrune
{
    delegate void CollisionCallback(Wrapper w1, Wrapper w2);
 
    class Wrapper
    {
        public Node xBegin;
        public Node xEnd;
        public Node yBegin;
        public Node yEnd;
        public Geometry geometry;
        public List<Geometry> colliders;
        public Wrapper(Geometry geometry)
        {
            this.geometry = geometry;
            this.colliders = new List<Geometry>();
            this.xBegin = new Node(thistrue);
            this.xEnd = new Node(thisfalse);
            this.yBegin = new Node(thistrue);
            this.yEnd = new Node(thisfalse);
        }

        public void Update()
        {
            colliders.Clear();
            xBegin.value = geometry.AABB.Min.X;
            xEnd.value = geometry.AABB.Max.X;
            yBegin.value = geometry.AABB.Min.Y;
            yEnd.value = geometry.AABB.Max.Y;
        }

    }

 
    class Node
    {
        public bool begin;
        public float value;
        public Wrapper wrapper;
        public Node(Wrapper wrapper, bool begin)
        {
            this.wrapper = wrapper;
            this.begin = begin;
        }

    }

 
    List<Wrapper> wrappers = new List<Wrapper>();
    List<Node> xList = new List<Node>();
    List<Node> yList = new List<Node>();
 
    public void AddGeometry(Geometry item)
    {
        Wrapper wrapper = new Wrapper(item);
        wrappers.Add(wrapper);
        xList.Add(wrapper.xBegin);
        xList.Add(wrapper.xEnd);
        yList.Add(wrapper.yBegin);
        yList.Add(wrapper.yEnd);
    }

    public void RemoveDisposed()
    {
        if (wrappers.RemoveAll(delegate(Wrapper w) return w.geometry.Body.IsDisposed; }) > 0)
        {
            xList.RemoveAll(delegate(Node n) return n.wrapper.geometry.Body.IsDisposed; });
            yList.RemoveAll(delegate(Node n) return n.wrapper.geometry.Body.IsDisposed; });
        }

    }

    public void Run()
    {
        Update();
        RunAxis(xList, HandleFirstCollision);
        RunAxis(yList, HandleSecondCollision);
    }

    /// <summary>
    
/// Updates the nodes and sorts them.
    
/// </summary>

    void Update()
    {
        foreach (Wrapper wrapper in wrappers)
        {
            wrapper.Update();
        }

        xList.Sort(delegate(Node l, Node r) return l.value.CompareTo(r.value); });
        yList.Sort(delegate(Node l, Node r) return l.value.CompareTo(r.value); });
    }

    /// <summary>
    
/// Runs the collision detection on a axis
    
/// </summary>

    void RunAxis(List<Node> list, CollisionCallback callback)
    {
        LinkedList<Wrapper> proximityList = new LinkedList<Wrapper>();
        foreach (Node node in list)
        {
            if (node.begin)
            {
                foreach (Wrapper wrapper in proximityList)
                {
                    callback(node.wrapper, wrapper);
                }

                proximityList.AddLast(node.wrapper);
            }

            else
            {
                proximityList.Remove(node.wrapper);
            }

        }

    }

    /// <summary>
    
/// when there is a collsion along the first axis
    
/// and if there is no early fail conditions then they are
    
/// added to each others colliders list
    
/// </summary>

    void HandleFirstCollision(Wrapper w1, Wrapper w2)
    {
        //if(early fail conditions) {return;}
        w1.colliders.Add(w2.geometry);
        w2.colliders.Add(w1.geometry);
    }

    /// <summary>
    
/// when there is a collision along the second axis then 
    
/// it checks to see if there was a collision along the first. 
    
/// if there is then the 2 geometries bounding boxes are colliding.
    
/// </summary>

    void HandleSecondCollision(Wrapper w1, Wrapper w2)
    {
        if (w1.colliders.Contains(w2.geometry))
        {
            //this is a confirmed broadphase collision
            
//so add a new arbiter or something for 
            
//w1.geometry and w2.geometry
        }

    }

}
目录
相关文章
|
编译器 开发工具 git
【Git异常】You are in ‘detached HEAD‘ state, which means that you‘re not on any branch Checkout a branch
【Git异常】You are in ‘detached HEAD‘ state, which means that you‘re not on any branch Checkout a branch
290 0
|
开发工具 git
Git出现MERGING状态
Git合并时有冲突,出现MERGING状态
6880 0
|
Shell 开发工具 git
【Git】解决Untracked Files Prevent Checkout的问题
【Git】解决Untracked Files Prevent Checkout的问题
2214 0
|
Docker 容器
29-Docker-常用命令详解-docker history/diff
29-Docker-常用命令详解-docker history/diff
|
开发工具 git
解决报错:Remove untracked files, stash or commit any changes, and try again
解决报错:Remove untracked files, stash or commit any changes, and try again
111 1
|
Java 开发工具 Maven
git解决error: The following untracked working tree files would be overwritten by c
git解决error: The following untracked working tree files would be overwritten by c
1740 0
|
算法 安全 Linux
Git 拉取项目小技巧之切换分支error: The following untracked working tree files would be overwritten by checkout:
Git 拉取项目小技巧之切换分支error: The following untracked working tree files would be overwritten by checkout:
755 0
Git 拉取项目小技巧之切换分支error: The following untracked working tree files would be overwritten by checkout:
|
开发工具 git
Git - Error:The following untracked working tree files would be overwritten by checkout
Git - Error:The following untracked working tree files would be overwritten by checkout
811 0
|
Docker 容器
“ docker logs -f --tail ”查看日志:“docker logs“ requires exactly 1 argument.
“ docker logs -f --tail ”查看日志:“docker logs“ requires exactly 1 argument.
555 0
“ docker logs -f --tail ”查看日志:“docker logs“ requires exactly 1 argument.
|
开发工具 git
同步本地远程分支 git remote prune origin
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/voidreturn/article/details/79578766 git ...
1423 0

热门文章

最新文章