2021-11-28 13:00:24 +00:00
|
|
|
|
#region ================== Copyright (c) 2021 Boris Iwanski
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
|
*
|
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
|
*
|
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
|
* (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
*
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the
|
|
|
|
|
*
|
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
|
* along with this program.If not, see<http://www.gnu.org/licenses/>.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region ================== Namespaces
|
|
|
|
|
|
|
|
|
|
using CodeImp.DoomBuilder.Geometry;
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
2022-01-06 17:46:37 +00:00
|
|
|
|
namespace CodeImp.DoomBuilder.UDBScript.Wrapper
|
2021-11-28 13:00:24 +00:00
|
|
|
|
{
|
|
|
|
|
internal struct Angle2DWrapper
|
|
|
|
|
{
|
2022-11-13 00:15:17 +00:00
|
|
|
|
#region ================== Methods
|
|
|
|
|
|
2021-11-28 13:00:24 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Converts a Doom angle (where 0° is east) to a real world angle (where 0° is north).
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="doomangle">Doom angle in degrees</param>
|
|
|
|
|
/// <returns>Doom angle in degrees</returns>
|
2022-11-13 00:15:17 +00:00
|
|
|
|
public double doomToReal(int doomangle)
|
2021-11-28 13:00:24 +00:00
|
|
|
|
{
|
|
|
|
|
return normalized(doomangle + 90);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Converts a Doom angle (where 0° is east) to a real world angle (where 0° is north) in radians.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="doomangle">Doom angle in degrees</param>
|
|
|
|
|
/// <returns>Doom angle in radians</returns>
|
2022-11-13 00:15:17 +00:00
|
|
|
|
public double doomToRealRad(int doomangle)
|
2021-11-28 13:00:24 +00:00
|
|
|
|
{
|
|
|
|
|
return Angle2D.DoomToReal(doomangle);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Converts a real world angle (where 0° is north) to a Doom angle (where 0° is east).
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="realangle">Real world angle in degrees</param>
|
|
|
|
|
/// <returns>Doom angle in degrees</returns>
|
2022-11-13 00:15:17 +00:00
|
|
|
|
public int realToDoom(double realangle)
|
2021-11-28 13:00:24 +00:00
|
|
|
|
{
|
|
|
|
|
return normalized((int)(realangle - 90));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2022-11-13 00:15:17 +00:00
|
|
|
|
/// Converts a real world angle (where 0° is north) to a Doom angle (where 0° is east) in radians.
|
2021-11-28 13:00:24 +00:00
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="realangle">Real world angle in radians</param>
|
|
|
|
|
/// <returns>Doom angle in degrees</returns>
|
2022-11-13 00:15:17 +00:00
|
|
|
|
public int realToDoomRad(double realangle)
|
2021-11-28 13:00:24 +00:00
|
|
|
|
{
|
|
|
|
|
return Angle2D.RealToDoom(realangle);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Converts radians to degrees.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="rad">Angle in radians</param>
|
|
|
|
|
/// <returns>Angle in degrees</returns>
|
2022-11-13 00:15:17 +00:00
|
|
|
|
public double radToDeg(double rad)
|
2021-11-28 13:00:24 +00:00
|
|
|
|
{
|
|
|
|
|
return Angle2D.RadToDeg(rad);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Converts degrees to radians.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="deg">Angle in degrees</param>
|
|
|
|
|
/// <returns>Angle in radians</returns>
|
2022-11-13 00:15:17 +00:00
|
|
|
|
public double degToRad(double deg)
|
2021-11-28 13:00:24 +00:00
|
|
|
|
{
|
|
|
|
|
return Angle2D.DegToRad(deg);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Normalizes an angle in degrees so that it is bigger or equal to 0° and smaller than 360°.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="angle">Angle in degrees</param>
|
|
|
|
|
/// <returns>Normalized angle in degrees</returns>
|
2022-11-13 00:15:17 +00:00
|
|
|
|
public int normalized(int angle)
|
2021-11-28 13:00:24 +00:00
|
|
|
|
{
|
|
|
|
|
while (angle < 0) angle += 360;
|
|
|
|
|
while (angle >= 360) angle -= 360;
|
|
|
|
|
return angle;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Normalizes an angle in radians so that it is bigger or equal to 0 and smaller than 2 Pi.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="angle">Angle in radians</param>
|
|
|
|
|
/// <returns>Normalized angle in radians</returns>
|
2022-11-13 00:15:17 +00:00
|
|
|
|
public double normalizedRad(double angle)
|
2021-11-28 13:00:24 +00:00
|
|
|
|
{
|
|
|
|
|
return Angle2D.Normalized(angle);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Returns the angle between three positions.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="p1">First position</param>
|
|
|
|
|
/// <param name="p2">Second position</param>
|
|
|
|
|
/// <param name="p3">Third position</param>
|
|
|
|
|
/// <returns>Angle in degrees</returns>
|
2022-11-13 00:15:17 +00:00
|
|
|
|
public double getAngle(object p1, object p2, object p3)
|
2021-11-28 13:00:24 +00:00
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2022-11-13 00:15:17 +00:00
|
|
|
|
Vector2D v1 = BuilderPlug.Me.GetVector3DFromObject(p1);
|
|
|
|
|
Vector2D v2 = BuilderPlug.Me.GetVector3DFromObject(p2);
|
|
|
|
|
Vector2D v3 = BuilderPlug.Me.GetVector3DFromObject(p3);
|
2021-11-28 13:00:24 +00:00
|
|
|
|
|
|
|
|
|
return Angle2D.RadToDeg(Angle2D.GetAngle(v1, v2, v3));
|
|
|
|
|
}
|
|
|
|
|
catch (CantConvertToVectorException e)
|
|
|
|
|
{
|
|
|
|
|
throw BuilderPlug.Me.ScriptRunner.CreateRuntimeException(e.Message);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Returns the angle between three positions in radians.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="p1">First position</param>
|
|
|
|
|
/// <param name="p2">Second position</param>
|
|
|
|
|
/// <param name="p3">Third position</param>
|
|
|
|
|
/// <returns>Angle in radians</returns>
|
2022-11-13 00:15:17 +00:00
|
|
|
|
public double getAngleRad(object p1, object p2, object p3)
|
2021-11-28 13:00:24 +00:00
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2022-11-13 00:15:17 +00:00
|
|
|
|
Vector2D v1 = BuilderPlug.Me.GetVector3DFromObject(p1);
|
|
|
|
|
Vector2D v2 = BuilderPlug.Me.GetVector3DFromObject(p2);
|
|
|
|
|
Vector2D v3 = BuilderPlug.Me.GetVector3DFromObject(p3);
|
2021-11-28 13:00:24 +00:00
|
|
|
|
|
|
|
|
|
return Angle2D.GetAngle(v1, v2, v3);
|
|
|
|
|
}
|
|
|
|
|
catch (CantConvertToVectorException e)
|
|
|
|
|
{
|
|
|
|
|
throw BuilderPlug.Me.ScriptRunner.CreateRuntimeException(e.Message);
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-11-13 00:15:17 +00:00
|
|
|
|
|
|
|
|
|
#endregion
|
2021-11-28 13:00:24 +00:00
|
|
|
|
}
|
|
|
|
|
}
|