forked from sin365/AxibugEmuOnline
避免使用三角函数判断死区 :D
This commit is contained in:
parent
449ef4a597
commit
baf2e5a8d6
@ -19,20 +19,20 @@ namespace AxibugEmuOnline.Client.InputDevices
|
|||||||
var axis = GetVector2();
|
var axis = GetVector2();
|
||||||
|
|
||||||
var dir = GetDirection(axis, 0.15f);
|
var dir = GetDirection(axis, 0.15f);
|
||||||
Up.m_performing = (dir &= Direction.Up) > 0;
|
Up.m_performing = (dir & Direction.Up) > 0;
|
||||||
Up.Update();
|
Up.Update();
|
||||||
|
|
||||||
Down.m_performing = (dir &= Direction.Down) > 0;
|
Down.m_performing = (dir & Direction.Down) > 0;
|
||||||
Down.Update();
|
Down.Update();
|
||||||
|
|
||||||
Left.m_performing = (dir &= Direction.Left) > 0;
|
Left.m_performing = (dir & Direction.Left) > 0;
|
||||||
Left.Update();
|
Left.Update();
|
||||||
|
|
||||||
Right.m_performing = (dir &= Direction.Right) > 0;
|
Right.m_performing = (dir & Direction.Right) > 0;
|
||||||
Right.Update();
|
Right.Update();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
public class VirtualButton : InputControl_C
|
public class VirtualButton : InputControl_C
|
||||||
{
|
{
|
||||||
@ -68,49 +68,29 @@ namespace AxibugEmuOnline.Client.InputDevices
|
|||||||
|
|
||||||
static Direction GetDirection(Vector2 input, float deadzone)
|
static Direction GetDirection(Vector2 input, float deadzone)
|
||||||
{
|
{
|
||||||
// 检查死区:如果点在死区半径内,返回无
|
//// 检查死区:如果点在死区半径内,返回无
|
||||||
if (input.magnitude <= deadzone)
|
if (input.magnitude <= deadzone)
|
||||||
return Direction.None;
|
return Direction.None;
|
||||||
|
|
||||||
// 标准化向量(确保在单位圆上)
|
|
||||||
Vector2 normalized = input.normalized;
|
|
||||||
|
|
||||||
// 计算点与四个方向基准向量的点积
|
// 计算点与四个方向基准向量的点积
|
||||||
float dotUp = Vector2.Dot(normalized, Vector2.up); // (0, 1)
|
//float dotUp = Vector2.Dot(normalized, Vector2.up); // (0, 1)
|
||||||
float dotDown = Vector2.Dot(normalized, Vector2.down); // (0, -1)
|
//float dotDown = Vector2.Dot(normalized, Vector2.down); // (0, -1)
|
||||||
float dotRight = Vector2.Dot(normalized, Vector2.right); // (1, 0)
|
//float dotRight = Vector2.Dot(normalized, Vector2.right); // (1, 0)
|
||||||
float dotLeft = Vector2.Dot(normalized, Vector2.left); // (-1, 0)
|
//float dotLeft = Vector2.Dot(normalized, Vector2.left); // (-1, 0)
|
||||||
|
|
||||||
|
// 标准化向量(确保在单位圆上)
|
||||||
|
Vector2 normalized = input.normalized;
|
||||||
// 找出最大点积对应的方向
|
// 找出最大点积对应的方向
|
||||||
Direction bestDirection = Direction.None;
|
Direction bestDirection = Direction.None;
|
||||||
float maxDot = -1f; // 初始化为最小值
|
|
||||||
|
|
||||||
// 检查上方向
|
// 检查上方向
|
||||||
if (dotUp > maxDot)
|
if (normalized.y > deadzone) bestDirection |= Direction.Up;
|
||||||
{
|
|
||||||
maxDot = dotUp;
|
|
||||||
bestDirection |= Direction.Up;
|
|
||||||
}
|
|
||||||
|
|
||||||
// 检查下方向
|
// 检查下方向
|
||||||
if (dotDown > maxDot)
|
if (normalized.y < -1 * deadzone) bestDirection |= Direction.Down;
|
||||||
{
|
|
||||||
maxDot = dotDown;
|
|
||||||
bestDirection |= Direction.Down;
|
|
||||||
}
|
|
||||||
|
|
||||||
// 检查右方向
|
// 检查右方向
|
||||||
if (dotRight > maxDot)
|
if (normalized.x > deadzone) bestDirection |= Direction.Right;
|
||||||
{
|
|
||||||
maxDot = dotRight;
|
|
||||||
bestDirection |= Direction.Right;
|
|
||||||
}
|
|
||||||
|
|
||||||
// 检查左方向
|
// 检查左方向
|
||||||
if (dotLeft > maxDot)
|
if (normalized.x < -1 * deadzone) bestDirection |= Direction.Left;
|
||||||
{
|
|
||||||
bestDirection |= Direction.Left;
|
|
||||||
}
|
|
||||||
|
|
||||||
return bestDirection;
|
return bestDirection;
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user