English 中文(简体)
将3D标注在轴心上,同世界轴心一样,
原标题:Rotating 3D object on it s axis as oppose to the world axis - XNA
  • 时间:2010-06-20 19:39:01
  •  标签:
  • c#
  • xna

Hey all, i ve been working on this small project in XNA the goal is pretty basic i have a 3D model which pops up somewhere on the screen, the object is suppose to rotate on it s y axis,

The problem is: what happens instead is that the models rotate around themselves and also start to orbit the middle point between themselves and the 0,0,0 point in the screen, so it looks like the middle model is rotating normally while the other 2 act in the afore mentioned way (included img s of the rotating models below).

“某些”细节

我的架构就是这样,即有3个模型(这些模型吸收了狩猎、Position、Size @ ctor)。

private void initDradles()
    {
        m_Dradles[0] = new DradlePositionColor(this, new Vector3(15, 0, 0), 8);
        m_Dradles[1] = new DradlePositionColor(this, new Vector3(0, 0, 0), 8);
        m_Dradles[2] = new DradlePositionColor(this, new Vector3(-15, 0, 0), 8);
        this.Components.Add(m_Dradles[0]);
        this.Components.Add(m_Dradles[1]);
        this.Components.Add(m_Dradles[2]);
    }

这些模型首先以图1所示方式出现在屏幕上。

他们都拥有同样的基本影响,但有一个不同的世界矩阵。

模型的基本效果生成和分配

protected override void Initialize()
    {
        float k_NearPlaneDistance = 0.5f;
        float k_FarPlaneDistance = 1000.0f;
        float k_ViewAngle = MathHelper.PiOver4;

        // we are storing the field-of-view data in a matrix:
        m_ProjectionFieldOfView = Matrix.CreatePerspectiveFieldOfView(
            k_ViewAngle,
            GraphicsDevice.Viewport.AspectRatio,
            k_NearPlaneDistance,
            k_FarPlaneDistance);

        // we want to shoot the center of the world:
        Vector3 targetPosition = new Vector3(0, 0, 0);//Vector3.Zero;
        // we are standing 100 units in front of our target:
        Vector3 pointOfViewPosition = new Vector3(0, 0, 130);
        // we are not standing on our head:
        Vector3 pointOfViewUpDirection = new Vector3(0, 1, 0);

        // we are storing the point-of-view data in a matrix:
        m_PointOfView = Matrix.CreateLookAt(
            pointOfViewPosition, targetPosition, pointOfViewUpDirection);

        base.Initialize();
    }

    protected override void LoadContent()
    {
        // we are working with the out-of-the box shader that comes with XNA:
        m_BasicEffect = new BasicEffect(this.GraphicsDevice, null);
        m_BasicEffect.View = m_PointOfView;
        m_BasicEffect.Projection = m_ProjectionFieldOfView;
        m_BasicEffect.VertexColorEnabled = true;

        // we are working with colored vertices
        GraphicsDevice.VertexDeclaration = new VertexDeclaration(
            GraphicsDevice, VertexPositionColor.VertexElements);

        // we did not use certain clockwise ordering in our vertex buffer
        // and we don t want antthing to be culled away..
        this.GraphicsDevice.RenderState.CullMode = CullMode.None;

        foreach (var dradle in m_Dradles)
        {
            dradle.BasicEffect = m_BasicEffect;
        }

        base.LoadContent();
    }

在每种模式中,该守则采用更新方法:

private void BuildWorldMatrix()
    {
        m_WorldMatrix = Matrix.Identity;
        m_WorldMatrix *= Matrix.CreateScale(m_Scales);
        m_WorldMatrix *= Matrix.CreateRotationX(m_Rotations.X);
        m_WorldMatrix *= Matrix.CreateRotationY(m_Rotations.Y);
        m_WorldMatrix *= Matrix.CreateRotationZ(m_Rotations.Z);
        m_WorldMatrix *= Matrix.CreateTranslation(m_Position);
            ///*I*/ Matrix.Identity *
            ///*S*/ Matrix.CreateScale(m_Scales) *
            ///*R*/ Matrix.CreateRotationX(m_Rotations.X) *
            //        Matrix.CreateRotationY(m_Rotations.Y) *
            //        Matrix.CreateRotationZ(m_Rotations.Z) *
            ///* No Orbit */
            ///*T*/ Matrix.CreateTranslation(m_Position);
    }

    public override void Update(GameTime gameTime)
    {
        m_Rotations.Y += (float)gameTime.ElapsedGameTime.TotalSeconds;
        BuildWorldMatrix();

        base.Update(gameTime);
    }

以及这一代法

public override void Draw(GameTime gameTime)
    {
        m_BasicEffect.World = m_WorldMatrix;
        m_BasicEffect.Begin();
        foreach (EffectPass pass in m_BasicEffect.CurrentTechnique.Passes)
        {
            pass.Begin();
            Render();
            pass.End();
        }
        m_BasicEffect.End();

        base.Draw(gameTime);
    }

    public override void Render()
    {
        renderVerticesBox(m_BodyVertices);
        renderVerticesBox(m_HandleVertices);
        Game.GraphicsDevice.DrawUserPrimitives<VertexPositionColor>(
            PrimitiveType.TriangleFan, m_PyramidVertices, 0, 4);
    }

    private void renderVerticesBox(VertexPositionColor[] m_Vertices)
    {
        Game.GraphicsDevice.DrawUserPrimitives<VertexPositionColor>(
            PrimitiveType.TriangleStrip, m_Vertices, 0, 8);

        Game.GraphicsDevice.DrawUserPrimitives<VertexPositionColor>(
            PrimitiveType.TriangleStrip, m_Vertices, 10, 2);

        Game.GraphicsDevice.DrawUserPrimitives<VertexPositionColor>(
            PrimitiveType.TriangleStrip, m_Vertices, 14, 2);
    }

image when rotating alt text http://www.freeimagehosting.net/uploads/b0aa83d0d6.png

形象的开始

alt text http://www.freeimagehosting.net/uploads/ee9e5c379c.png

最佳回答

我认为,你需要走下行。

m_WorldMatrix *= Matrix.CreateTranslation(m_Position);

在采用轮任矩阵之前,就这样做。

问题回答

暂无回答




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签