[英]How rotate a 3D cube at its center XNA?
I try to rotate a 3D cube on itself from its center, not the edge. 我尝试从中心而不是边缘旋转3D立方体。 Here is my code used. 这是我使用的代码。
public rotatemyCube()
{
...
Matrix newTransform = Matrix.CreateScale(scale) * Matrix.CreateRotationY(rotationLoot) * Matrix.CreateTranslation(translation);
my3Dcube.Transform = newTransform;
....
public void updateRotateCube()
{
rotationLoot += 0.01f;
}
My cube rotate fine, but not from the center. 我的立方体旋转很好,但不是从中心旋转。 Here is a schematic that explains my problem. 这是一个解释我的问题的示意图。
And i need this: 我需要这个:
my complete code 我的完整代码
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;
}
A rotation matrix rotates vertices around the origin of the coordinate system. 旋转矩阵围绕坐标系的原点旋转顶点。 In order to rotate around a certain point you have to make it the origin. 为了绕某一点旋转,你必须使它成为原点。 This can be done by simply subtracting the rotation point from every vertex in the shape. 这可以通过简单地从形状中的每个顶点减去旋转点来完成。
var rotationCenter = new Vector3(0.5f, 0.5f, 0.5f);
Matrix transformation = Matrix.CreateTranslation(-rotationCenter)
* Matrix.CreateScale(scaling)
* Matrix.CreateRotationY(rotation)
* Matrix.CreateTranslation(position);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.