Ejemplo n.º 1
0
        private void DrawGun(Pen pen, TestSpaceObject obj, Vector3 gun_start, Vector3 gun_end)
        {
            Vector3 gun_word_start = Shape.TransformToWorldPos(obj.Position, gun_start, obj.CosDir, obj.SinDir);
            Vector3 gun_word_end   = Shape.TransformToWorldPos(obj.Position, gun_end, obj.CosDir, obj.SinDir);

            graphics_.Graphics.DrawLine(gun_pen_, gun_word_start.X, gun_word_start.Z, gun_word_end.X, gun_word_end.Z);
        }
Ejemplo n.º 2
0
        private void DrawObj(Pen pen, TestSpaceObject obj)
        {
            obj.GetCollideShape().Transform(obj.GetPosition(), obj.CosDir, obj.SinDir);
            Circle c = obj.GetCollideShape() as Circle;

            if (c != null)
            {
                DrawCircel(pen, c);
                DrawFace(pen, c, obj.GetFaceDirection());
                DrawGun(pen, obj, gun_start_pos_, gun_end_pos_);
            }
        }
Ejemplo n.º 3
0
        private void DrawObjs()
        {
            int ct = space_objs_.Count;

            for (int i = 0; i < ct; ++i)
            {
                TestSpaceObject obj = space_objs_[i];
                Vector3         pos = obj.Position;
                graphics_.Graphics.FillEllipse(space_obj_brush, pos.x - cell_width_, pos.z - cell_width_, cell_width_ * 2, cell_width_ * 2);
                graphics_.Graphics.DrawEllipse(space_obj_pen, pos.x - cell_width_, pos.z - cell_width_, cell_width_ * 2, cell_width_ * 2);
            }

            prkdtree_.VisitTree((float x0, float z0, float x1, float z1, int begin, int end, PrKdTreeObject[] objs) =>
            {
                graphics_.Graphics.DrawLine(prkdtree_pen, x0, z0, x1, z1);
            });

            kdtree_.VisitTree((float x0, float z0, float x1, float z1, int begin, int end, KdTreeObject[] objs) =>
            {
                //graphics_.Graphics.DrawLine(kdtree_pen, x0, z0, x1, z1);
                graphics_.Graphics.DrawLine(kdtree_pen, x0, z0, x0, z1);
                graphics_.Graphics.DrawLine(kdtree_pen, x0, z1, x1, z1);
                graphics_.Graphics.DrawLine(kdtree_pen, x1, z1, x1, z0);
                graphics_.Graphics.DrawLine(kdtree_pen, x1, z0, x0, z0);
            });

            /*
             * spatial_system_.TriangulationNetworkFinder.KdTree.VisitTree((float x0, float z0, float x1, float z1, int begin, int end, TriangleNode[] objs) =>
             * {
             *  graphics_.Graphics.DrawLine(kdgeotree_pen, x0, z0, x1, z1);
             * });
             */

            ct = selected_objs_.Count;
            for (int i = 0; i < ct; ++i)
            {
                TestSpaceObject obj = selected_objs_[i];
                Vector3         pos = obj.Position;
                graphics_.Graphics.FillEllipse(prkdtree_select_brush, pos.x - cell_width_, pos.z - cell_width_ / 2, cell_width_, cell_width_);
            }

            ct = selected_objs2_.Count;
            for (int i = 0; i < ct; ++i)
            {
                TestSpaceObject obj = selected_objs2_[i];
                Vector3         pos = obj.Position;
                graphics_.Graphics.FillEllipse(kdtree_select_brush, pos.x, pos.z - cell_width_ / 2, cell_width_, cell_width_);
            }
        }
Ejemplo n.º 4
0
        private double GetRotateDegree(TestSpaceObject obj, Vector3 gun_word_end, float mouse_x, float mouse_y)
        {
            Vector3 center            = obj.GetPosition();
            Vector3 mouse_center_vect = new Vector3(mouse_x - center.X, 0, mouse_y - center.Z);

            Vector3 gun_word_start       = GetGunStartPos(center, gun_word_end, obj.GetFaceDirection());
            float   rotate_circle_radius = DashFire.Geometry.GetMod(new Vector3(mouse_x - center.X, 0, mouse_y - center.Z));
            Vector3 hit_point;

            if (!DashFire.Geometry.GetRayWithCircleFirstHitPoint(gun_word_start,
                                                                 DashFire.Geometry.GetVectorFromDir(obj.GetFaceDirection()),
                                                                 center, rotate_circle_radius,
                                                                 out hit_point))
            {
                return(obj.GetFaceDirection());
            }
            Vector3 hit_vect      = new Vector3(hit_point.X - center.X, 0, hit_point.Z - center.Z);
            float   rotate_degree = DashFire.Geometry.GetVectorAngle(mouse_center_vect, hit_vect);

            double judge_gun_dir = Math.Atan2(mouse_x - gun_word_end.X, mouse_y - gun_word_end.Z);
            double degree        = (judge_gun_dir - obj.GetFaceDirection()) % (2 * Math.PI);

            if (Math.Abs(degree) > Math.PI)
            {
                if (degree > 0)
                {
                    return(obj.GetFaceDirection() - rotate_degree);
                }
                else
                {
                    return(obj.GetFaceDirection() + rotate_degree);
                }
            }
            else
            {
                if (degree > 0)
                {
                    return(obj.GetFaceDirection() + rotate_degree);
                }
                else
                {
                    return(obj.GetFaceDirection() - rotate_degree);
                }
            }
        }
