2021-04-08 20:09:59 +08:00
|
|
|
|
using UnityEngine;
|
|
|
|
|
|
2021-04-20 00:25:04 +08:00
|
|
|
|
#if UNITY
|
2021-04-08 20:09:59 +08:00
|
|
|
|
namespace ET
|
|
|
|
|
{
|
|
|
|
|
public static class PositionHelper
|
|
|
|
|
{
|
|
|
|
|
public static Vector3 RayCastV2ToV3(Vector2 pos)
|
|
|
|
|
{
|
|
|
|
|
return new Vector3(pos.x, 0, pos.y);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static Vector3 RayCastXYToV3(float x, float y)
|
|
|
|
|
{
|
|
|
|
|
return new Vector3(x, 0, y);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static Vector3 RayCastV3ToV3(Vector3 pos)
|
|
|
|
|
{
|
|
|
|
|
return new Vector3(pos.x, 0, pos.z);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static Quaternion AngleToQuaternion(int angle)
|
|
|
|
|
{
|
|
|
|
|
return Quaternion.AngleAxis(-angle, Vector3.up) * Quaternion.AngleAxis(90, Vector3.up);
|
|
|
|
|
}
|
|
|
|
|
public static Quaternion GetVector3ToQuaternion(Vector3 source, Vector3 dire)
|
|
|
|
|
{
|
|
|
|
|
Vector3 nowPos = source;
|
|
|
|
|
if (nowPos == dire)
|
|
|
|
|
{
|
|
|
|
|
return new Quaternion();
|
|
|
|
|
}
|
|
|
|
|
Vector3 direction = (dire - nowPos).normalized;
|
|
|
|
|
return Quaternion.LookRotation(direction, Vector3.up);
|
|
|
|
|
}
|
|
|
|
|
public static Vector3 GetVector2ToQuaternion(Vector3 source, Vector2 target)
|
|
|
|
|
{
|
|
|
|
|
Vector3 direction;
|
|
|
|
|
|
|
|
|
|
if (target.x< source.x)
|
|
|
|
|
{
|
|
|
|
|
direction = new Vector3(0,180,0);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
direction = Vector3.zero ;
|
|
|
|
|
}
|
|
|
|
|
return direction;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//public static float Distance2D(Unit u1, Unit u2)
|
|
|
|
|
//{
|
|
|
|
|
// Vector2 v1 = new Vector2(u1.GameObject.transform.position.x, u1.GameObject.transform.position.z);
|
|
|
|
|
// Vector2 v2 = new Vector2(u2.GameObject.transform.position.x, u2.GameObject.transform.position.z);
|
|
|
|
|
// return Vector2.Distance(v1, v2);
|
|
|
|
|
//}
|
|
|
|
|
|
|
|
|
|
public static float Distance2D(Vector3 v1, Vector3 v2)
|
|
|
|
|
{
|
|
|
|
|
Vector2 d1 = new Vector2(v1.x, v1.z);
|
|
|
|
|
Vector2 d2 = new Vector2(v2.x, v2.z);
|
|
|
|
|
return Vector2.Distance(d1, d2);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static Quaternion GetAngleToQuaternion(float angle)
|
|
|
|
|
{
|
|
|
|
|
return Quaternion.AngleAxis(-angle, Vector3.up) * Quaternion.AngleAxis(90, Vector3.up);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static float Vector3ToAngle360(Vector3 from, Vector3 to)
|
|
|
|
|
{
|
|
|
|
|
float angle = Vector3.Angle(from, to);
|
|
|
|
|
Vector3 cross = Vector3.Cross(from, to);
|
|
|
|
|
return cross.y > 0? angle : 360 - angle;
|
|
|
|
|
}
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 求点到直线的距离,采用数学公式Ax+By+C = 0; d = A*p.x + B * p.y + C / sqrt(A^2 + B ^ 2)
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="startPoint"></param>
|
|
|
|
|
/// <param name="endPoint"></param>
|
|
|
|
|
/// <param name="point"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public static float DistanceOfPointToVector(Vector3 startPoint, Vector3 endPoint, Vector3 point)
|
|
|
|
|
{
|
|
|
|
|
Vector2 startVe2 = startPoint.IgnoreYAxis();
|
|
|
|
|
Vector2 endVe2 = endPoint.IgnoreYAxis();
|
|
|
|
|
float A = endVe2.y - startVe2.y;
|
|
|
|
|
float B = startVe2.x - endVe2.x;
|
|
|
|
|
float C = endVe2.x * startVe2.y - startVe2.x * endVe2.y;
|
|
|
|
|
float denominator = Mathf.Sqrt(A * A + B * B);
|
|
|
|
|
Vector2 pointVe2 = point.IgnoreYAxis();
|
|
|
|
|
return Mathf.Abs((A * pointVe2.x + B * pointVe2.y + C) / denominator); ;
|
|
|
|
|
}
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 判断射线是否碰撞到球体,如果碰撞到,返回射线起点到碰撞点之间的距离
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="ray"></param>
|
|
|
|
|
/// <param name="center"></param>
|
|
|
|
|
/// <param name="redius"></param>
|
|
|
|
|
/// <param name="dist"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public static bool RayCastSphere(Ray ray, Vector3 center, float redius, out float dist)
|
|
|
|
|
{
|
|
|
|
|
dist = 0;
|
|
|
|
|
Vector3 ma = center - ray.origin;
|
|
|
|
|
float distance = Vector3.Cross(ma, ray.direction).magnitude / ray.direction.magnitude;
|
|
|
|
|
if (distance < redius)
|
|
|
|
|
{
|
|
|
|
|
float op = GGTheorem(Vector3.Distance(center, ray.origin), distance);
|
|
|
|
|
float rp = GGTheorem(redius, distance);
|
|
|
|
|
dist = op - rp;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 勾股定理
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="x"></param>
|
|
|
|
|
/// <param name="y"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public static float GGTheorem(float x, float y)
|
|
|
|
|
{
|
|
|
|
|
return Mathf.Sqrt(x * x + y * y);
|
|
|
|
|
}
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 去掉三维向量的Y轴,把向量投射到xz平面。
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="vector3"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public static Vector2 IgnoreYAxis(this Vector3 vector3)
|
|
|
|
|
{
|
|
|
|
|
return new Vector2(vector3.x, vector3.z);
|
|
|
|
|
}
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 判断目标点是否位于向量的左边
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <returns>True is on left, false is on right</returns>
|
|
|
|
|
public static bool PointOnLeftSideOfVector(this Vector3 vector3, Vector3 originPoint, Vector3 point)
|
|
|
|
|
{
|
|
|
|
|
Vector2 originVec2 = originPoint.IgnoreYAxis();
|
|
|
|
|
|
|
|
|
|
Vector2 pointVec2 = (point.IgnoreYAxis() - originVec2).normalized;
|
|
|
|
|
|
|
|
|
|
Vector2 vector2 = vector3.IgnoreYAxis();
|
|
|
|
|
|
|
|
|
|
float verticalX = originVec2.x;
|
|
|
|
|
|
|
|
|
|
float verticalY = (-verticalX * vector2.x) / vector2.y;
|
|
|
|
|
|
|
|
|
|
Vector2 norVertical = (new Vector2(verticalX, verticalY)).normalized;
|
|
|
|
|
|
|
|
|
|
float dotValue = Vector2.Dot(norVertical, pointVec2);
|
|
|
|
|
|
|
|
|
|
return dotValue < 0f;
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-04-20 00:25:04 +08:00
|
|
|
|
}
|
|
|
|
|
#endif
|