如何将3D对象在XNA? [英] how to move 3d object in XNA?

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

问题描述

我想搬到一个三维汽车模型,当我preSS向左或向右箭头键更改角度,当我preSS向上箭头的汽车驱动器。

I want to move a 3d car model, when I press the left or right arrow key I change the angle, when I press the up arrow the car drives.

这是在code。在更新方法:

This is the code in the update method:

float dirX = (float)Math.Sin(angle);
float dirY = (float)Math.Cos(angle);

if (Keyboard.GetState().IsKeyDown(Keys.Up))
        {
            position += new Vector3(-dirX, dirY, 0);

            if (Keyboard.GetState().IsKeyDown(Keys.Left))
            {
                angle += 0.015f;
            }

            if (Keyboard.GetState().IsKeyDown(Keys.Right))
            {
                angle -= 0.015f;
            }
        }

这是计算的部分,但显然我还需要动车在屏幕上。 我想车子向前走,不起来了,所以我想我应该旋转在X轴旋转90度,并且我也想旋转车的时候我preSS向左或向右按​​键。

This is the calculating part, but obviously I also need to move the car on the screen. I want the car to move forward, not up, so I thought I should rotate it 90 degrees on the X axis, and also I want to rotate the car when I press the left or right keys.

我写这篇code:

world = Matrix.CreateTranslation(position) * Matrix.CreateRotationY(angle) * Matrix.CreateFromAxisAngle(Vector3.UnitX, MathHelper.ToRadians(-90));

这code不工作,有谁能够告诉我,我该怎么移动?

This code isn't working, can anybody tell me how can I move it?

推荐答案

您应该知道,你的Y轴实际上是上,而不是前进(按约定)。虽然这个问题有许多解决方案,最快捷的解决你的方式是:

You should be aware that your Y axis is actually "up", not "forward" (by conventions). While this problem has many solutions, quickest way to fix yours is that:

float dirX = (float)Math.Sin(angle);
float dirZ = (float)Math.Cos(angle);

position += new Vector3(dirX, 0, dirZ); 

那么,你应该乘以你的变换矩阵中的正确的顺序:

Then, you should multiply your transformation matrices in the correct order:

Scale * Rotation * Translation

而你的情况转换为:

Which in your case translates to:

// this will rotate car around the Y axis, and then translate it to correct location
world = Matrix.CreateRotationY(angle) * Matrix.CreateTranslation(position);

一个建议,做A型建议,并使用方向*速度。

One suggestion, do as A-Type suggested, and use direction * speed.

这篇关于如何将3D对象在XNA?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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