【问题标题】:as3 Quadtree being slowas3四叉树很慢
【发布时间】:2014-05-28 21:08:38
【问题描述】:

这是我的四叉树类,但我还没有添加碰撞检测,在所有在线示例中,它们可以在 60 fps 下通过碰撞检测获得 500 +,但我的一个仅以 20 fps 运行而没有碰撞检测。

我正在关注本教程 http://gamedevelopment.tutsplus.com/tutorials/quick-tip-use-quadtrees-to-detect-likely-collisions-in-2d-space--gamedev-374 这是 java 但我使用 as3

public class Quadtree extends Entity
{
    private var Max_objects:int = 1;
    private var Max_levels:int = 5;

    private var level:int;
    private var objects:Vector.<Rectangle>;
    public var rectangle:Rectangle;
    public var Quadtree_list:Vector.<Quadtree>;

    public function Quadtree(tmp_level:int , tmp_rec:Rectangle) 
    {
        level = tmp_level;
        objects = new Vector.<Rectangle>();
        rectangle = tmp_rec;
        Quadtree_list = new Vector.<Quadtree>();
        Quadtree_list.length = 3;

    }

     public function clear():void
     {
        objects.length = 0;
        for (var i:Number = 0; i < Quadtree_list.length ; i++)
        {
            if (Quadtree_list[i] != null)
            {
                Quadtree_list[i].clear();
                world.remove(Quadtree_list[i]);
                Quadtree_list[i] = null;

            }
        }
     }

    public function split():void
    {
        var subWidth:int = rectangle.width / 2;
        var subHeight:int = rectangle.height / 2;
        var xx:int = rectangle.x;
        var yy:int = rectangle.y;

        Base._world.add(Quadtree_list[0] = new Quadtree(level + 1, new Rectangle(xx + subWidth, yy, subWidth, subHeight)));
        Base._world.add(Quadtree_list[1] = new Quadtree(level+1,new Rectangle(xx ,yy,subWidth,subHeight)));
        Base._world.add(Quadtree_list[2] = new Quadtree(level+1,new Rectangle(xx,yy + subHeight,subWidth,subHeight)));
        Base._world.add(Quadtree_list[3] = new Quadtree(level+1,new Rectangle(xx + subWidth,yy + subHeight,subWidth,subHeight)));
    }

    /*
     * Determine which node the object belongs to. -1 means
     * object cannot completely fit within a child node and is part
     * of the parent node
     */
    public function get_index(tmp_rect:Rectangle):Number
    {
        var index:int = -1;
        var verticalMidpoint:Number = rectangle.x + (rectangle.width / 2);
        var horizontalMidpoint:Number = rectangle.y + (rectangle.height / 2);

        // Object can completely fit within the top quadrants
        var topQuadrant:Boolean = (tmp_rect.y < horizontalMidpoint && tmp_rect.y + tmp_rect.height < horizontalMidpoint);
         // Object can completely fit within the bottom quadrants
        var bottomQuadrant:Boolean = (tmp_rect.y > horizontalMidpoint);

         // Object can completely fit within the left quadrants
         if (tmp_rect.x < verticalMidpoint && tmp_rect.x + tmp_rect.width < verticalMidpoint)
         {
             if (topQuadrant)
             {
                 index = 1;
             }
             else if (bottomQuadrant)
             {
                 index = 2;
             }
         }
         else 
         // Object can completely fit within the right quadrants
         if (tmp_rect.x > verticalMidpoint)
         {
             if (topQuadrant)
             {
                 index = 0;
             }
             else if (bottomQuadrant)
             {
                 index = 3;
             }
         }
         return index;
    }

    /*
     * Insert the object into the quadtree. If the node
     * exceeds the capacity, it will split and add all
     * objects to their corresponding nodes.
     */
    public function insert(tmp_rect:Rectangle):void
    {

        if (Quadtree_list[0] != null)
        {
            var index:int = get_index(tmp_rect);
            if (index != -1)
            {
                Quadtree_list[index].insert(tmp_rect)
                return;
            }
        }

        objects.push(tmp_rect);

        if (objects.length > Max_objects && level < Max_levels)
        {
            if (Quadtree_list[0] == null)
            {
                split(); 
            }
            var i:int = 0;
            while (i < objects.length)
            {
                var indexx:int = get_index(objects[i]);
                if (indexx != -1)
                {
                    Quadtree_list[indexx].insert(objects[i]);
                    objects.splice(i, 1);
                }
                else 
                {
                    i++;
                }
            }

        }
    }

你能明白为什么它的表现不是很好吗?

【问题讨论】:

    标签: actionscript-3 quadtree


    【解决方案1】:

    如果不确切了解您的使用方式,很难确定。另外,它扩展了可能正在做的实体......任何事情:)

    我也不是四叉树专家,但如果你经常调用 split(),看起来它最终可能会很费力 - 对新四叉树和新矩形的大量实例化调用。如果这确实是一个瓶颈,您可以考虑实例化一个您刚刚传递的矩形实例。与四叉树相同。或者使用对象池,这样您至少可以回收而不是疯狂地创建新事物。

    希望对你有帮助:)

    【讨论】:

      猜你喜欢
      • 2017-08-09
      • 1970-01-01
      • 2013-05-12
      • 2014-06-14
      • 2015-05-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多