Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/291.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何围绕其中心旋转三维模型?_C#_3d_Xna - Fatal编程技术网

C# 如何围绕其中心旋转三维模型?

C# 如何围绕其中心旋转三维模型?,c#,3d,xna,C#,3d,Xna,我正在做一个3d汽车游戏,我有一个旋转的问题。 我想绕模型本身旋转,但当我移动时,它开始四处移动 世界 问题是:如何创建模型移动的中心 我试图像这样更改代码: effect.World = Matrix.CreateRotationZ(modelRotation) * effect.World = Matrix.CreateTranslation(position); 现在,它不再相对于模型向前移动,而是沿着设定的方向移动! &这是我的代码: effect.World = Matrix

我正在做一个3d汽车游戏,我有一个旋转的问题。 我想绕模型本身旋转,但当我移动时,它开始四处移动 世界

问题是:如何创建模型移动的中心

我试图像这样更改代码:

 effect.World = Matrix.CreateRotationZ(modelRotation) *  effect.World = Matrix.CreateTranslation(position); 
现在,它不再相对于模型向前移动,而是沿着设定的方向移动! &这是我的代码:

 effect.World = Matrix.CreateTranslation(position) * Matrix.CreateRotationZ(modelRotation); 
                effect.View = camera.View; 
                effect.Projection = camera.Projection;

我有一些建议可以帮助您开始:

  • DirectX/Xna中的矩阵乘法顺序与您在学校学到的不同 在学校,
    v=aby
    的意思是:
    v=aby
    。所以当链接矩阵时,首先应用B

    如果你想组合矩阵A和B,你可以像
    C=AB

    在Directx/XNA中,顺序相反。要组合矩阵B和A,您需要编写
    var C=B*A

    为了避免犯错误,我采用了一种命名约定:每个矩阵(或变换)被称为
    AtoB
    WorldToView
    ModelToWorld
    ,或
    ModelToRotatedModel

    这提醒您,第一个矩阵的输出必须与右矩阵的输入匹配:

      var modelToView = modelToWorld * worldToView; 
    
    而不是:

      var nowhereToNowhere = worldToView * modelToWorld; 
    
    这对我帮助很大,我希望它能帮助你解决矩阵问题

    附言。
    我希望你的汽车模型的起源在汽车的中心,否则它仍然会奇怪地移动

    尝试切换这些值:

    effect.World=Matrix.CreateTranslation(位置)*Matrix.CreateRotationZ(modelRotation)

    因此,它变成:

    effect.World=Matrix.CreateRotationZ(modelRotation)*Matrix.CreateTranslation(位置)

    我遵循一个简单的首字母缩略词,叫做
    ISROT

    身份

    鳞片

    轮换

    定向

    翻译


    您从右向左工作,因此您总是以
    翻译

    结束语句。我建议您坚持使用每个语句的一个赋值运算符。如果不这样做,结果可能会令人困惑、惊讶和难以理解。当您这样做时发生了什么:effect.World=Matrix.CreateRotationZ(modelRotation)*effect.World=Matrix.CreateTranslation(position);我很困惑,你们说它“朝着设定的方向移动”,你们这是什么意思?如何得到汽车的原点?@zionpi最简单的方法是计算汽车的边界框,然后取这个框的中心。