简体   繁体   English

图像和方向C#的导弹旋转

[英]Missile Rotation of Image and Direction C#

I have been fiddling with MathHelp.Lerp(); 我一直在摆弄MathHelp.Lerp(); trying to find the easiest way to rotate the direction the missile is traveling. 试图找到最简单的方法来旋转导弹的行进方向。 Right now it will instantly go the direction the player/mouse is located. 现在,它会立即转到播放器/鼠标所在的方向。

Would Larp be the best to use or would some other type of direction rotation be what I should be after? Larp是最好用的还是我应该追求的其他类型的方向旋转? Latest post , example of what I mean . 最新帖子我的意思的例子

    protected override void Update(GameTime gameTime)
    {
        // Allows the game to exit
        if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
            this.Exit();

        delta = (float)gameTime.ElapsedGameTime.TotalSeconds * Speed;

        direction = mousePosition - missilePosition;
        direction.Normalize();

        missilePosition += direction * delta;

        //missilePosition = Vector2.Lerp(missilePosition, mousePosition, 2.0f);

        mouse = Mouse.GetState();

        mousePosition = new Vector2(mouse.X, mouse.Y);

        base.Update(gameTime);
    }

Having the image rotate with the direction of the missile is also something that I have been looking up and trying to figure out. 我一直在寻找并尝试找出使图像沿导弹方向旋转的方法。 How would I have the image rotate as the direction rotates? 随着方向旋转,图像将如何旋转?

If you use velocity to move your missile, you can use this code (maybe you will need to normalize it). 如果使用速度移动导弹,则可以使用此代码(也许需要对其进行归一化)。 writing from head. 从头开始写作。

rotation = -(float)Math.Atan2(velocity.Y, velocity.X);

so if your velocity is (1,0) meaning your misslile is going straight from left to right. 因此,如果您的速度为(1,0),则表示您的错位是从左向右直线移动。 Atan2(1,0) will give you 1.57 radiants (90 degrees). Atan2(1,0)将为您提供1.57辐射(90度)。 to make this working, your texture of missile must be faced up. 要使其正常工作,您的导弹质地必须朝上。 and this code will turn it to 90 degrees, to the right. 该代码会将其向右旋转90度。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM