首页 > 解决方案 > 带有自定义 3D 图形库的 c++ - 在条件之间使用或运算符编写一个 while 循环

问题描述

我正在尝试编写一个循环代码块,该代码块在 c++ 中询问以下内容

while object A, or B or C or D == X, Y, Z (coords)

if A meets conditions, move object A East.

If B meets conditions, move object B East.

等等,

我已经将每个对象坐标预先建立为一个字符串,所以我只需要将它们与一个控件进行比较,目前,我只是有一个循环发生,它无法识别哪个对象正在触发它。

读取坐标和移动对象的代码位于特定库中,我的导师建议使用数组和 while 循环来实现这一点,但即使花了几个小时研究它,我也不知道该怎么做。

我来自统一和虚幻引擎的背景,所以像这样简单的事情让我头疼,因为我只想在点上使用条件,而不是对每个对象做出反应。

我对 c# 很陌生,所以请尽量保持解决方案简单谢谢!

编辑:这是我目前使用的代码

//Load strings at the start of the game
```string bblocal = (ballblue->GetLocalX, ballblue->GetLocalY, ballblue->GetLocalZ);
        string bilocal = (ballindigo->GetLocalX,ballbindigo->GetLocalY, ballindigo->GetLocalZ);
        string bvlocal = (ballviolet->GetLocalX,ballviolet->GetLocalY, ballviolet->GetLocalZ);
        string bflocal = (ballfawn->GetLocalX,ballfawn->GetLocalY, ballfawn->GetLocalZ);

        string bb = bblocal;
        string bi = bilocal;
        string bv = bvlocal;
        string bf = bflocal;```

  //For each tick, check the following
```while (bb || bi || bv || vf == (-50, 10, 50)
        {
            //Turn blue and move it to next point
if blue
{
ballblue->MoveX (0);
ballblue->MoveZ (100);
ballblue->RotateX (-50);
ballblue->RotateZ (50);
}
            //Turn Indigo and move it to next point
            //Turn Violet and move it to next point
            //Turn Fawn and move it to next point

        }

标签: c++

解决方案


对于给定的类点,例如:

public class Point
{
    public double X { get; set; }
    public double Y { get; set; }
    public double Z { get; set; }
}

你有一个定义点的早午餐:

var A = new Point{X=1, Y=0, Z=0};
var B = new Point{X=0, Y=1, Z=0};
var C = new Point{X=0, Y=0, Z=1};
// Etc

和一个参考点来比较它们。列出您必须在数组中检查的所有点。

var referencePoint = new Point{X=0, Y=0, Z=1};
var pointsToCheck = new []{A, B, C};

对这个数组的每个元素进行验证。您可以通过变量访问该点。

foreach(var p in pointsToCheck){

    // Would be nice If Point add IEquatable<Point> with Equals gethashcode
    if(    p.X == referencePoint.X
        && p.Y == referencePoint.Y
        && p.Z == referencePoint.Z
    )
    {
        //Do something!
        p.X ++;
        // Break; // if only the first must be moved.
    }
}

直接用 LinQ 查询

pointsToCheck.Where( p => p.X == referencePoint.X
        && p.Y == referencePoint.Y
        && p.Z == referencePoint.Z) ;

如果你有一个指向位置的“角”数组,你将需要 IEquatable。

var currentCornerIndex = Array.IndexOf(cornersList, CurrentPoint);
//Will return the next corner: 
// if it's the last corner return the first
// if not a corner return the first.
var nextCornerIndex = (currentCornerIndex + 1) % cornersList.Length;
var nextCorner = cornersList[nextCornerIndex];

推荐阅读