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 = shapes[selectedIndex].rigidbody; Vector2 position = t.position; selectedOffset = mousePos - position; float mouseAngle = Mathf.Atan2(selectedOffset.y, selectedOffset.x) * Mathf.Rad2Deg; selectedAngle = mouseAngle - t.rotation; } } else if (Input.GetMouseButtonUp(0)) { selectedIndex = -1; } else if (Input.GetMouseButton(0)) { if (selectedIndex < 0) { return; } Rigidbody t = shapes[selectedIndex].rigidbody; 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.rotation = angle; } } }
public bool contains(Vector2 point) { return(GJKTool.contains(points, point)); }