Ejemplo n.º 1
0
        public Vector2 findNextDirection()
        {
            if (simplex.count() == 2)
            {
                Vector2 crossPoint = GJKTool.getPerpendicularToOrigin(simplex.get(0), simplex.get(1));
                // 取靠近原点方向的向量
                return(Vector2.zero - crossPoint);
            }
            else if (simplex.count() == 3)
            {
                Vector2 crossOnCA = GJKTool.getPerpendicularToOrigin(simplex.get(2), simplex.get(0));
                Vector2 crossOnCB = GJKTool.getPerpendicularToOrigin(simplex.get(2), simplex.get(1));

                // 保留距离原点近的,移除较远的那个点
                if (crossOnCA.sqrMagnitude < crossOnCB.sqrMagnitude)
                {
                    simplex.remove(1);
                    return(Vector2.zero - crossOnCA);
                }
                else
                {
                    simplex.remove(0);
                    return(Vector2.zero - crossOnCB);
                }
            }
            else
            {
                // 不应该执行到这里
                return(new Vector2(0, 0));
            }
        }
Ejemplo n.º 2
0
        void UpdateSelection()
        {
            if (Input.GetMouseButtonDown(0))
            {
                Vector2 mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
                //Debug.Log("mousePos: " + mousePos);

                selectedIndex = -1;
                for (int i = 0; i < shapes.Count; ++i)
                {
                    if (GJKTool.contains(shapes[i].vertices, mousePos))
                    {
                        selectedIndex = i;
                        break;
                    }
                }

                if (selectedIndex >= 0)
                {
                    var     t        = colliders[selectedIndex].transform;
                    Vector2 position = t.position;
                    selectedOffset = mousePos - position;

                    float mouseAngle = Mathf.Atan2(selectedOffset.y, selectedOffset.x) * Mathf.Rad2Deg;
                    selectedAngle = mouseAngle - t.eulerAngles.z;
                }
            }
            else if (Input.GetMouseButtonUp(0))
            {
                selectedIndex = -1;
            }
            else if (Input.GetMouseButton(0))
            {
                if (selectedIndex < 0)
                {
                    return;
                }

                Transform t = colliders[selectedIndex].transform;

                Vector2 mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
                if (selectedOffset.magnitude < 0.4f) // 移动
                {
                    Vector3 position = t.position;
                    position.x = mousePos.x - selectedOffset.x;
                    position.y = mousePos.y - selectedOffset.y;
                    t.position = position;
                }
                else // 旋转
                {
                    Vector2 position   = t.position;
                    Vector2 mouseDir   = mousePos - position;
                    float   mouseAngle = Mathf.Atan2(mouseDir.y, mouseDir.x) * Mathf.Rad2Deg;

                    float angle = mouseAngle - selectedAngle;
                    t.eulerAngles = new Vector3(0, 0, angle);
                }
            }
        }
Ejemplo n.º 3
0
 public bool contains(Vector2 point)
 {
     return(GJKTool.contains(points, point));
 }