0

我在尝试串联移动简单模型时遇到问题。我有 20 个较小的模型连接到一个较大的模型。它本质上是一个带有多个外部大炮的飞碟。我见过其他问题,比如这个,看起来几乎是我想要的。但是,这只会创建绘图转换。我实际上需要在 3d 空间中移动子模型,因为它们可以被独立破坏,因此需要碰撞检测。这是我旋转父级的方式(在 Update() 函数中):

angle += 0.15f;

RotationMatrix = Matrix.CreateRotationX(MathHelper.PiOver2) * Matrix.CreateRotationZ(MathHelper.ToRadians(angle) * MathHelper.PiOver2);

我已经尝试了很多解决方案,但定位总是关闭,所以它们看起来并不真正依附。这是我得到的最接近的:

cannons[x].RotationMatrix = Matrix.CreateRotationX(MathHelper.PiOver2) * 
Matrix.CreateRotationZ(MathHelper.ToRadians(angle + cannons[x].angle) * 
MathHelper.PiOver2);

cannons[x].position.X = (float)(Math.Cos(MathHelper.ToRadians(angle + cannons[x].originAngle) *
 MathHelper.PiOver2) * 475) + position.X;

cannons[x].position.Y = (float)(Math.Sin(MathHelper.ToRadians(angle + cannons[x].originAngle) *
 MathHelper.PiOver2) * 475) + position.Y;

我在那段代码中做错了什么?或者,如果我的方法完全关闭,那么这样做的正确方法是什么?

4

1 回答 1

2

您应该对所有内容使用矩阵转换。

class Physics {

 public Vector3 Traslation;
 public Vector3 Scale;
 public Quaternion Rotation;

 public Physics Parent;
 public List<Physics> Children;
 public Matrix World;


 public Update() {
      World =   Matrix.CreateScale(Scale) 
              * Matrix.CreateFromQuaternion(Rotation) 
              * Matrix.CreateTranslation;
      if (Parent!=null) {
         World *= Parent.World ;
      }    
      foreach (var child in children) child.Update();         
 }
}

意识到这段代码没有优化,可以改进。

这样,您应该有一个用于大型模型的物理对象和 20 个用于小型模型的物理对象,通过 Parent 属性附加到大型模型。

如果您需要绝对坐标中的对象的 Traslation、Rotation 和 Scale,您可以使用 Matrix.Decompose 方法,尽管将 World 矩阵传递给您的着色器以转换顶点要好得多。

于 2013-08-21T08:15:01.140 回答