Ejemplo n.º 5
0
        private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
        {
            if (!IsValid())
            {
                return;
            }
            line_end_ = new Point(e.X, e.Y);
            CellPos end = new CellPos();

            cell_manager_.GetCell(new Vector3(e.X, 0, map_height_ - e.Y), out end.row, out end.col);
            CellPos start = new CellPos();

            cell_manager_.GetCell(new Vector3(line_start_.X, 0, map_height_ - line_start_.Y), out start.row, out start.col);

            if (e.Button == MouseButtons.Left)
            {
                switch (left_button_function_)
                {
                case FunctionType.kSetStaticBlock:
                {
                    int  row         = end.row;
                    int  col         = end.col;
                    byte obstacle    = byte.Parse(obstacleType.Text);
                    byte oldObstacle = cell_manager_.GetCellStatus(row, col);
                    cell_manager_.SetCellStatus(row, col, obstacle);

                    if (map_patch_parser_.Exist(row, col))
                    {
                        map_patch_parser_.Update(row, col, obstacle);
                    }
                    else
                    {
                        map_patch_parser_.Update(row, col, obstacle, oldObstacle);
                    }

                    UpdateObstacleGraph();
                }
                break;

                case FunctionType.kHitPoint:
                {
                    long           stTime = TimeUtility.Instance.GetElapsedTimeUs();
                    List <CellPos> result = cell_manager_.GetCellsCrossByLine(new Vector3(line_start_.X, 0, map_height_ - line_start_.Y),
                                                                              new Vector3(line_end_.X, 0, map_height_ - line_end_.Y));
                    long edTime = TimeUtility.Instance.GetElapsedTimeUs();
                    this.Text = "pos(" + line_start_.X + "," + line_start_.Y + "->" + line_end_.X + "," + line_end_.Y + ") GetCellsCrossByLine consume " + (edTime - stTime) + "us";
                    hit_points_.Clear();
                    foreach (CellPos pos in result)
                    {
                        hit_points_.Add(pos);
                    }
                }
                break;

                case FunctionType.kGetCell:
                {
                    byte obstacle = cell_manager_.GetCellStatus(end.row, end.col);
                    obstacleType.Text = obstacle.ToString();
                    this.Text         = "pos(" + e.X + "," + e.Y + ") cell(" + end.row + "," + end.col + ") obstacle:" + obstacle;
                }
                break;

                case FunctionType.kAddObj:
                {
                    TestSpaceObject obj = new TestSpaceObject(next_space_objid_);
                    ++next_space_objid_;
                    obj.SetPosition(new Vector3(e.X, 0, e.Y));
                    space_objs_.Add(obj);

                    prkdtree_.Clear();
                    kdtree_.Clear();
                    int objCt = space_objs_.Count;
                    if (objCt > 0)
                    {
                        ISpaceObject[] temp = new ISpaceObject[objCt];
                        for (int i = 0; i < objCt; ++i)
                        {
                            temp[i] = space_objs_[i];
                        }
                        long stTime1 = TimeUtility.Instance.GetElapsedTimeUs();
                        prkdtree_.Build(temp);
                        long edTime1 = TimeUtility.Instance.GetElapsedTimeUs();

                        long stTime2 = TimeUtility.Instance.GetElapsedTimeUs();
                        kdtree_.Build(temp);
                        long edTime2 = TimeUtility.Instance.GetElapsedTimeUs();

                        System.Diagnostics.Stopwatch stopWatch = new System.Diagnostics.Stopwatch();
                        stopWatch.Start();
                        cell_manager_.ClearDynamic();
                        foreach (ISpaceObject spaceObj in space_objs_)
                        {
                            List <CellPos> cells = cell_manager_.GetCellsInCircle(Transform(spaceObj.GetPosition()), 20);
                            foreach (CellPos cellpos in cells)
                            {
                                cell_manager_.SetCellStatus(cellpos.row, cellpos.col, BlockType.DYNAMIC_BLOCK);
                            }
                        }
                        stopWatch.Stop();


                        this.Text = "obj num " + objCt + " prkdtree consume " + (edTime1 - stTime1) + "us kdtree consume " + (edTime2 - stTime1) + "us dynmic consume " + (stopWatch.ElapsedTicks) + "ticks";
                    }
                }
                break;

                case FunctionType.kQueryObj:
                {
                    Vector3 pos   = new Vector3(e.X, 0, e.Y);
                    int     objCt = space_objs_.Count;
                    if (objCt > 0)
                    {
                        float radius  = float.Parse(queryRadius.Text);
                        long  stTime1 = TimeUtility.Instance.GetElapsedTimeUs();
                        selected_objs_.Clear();
                        prkdtree_.Query(pos, radius * scale, (float distSqr, PrKdTreeObject treeObj) =>
                            {
                                selected_objs_.Add(treeObj.SpaceObject as TestSpaceObject);
                            });
                        long edTime1 = TimeUtility.Instance.GetElapsedTimeUs();

                        long stTime2 = TimeUtility.Instance.GetElapsedTimeUs();
                        selected_objs2_.Clear();
                        kdtree_.Query(pos, radius * scale, (float distSqr, KdTreeObject treeObj) =>
                            {
                                selected_objs2_.Add(treeObj.SpaceObject as TestSpaceObject);
                            });
                        long edTime2 = TimeUtility.Instance.GetElapsedTimeUs();

                        this.Text = "obj num " + objCt + " query prkdtree " + selected_objs_.Count + " consume " + (edTime1 - stTime1) + "us query kdtree " + selected_objs2_.Count + " consume " + (edTime2 - stTime2) + "us";
                    }
                }
                break;
                }
            }
            else
            {
                if (e.Button == MouseButtons.Right && left_button_function_ == FunctionType.kSetStaticBlock)
                {
                    int row, col;
                    cell_manager_.GetCell(new Vector3(e.X, 0, e.Y), out row, out col);
                    byte oldObstacle = cell_manager_.GetCellStatus(row, col);
                    byte obstacle    = BlockType.NOT_BLOCK;
                    cell_manager_.SetCellStatus(end.row, end.col, obstacle);

                    if (map_patch_parser_.Exist(row, col))
                    {
                        map_patch_parser_.Update(row, col, obstacle);
                    }
                    else
                    {
                        map_patch_parser_.Update(row, col, obstacle, oldObstacle);
                    }

                    UpdateObstacleGraph();
                }
                else
                {
                    long stTime = TimeUtility.Instance.GetElapsedTimeUs();
                    found_path_ = spatial_system_.FindPathWithCellMap(new Vector3(line_start_.X, 0, map_height_ - line_start_.Y), new Vector3(line_end_.X, 0, map_height_ - line_end_.Y));
                    long edTime = TimeUtility.Instance.GetElapsedTimeUs();

                    long stTime2 = TimeUtility.Instance.GetElapsedTimeUs();
                    int  stRow, stCol, edRow, edCol;
                    spatial_system_.GetCell(new Vector3(line_start_.X, 0, map_height_ - line_start_.Y), out stRow, out stCol);
                    spatial_system_.GetCell(new Vector3(line_end_.X, 0, map_height_ - line_end_.Y), out edRow, out edCol);
                    //List<CellPos> path = new List<CellPos>();
                    //tree_cache_finder_.GetPath(new CellPos(stRow, stCol), new CellPos(edRow, edCol), path);
                    found_path_ = jump_point_finder_.FindPath(new CellPos(stRow, stCol), new CellPos(edRow, edCol));
                    long edTime2 = TimeUtility.Instance.GetElapsedTimeUs();

                    long stTime3 = TimeUtility.Instance.GetElapsedTimeUs();
                    found_path_ = spatial_system_.FindPath(new Vector3(line_start_.X, 0, map_height_ - line_start_.Y), new Vector3(line_end_.X, 0, map_height_ - line_end_.Y));
                    long edTime3 = TimeUtility.Instance.GetElapsedTimeUs();

                    /*//
                     * found_path_.Clear();
                     * if (path.Count > 0) {
                     * foreach (CellPos p in path) {
                     * found_path_.Add(spatial_system_.GetCellCenter(p.row, p.col));
                     * }
                     * }
                     * ///*/
                    this.Text = "findpath:" + new Vector2(line_start_.X, map_height_ - line_start_.Y).ToString() + " to " + new Vector2(line_end_.X, map_height_ - line_end_.Y).ToString() + " consume " + (edTime - stTime) / 1000.0f + "ms no preprocess consume " + (edTime2 - stTime2) / 1000.0f + "ms triangulation network consume " + (edTime3 - stTime3) / 1000.0f + "ms";
                }
            }
            is_mouse_down_ = false;
            key_hit_       = 0;
        }