2012-04-13 33 views
0

オブジェクトを所定の角度に移動したいですが、Y軸だけ上下に移動します。
Vector2 unitV = new Vector2((float)Math.Sin(player.angle), (float)Math.Cos(player.angle));
unitV.Normalize();
player.model.Position += Vector2.Multiply(unitV,player.model.Speed) * (float)gameTime.ElapsedGameTime.TotalSeconds;
C#Vector2オブジェクトをある角度に向かって移動する方法

+1

あなたのコードは、私には正常に見える(しかし、私は非常に疲れています!)。 'player.model.Position'を設定する前に、' Vector2.Multiply(unitV、player.model.Speed)*(float)gameTime.ElapsedGameTime.TotalSeconds'の実際の値は何ですか? – spender

+0

http://www.riemers.net/eng/Tutorials/XNA/Csharp/series2d.php – phadaphunk

+0

ありがとう、私はそれを他の方法でやった –

答えて

1

私は練習中にこの問題に直面しましたが、ここでは解決策が見つかりました。これがあなたに役立つことを願っています。 XNA 4 Cシャープを使用。

宣言:anglecorrectionは、その角度「アップ」に向かってオブジェクトを移動するために必要とされること

Texture2D sprite; 
Vector2 spritePosition = Vector2.Zero; 
Vector2 spriteOriginalPos; 
float spriterotation = 0; 
float anglecorrection = (Math.PI * 90/180.0); 
float speed = 1; 

は注意してください。

ロード:

//Load basic texture to make it recognizable :) 
sprite= Content.Load<Texture2D>("spritetexture"); 
//Default position in middle 
spritePosition = new Vector2(
        (graphics.GraphicsDevice.Viewport.Width/2) - (sprite.Width/2), 
        (graphics.GraphicsDevice.Viewport.Height/2) - (sprite.Height/2)); 
//Sprite centering 
spriteOriginalPos.X = sprite.Width/2; 
spriteOriginalPos.Y = sprite.Height/2; 

更新:

if (Keyboard.GetState(PlayerIndex.One).IsKeyDown(Keys.Up)) 
{ 
    spritePosition.X += speed * (float)Math.Cos(spriterotation - anglecorrection); 
    spritePosition.Y += speed * (float)Math.Sin(spriterotation - anglecorrection); 
} 

ドロー:

spriteBatch.Draw(sprite, spritePosition, null, Color.Black, spriterotation, spriteOriginalPos, 1.0f, SpriteEffects.None, 0f); 
関連する問題