简体   繁体   English

将图像旋转到鼠标的当前位置

[英]Rotating an image towards the mouse's current position

I am trying to make a simple game in XNA. 我想在XNA中制作一个简单的游戏。

I have a player which has spritesheet next to it. 我有一个旁边有精灵表的玩家。 The spritesheet is a sort of weapon, with a tip. spritesheet是一种带尖端的武器。

How can I get this sprite to rotate with the tip facing the mouse position? 如何让这个精灵旋转,尖端朝向鼠标位置?

        float y2 = m_Mouse.Y;
        float y1 = m_WeaponOrigin.Y;
        float x2 = m_Mouse.X;
        float x1 = m_WeaponOrigin.X;

        // Get angle from mouse position.
        m_Radians = (float) Math.Atan2((y2 - y1), (x2 - x1));

Drawing with: 
activeSpriteBatch.Draw(m_WeaponImage, m_WeaponPos, r, Color.White, m_Radians, m_WeaponOrigin, 1.0f, SpriteEffects.None, 0.100f);

while this makes it rotate, it does not follow the mouse correctly, and it behaves weirdly. 虽然这使它旋转,但它不能正确地跟随鼠标,并且它表现得很奇怪。

Any hints on how to make this work? 有关如何使这项工作的任何提示?

Another issue I have is to define a point and which is the muzzle, and update it based on the angle as well, so that shots will fire correctly from that point towards the mouse. 我的另一个问题是定义一个点,它是枪口,并根据角度更新它,这样射击将从该点向鼠标正确射击。

Thanks 谢谢


Screenshots: 截图: 早,让鼠标和光标到位

玩超级激光

在每个敌人类型上使用超级激光器

Thanks again, turned out to be a fun game. 再次感谢,原来是一个有趣的游戏。

Basically, use Math.Atan2 . 基本上,使用Math.Atan2

Vector2 mousePosition = new Vector2(mouseState.X, mouseState.Y);
Vector2 dPos = _arrow.Position - mousePosition;

_arrow.Rotation = (float)Math.Atan2(dPos.Y, dPos.X);

Proof of concept (I used the plus texture for the cursor - it doesn't show on the sceenshot unfortunately): 概念证明 (我使用了光标的加号纹理 - 不幸的是它没有显示在sceenshot上):

指向光标


"What is _arrow ?" “什么是_arrow ?”

In that example _arrow is of type Sprite , which might come in handy in some situations, and will sure make your code look a bit cleaner: 在该示例中, _arrowSprite类型,在某些情况下可能会派上用场,并且确保您的代码看起来更清晰:

public class Sprite
{
    public Texture2D Texture { get; private set; }

    public Vector2 Position { get; set; }
    public float Rotation { get; set; }
    public float Scale { get; set; }

    public Vector2 Origin { get; set; }
    public Color Color { get; set; }

    public Sprite(Texture2D texture)
    {
        this.Texture = texture;
    }

    public void Draw(SpriteBatch spriteBatch, GameTime gameTime)
    {
        spriteBatch.Draw(this.Texture, 
                         this.Position, 
                         null, 
                         this.Color, 
                         this.Rotation, 
                         this.Origin, 
                         this.Scale, 
                         SpriteEffects.None, 
                         0f);
    }
}

Declare: 宣布:

Sprite _arrow;

Initiate: 发起:

Texture2D arrowTexture = this.Content.Load<Texture2D>("ArrowUp");
_arrow = new Sprite(arrowTexture)
        {
            Position = new Vector2(100, 100),
            Color = Color.White,
            Rotation = 0f,
            Scale = 1f,
            Origin = new Vector2(arrowTexture.Bounds.Center.X, arrowTexture.Bounds.Center.Y)
        };

Draw: 画:

_spriteBatch.Begin();
_arrow.Draw(_spriteBatch, gameTime);
_spriteBatch.End();

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

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