c# - 我如何计算和创建三角形的位置?

标签 c# unity3d

private void FormationTriangle()
{
    newpositions = new List<Vector3>();
    for (int x = 0; x < squadMembers.Count; x++)
    {
        for (int y = x; y < 2 * (squadMembers.Count - x) - 1; y++)
        {
            Vector3 position = new Vector3(x, y);
            newpositions.Add(position);
        }
    }

    move = true;
    formation = Formation.Square;
}

循环是错误的。它使 squadMembers 排成一行。 甚至不接近三角形。 我希望小队成员站成三角形。

这是移动部分:但问题在于计算三角形位置的循环。我做的其他编队工作正常。

private void MoveToNextFormation()
{
    if (randomSpeed == false)
    {
        if (step.Length > 0)
            step[0] = moveSpeed * Time.deltaTime;
    }

    for (int i = 0; i < squadMembers.Count; i++)
    {
        squadMembers[i].transform.LookAt(newpositions[i]);
        if (randomSpeed == true)
        {
            squadMembers[i].transform.position = Vector3.MoveTowards(
                squadMembers[i].transform.position, newpositions[i], step[i]);
        }
        else
        {
            squadMembers[i].transform.position = Vector3.MoveTowards(
                squadMembers[i].transform.position, newpositions[i], step[0]);
        }
        if (Vector3.Distance(squadMembers[i].transform.position, newpositions[i]) < 
            threshold)
        {
            if (squareFormation == true)
            {
                Vector3 degrees = new Vector3(0, 0, 0);
                Quaternion quaternion = Quaternion.Euler(degrees);
                squadMembers[i].transform.rotation = Quaternion.Slerp(
                    squadMembers[i].transform.rotation, quaternion, 
                    rotateSpeed * Time.deltaTime);
            }
            else
            {
                squadMembers[i].transform.rotation = Quaternion.Slerp(
                    squadMembers[i].transform.rotation, quaternions[i], 
                    rotateSpeed * Time.deltaTime);
            }
        }
    }
}

最佳答案

这个答案会产生一个这样排列的三角形:

    x
   x x
  x x x
 x x x x
x x x x x 

或者,如果没有足够的空间来填满整个三角形:

    x
   x x
  x x x
 x x x x
x   x   x 

由于无法保证您拥有完美的三角形单元数,因此您应该高估三角形的大小,计算已放置的单元数量,然后在达到限制时停止放置它们。

首先,find the height of the smallest triangular number greater than your number of units, and that triangular number itself :

int height = Mathf.CeilToInt( (Mathf.Sqrt(8*squadMembers.Count+1f)-1f)/2 ) 
int slots = (int)(height * (height+1f)/2f)

然后,找到第一个单元的位置。我们需要知道how many rows我们拥有的插槽数量以及底部一排插槽的宽度:

float verticalModifier = 0.8f;  // 0.8f to decrease vertical space
float horizontalModifier = 1.25f; // 1.25f to increase horizontal space

float width = 0.5f * (height-1f);
Vector3 startPos = new Vector3(width* horizontalModifier, 0f, (float)(height-1f) * verticalModifier);

然后,添加直到添加到足够为止

int finalRowCount = height - slots + squadMembers.Count;
for (int rowNum = 0 ; rowNum < height && newpositions.Count < squadMembers.Count; rowNum++) {
    for (int i = 0 ; i < rowNum+1 && newpositions.Count < squadMembers.Count ; i++ ) {
        float xOffset = 0f;

        if (rowNum+1 == height) {
            // If we're in the last row, stretch it ...
            if (finalRowCount !=1) {
                // Unless there's only one item in the last row. 
                // If that's the case, leave it centered.

                xOffset = Mathf.Lerp(
                        rowNum/2f,
                        -rowNum/2f,
                        i/(finalRowCount-1f)
                        ) * horizontalModifier;
            }
        }
        else {
            xOffset = (i-rowNum /2f) * horizontalModifier; 
        }

        float yOffset = (float)rowNum * verticalModifier; 

        Vector3 position = new Vector3(
                startPos.x + xOffset, 0f, startPos.y - yOffset);
        newpositions.Add(position);

    }
}

关于c# - 我如何计算和创建三角形的位置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53162566/

相关文章:

c# - unity3d垂直布局组扩展方向如何改变?

c# - 如何检测安装的 MS-Office 版本?

c# - 使用 BeginPeek 后,我是否必须调用 EndPeek?

c# - .net MVC 使用数据库的简单成员身份验证

unity3d - Unity2d 与 Corona

android - Unity 应用程序在我的设备上崩溃

c# - 在 C# 中使用正则表达式以任意顺序在字符串中查找单词的最佳方法是什么?

c# - Web service call slow from dev 解决方案

image - Sprite 和纹理的区别?

android - 检查触摸点是否在 Unity 中的盒子碰撞器内