矢量数学,找到角度 [英] Vector math, finding the angle

查看:40
本文介绍了矢量数学,找到角度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过编写一个小型 2D 游戏来学习 XNA,这是一个自上而下的视角,我尝试进行双重运动,使用左右键和上下键沿轴移动,以及向右看鼠标光标,以便玩家可以同时奔跑和瞄准.

我有一个用于玩家位置的向量 (m_PlayerPos) 和一个用于鼠标位置的向量 (m_MousePos),我正在尝试获得指向鼠标位置的正确角度.

我用的是公式法:

<前>公共静态浮点角(Vector2 from, Vector2 to){return (float)Math.Atan2(from.X - to.X, from.Y - to.Y);}

这有效,但由于某种原因,该方法只能沿 x 轴进行一半.当鼠标位于玩家的右侧时,玩家会看向鼠标.

但是如果我移动到播放器的顶部,它会向下看,如果鼠标在播放器下方,则播放器会向上看.所以我需要反转 Y 轴,但我不确定如何.

提前感谢您的任何反馈.

解决方案

使用 to.Y - from.Y.

I am trying to learn XNA by writing a small 2D game, it's a Top-Down perspective and Im trying to have double movement, moving along the axis using Left-Right and Up-Down keys, as well as looking right at the mouse cursor, so that the player can run and aim at the same time.

I have one vector for the player position (m_PlayerPos), and one vector for the mouse position (m_MousePos), and im trying to get the correct angle towards the mouse position.

Im using the formula method:

public static float Angle(Vector2 from, Vector2 to)
{
    return (float)Math.Atan2(from.X - to.X, from.Y - to.Y);
}

This works, but for some reason the method only works half-way, along the x-axis. When the mouse is to the exact left of right of the player, the player looks right at the mouse.

But if I move to the top of the player, it looks down, and if the mouse is below the player, the player looks up. So I need to inverse the Y axis, but Im not sure how.

Thanks in advance for any feedback.

解决方案

Use to.Y - from.Y.

这篇关于矢量数学,找到角度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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