我正在搞乱XNA中的一些东西,并试图在小行星风格周围移动物体,因为你按左右旋转和上/下按照你指向的方向向前和向后移动.
我已经完成了精灵的旋转,但是我不能让对象沿你指向的方向移动,它总是在x = 0轴上上下移动.
我猜这是直截了当的,但我无法弄明白.我的"船"类具有以下属性值得注意:
Vector2 Position
float Rotation
Run Code Online (Sandbox Code Playgroud)
"ship"类有一个更新方法,处理输入,到目前为止,我有以下内容:
public void Update(GameTime gameTime)
{
KeyboardState keyboard = Keyboard.GetState();
GamePadState gamePad = GamePad.GetState(PlayerIndex.One);
float x = Position.X;
float y = Position.Y;
if (keyboard.IsKeyDown(Keys.Left)) Rotation -= 0.1f;
if (keyboard.IsKeyDown(Keys.Right)) Rotation += 0.1f;
if (keyboard.IsKeyDown(Keys.Up)) ??;
if (keyboard.IsKeyDown(Keys.Down)) ??;
this.Position = new Vector2(x, y);
}
Run Code Online (Sandbox Code Playgroud)
非常感激任何的帮助!
好的,所以这就是我做的方式(我知道会有一个非触发解决方案!)
float x = Position.X;
float y = Position.Y;
Matrix m = Matrix.CreateRotationZ(Rotation);
if (keyboard.IsKeyDown(Keys.Left)) Rotation -= 0.1f;
if (keyboard.IsKeyDown(Keys.Right)) Rotation += 0.1f;
if (keyboard.IsKeyDown(Keys.Up))
{
x += m.M12 * 5.0f;
y -= m.M11 * 5.0f;
}
if (keyboard.IsKeyDown(Keys.Down))
{
x -= m.M12 * 5.0f;
y += m.M11 * 5.0f;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4178 次 |
| 最近记录: |