在 xna 中定位 3d 对象 [英] Position 3d objects in xna

查看:41
本文介绍了在 xna 中定位 3d 对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对 xna 开发很陌生,想从 Primitives3D 中定位立方体示例通过将位置向量传递给构造函数..不幸的是它不起作用..相反,它只是在旋转..

I'm pretty new to xna development and want to position the Cubes from the Primitives3D sample by passing a position vector to the constructor .. unfortunatly it does not work .. instead it is just spinning arround..

我就是这样修改cubeprimitive类的代码的:

Thats how i modified the code of the cubeprimitive class:

  public class CubePrimitive : GeometricPrimitive
{
    public Vector3 position;

    /// <summary>
    /// Constructs a new cube primitive, using default settings.
    /// </summary>
    public CubePrimitive(GraphicsDevice graphicsDevice)
        : this(graphicsDevice, 1, new Vector3(-3,0,0))
    {
    }

    /// <summary>
    /// Constructs a new cube primitive, with the specified size.
    /// </summary>
    public CubePrimitive(GraphicsDevice graphicsDevice, float size, Vector3 posi)
    {
        this.position = posi;

        // A cube has six faces, each one pointing in a different direction.
        Vector3[] normals =
        {
            new Vector3(0, 0, 1),
            new Vector3(0, 0, -1),
            new Vector3(1, 0, 0),
            new Vector3(-1, 0, 0),
            new Vector3(0, 1, 0),
            new Vector3(0, -1, 0),
        };

        // Create each face in turn.
        foreach (Vector3 normal in normals)
        {
            // Get two vectors perpendicular to the face normal and to each other.
            Vector3 side1 = new Vector3(normal.Y, normal.Z, normal.X);
            Vector3 side2 = Vector3.Cross(normal, side1);

            // Six indices (two triangles) per face.
            AddIndex(CurrentVertex + 0);
            AddIndex(CurrentVertex + 1);
            AddIndex(CurrentVertex + 2);

            AddIndex(CurrentVertex + 0);
            AddIndex(CurrentVertex + 2);
            AddIndex(CurrentVertex + 3);

            // Four vertices per face.
            AddVertex(posi+(normal - side1 - side2) * size / 2, normal);
            AddVertex(posi + (normal - side1 + side2) * size / 2, normal);
            AddVertex(posi + (normal + side1 + side2) * size / 2, normal);
            AddVertex(posi + (normal + side1 - side2) * size / 2, normal);
        }

        InitializePrimitive(graphicsDevice);
    }
}

你能告诉我如何正确修改它吗?.. 提前致谢:)

Can you please show me how to modify it correctly? .. thanks in advance :)

推荐答案

传统上,当您想要移动模型时,您所做的不是修改模型的每个顶点,而是更改 World 矩阵.

Traditionally what you do when you want to move a model around is, instead of modifying each of the vertices of the model, you change the World matrix.

>

在 Primitives3DGame.cs 中找到这一行:

Find this line in Primitives3DGame.cs:

Matrix world = Matrix.CreateFromYawPitchRoll(yaw, pitch, roll);

然后把它改成这样:

Matrix world = Matrix.CreateFromYawPitchRoll(yaw, pitch, roll)
               * Matrix.CreateTranslation(-3, 0, 0);

(当然,删除您在问题中发布的更改.)

(And, of course, remove the change you have made, as posted in your question.)

据我所知,您的代码实际上是有效的.但也许您希望立方体旋转但保持在同一个位置(这就是上面的代码所做的).您正在做的实际上与以下内容相同:

As far as I can tell your code is actually working. But perhaps you were expecting the cube to rotate but stay in the same place (which is what the above code does). What you are doing is effectively the same as:

Matrix world = Matrix.CreateTranslation(-3, 0, 0)
               * Matrix.CreateFromYawPitchRoll(yaw, pitch, roll);

注意相反的顺序 - 您首先进行模型的平移,然后然后围绕原点旋转.

Note the reversed order - you're doing the translation of your model first and then rotating around the origin.

这篇关于在 xna 中定位 3d 对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