Meh*_*ard 3 c# xna transform rotation matrix
我尝试从中心而不是边缘旋转3D立方体.这是我使用的代码.
public rotatemyCube()
{
...
Matrix newTransform = Matrix.CreateScale(scale) * Matrix.CreateRotationY(rotationLoot) * Matrix.CreateTranslation(translation);
my3Dcube.Transform = newTransform;
....
public void updateRotateCube()
{
rotationLoot += 0.01f;
}
Run Code Online (Sandbox Code Playgroud)
我的立方体旋转很好,但不是从中心旋转.这是一个解释我的问题的示意图.

我需要这个:

我的完整代码
private void updateMatriceCubeToRotate()
{
foreach (List<instancedModel> ListInstance in listStructureInstance)
{
foreach (instancedModel instanceLoot in ListInstance)
{
if (my3Dcube.IsAloot)
{
Vector3 scale;
Quaternion rotation;
Vector3 translation;
//I get the position, rotation, scale of my cube
my3Dcube.Transform.Decompose(out scale,out rotation,out translation);
var rotationCenter = new Vector3(0.1f, 0.1f, 0.1f);
//Create new transformation with new rotation
Matrix transformation =
Matrix.CreateTranslation(- rotationCenter)
* Matrix.CreateScale(scale)
* Matrix.CreateRotationY(rotationLoot)
* Matrix.CreateTranslation( translation);
my3Dcube.Transform = transformation;
}
}
}
//Incremente rotation
rotationLoot += 0.05f;
}
Run Code Online (Sandbox Code Playgroud)
旋转矩阵围绕坐标系的原点旋转顶点.为了绕某一点旋转,你必须使它成为原点.这可以通过简单地从形状中的每个顶点减去旋转点来完成.

var rotationCenter = new Vector3(0.5f, 0.5f, 0.5f);
Matrix transformation = Matrix.CreateTranslation(-rotationCenter)
* Matrix.CreateScale(scaling)
* Matrix.CreateRotationY(rotation)
* Matrix.CreateTranslation(position);
Run Code Online (Sandbox Code Playgroud)