You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

140 lines
4.5 KiB

using System.Collections;
using System.Collections.Generic;
using System;
namespace Sog
{
public class GameUtils
{
public static Fixed64Vector2 FacingToVec2(Fixed64 Facing)
{
Fixed64 f = Facing * Fixed64.Deg2Rad;
Fixed64 x = FixedMath.Sin(f);
Fixed64 y = FixedMath.Cos(f);
return new Fixed64Vector2(x, y);
}
public static Fixed64Vector3 FacingToVec3(Fixed64 Facing)
{
Fixed64 f = Facing * Fixed64.Deg2Rad;
Fixed64 x = FixedMath.Sin(f);
Fixed64 z = FixedMath.Cos(f);
return new Fixed64Vector3(x, 0, z);
}
public static Fixed64 Vec2ToFacing(Fixed64Vector2 v)
{
return (0f - (FixedMath.Atan2(v.y, v.x) * Fixed64.Rad2Deg - 90f)) % 360f;
}
public static Fixed64 Vec3ToFacing(Fixed64Vector3 v)
{
return (0f - (FixedMath.Atan2(v.z, v.x) * Fixed64.Rad2Deg - 90f)) % 360f;
}
public static Fixed64 UnsignedTurnAmount(Fixed64 fromFacing, Fixed64 toFacing)
{
Fixed64 f = FixedMath.DeltaAngle(fromFacing, toFacing);
return FixedMath.Abs(f);
}
public static bool IsClockwise(Fixed64Vector2 a, Fixed64Vector2 b)
{
return a.x * b.y - a.y * b.x < 0;
}
//angel是x,y平面坐标系,朝向X方向为0度,facing是unity的x,y,z坐标系里的x,z平面角度,朝向z为0度,差了90度
public static Fixed64 FacingToAngle(Fixed64 facing)
{
return (90 - facing) % 360;
}
public static Fixed64 AngleToFacing(Fixed64 angle)
{
return (90 - angle) % 360;
}
public static Fixed64 ReverseFacing(Fixed64 f)
{
return (f + 180) % 360;
}
public static Fixed64 AdjustFacing(Fixed64 facing, Fixed64 adjustment)
{
for (facing += adjustment; facing <= -180; facing += 360)
{
}
while (facing > 180)
{
facing -= 360;
}
return facing;
}
public static Fixed64Vector2 RotateAboutPoint2DDegrees(Fixed64Vector2 startPos, Fixed64Vector2 pivotPoint, Fixed64 angle)
{
Fixed64 f = angle * Fixed64.Deg2Rad;
Fixed64 sin = FixedMath.Sin(f);
Fixed64 cos = FixedMath.Cos(f);
Fixed64 dx = startPos.x - pivotPoint.x;
Fixed64 dy = startPos.y - pivotPoint.y;
Fixed64 x = dx * cos - dy * sin;
Fixed64 y = dx * sin + dy * cos;
return new Fixed64Vector2(x + pivotPoint.x, y + pivotPoint.y);
}
public static Fixed64Vector3 RotateAboutPoint2DDegrees(Fixed64Vector3 startPos, Fixed64Vector3 pivotPoint, Fixed64 angle)
{
Fixed64 f = angle * Fixed64.Deg2Rad;
Fixed64 sin = FixedMath.Sin(f);
Fixed64 cos = FixedMath.Cos(f);
Fixed64 dx = startPos.x - pivotPoint.x;
Fixed64 dz = startPos.z - pivotPoint.z;
Fixed64 x = dx * cos - dz * sin;
Fixed64 z = dx * sin + dz * cos;
return new Fixed64Vector3(x + pivotPoint.x, 0, z + pivotPoint.z);
}
//public static Fixed64Vector3 RotateAround(Fixed64Vector3 center, Fixed64Vector3 pos, int angle)
//{
// var dir = pos - center;
// Fixed64 f = angle * Fixed64.Deg2Rad;
// Fixed64 sin = FixedMath.Sin(f);
// Fixed64 cos = FixedMath.Cos(f);
// var x = dir.x * cos - dir.z * sin;
// var z = dir.x * sin + dir.z * cos;
// return center + new Fixed64Vector3 { x = x, z = z };
//}
public static Fixed64Vector2 RotateAboutOrigin2DDegrees(Fixed64Vector2 pos, Fixed64 angle)
{
Fixed64 f = angle * Fixed64.Deg2Rad;
Fixed64 num = FixedMath.Sin(f);
Fixed64 num2 = FixedMath.Cos(f);
Fixed64 x = num2 * pos.x - num * pos.y;
Fixed64 y = num * pos.x + num2 * pos.y;
return new Fixed64Vector2(x, y);
}
public static Fixed64Vector3 IntFloatToFixedPos(int posX, int posZ)
{
Fixed64Vector3 vec = new Fixed64Vector3(posX, 0, posZ);
vec.x /= 1000;
vec.z /= 1000;
return vec;
}
public static Vector3IntFloat FixedToIntFloat(Fixed64Vector3 pos)
{
return new Vector3IntFloat() { x = (int)(pos.x * 1000), y = (int)(pos.y * 1000), z = (int)(pos.z * 1000) };
}
}
}