From b026ee7ef80308018b6dd54919c92a62f49328fa Mon Sep 17 00:00:00 2001 From: hondacrx Date: Mon, 15 Nov 2021 18:04:57 -0500 Subject: [PATCH] More cleanups of custom classes in favor of .NET runtime types. --- Source/Framework/GameMath/AxisAlignedBox.cs | 10 +- .../Framework/GameMath/CollisionDetection.cs | 33 +- Source/Framework/GameMath/Matrix2.cs | 614 --------- Source/Framework/GameMath/Matrix3.cs | 807 ------------ Source/Framework/GameMath/Matrix4.cs | 900 -------------- Source/Framework/GameMath/Plane.cs | 260 ---- Source/Framework/GameMath/Quaternion.cs | 825 ------------- Source/Framework/GameMath/Ray.cs | 17 +- Source/Framework/GameMath/Vector2.cs | 947 -------------- Source/Framework/GameMath/Vector3.cs | 1098 ----------------- Source/Framework/GameMath/Vector4.cs | 1034 ---------------- Source/Framework/IO/ByteBuffer.cs | 2 +- Source/Framework/IO/SocketBuffer.cs | 2 - Source/Framework/Util/Extensions.cs | 114 ++ Source/Framework/Web/API/ApiRequest.cs | 2 - Source/Game/AI/CoreAI/PassiveAI.cs | 1 - Source/Game/AI/SmartScripts/SmartScript.cs | 26 +- Source/Game/Achievements/CriteriaHandler.cs | 1 - Source/Game/AuctionHouse/AuctionManager.cs | 2 - Source/Game/BattleFields/BattleField.cs | 3 +- .../BattleFields/Zones/WinterGraspConst.cs | 2 +- Source/Game/BattleGrounds/BattleGround.cs | 4 +- .../Game/BattleGrounds/Zones/ArathiBasin.cs | 1 - Source/Game/BattleGrounds/Zones/EyeofStorm.cs | 1 - .../BattleGrounds/Zones/StrandofAncients.cs | 3 +- .../Game/BattleGrounds/Zones/WarsongGluch.cs | 2 - Source/Game/Chat/Channels/ChannelAppenders.cs | 1 - .../Game/Chat/Commands/BNetAccountCommands.cs | 1 - .../Game/Chat/Commands/GameObjectCommands.cs | 6 +- Source/Game/Chat/LanguageManager.cs | 4 - .../Collision/BoundingIntervalHierarchy.cs | 60 +- .../BoundingIntervalHierarchyWrap.cs | 1 + Source/Game/Collision/Callbacks.cs | 19 +- Source/Game/Collision/DynamicTree.cs | 7 +- .../Game/Collision/Management/VMapManager.cs | 4 +- Source/Game/Collision/Maps/MapTree.cs | 7 +- .../Game/Collision/Models/GameObjectModel.cs | 35 +- Source/Game/Collision/Models/IModel.cs | 3 +- Source/Game/Collision/Models/ModelInstance.cs | 24 +- Source/Game/Collision/Models/WorldModel.cs | 1 + Source/Game/Collision/RegularGrid2D.cs | 1 + .../DataStorage/AreaTriggerDataStorage.cs | 2 +- .../DataStorage/ClientReader/DB6Storage.cs | 6 +- .../Game/DataStorage/ClientReader/DBReader.cs | 4 +- Source/Game/DataStorage/DB2Manager.cs | 5 +- Source/Game/DataStorage/M2Storage.cs | 2 +- Source/Game/DataStorage/Structs/A_Records.cs | 2 +- Source/Game/DataStorage/Structs/C_Records.cs | 2 +- Source/Game/DataStorage/Structs/G_Records.cs | 2 +- Source/Game/DataStorage/Structs/J_Records.cs | 3 +- Source/Game/DataStorage/Structs/L_Records.cs | 2 +- .../Game/DataStorage/Structs/M2Structure.cs | 1 + Source/Game/DataStorage/Structs/M_Records.cs | 3 +- Source/Game/DataStorage/Structs/T_Records.cs | 2 +- Source/Game/DataStorage/Structs/U_Records.cs | 3 +- Source/Game/DataStorage/Structs/V_Records.cs | 2 +- Source/Game/DataStorage/Structs/W_Records.cs | 1 - .../Game/Entities/AreaTrigger/AreaTrigger.cs | 2 +- .../AreaTrigger/AreaTriggerTemplate.cs | 2 +- Source/Game/Entities/Creature/Creature.cs | 1 - Source/Game/Entities/Creature/CreatureData.cs | 1 - Source/Game/Entities/Creature/Gossip.cs | 2 +- Source/Game/Entities/DynamicObject.cs | 1 - Source/Game/Entities/GameObject/GameObject.cs | 13 +- .../Entities/GameObject/GameObjectData.cs | 7 +- .../Entities/Item/AzeriteEmpoweredItem.cs | 3 - Source/Game/Entities/Item/AzeriteItem.cs | 1 - Source/Game/Entities/Object/Position.cs | 2 +- .../Entities/Object/Update/UpdateFields.cs | 2 +- Source/Game/Entities/Object/WorldObject.cs | 2 +- Source/Game/Entities/Player/Player.Items.cs | 2 - Source/Game/Entities/Player/Player.Spells.cs | 1 - Source/Game/Entities/Taxi/TaxiPathGraph.cs | 2 +- Source/Game/Entities/Transport.cs | 4 +- Source/Game/Entities/Unit/CharmInfo.cs | 2 +- Source/Game/Entities/Unit/Unit.Fields.cs | 3 - Source/Game/Entities/Unit/Unit.Movement.cs | 2 +- Source/Game/Entities/Unit/Unit.Pets.cs | 1 - Source/Game/Entities/Unit/Unit.cs | 3 +- Source/Game/Garrisons/Garrisons.cs | 4 +- Source/Game/Globals/ObjectManager.cs | 6 +- .../Game/Handlers/AdventureJournalHandler.cs | 10 - Source/Game/Handlers/AdventureMapHandler.cs | 11 - Source/Game/Handlers/CombatHandler.cs | 1 - Source/Game/Handlers/HotfixHandler.cs | 1 - Source/Game/Handlers/PetitionsHandler.cs | 1 - Source/Game/Handlers/RAFHandler.cs | 5 - Source/Game/Loot/Loot.cs | 1 - Source/Game/Maps/GridMap.cs | 8 +- Source/Game/Maps/Map.cs | 2 +- Source/Game/Maps/TransportManager.cs | 8 +- Source/Game/Movement/AbstractFollower.cs | 5 - .../Generators/FlightPathMovementGenerator.cs | 2 +- .../Generators/GenericMovementGenerator.cs | 1 - .../Game/Movement/Generators/PathGenerator.cs | 14 +- .../Movement/Generators/RandomMovement.cs | 1 - .../SplineChainMovementGenerator.cs | 2 +- Source/Game/Movement/MotionMaster.cs | 2 +- Source/Game/Movement/MoveSpline.cs | 4 +- Source/Game/Movement/MoveSplineInit.cs | 2 +- Source/Game/Movement/MoveSplineInitArgs.cs | 5 +- Source/Game/Movement/Spline.cs | 32 +- .../Game/Networking/Packets/AddonPackets.cs | 6 - .../Packets/AdventureJournalPackets.cs | 4 - .../Networking/Packets/AdventureMapPackets.cs | 7 - .../Networking/Packets/AreaTriggerPackets.cs | 3 +- .../Game/Networking/Packets/AzeritePackets.cs | 1 - .../Networking/Packets/BattleGroundPackets.cs | 2 +- .../Networking/Packets/CharacterPackets.cs | 2 +- .../Networking/Packets/ClientConfigPackets.cs | 1 - .../Game/Networking/Packets/HotfixPackets.cs | 3 - Source/Game/Networking/Packets/MiscPackets.cs | 2 +- .../Networking/Packets/MovementPackets.cs | 4 +- Source/Game/Networking/Packets/NPCPackets.cs | 4 +- Source/Game/Networking/Packets/PetPackets.cs | 2 +- .../Game/Networking/Packets/QueryPackets.cs | 2 +- .../Networking/Packets/ReferAFriendPackets.cs | 1 - .../Game/Networking/Packets/SpellPackets.cs | 2 +- .../Game/Networking/Packets/TicketPackets.cs | 2 +- Source/Game/OutdoorPVP/OutdoorPvP.cs | 2 +- Source/Game/Scenarios/ScenarioManager.cs | 1 - Source/Game/Scripting/ScriptManager.cs | 2 +- Source/Game/Spells/Auras/AuraEffect.cs | 1 - Source/Game/Spells/SpellEffects.cs | 12 +- Source/Game/SupportSystem/SupportTickets.cs | 2 +- Source/Scripts/Pets/Mage.cs | 3 - Source/Scripts/Spells/Hunter.cs | 1 - Source/Scripts/Spells/Items.cs | 4 +- Source/Scripts/Spells/Priest.cs | 1 - Source/Scripts/Spells/Warrior.cs | 2 +- Source/Scripts/World/ItemScripts.cs | 5 +- Source/Scripts/World/NpcSpecial.cs | 8 +- 132 files changed, 373 insertions(+), 6833 deletions(-) delete mode 100644 Source/Framework/GameMath/Matrix2.cs delete mode 100644 Source/Framework/GameMath/Matrix3.cs delete mode 100644 Source/Framework/GameMath/Matrix4.cs delete mode 100644 Source/Framework/GameMath/Plane.cs delete mode 100644 Source/Framework/GameMath/Quaternion.cs delete mode 100644 Source/Framework/GameMath/Vector2.cs delete mode 100644 Source/Framework/GameMath/Vector3.cs delete mode 100644 Source/Framework/GameMath/Vector4.cs diff --git a/Source/Framework/GameMath/AxisAlignedBox.cs b/Source/Framework/GameMath/AxisAlignedBox.cs index 5d0c7d4e1..a4f8b6559 100644 --- a/Source/Framework/GameMath/AxisAlignedBox.cs +++ b/Source/Framework/GameMath/AxisAlignedBox.cs @@ -16,8 +16,8 @@ */ using System; +using System.Numerics; using System.Runtime.Serialization; -using System.Security.Permissions; namespace Framework.GameMath { @@ -233,14 +233,14 @@ namespace Framework.GameMath public void merge(AxisAlignedBox a) { - Lo = Lo.Min(a.Lo); - Hi = Hi.Max(a.Hi); + Lo = Vector3.Min(_lo, a.Lo); + Hi = Vector3.Max(Hi, a.Hi); } public void merge(Vector3 a) { - _lo = _lo.Min(a); - _hi = _hi.Max(a); + _lo = Vector3.Min(_lo, a); + _hi = Vector3.Max(_hi, a); } public static AxisAlignedBox Zero() diff --git a/Source/Framework/GameMath/CollisionDetection.cs b/Source/Framework/GameMath/CollisionDetection.cs index ee6c2ebff..b043c0198 100644 --- a/Source/Framework/GameMath/CollisionDetection.cs +++ b/Source/Framework/GameMath/CollisionDetection.cs @@ -16,6 +16,7 @@ */ using System; +using System.Numerics; namespace Framework.GameMath { @@ -26,7 +27,7 @@ namespace Framework.GameMath Vector3 normal = Vector3.Zero; if (collisionLocationForMovingPointFixedAABox(origin, dir, box, ref location, out Inside, ref normal)) { - return (location - origin).magnitude(); + return Vector3.Distance(location, origin); } else { @@ -43,26 +44,26 @@ namespace Framework.GameMath // Find candidate planes. for (int i = 0; i < 3; ++i) { - if (origin[i] < MinB[i]) + if (origin.GetAt(i) < MinB.GetAt(i)) { - location[i] = MinB[i]; + location.SetAt(MinB.GetAt(i), i); Inside = false; // Calculate T distances to candidate planes - if ((uint)dir[i] != 0) + if ((uint)dir.GetAt(i) != 0) { - MaxT[i] = (MinB[i] - origin[i]) / dir[i]; + MaxT.SetAt((MinB.GetAt(i) - origin.GetAt(i)) / dir.GetAt(i), i); } } - else if (origin[i] > MaxB[i]) + else if (origin.GetAt(i) > MaxB.GetAt(i)) { - location[i] = MaxB[i]; + location.SetAt(MaxB.GetAt(i), i); Inside = false; // Calculate T distances to candidate planes - if ((uint)dir[i] != 0) + if ((uint)dir.GetAt(i) != 0) { - MaxT[i] = (MaxB[i] - origin[i]) / dir[i]; + MaxT.SetAt((MaxB.GetAt(i) - origin.GetAt(i)) / dir.GetAt(i), i); } } } @@ -76,18 +77,18 @@ namespace Framework.GameMath // Get largest of the maxT's for final choice of intersection int WhichPlane = 0; - if (MaxT[1] > MaxT[WhichPlane]) + if (MaxT.Y > MaxT.GetAt(WhichPlane)) { WhichPlane = 1; } - if (MaxT[2] > MaxT[WhichPlane]) + if (MaxT.Z > MaxT.GetAt(WhichPlane)) { WhichPlane = 2; } // Check final candidate actually inside box - if (Convert.ToBoolean((uint)MaxT[WhichPlane] & 0x80000000)) + if (Convert.ToBoolean((uint)MaxT.GetAt(WhichPlane) & 0x80000000)) { // Miss the box return false; @@ -97,9 +98,9 @@ namespace Framework.GameMath { if (i != WhichPlane) { - location[i] = origin[i] + MaxT[WhichPlane] * dir[i]; - if ((location[i] < MinB[i]) || - (location[i] > MaxB[i])) + location.SetAt(origin.GetAt(i) + MaxT.GetAt(WhichPlane) * dir.GetAt(i), i); + if ((location.GetAt(i) < MinB.GetAt(i)) || + (location.GetAt(i) > MaxB.GetAt(i))) { // On this plane we're outside the box extents, so // we miss the box @@ -110,7 +111,7 @@ namespace Framework.GameMath // Choose the normal to be the plane normal facing into the ray normal = Vector3.Zero; - normal[WhichPlane] = (float)((dir[WhichPlane] > 0) ? -1.0 : 1.0); + normal.SetAt((float)((dir.GetAt(WhichPlane) > 0) ? -1.0 : 1.0), WhichPlane); return true; } diff --git a/Source/Framework/GameMath/Matrix2.cs b/Source/Framework/GameMath/Matrix2.cs deleted file mode 100644 index fe004950a..000000000 --- a/Source/Framework/GameMath/Matrix2.cs +++ /dev/null @@ -1,614 +0,0 @@ -/* - * Copyright (C) 2012-2020 CypherCore - * Copyright (C) 2003-2004 Eran Kampf eran@ekampf.com http://www.ekampf.com - * - * 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 . - */ - -using System; -using System.Collections.Generic; -using System.ComponentModel; -using System.Diagnostics; -using System.Runtime.InteropServices; -using System.Text.RegularExpressions; - -namespace Framework.GameMath -{ - /// - /// Represents a 2-dimentional single-precision floating point matrix. - /// - [Serializable] - [StructLayout(LayoutKind.Sequential)] - [TypeConverter(typeof(ExpandableObjectConverter))] - public struct Matrix2 : ICloneable - { - #region Private Fields - private float _m11, _m12; - private float _m21, _m22; - #endregion - - #region Constructors - /// - /// Initializes a new instance of the structure with the specified values. - /// - public Matrix2(float m11, float m12, float m21, float m22) - { - _m11 = m11; _m12 = m12; - _m21 = m21; _m22 = m22; - } - /// - /// Initializes a new instance of the structure with the specified values. - /// - /// An array containing the matrix values in row-major order. - public Matrix2(float[] elements) - { - Debug.Assert(elements != null); - Debug.Assert(elements.Length >= 4); - - _m11 = elements[0]; _m12 = elements[1]; - _m21 = elements[2]; _m22 = elements[3]; - } - /// - /// Initializes a new instance of the structure with the specified values. - /// - /// An array containing the matrix values in row-major order. - public Matrix2(List elements) - { - Debug.Assert(elements != null); - Debug.Assert(elements.Count >= 4); - - _m11 = elements[0]; _m12 = elements[1]; - _m21 = elements[2]; _m22 = elements[3]; - } - /// - /// Initializes a new instance of the structure with the specified values. - /// - /// A instance holding values for the first column. - /// A instance holding values for the second column. - public Matrix2(Vector2 column1, Vector2 column2) - { - _m11 = column1.X; _m12 = column2.X; - _m21 = column1.Y; _m22 = column2.Y; - } - /// - /// Initializes a new instance of the class using a given matrix. - /// - public Matrix2(Matrix2 m) - { - _m11 = m.M11; _m12 = m.M12; - _m21 = m.M21; _m22 = m.M22; - } - #endregion - - #region Constants - /// - /// 4-dimentional single-precision floating point zero matrix. - /// - public static readonly Matrix2 Zero = new(0, 0, 0, 0); - /// - /// 4-dimentional single-precision floating point identity matrix. - /// - public static readonly Matrix2 Identity = new(1, 0, 0, 1); - #endregion - - #region Public Properties - /// - /// Gets or sets the value of the [1,1] matrix element. - /// - /// A single-precision floating-point number. - public float M11 - { - get { return _m11; } - set { _m11 = value; } - } - /// - /// Gets or sets the value of the [1,2] matrix element. - /// - /// A single-precision floating-point number. - public float M12 - { - get { return _m12; } - set { _m12 = value; } - } - - /// - /// Gets or sets the value of the [2,1] matrix element. - /// - /// A single-precision floating-point number. - public float M21 - { - get { return _m21; } - set { _m21 = value; } - } - /// - /// Gets or sets the value of the [2,2] matrix element. - /// - /// A single-precision floating-point number. - public float M22 - { - get { return _m22; } - set { _m22 = value; } - } - - /// - /// Gets the matrix's trace value. - /// - /// A single-precision floating-point number. - public float Trace - { - get - { - return _m11 + _m22; - } - } - #endregion - - #region ICloneable Members - /// - /// Creates an exact copy of this object. - /// - /// The object this method creates, cast as an object. - object ICloneable.Clone() - { - return new Matrix2(this); - } - /// - /// Creates an exact copy of this object. - /// - /// The object this method creates. - public Matrix2 Clone() - { - return new Matrix2(this); - } - #endregion - - #region Public Static Parse Methods - private const string regularExp = @"2x2\s*\[(?.*),(?.*),(?.*),(?.*)\]"; - /// - /// Converts the specified string to its equivalent. - /// - /// A string representation of a . - /// A that represents the vector specified by the parameter. - /// - /// The string should be in the following form: "2x2..matrix elements..>".
- /// Exmaple : "2x2[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]" - ///
- public static Matrix2 Parse(string value) - { - Regex r = new(regularExp, RegexOptions.Singleline | RegexOptions.IgnorePatternWhitespace); - Match m = r.Match(value); - if (m.Success) - { - return new Matrix2( - float.Parse(m.Result("${m11}")), - float.Parse(m.Result("${m12}")), - - float.Parse(m.Result("${m21}")), - float.Parse(m.Result("${m22}")) - ); - } - else - { - throw new Exception("Unsuccessful Match."); - } - } - /// - /// Converts the specified string to its equivalent. - /// A return value indicates whether the conversion succeeded or failed. - /// - /// A string representation of a . - /// - /// When this method returns, if the conversion succeeded, - /// contains a representing the vector specified by . - /// - /// if value was converted successfully; otherwise, . - /// - /// The string should be in the following form: "2x2..matrix elements..>".
- /// Exmaple : "2x2[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]" - ///
- public static bool TryParse(string value, out Matrix2 result) - { - Regex r = new(regularExp, RegexOptions.Singleline); - Match m = r.Match(value); - if (m.Success) - { - result = new Matrix2( - float.Parse(m.Result("${m11}")), - float.Parse(m.Result("${m12}")), - - float.Parse(m.Result("${m21}")), - float.Parse(m.Result("${m22}")) - ); - - return true; - } - - result = Matrix2.Zero; - return false; - } - #endregion - - #region Public Static Matrix Arithmetics - /// - /// Adds two matrices. - /// - /// A instance. - /// A instance. - /// A new instance containing the sum. - public static Matrix2 Add(Matrix2 left, Matrix2 right) - { - return new Matrix2( - left.M11 + right.M11, left.M12 + right.M12, - left.M21 + right.M21, left.M22 + right.M22 - ); - } - /// - /// Adds a matrix and a scalar. - /// - /// A instance. - /// A single-precision floating-point number. - /// A new instance containing the sum. - public static Matrix2 Add(Matrix2 matrix, float scalar) - { - return new Matrix2( - matrix.M11 + scalar, matrix.M12 + scalar, - matrix.M21 + scalar, matrix.M22 + scalar - ); - } - /// - /// Adds two matrices and put the result in a third matrix. - /// - /// A instance. - /// A instance. - /// A instance to hold the result. - public static void Add(Matrix2 left, Matrix2 right, ref Matrix2 result) - { - result.M11 = left.M11 + right.M11; - result.M12 = left.M12 + right.M12; - - result.M21 = left.M21 + right.M21; - result.M22 = left.M22 + right.M22; - } - /// - /// Adds a matrix and a scalar and put the result in a third matrix. - /// - /// A instance. - /// A single-precision floating-point number. - /// A instance to hold the result. - public static void Add(Matrix2 matrix, float scalar, ref Matrix2 result) - { - result.M11 = matrix.M11 + scalar; - result.M12 = matrix.M12 + scalar; - - result.M21 = matrix.M21 + scalar; - result.M22 = matrix.M22 + scalar; - } - /// - /// Subtracts a matrix from a matrix. - /// - /// A instance to subtract from. - /// A instance to subtract. - /// A new instance containing the difference. - /// result[x][y] = left[x][y] - right[x][y] - public static Matrix2 Subtract(Matrix2 left, Matrix2 right) - { - return new Matrix2( - left.M11 - right.M11, left.M12 - right.M12, - left.M21 - right.M21, left.M22 - right.M22 - ); - } - /// - /// Subtracts a scalar from a matrix. - /// - /// A instance. - /// A single-precision floating-point number. - /// A new instance containing the difference. - public static Matrix2 Subtract(Matrix2 matrix, float scalar) - { - return new Matrix2( - matrix.M11 - scalar, matrix.M12 - scalar, - matrix.M21 - scalar, matrix.M22 - scalar - ); - } - /// - /// Subtracts a matrix from a matrix and put the result in a third matrix. - /// - /// A instance to subtract from. - /// A instance to subtract. - /// A instance to hold the result. - /// result[x][y] = left[x][y] - right[x][y] - public static void Subtract(Matrix2 left, Matrix2 right, ref Matrix2 result) - { - result.M11 = left.M11 - right.M11; - result.M12 = left.M12 - right.M12; - - result.M21 = left.M21 - right.M21; - result.M22 = left.M22 - right.M22; - } - /// - /// Subtracts a scalar from a matrix and put the result in a third matrix. - /// - /// A instance. - /// A single-precision floating-point number. - /// A instance to hold the result. - public static void Subtract(Matrix2 matrix, float scalar, ref Matrix2 result) - { - result.M11 = matrix.M11 - scalar; - result.M12 = matrix.M12 - scalar; - - result.M21 = matrix.M21 - scalar; - result.M22 = matrix.M22 - scalar; - } - /// - /// Multiplies two matrices. - /// - /// A instance. - /// A instance. - /// A new instance containing the result. - public static Matrix2 Multiply(Matrix2 left, Matrix2 right) - { - return new Matrix2( - left.M11 * right.M11 + left.M12 * right.M21, - left.M11 * right.M12 + left.M12 * right.M22, - left.M21 * right.M11 + left.M22 * right.M21, - left.M21 * right.M12 + left.M22 * right.M22 - ); - } - /// - /// Multiplies two matrices and put the result in a third matrix. - /// - /// A instance. - /// A instance. - /// A instance to hold the result. - public static void Multiply(Matrix2 left, Matrix2 right, ref Matrix2 result) - { - result.M11 = left.M11 * right.M11 + left.M12 * right.M21; - result.M12 = left.M11 * right.M12 + left.M12 * right.M22; - - result.M21 = left.M21 * right.M11 + left.M22 * right.M21; - result.M22 = left.M21 * right.M12 + left.M22 * right.M22; - } - /// - /// Transforms a given vector by a matrix. - /// - /// A instance. - /// A instance. - /// A new instance containing the result. - public static Vector2 Transform(Matrix2 matrix, Vector2 vector) - { - return new Vector2( - (matrix.M11 * vector.X) + (matrix.M12 * vector.Y), - (matrix.M21 * vector.X) + (matrix.M22 * vector.Y)); - } - /// - /// Transforms a given vector by a matrix and put the result in a vector. - /// - /// A instance. - /// A instance. - /// A instance to hold the result. - public static void Transform(Matrix2 matrix, Vector2 vector, ref Vector2 result) - { - result.X = (matrix.M11 * vector.X) + (matrix.M12 * vector.Y); - result.Y = (matrix.M21 * vector.X) + (matrix.M22 * vector.Y); - } - /// - /// Transposes a matrix. - /// - /// A instance. - /// A new instance containing the transposed matrix. - public static Matrix2 Transpose(Matrix2 m) - { - Matrix2 t = new(m); - t.Transpose(); - return t; - } - #endregion - - #region System.Object overrides - /// - /// Returns the hashcode for this instance. - /// - /// A 32-bit signed integer hash code. - public override int GetHashCode() - { - return - _m11.GetHashCode() ^ _m12.GetHashCode() ^ - _m21.GetHashCode() ^ _m22.GetHashCode(); - } - /// - /// Returns a value indicating whether this instance is equal to - /// the specified object. - /// - /// An object to compare to this instance. - /// if is a and has the same values as this instance; otherwise, . - public override bool Equals(object obj) - { - if (obj is Matrix2) - { - Matrix2 m = (Matrix2)obj; - return - (_m11 == m.M11) && (_m12 == m.M12) && - (_m21 == m.M21) && (_m22 == m.M22); - } - return false; - } - /// - /// Returns a string representation of this object. - /// - /// A string representation of this object. - public override string ToString() - { - return $"2x2[{_m11}, {_m12}, {_m21}, {_m22}]"; - } - #endregion - - #region Public Methods - /// - /// Calculates the determinant value of the matrix. - /// - /// The determinant value of the matrix. - public float GetDeterminant() - { - return (_m11 * _m22) - (_m12 * _m21); - } - /// - /// Transposes this matrix. - /// - public void Transpose() - { - MathFunctions.Swap(ref _m12, ref _m21); - } - #endregion - - #region Comparison Operators - /// - /// Tests whether two specified matrices are equal. - /// - /// A instance. - /// A instance. - /// if the two matrices are equal; otherwise, . - public static bool operator ==(Matrix2 left, Matrix2 right) - { - return ValueType.Equals(left, right); - } - /// - /// Tests whether two specified matrices are not equal. - /// - /// A instance. - /// A instance. - /// if the two matrices are not equal; otherwise, . - public static bool operator !=(Matrix2 left, Matrix2 right) - { - return !ValueType.Equals(left, right); - } - #endregion - - #region Binary Operators - /// - /// Adds two matrices. - /// - /// A instance. - /// A instance. - /// A new instance containing the sum. - public static Matrix2 operator +(Matrix2 left, Matrix2 right) - { - return Matrix2.Add(left, right); - } - /// - /// Adds a matrix and a scalar. - /// - /// A instance. - /// A single-precision floating-point number. - /// A new instance containing the sum. - public static Matrix2 operator +(Matrix2 matrix, float scalar) - { - return Matrix2.Add(matrix, scalar); - } - /// - /// Adds a matrix and a scalar. - /// - /// A instance. - /// A single-precision floating-point number. - /// A new instance containing the sum. - public static Matrix2 operator +(float scalar, Matrix2 matrix) - { - return Matrix2.Add(matrix, scalar); - } - /// - /// Subtracts a matrix from a matrix. - /// - /// A instance. - /// A instance. - /// A new instance containing the difference. - public static Matrix2 operator -(Matrix2 left, Matrix2 right) - { - return Matrix2.Subtract(left, right); - } - /// - /// Subtracts a scalar from a matrix. - /// - /// A instance. - /// A single-precision floating-point number. - /// A new instance containing the difference. - public static Matrix2 operator -(Matrix2 matrix, float scalar) - { - return Matrix2.Subtract(matrix, scalar); - } - /// - /// Multiplies two matrices. - /// - /// A instance. - /// A instance. - /// A new instance containing the result. - public static Matrix2 operator *(Matrix2 left, Matrix2 right) - { - return Matrix2.Multiply(left, right); - } - /// - /// Transforms a given vector by a matrix. - /// - /// A instance. - /// A instance. - /// A new instance containing the result. - public static Vector2 operator *(Matrix2 matrix, Vector2 vector) - { - return Matrix2.Transform(matrix, vector); - } - #endregion - - #region Indexing Operators - /// - /// Indexer allowing to access the matrix elements by an index - /// where index = 2*row + column. - /// - public unsafe float this[int index] - { - get - { - if (index < 0 || index >= 4) - throw new IndexOutOfRangeException("Invalid matrix index!"); - - fixed (float* f = &_m11) - { - return *(f + index); - } - } - set - { - if (index < 0 || index >= 4) - throw new IndexOutOfRangeException("Invalid matrix index!"); - - fixed (float* f = &_m11) - { - *(f + index) = value; - } - } - } - /// - /// Indexer allowing to access the matrix elements by row and column. - /// - public float this[int row, int column] - { - get - { - return this[(row - 1) * 2 + (column - 1)]; - } - set - { - this[(row - 1) * 2 + (column - 1)] = value; - } - } - #endregion - } -} diff --git a/Source/Framework/GameMath/Matrix3.cs b/Source/Framework/GameMath/Matrix3.cs deleted file mode 100644 index da8724f95..000000000 --- a/Source/Framework/GameMath/Matrix3.cs +++ /dev/null @@ -1,807 +0,0 @@ -/* - * Copyright (C) 2012-2020 CypherCore - * Copyright (C) 2003-2004 Eran Kampf eran@ekampf.com http://www.ekampf.com - * - * 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 . - */ - -using System; -using System.Collections.Generic; -using System.ComponentModel; -using System.Diagnostics; -using System.Runtime.InteropServices; -using System.Text.RegularExpressions; - -namespace Framework.GameMath -{ - /// - /// Represents a 3-dimentional single-precision floating point matrix. - /// - [Serializable] - [StructLayout(LayoutKind.Sequential)] - [TypeConverter(typeof(ExpandableObjectConverter))] - public struct Matrix3 : ICloneable - { - #region Private Fields - private float _m11, _m12, _m13; - private float _m21, _m22, _m23; - private float _m31, _m32, _m33; - #endregion - - #region Constructors - /// - /// Initializes a new instance of the structure with the specified values. - /// - public Matrix3(float m11, float m12, float m13, float m21, float m22, float m23, float m31, float m32, float m33) - { - _m11 = m11; _m12 = m12; _m13 = m13; - _m21 = m21; _m22 = m22; _m23 = m23; - _m31 = m31; _m32 = m32; _m33 = m33; - } - /// - /// Initializes a new instance of the structure with the specified values. - /// - /// An array containing the matrix values in row-major order. - public Matrix3(float[] elements) - { - Debug.Assert(elements != null); - Debug.Assert(elements.Length >= 9); - - _m11 = elements[0]; _m12 = elements[1]; _m13 = elements[2]; - _m21 = elements[3]; _m22 = elements[4]; _m23 = elements[5]; - _m31 = elements[6]; _m32 = elements[7]; _m33 = elements[8]; - } - /// - /// Initializes a new instance of the structure with the specified values. - /// - /// An array containing the matrix values in row-major order. - public Matrix3(List elements) - { - Debug.Assert(elements != null); - Debug.Assert(elements.Count >= 9); - - _m11 = elements[0]; _m12 = elements[1]; _m13 = elements[2]; - _m21 = elements[3]; _m22 = elements[4]; _m23 = elements[5]; - _m31 = elements[6]; _m32 = elements[7]; _m33 = elements[8]; - } - /// - /// Initializes a new instance of the structure with the specified values. - /// - /// A instance holding values for the first column. - /// A instance holding values for the second column. - /// A instance holding values for the third column. - public Matrix3(Vector3 column1, Vector3 column2, Vector3 column3) - { - _m11 = column1.X; _m12 = column2.X; _m13 = column3.X; - _m21 = column1.Y; _m22 = column2.Y; _m23 = column3.Y; - _m31 = column1.Z; _m32 = column2.Z; _m33 = column3.Z; - } - /// - /// Initializes a new instance of the class using a given matrix. - /// - public Matrix3(Matrix3 m) - { - _m11 = m.M11; _m12 = m.M12; _m13 = m.M13; - _m21 = m.M21; _m22 = m.M22; _m23 = m.M23; - _m31 = m.M31; _m32 = m.M32; _m33 = m.M33; - } - #endregion - - #region Constants - /// - /// 4-dimentional single-precision floating point zero matrix. - /// - public static readonly Matrix3 Zero = new(0, 0, 0, 0, 0, 0, 0, 0, 0); - /// - /// 4-dimentional single-precision floating point identity matrix. - /// - public static readonly Matrix3 Identity = new(1, 0, 0, 0, 1, 0, 0, 0, 1); - #endregion - - #region Public Properties - /// - /// Gets or sets the value of the [1,1] matrix element. - /// - /// A single-precision floating-point number. - public float M11 - { - get { return _m11; } - set { _m11 = value; } - } - /// - /// Gets or sets the value of the [1,2] matrix element. - /// - /// A single-precision floating-point number. - public float M12 - { - get { return _m12; } - set { _m12 = value; } - } - /// - /// Gets or sets the value of the [1,3] matrix element. - /// - /// A single-precision floating-point number. - public float M13 - { - get { return _m13; } - set { _m13 = value; } - } - - - /// - /// Gets or sets the value of the [2,1] matrix element. - /// - /// A single-precision floating-point number. - public float M21 - { - get { return _m21; } - set { _m21 = value; } - } - /// - /// Gets or sets the value of the [2,2] matrix element. - /// - /// A single-precision floating-point number. - public float M22 - { - get { return _m22; } - set { _m22 = value; } - } - /// - /// Gets or sets the value of the [2,3] matrix element. - /// - /// A single-precision floating-point number. - public float M23 - { - get { return _m23; } - set { _m23 = value; } - } - - - /// - /// Gets or sets the value of the [3,1] matrix element. - /// - /// A single-precision floating-point number. - public float M31 - { - get { return _m31; } - set { _m31 = value; } - } - /// - /// Gets or sets the value of the [3,2] matrix element. - /// - /// A single-precision floating-point number. - public float M32 - { - get { return _m32; } - set { _m32 = value; } - } - /// - /// Gets or sets the value of the [3,3] matrix element. - /// - /// A single-precision floating-point number. - public float M33 - { - get { return _m33; } - set { _m33 = value; } - } - - /// - /// Gets the matrix's trace value. - /// - /// A single-precision floating-point number. - public float Trace - { - get - { - return _m11 + _m22 + _m33; - } - } - #endregion - - #region ICloneable Members - /// - /// Creates an exact copy of this object. - /// - /// The object this method creates, cast as an object. - object ICloneable.Clone() - { - return new Matrix3(this); - } - /// - /// Creates an exact copy of this object. - /// - /// The object this method creates. - public Matrix3 Clone() - { - return new Matrix3(this); - } - #endregion - - #region Public Static Parse Methods - private const string regularExp = @"3x3\s*\[(?.*),(?.*),(?.*),(?.*),(?.*),(?.*),(?.*),(?.*),(?.*)\]"; - /// - /// Converts the specified string to its equivalent. - /// - /// A string representation of a . - /// A that represents the vector specified by the parameter. - /// - /// The string should be in the following form: "3x3..matrix elements..>".
- /// Exmaple : "3x3[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]" - ///
- public static Matrix3 Parse(string value) - { - Regex r = new(regularExp, RegexOptions.Singleline | RegexOptions.IgnorePatternWhitespace); - Match m = r.Match(value); - if (m.Success) - { - return new Matrix3( - float.Parse(m.Result("${m11}")), - float.Parse(m.Result("${m12}")), - float.Parse(m.Result("${m13}")), - - float.Parse(m.Result("${m21}")), - float.Parse(m.Result("${m22}")), - float.Parse(m.Result("${m23}")), - - float.Parse(m.Result("${m31}")), - float.Parse(m.Result("${m32}")), - float.Parse(m.Result("${m33}")) - ); - } - else - { - throw new Exception("Unsuccessful Match."); - } - } - /// - /// Converts the specified string to its equivalent. - /// A return value indicates whether the conversion succeeded or failed. - /// - /// A string representation of a . - /// - /// When this method returns, if the conversion succeeded, - /// contains a representing the vector specified by . - /// - /// if value was converted successfully; otherwise, . - /// - /// The string should be in the following form: "3x3..matrix elements..>".
- /// Exmaple : "3x3[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]" - ///
- public static bool TryParse(string value, out Matrix3 result) - { - Regex r = new(regularExp, RegexOptions.Singleline); - Match m = r.Match(value); - if (m.Success) - { - result = new Matrix3( - float.Parse(m.Result("${m11}")), - float.Parse(m.Result("${m12}")), - float.Parse(m.Result("${m13}")), - - float.Parse(m.Result("${m21}")), - float.Parse(m.Result("${m22}")), - float.Parse(m.Result("${m23}")), - - float.Parse(m.Result("${m31}")), - float.Parse(m.Result("${m32}")), - float.Parse(m.Result("${m33}")) - ); - - return true; - } - - result = Matrix3.Zero; - return false; - } - #endregion - - #region Public Static Matrix Arithmetics - /// - /// Adds two matrices. - /// - /// A instance. - /// A instance. - /// A new instance containing the sum. - public static Matrix3 Add(Matrix3 left, Matrix3 right) - { - return new Matrix3( - left.M11 + right.M11, left.M12 + right.M12, left.M13 + right.M13, - left.M21 + right.M21, left.M22 + right.M22, left.M23 + right.M23, - left.M31 + right.M31, left.M32 + right.M32, left.M33 + right.M33 - ); - } - /// - /// Adds a matrix and a scalar. - /// - /// A instance. - /// A single-precision floating-point number. - /// A new instance containing the sum. - public static Matrix3 Add(Matrix3 matrix, float scalar) - { - return new Matrix3( - matrix.M11 + scalar, matrix.M12 + scalar, matrix.M13 + scalar, - matrix.M21 + scalar, matrix.M22 + scalar, matrix.M23 + scalar, - matrix.M31 + scalar, matrix.M32 + scalar, matrix.M33 + scalar - ); - } - /// - /// Adds two matrices and put the result in a third matrix. - /// - /// A instance. - /// A instance. - /// A instance to hold the result. - public static void Add(Matrix3 left, Matrix3 right, ref Matrix3 result) - { - result.M11 = left.M11 + right.M11; - result.M12 = left.M12 + right.M12; - result.M13 = left.M13 + right.M13; - - result.M21 = left.M21 + right.M21; - result.M22 = left.M22 + right.M22; - result.M23 = left.M23 + right.M23; - - result.M31 = left.M31 + right.M31; - result.M32 = left.M32 + right.M32; - result.M33 = left.M33 + right.M33; - } - /// - /// Adds a matrix and a scalar and put the result in a third matrix. - /// - /// A instance. - /// A single-precision floating-point number. - /// A instance to hold the result. - public static void Add(Matrix3 matrix, float scalar, ref Matrix3 result) - { - result.M11 = matrix.M11 + scalar; - result.M12 = matrix.M12 + scalar; - result.M13 = matrix.M13 + scalar; - - result.M21 = matrix.M21 + scalar; - result.M22 = matrix.M22 + scalar; - result.M23 = matrix.M23 + scalar; - - result.M31 = matrix.M31 + scalar; - result.M32 = matrix.M32 + scalar; - result.M33 = matrix.M33 + scalar; - } - /// - /// Subtracts a matrix from a matrix. - /// - /// A instance to subtract from. - /// A instance to subtract. - /// A new instance containing the difference. - /// result[x][y] = left[x][y] - right[x][y] - public static Matrix3 Subtract(Matrix3 left, Matrix3 right) - { - return new Matrix3( - left.M11 - right.M11, left.M12 - right.M12, left.M13 - right.M13, - left.M21 - right.M21, left.M22 - right.M22, left.M23 - right.M23, - left.M31 - right.M31, left.M32 - right.M32, left.M33 - right.M33 - ); - } - /// - /// Subtracts a scalar from a matrix. - /// - /// A instance. - /// A single-precision floating-point number. - /// A new instance containing the difference. - public static Matrix3 Subtract(Matrix3 matrix, float scalar) - { - return new Matrix3( - matrix.M11 - scalar, matrix.M12 - scalar, matrix.M13 - scalar, - matrix.M21 - scalar, matrix.M22 - scalar, matrix.M23 - scalar, - matrix.M31 - scalar, matrix.M32 - scalar, matrix.M33 - scalar - ); - } - /// - /// Subtracts a matrix from a matrix and put the result in a third matrix. - /// - /// A instance to subtract from. - /// A instance to subtract. - /// A instance to hold the result. - /// result[x][y] = left[x][y] - right[x][y] - public static void Subtract(Matrix3 left, Matrix3 right, ref Matrix3 result) - { - result.M11 = left.M11 - right.M11; - result.M12 = left.M12 - right.M12; - result.M13 = left.M13 - right.M13; - - result.M21 = left.M21 - right.M21; - result.M22 = left.M22 - right.M22; - result.M23 = left.M23 - right.M23; - - result.M31 = left.M31 - right.M31; - result.M32 = left.M32 - right.M32; - result.M33 = left.M33 - right.M33; - } - /// - /// Subtracts a scalar from a matrix and put the result in a third matrix. - /// - /// A instance. - /// A single-precision floating-point number. - /// A instance to hold the result. - public static void Subtract(Matrix3 matrix, float scalar, ref Matrix3 result) - { - result.M11 = matrix.M11 - scalar; - result.M12 = matrix.M12 - scalar; - result.M13 = matrix.M13 - scalar; - - result.M21 = matrix.M21 - scalar; - result.M22 = matrix.M22 - scalar; - result.M23 = matrix.M23 - scalar; - - result.M31 = matrix.M31 - scalar; - result.M32 = matrix.M32 - scalar; - result.M33 = matrix.M33 - scalar; - } - /// - /// Multiplies two matrices. - /// - /// A instance. - /// A instance. - /// A new instance containing the result. - public static Matrix3 Multiply(Matrix3 left, Matrix3 right) - { - return new Matrix3( - left.M11 * right.M11 + left.M12 * right.M21 + left.M13 * right.M31, - left.M11 * right.M12 + left.M12 * right.M22 + left.M13 * right.M32, - left.M11 * right.M13 + left.M12 * right.M23 + left.M13 * right.M33, - - left.M21 * right.M11 + left.M22 * right.M21 + left.M23 * right.M31, - left.M21 * right.M12 + left.M22 * right.M22 + left.M23 * right.M32, - left.M21 * right.M13 + left.M22 * right.M23 + left.M23 * right.M33, - - left.M31 * right.M11 + left.M32 * right.M21 + left.M33 * right.M31, - left.M31 * right.M12 + left.M32 * right.M22 + left.M33 * right.M32, - left.M31 * right.M13 + left.M32 * right.M23 + left.M33 * right.M33 - ); - } - /// - /// Multiplies two matrices and put the result in a third matrix. - /// - /// A instance. - /// A instance. - /// A instance to hold the result. - public static void Multiply(Matrix3 left, Matrix3 right, ref Matrix3 result) - { - result.M11 = left.M11 * right.M11 + left.M12 * right.M21 + left.M13 * right.M31; - result.M12 = left.M11 * right.M12 + left.M12 * right.M22 + left.M13 * right.M32; - result.M13 = left.M11 * right.M13 + left.M12 * right.M23 + left.M13 * right.M33; - - result.M21 = left.M21 * right.M11 + left.M22 * right.M21 + left.M23 * right.M31; - result.M22 = left.M21 * right.M12 + left.M22 * right.M22 + left.M23 * right.M32; - result.M23 = left.M21 * right.M13 + left.M22 * right.M23 + left.M23 * right.M33; - - result.M31 = left.M31 * right.M11 + left.M32 * right.M21 + left.M33 * right.M31; - result.M32 = left.M31 * right.M12 + left.M32 * right.M22 + left.M33 * right.M32; - result.M33 = left.M31 * right.M13 + left.M32 * right.M23 + left.M33 * right.M33; - } - /// - /// Transforms a given vector by a matrix. - /// - /// A instance. - /// A instance. - /// A new instance containing the result. - public static Vector3 Transform(Matrix3 matrix, Vector3 vector) - { - return new Vector3( - (matrix.M11 * vector.X) + (matrix.M12 * vector.Y) + (matrix.M13 * vector.Z), - (matrix.M21 * vector.X) + (matrix.M22 * vector.Y) + (matrix.M23 * vector.Z), - (matrix.M31 * vector.X) + (matrix.M32 * vector.Y) + (matrix.M33 * vector.Z)); - } - /// - /// Transforms a given vector by a matrix and put the result in a vector. - /// - /// A instance. - /// A instance. - /// A instance to hold the result. - public static void Transform(Matrix3 matrix, Vector3 vector, ref Vector3 result) - { - result.X = (matrix.M11 * vector.X) + (matrix.M12 * vector.Y) + (matrix.M13 * vector.Z); - result.Y = (matrix.M21 * vector.X) + (matrix.M22 * vector.Y) + (matrix.M23 * vector.Z); - result.Z = (matrix.M31 * vector.X) + (matrix.M32 * vector.Y) + (matrix.M33 * vector.Z); - } - /// - /// Transposes a matrix. - /// - /// A instance. - /// A new instance containing the transposed matrix. - public static Matrix3 Transpose(Matrix3 m) - { - Matrix3 t = new(m); - t.Transpose(); - return t; - } - - public static Matrix3 fromEulerAnglesZYX(float fYAngle, float fPAngle, float fRAngle) - { - float fCos, fSin; - - fCos = (float)Math.Cos(fYAngle); - fSin = (float)Math.Sin(fYAngle); - Matrix3 kZMat = new(fCos, -fSin, 0.0f, fSin, fCos, 0.0f, 0.0f, 0.0f, 1.0f); - - fCos = (float)Math.Cos(fPAngle); - fSin = (float)Math.Sin(fPAngle); - Matrix3 kYMat = new(fCos, 0.0f, fSin, 0.0f, 1.0f, 0.0f, -fSin, 0.0f, fCos); - - fCos = (float)Math.Cos(fRAngle); - fSin = (float)Math.Sin(fRAngle); - Matrix3 kXMat = new(1.0f, 0.0f, 0.0f, 0.0f, fCos, -fSin, 0.0f, fSin, fCos); - - return (kZMat * (kYMat * kXMat)); - } - - public Matrix3 inverse(float fTolerance = (float)1e-06) - { - Matrix3 kInverse = Matrix3.Zero; - inverse(ref kInverse, fTolerance); - return kInverse; - } - bool inverse(ref Matrix3 rkInverse, float fTolerance) - { - // Invert a 3x3 using cofactors. This is about 8 times faster than - // the Numerical Recipes code which uses Gaussian elimination. - rkInverse.M11 = M22 * M33 - - M23 * M32; - rkInverse.M12 = M13 * M32 - - M12 * M33; - rkInverse.M13 = M12 * M23 - - M13 * M22; - rkInverse.M21 = M23 * M31 - - M21 * M33; - rkInverse.M22 = M11 * M33 - - M13 * M31; - rkInverse.M23 = M13 * M21 - - M11 * M23; - rkInverse.M31 = M21 * M32 - - M22 * M31; - rkInverse.M32 = M12 * M31 - - M11 * M32; - rkInverse.M33 = M11 * M22 - - M12 * M21; - - - float fDet = - M11 * rkInverse.M11 + - M12 * rkInverse.M21 + - M13 * rkInverse.M31; - - if (Math.Abs(fDet) <= fTolerance) - return false; - - float fInvDet = (float)(1.0 / fDet); - - rkInverse.M11 *= fInvDet; - rkInverse.M12 *= fInvDet; - rkInverse.M13 *= fInvDet; - rkInverse.M21 *= fInvDet; - rkInverse.M22 *= fInvDet; - rkInverse.M23 *= fInvDet; - rkInverse.M31 *= fInvDet; - rkInverse.M32 *= fInvDet; - rkInverse.M33 *= fInvDet; - - return true; - } - #endregion - - #region System.Object overrides - /// - /// Returns the hashcode for this instance. - /// - /// A 32-bit signed integer hash code. - public override int GetHashCode() - { - return - _m11.GetHashCode() ^ _m12.GetHashCode() ^ _m13.GetHashCode() ^ - _m21.GetHashCode() ^ _m22.GetHashCode() ^ _m23.GetHashCode() ^ - _m31.GetHashCode() ^ _m32.GetHashCode() ^ _m33.GetHashCode(); - } - /// - /// Returns a value indicating whether this instance is equal to - /// the specified object. - /// - /// An object to compare to this instance. - /// if is a and has the same values as this instance; otherwise, . - public override bool Equals(object obj) - { - if (obj is Matrix3) - { - Matrix3 m = (Matrix3)obj; - return - (_m11 == m.M11) && (_m12 == m.M12) && (_m13 == m.M13) && - (_m21 == m.M21) && (_m22 == m.M22) && (_m23 == m.M23) && - (_m31 == m.M31) && (_m32 == m.M32) && (_m33 == m.M33); - } - return false; - } - /// - /// Returns a string representation of this object. - /// - /// A string representation of this object. - public override string ToString() - { - return $"3x3[{_m11}, {_m12}, {_m13}, {_m21}, {_m22}, {_m23}, {_m31}, {_m32}, {_m33}]"; - } - #endregion - - #region Public Methods - /// - /// Calculates the determinant value of the matrix. - /// - /// The determinant value of the matrix. - public float GetDeterminant() - { - // rule of Sarrus - return - _m11 * _m22 * _m33 + _m12 * _m23 * _m31 + _m13 * _m21 * _m32 - - _m13 * _m22 * _m31 - _m11 * _m23 * _m32 - _m12 * _m21 * _m33; - } - /// - /// Transposes this matrix. - /// - public void Transpose() - { - MathFunctions.Swap(ref _m12, ref _m21); - MathFunctions.Swap(ref _m13, ref _m31); - MathFunctions.Swap(ref _m23, ref _m32); - } - #endregion - - #region Comparison Operators - /// - /// Tests whether two specified matrices are equal. - /// - /// A instance. - /// A instance. - /// if the two matrices are equal; otherwise, . - public static bool operator ==(Matrix3 left, Matrix3 right) - { - return ValueType.Equals(left, right); - } - /// - /// Tests whether two specified matrices are not equal. - /// - /// A instance. - /// A instance. - /// if the two matrices are not equal; otherwise, . - public static bool operator !=(Matrix3 left, Matrix3 right) - { - return !ValueType.Equals(left, right); - } - #endregion - - #region Binary Operators - /// - /// Adds two matrices. - /// - /// A instance. - /// A instance. - /// A new instance containing the sum. - public static Matrix3 operator +(Matrix3 left, Matrix3 right) - { - return Matrix3.Add(left, right); - } - /// - /// Adds a matrix and a scalar. - /// - /// A instance. - /// A single-precision floating-point number. - /// A new instance containing the sum. - public static Matrix3 operator +(Matrix3 matrix, float scalar) - { - return Matrix3.Add(matrix, scalar); - } - /// - /// Adds a matrix and a scalar. - /// - /// A instance. - /// A single-precision floating-point number. - /// A new instance containing the sum. - public static Matrix3 operator +(float scalar, Matrix3 matrix) - { - return Matrix3.Add(matrix, scalar); - } - /// - /// Subtracts a matrix from a matrix. - /// - /// A instance. - /// A instance. - /// A new instance containing the difference. - public static Matrix3 operator -(Matrix3 left, Matrix3 right) - { - return Matrix3.Subtract(left, right); - } - /// - /// Subtracts a scalar from a matrix. - /// - /// A instance. - /// A single-precision floating-point number. - /// A new instance containing the difference. - public static Matrix3 operator -(Matrix3 matrix, float scalar) - { - return Matrix3.Subtract(matrix, scalar); - } - /// - /// Multiplies two matrices. - /// - /// A instance. - /// A instance. - /// A new instance containing the result. - public static Matrix3 operator *(Matrix3 left, Matrix3 right) - { - return Matrix3.Multiply(left, right); - } - /// - /// Transforms a given vector by a matrix. - /// - /// A instance. - /// A instance. - /// A new instance containing the result. - public static Vector3 operator *(Matrix3 matrix, Vector3 vector) - { - return Matrix3.Transform(matrix, vector); - } - public static Vector3 operator *(Vector3 rkPoint, Matrix3 rkMatrix) - { - return (Matrix3.Transpose(rkMatrix) * rkPoint); - } - #endregion - - #region Indexing Operators - /// - /// Indexer allowing to access the matrix elements by an index - /// where index = 2*row + column. - /// - public unsafe float this[int index] - { - get - { - if (index < 0 || index >= 9) - throw new IndexOutOfRangeException("Invalid matrix index!"); - - fixed (float* f = &_m11) - { - return *(f + index); - } - } - set - { - if (index < 0 || index >= 9) - throw new IndexOutOfRangeException("Invalid matrix index!"); - - fixed (float* f = &_m11) - { - *(f + index) = value; - } - } - } - /// - /// Indexer allowing to access the matrix elements by row and column. - /// - public float this[int row, int column] - { - get - { - return this[row * 3 + column]; - } - set - { - this[row * 3 + column] = value; - } - } - #endregion - } -} diff --git a/Source/Framework/GameMath/Matrix4.cs b/Source/Framework/GameMath/Matrix4.cs deleted file mode 100644 index d472abf0c..000000000 --- a/Source/Framework/GameMath/Matrix4.cs +++ /dev/null @@ -1,900 +0,0 @@ -/* - * Copyright (C) 2012-2020 CypherCore - * Copyright (C) 2003-2004 Eran Kampf eran@ekampf.com http://www.ekampf.com - * - * 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 . - */ - -using System; -using System.Collections.Generic; -using System.ComponentModel; -using System.Diagnostics; -using System.Runtime.InteropServices; -using System.Text.RegularExpressions; - -namespace Framework.GameMath -{ - /// - /// Represents a 4-dimentional single-precision floating point matrix. - /// - [Serializable] - [StructLayout(LayoutKind.Sequential)] - [TypeConverter(typeof(ExpandableObjectConverter))] - public struct Matrix4 : ICloneable - { - #region Private Fields - private float _m11, _m12, _m13, _m14; - private float _m21, _m22, _m23, _m24; - private float _m31, _m32, _m33, _m34; - private float _m41, _m42, _m43, _m44; - #endregion - - #region Constructors - /// - /// Initializes a new instance of the structure with the specified values. - /// - public Matrix4(float m11, float m12, float m13, float m14, float m21, float m22, float m23, float m24, float m31, float m32, float m33, float m34, float m41, float m42, float m43, float m44) - { - _m11 = m11; _m12 = m12; _m13 = m13; _m14 = m14; - _m21 = m21; _m22 = m22; _m23 = m23; _m24 = m24; - _m31 = m31; _m32 = m32; _m33 = m33; _m34 = m34; - _m41 = m41; _m42 = m42; _m43 = m43; _m44 = m44; - } - /// - /// Initializes a new instance of the structure with the specified values. - /// - /// An array containing the matrix values in row-major order. - public Matrix4(float[] elements) - { - Debug.Assert(elements != null); - Debug.Assert(elements.Length >= 16); - - _m11 = elements[0]; _m12 = elements[1]; _m13 = elements[2]; _m14 = elements[3]; - _m21 = elements[4]; _m22 = elements[5]; _m23 = elements[6]; _m24 = elements[7]; - _m31 = elements[8]; _m32 = elements[9]; _m33 = elements[10]; _m34 = elements[11]; - _m41 = elements[12]; _m42 = elements[13]; _m43 = elements[14]; _m44 = elements[15]; - } - /// - /// Initializes a new instance of the structure with the specified values. - /// - /// An array containing the matrix values in row-major order. - public Matrix4(List elements) - { - Debug.Assert(elements != null); - Debug.Assert(elements.Count >= 16); - - _m11 = elements[0]; _m12 = elements[1]; _m13 = elements[2]; _m14 = elements[3]; - _m21 = elements[4]; _m22 = elements[5]; _m23 = elements[6]; _m24 = elements[7]; - _m31 = elements[8]; _m32 = elements[9]; _m33 = elements[10]; _m34 = elements[11]; - _m41 = elements[12]; _m42 = elements[13]; _m43 = elements[14]; _m44 = elements[15]; - } - /// - /// Initializes a new instance of the structure with the specified values. - /// - /// A instance holding values for the first column. - /// A instance holding values for the second column. - /// A instance holding values for the third column. - /// A instance holding values for the fourth column. - public Matrix4(Vector4 column1, Vector4 column2, Vector4 column3, Vector4 column4) - { - _m11 = column1.X; _m12 = column2.X; _m13 = column3.X; _m14 = column4.X; - _m21 = column1.Y; _m22 = column2.Y; _m23 = column3.Y; _m24 = column4.Y; - _m31 = column1.Z; _m32 = column2.Z; _m33 = column3.Z; _m34 = column4.Z; - _m41 = column1.W; _m42 = column2.W; _m43 = column3.W; _m44 = column4.W; - } - /// - /// Initializes a new instance of the class using a given matrix. - /// - public Matrix4(Matrix4 m) - { - _m11 = m.M11; _m12 = m.M12; _m13 = m.M13; _m14 = m.M14; - _m21 = m.M21; _m22 = m.M22; _m23 = m.M23; _m24 = m.M24; - _m31 = m.M31; _m32 = m.M32; _m33 = m.M33; _m34 = m.M34; - _m41 = m.M41; _m42 = m.M42; _m43 = m.M43; _m44 = m.M44; - } - #endregion - - #region Constants - /// - /// 4-dimentional single-precision floating point zero matrix. - /// - public static readonly Matrix4 Zero = new(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0); - /// - /// 4-dimentional single-precision floating point identity matrix. - /// - public static readonly Matrix4 Identity = new(1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1); - #endregion - - #region Public Properties - /// - /// Gets or sets the value of the [1,1] matrix element. - /// - /// A single-precision floating-point number. - public float M11 - { - get { return _m11; } - set { _m11 = value; } - } - /// - /// Gets or sets the value of the [1,2] matrix element. - /// - /// A single-precision floating-point number. - public float M12 - { - get { return _m12; } - set { _m12 = value; } - } - /// - /// Gets or sets the value of the [1,3] matrix element. - /// - /// A single-precision floating-point number. - public float M13 - { - get { return _m13; } - set { _m13 = value; } - } - /// - /// Gets or sets the value of the [1,4] matrix element. - /// - /// A single-precision floating-point number. - public float M14 - { - get { return _m14; } - set { _m14 = value; } - } - - - /// - /// Gets or sets the value of the [2,1] matrix element. - /// - /// A single-precision floating-point number. - public float M21 - { - get { return _m21; } - set { _m21 = value; } - } - /// - /// Gets or sets the value of the [2,2] matrix element. - /// - /// A single-precision floating-point number. - public float M22 - { - get { return _m22; } - set { _m22 = value; } - } - /// - /// Gets or sets the value of the [2,3] matrix element. - /// - /// A single-precision floating-point number. - public float M23 - { - get { return _m23; } - set { _m23 = value; } - } - /// - /// Gets or sets the value of the [2,4] matrix element. - /// - /// A single-precision floating-point number. - public float M24 - { - get { return _m24; } - set { _m24 = value; } - } - - - /// - /// Gets or sets the value of the [3,1] matrix element. - /// - /// A single-precision floating-point number. - public float M31 - { - get { return _m31; } - set { _m31 = value; } - } - /// - /// Gets or sets the value of the [3,2] matrix element. - /// - /// A single-precision floating-point number. - public float M32 - { - get { return _m32; } - set { _m32 = value; } - } - /// - /// Gets or sets the value of the [3,3] matrix element. - /// - /// A single-precision floating-point number. - public float M33 - { - get { return _m33; } - set { _m33 = value; } - } - /// - /// Gets or sets the value of the [3,4] matrix element. - /// - /// A single-precision floating-point number. - public float M34 - { - get { return _m34; } - set { _m34 = value; } - } - - - /// - /// Gets or sets the value of the [4,1] matrix element. - /// - /// A single-precision floating-point number. - public float M41 - { - get { return _m41; } - set { _m41 = value; } - } - /// - /// Gets or sets the value of the [4,2] matrix element. - /// - /// A single-precision floating-point number. - public float M42 - { - get { return _m42; } - set { _m42 = value; } - } - /// - /// Gets or sets the value of the [4,3] matrix element. - /// - /// A single-precision floating-point number. - public float M43 - { - get { return _m43; } - set { _m43 = value; } - } - /// - /// Gets or sets the value of the [4,4] matrix element. - /// - /// A single-precision floating-point number. - public float M44 - { - get { return _m44; } - set { _m44 = value; } - } - - /// - /// Gets the matrix's trace value. - /// - /// A single-precision floating-point number. - public float Trace - { - get - { - return _m11 + _m22 + _m33 + _m44; - } - } - #endregion - - #region ICloneable Members - /// - /// Creates an exact copy of this object. - /// - /// The object this method creates, cast as an object. - object ICloneable.Clone() - { - return new Matrix4(this); - } - /// - /// Creates an exact copy of this object. - /// - /// The object this method creates. - public Matrix4 Clone() - { - return new Matrix4(this); - } - #endregion - - #region Public Static Parse Methods - private const string regularExp = @"4x4\s*\[(?.*),(?.*),(?.*),(?.*),(?.*),(?.*),(?.*),(?.*),(?.*),(?.*),(?.*),(?.*),(?.*),(?.*),(?.*),(?.*)\]"; - /// - /// Converts the specified string to its equivalent. - /// - /// A string representation of a . - /// A that represents the vector specified by the parameter. - /// - /// The string should be in the following form: "4x4..matrix elements..>".
- /// Exmaple : "4x4[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]" - ///
- public static Matrix4 Parse(string value) - { - Regex r = new(regularExp, RegexOptions.Singleline | RegexOptions.IgnorePatternWhitespace); - Match m = r.Match(value); - if (m.Success) - { - return new Matrix4( - float.Parse(m.Result("${m11}")), - float.Parse(m.Result("${m12}")), - float.Parse(m.Result("${m13}")), - float.Parse(m.Result("${m14}")), - - float.Parse(m.Result("${m21}")), - float.Parse(m.Result("${m22}")), - float.Parse(m.Result("${m23}")), - float.Parse(m.Result("${m24}")), - - float.Parse(m.Result("${m31}")), - float.Parse(m.Result("${m32}")), - float.Parse(m.Result("${m33}")), - float.Parse(m.Result("${m34}")), - - float.Parse(m.Result("${m41}")), - float.Parse(m.Result("${m42}")), - float.Parse(m.Result("${m43}")), - float.Parse(m.Result("${m44}")) - ); - } - else - { - throw new Exception("Unsuccessful Match."); - } - } - /// - /// Converts the specified string to its equivalent. - /// A return value indicates whether the conversion succeeded or failed. - /// - /// A string representation of a . - /// - /// When this method returns, if the conversion succeeded, - /// contains a representing the vector specified by . - /// - /// if value was converted successfully; otherwise, . - /// - /// The string should be in the following form: "4x4..matrix elements..>".
- /// Exmaple : "4x4[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]" - ///
- public static bool TryParse(string value, out Matrix4 result) - { - Regex r = new(regularExp, RegexOptions.Singleline); - Match m = r.Match(value); - if (m.Success) - { - result = new Matrix4( - float.Parse(m.Result("${m11}")), - float.Parse(m.Result("${m12}")), - float.Parse(m.Result("${m13}")), - float.Parse(m.Result("${m14}")), - - float.Parse(m.Result("${m21}")), - float.Parse(m.Result("${m22}")), - float.Parse(m.Result("${m23}")), - float.Parse(m.Result("${m24}")), - - float.Parse(m.Result("${m31}")), - float.Parse(m.Result("${m32}")), - float.Parse(m.Result("${m33}")), - float.Parse(m.Result("${m34}")), - - float.Parse(m.Result("${m41}")), - float.Parse(m.Result("${m42}")), - float.Parse(m.Result("${m43}")), - float.Parse(m.Result("${m44}")) - ); - - return true; - } - - result = Matrix4.Zero; - return false; - } - #endregion - - #region Public Static Matrix Arithmetics - /// - /// Adds two matrices. - /// - /// A instance. - /// A instance. - /// A new instance containing the sum. - public static Matrix4 Add(Matrix4 left, Matrix4 right) - { - return new Matrix4( - left.M11 + right.M11, left.M12 + right.M12, left.M13 + right.M13, left.M14 + right.M14, - left.M21 + right.M21, left.M22 + right.M22, left.M23 + right.M23, left.M24 + right.M24, - left.M31 + right.M31, left.M32 + right.M32, left.M33 + right.M33, left.M34 + right.M34, - left.M41 + right.M41, left.M42 + right.M42, left.M43 + right.M43, left.M44 + right.M44 - ); - } - /// - /// Adds a matrix and a scalar. - /// - /// A instance. - /// A single-precision floating-point number. - /// A new instance containing the sum. - public static Matrix4 Add(Matrix4 matrix, float scalar) - { - return new Matrix4( - matrix.M11 + scalar, matrix.M12 + scalar, matrix.M13 + scalar, matrix.M14 + scalar, - matrix.M21 + scalar, matrix.M22 + scalar, matrix.M23 + scalar, matrix.M24 + scalar, - matrix.M31 + scalar, matrix.M32 + scalar, matrix.M33 + scalar, matrix.M34 + scalar, - matrix.M41 + scalar, matrix.M42 + scalar, matrix.M43 + scalar, matrix.M44 + scalar - ); - } - /// - /// Adds two matrices and put the result in a third matrix. - /// - /// A instance. - /// A instance. - /// A instance to hold the result. - public static void Add(Matrix4 left, Matrix4 right, ref Matrix4 result) - { - result.M11 = left.M11 + right.M11; - result.M12 = left.M12 + right.M12; - result.M13 = left.M13 + right.M13; - result.M14 = left.M14 + right.M14; - - result.M21 = left.M21 + right.M21; - result.M22 = left.M22 + right.M22; - result.M23 = left.M23 + right.M23; - result.M24 = left.M24 + right.M24; - - result.M31 = left.M31 + right.M31; - result.M32 = left.M32 + right.M32; - result.M33 = left.M33 + right.M33; - result.M34 = left.M34 + right.M34; - - result.M41 = left.M41 + right.M41; - result.M42 = left.M42 + right.M42; - result.M43 = left.M43 + right.M43; - result.M44 = left.M44 + right.M44; - } - /// - /// Adds a matrix and a scalar and put the result in a third matrix. - /// - /// A instance. - /// A single-precision floating-point number. - /// A instance to hold the result. - public static void Add(Matrix4 matrix, float scalar, ref Matrix4 result) - { - result.M11 = matrix.M11 + scalar; - result.M12 = matrix.M12 + scalar; - result.M13 = matrix.M13 + scalar; - result.M14 = matrix.M14 + scalar; - - result.M21 = matrix.M21 + scalar; - result.M22 = matrix.M22 + scalar; - result.M23 = matrix.M23 + scalar; - result.M24 = matrix.M24 + scalar; - - result.M31 = matrix.M31 + scalar; - result.M32 = matrix.M32 + scalar; - result.M33 = matrix.M33 + scalar; - result.M34 = matrix.M34 + scalar; - - result.M41 = matrix.M41 + scalar; - result.M42 = matrix.M42 + scalar; - result.M43 = matrix.M43 + scalar; - result.M44 = matrix.M44 + scalar; - } - /// - /// Subtracts a matrix from a matrix. - /// - /// A instance to subtract from. - /// A instance to subtract. - /// A new instance containing the difference. - /// result[x][y] = left[x][y] - right[x][y] - public static Matrix4 Subtract(Matrix4 left, Matrix4 right) - { - return new Matrix4( - left.M11 - right.M11, left.M12 - right.M12, left.M13 - right.M13, left.M14 - right.M14, - left.M21 - right.M21, left.M22 - right.M22, left.M23 - right.M23, left.M24 - right.M24, - left.M31 - right.M31, left.M32 - right.M32, left.M33 - right.M33, left.M34 - right.M34, - left.M41 - right.M41, left.M42 - right.M42, left.M43 - right.M43, left.M44 - right.M44 - ); - } - /// - /// Subtracts a scalar from a matrix. - /// - /// A instance. - /// A single-precision floating-point number. - /// A new instance containing the difference. - public static Matrix4 Subtract(Matrix4 matrix, float scalar) - { - return new Matrix4( - matrix.M11 - scalar, matrix.M12 - scalar, matrix.M13 - scalar, matrix.M14 - scalar, - matrix.M21 - scalar, matrix.M22 - scalar, matrix.M23 - scalar, matrix.M24 - scalar, - matrix.M31 - scalar, matrix.M32 - scalar, matrix.M33 - scalar, matrix.M34 - scalar, - matrix.M41 - scalar, matrix.M42 - scalar, matrix.M43 - scalar, matrix.M44 - scalar - ); - } - /// - /// Subtracts a matrix from a matrix and put the result in a third matrix. - /// - /// A instance to subtract from. - /// A instance to subtract. - /// A instance to hold the result. - /// result[x][y] = left[x][y] - right[x][y] - public static void Subtract(Matrix4 left, Matrix4 right, ref Matrix4 result) - { - result.M11 = left.M11 - right.M11; - result.M12 = left.M12 - right.M12; - result.M13 = left.M13 - right.M13; - result.M14 = left.M14 - right.M14; - - result.M21 = left.M21 - right.M21; - result.M22 = left.M22 - right.M22; - result.M23 = left.M23 - right.M23; - result.M24 = left.M24 - right.M24; - - result.M31 = left.M31 - right.M31; - result.M32 = left.M32 - right.M32; - result.M33 = left.M33 - right.M33; - result.M34 = left.M34 - right.M34; - - result.M41 = left.M41 - right.M41; - result.M42 = left.M42 - right.M42; - result.M43 = left.M43 - right.M43; - result.M44 = left.M44 - right.M44; - } - /// - /// Subtracts a scalar from a matrix and put the result in a third matrix. - /// - /// A instance. - /// A single-precision floating-point number. - /// A instance to hold the result. - public static void Subtract(Matrix4 matrix, float scalar, ref Matrix4 result) - { - result.M11 = matrix.M11 - scalar; - result.M12 = matrix.M12 - scalar; - result.M13 = matrix.M13 - scalar; - result.M14 = matrix.M14 - scalar; - - result.M21 = matrix.M21 - scalar; - result.M22 = matrix.M22 - scalar; - result.M23 = matrix.M23 - scalar; - result.M24 = matrix.M24 - scalar; - - result.M31 = matrix.M31 - scalar; - result.M32 = matrix.M32 - scalar; - result.M33 = matrix.M33 - scalar; - result.M34 = matrix.M34 - scalar; - - result.M41 = matrix.M41 - scalar; - result.M42 = matrix.M42 - scalar; - result.M43 = matrix.M43 - scalar; - result.M44 = matrix.M44 - scalar; - } - /// - /// Multiplies two matrices. - /// - /// A instance. - /// A instance. - /// A new instance containing the result. - public static Matrix4 Multiply(Matrix4 left, Matrix4 right) - { - return new Matrix4( - left.M11 * right.M11 + left.M12 * right.M21 + left.M13 * right.M31 + left.M14 * right.M41, - left.M11 * right.M12 + left.M12 * right.M22 + left.M13 * right.M32 + left.M14 * right.M42, - left.M11 * right.M13 + left.M12 * right.M23 + left.M13 * right.M33 + left.M14 * right.M43, - left.M11 * right.M14 + left.M12 * right.M24 + left.M13 * right.M34 + left.M14 * right.M44, - - left.M21 * right.M11 + left.M22 * right.M21 + left.M23 * right.M31 + left.M24 * right.M41, - left.M21 * right.M12 + left.M22 * right.M22 + left.M23 * right.M32 + left.M24 * right.M42, - left.M21 * right.M13 + left.M22 * right.M23 + left.M23 * right.M33 + left.M24 * right.M43, - left.M21 * right.M14 + left.M22 * right.M24 + left.M23 * right.M34 + left.M24 * right.M44, - - left.M31 * right.M11 + left.M32 * right.M21 + left.M33 * right.M31 + left.M34 * right.M41, - left.M31 * right.M12 + left.M32 * right.M22 + left.M33 * right.M32 + left.M34 * right.M42, - left.M31 * right.M13 + left.M32 * right.M23 + left.M33 * right.M33 + left.M34 * right.M43, - left.M31 * right.M14 + left.M32 * right.M24 + left.M33 * right.M34 + left.M34 * right.M44, - - left.M41 * right.M11 + left.M42 * right.M21 + left.M43 * right.M31 + left.M44 * right.M41, - left.M41 * right.M12 + left.M42 * right.M22 + left.M43 * right.M32 + left.M44 * right.M42, - left.M41 * right.M13 + left.M42 * right.M23 + left.M43 * right.M33 + left.M44 * right.M43, - left.M41 * right.M14 + left.M42 * right.M24 + left.M43 * right.M34 + left.M44 * right.M44 - ); - } - /// - /// Multiplies two matrices and put the result in a third matrix. - /// - /// A instance. - /// A instance. - /// A instance to hold the result. - public static void Multiply(Matrix4 left, Matrix4 right, ref Matrix4 result) - { - result.M11 = left.M11 * right.M11 + left.M12 * right.M21 + left.M13 * right.M31 + left.M14 * right.M41; - result.M12 = left.M11 * right.M12 + left.M12 * right.M22 + left.M13 * right.M32 + left.M14 * right.M42; - result.M13 = left.M11 * right.M13 + left.M12 * right.M23 + left.M13 * right.M33 + left.M14 * right.M43; - result.M14 = left.M11 * right.M14 + left.M12 * right.M24 + left.M13 * right.M34 + left.M14 * right.M44; - - result.M21 = left.M21 * right.M11 + left.M22 * right.M21 + left.M23 * right.M31 + left.M24 * right.M41; - result.M22 = left.M21 * right.M12 + left.M22 * right.M22 + left.M23 * right.M32 + left.M24 * right.M42; - result.M23 = left.M21 * right.M13 + left.M22 * right.M23 + left.M23 * right.M33 + left.M24 * right.M43; - result.M24 = left.M21 * right.M14 + left.M22 * right.M24 + left.M23 * right.M34 + left.M24 * right.M44; - - result.M31 = left.M31 * right.M11 + left.M32 * right.M21 + left.M33 * right.M31 + left.M34 * right.M41; - result.M32 = left.M31 * right.M12 + left.M32 * right.M22 + left.M33 * right.M32 + left.M34 * right.M42; - result.M33 = left.M31 * right.M13 + left.M32 * right.M23 + left.M33 * right.M33 + left.M34 * right.M43; - result.M34 = left.M31 * right.M14 + left.M32 * right.M24 + left.M33 * right.M34 + left.M34 * right.M44; - - result.M41 = left.M41 * right.M11 + left.M42 * right.M21 + left.M43 * right.M31 + left.M44 * right.M41; - result.M42 = left.M41 * right.M12 + left.M42 * right.M22 + left.M43 * right.M32 + left.M44 * right.M42; - result.M43 = left.M41 * right.M13 + left.M42 * right.M23 + left.M43 * right.M33 + left.M44 * right.M43; - result.M44 = left.M41 * right.M14 + left.M42 * right.M24 + left.M43 * right.M34 + left.M44 * right.M44; - } - /// - /// Transforms a given vector by a matrix. - /// - /// A instance. - /// A instance. - /// A new instance containing the result. - public static Vector4 Transform(Matrix4 matrix, Vector4 vector) - { - return new Vector4( - (matrix.M11 * vector.X) + (matrix.M12 * vector.Y) + (matrix.M13 * vector.Z) + (matrix.M14 * vector.W), - (matrix.M21 * vector.X) + (matrix.M22 * vector.Y) + (matrix.M23 * vector.Z) + (matrix.M24 * vector.W), - (matrix.M31 * vector.X) + (matrix.M32 * vector.Y) + (matrix.M33 * vector.Z) + (matrix.M34 * vector.W), - (matrix.M41 * vector.X) + (matrix.M42 * vector.Y) + (matrix.M43 * vector.Z) + (matrix.M44 * vector.W)); - } - /// - /// Transforms a given vector by a matrix and put the result in a vector. - /// - /// A instance. - /// A instance. - /// A instance to hold the result. - public static void Transform(Matrix4 matrix, Vector4 vector, ref Vector4 result) - { - result.X = (matrix.M11 * vector.X) + (matrix.M12 * vector.Y) + (matrix.M13 * vector.Z) + (matrix.M14 * vector.W); - result.Y = (matrix.M21 * vector.X) + (matrix.M22 * vector.Y) + (matrix.M23 * vector.Z) + (matrix.M24 * vector.W); - result.Z = (matrix.M31 * vector.X) + (matrix.M32 * vector.Y) + (matrix.M33 * vector.Z) + (matrix.M34 * vector.W); - result.W = (matrix.M41 * vector.X) + (matrix.M42 * vector.Y) + (matrix.M43 * vector.Z) + (matrix.M44 * vector.W); - } - /// - /// Transposes a matrix. - /// - /// A instance. - /// A new instance containing the transposed matrix. - public static Matrix4 Transpose(Matrix4 m) - { - Matrix4 t = new(m); - t.Transpose(); - return t; - } - #endregion - - #region System.Object overrides - /// - /// Returns the hashcode for this instance. - /// - /// A 32-bit signed integer hash code. - public override int GetHashCode() - { - return - _m11.GetHashCode() ^ _m12.GetHashCode() ^ _m13.GetHashCode() ^ _m14.GetHashCode() ^ - _m21.GetHashCode() ^ _m22.GetHashCode() ^ _m23.GetHashCode() ^ _m24.GetHashCode() ^ - _m31.GetHashCode() ^ _m32.GetHashCode() ^ _m33.GetHashCode() ^ _m34.GetHashCode() ^ - _m41.GetHashCode() ^ _m42.GetHashCode() ^ _m43.GetHashCode() ^ _m44.GetHashCode(); - } - /// - /// Returns a value indicating whether this instance is equal to - /// the specified object. - /// - /// An object to compare to this instance. - /// if is a and has the same values as this instance; otherwise, . - public override bool Equals(object obj) - { - if (obj is Matrix4) - { - Matrix4 m = (Matrix4)obj; - return - (_m11 == m.M11) && (_m12 == m.M12) && (_m13 == m.M13) && (_m14 == m.M14) && - (_m21 == m.M21) && (_m22 == m.M22) && (_m23 == m.M23) && (_m24 == m.M24) && - (_m31 == m.M31) && (_m32 == m.M32) && (_m33 == m.M33) && (_m34 == m.M34) && - (_m41 == m.M41) && (_m42 == m.M42) && (_m43 == m.M43) && (_m44 == m.M44); - } - return false; - } - /// - /// Returns a string representation of this object. - /// - /// A string representation of this object. - public override string ToString() - { - return - $"4x4[{_m11}, {_m12}, {_m13}, {_m14}, {_m21}, {_m22}, {_m23}, {_m24}, {_m31}, {_m32}, {_m33}, {_m34}, {_m41}, {_m42}, {_m43}, {_m44}]"; - } - #endregion - - #region Public Methods - /// - /// Calculates the determinant value of the matrix. - /// - /// The determinant value of the matrix. - public float GetDeterminant() - { - // float det = 0.0f; - // for (int col = 0; col < 4; col++) - // { - // if ((col % 2) == 0) - // det += this[0, col] * Minor(0, col).Determinant(); - // else - // det -= this[0, col] * Minor(0, col).Determinant(); - // } - // return det; - return - _m14 * _m23 * _m32 * _m41 - _m13 * _m24 * _m32 * _m41 - _m14 * _m22 * _m33 * _m41 + _m12 * _m24 * _m33 * _m41 + - _m13 * _m22 * _m34 * _m41 - _m12 * _m23 * _m34 * _m41 - _m14 * _m23 * _m31 * _m42 + _m13 * _m24 * _m31 * _m42 + - _m14 * _m21 * _m33 * _m42 - _m11 * _m24 * _m33 * _m42 - _m13 * _m21 * _m34 * _m42 + _m11 * _m23 * _m34 * _m42 + - _m14 * _m22 * _m31 * _m43 - _m12 * _m24 * _m31 * _m43 - _m14 * _m21 * _m32 * _m43 + _m11 * _m24 * _m32 * _m43 + - _m12 * _m21 * _m34 * _m43 - _m11 * _m22 * _m34 * _m43 - _m13 * _m22 * _m31 * _m44 + _m12 * _m23 * _m31 * _m44 + - _m13 * _m21 * _m32 * _m44 - _m11 * _m23 * _m32 * _m44 - _m12 * _m21 * _m33 * _m44 + _m11 * _m22 * _m33 * _m44; - } - /// - /// Transposes this matrix. - /// - public void Transpose() - { - MathFunctions.Swap(ref _m12, ref _m21); - MathFunctions.Swap(ref _m13, ref _m31); - MathFunctions.Swap(ref _m14, ref _m41); - MathFunctions.Swap(ref _m23, ref _m32); - MathFunctions.Swap(ref _m24, ref _m42); - MathFunctions.Swap(ref _m34, ref _m43); - } - #endregion - - #region Comparison Operators - /// - /// Tests whether two specified matrices are equal. - /// - /// A instance. - /// A instance. - /// if the two matrices are equal; otherwise, . - public static bool operator ==(Matrix4 left, Matrix4 right) - { - return ValueType.Equals(left, right); - } - /// - /// Tests whether two specified matrices are not equal. - /// - /// A instance. - /// A instance. - /// if the two matrices are not equal; otherwise, . - public static bool operator !=(Matrix4 left, Matrix4 right) - { - return !ValueType.Equals(left, right); - } - #endregion - - #region Binary Operators - /// - /// Adds two matrices. - /// - /// A instance. - /// A instance. - /// A new instance containing the sum. - public static Matrix4 operator +(Matrix4 left, Matrix4 right) - { - return Matrix4.Add(left, right); - } - /// - /// Adds a matrix and a scalar. - /// - /// A instance. - /// A single-precision floating-point number. - /// A new instance containing the sum. - public static Matrix4 operator +(Matrix4 matrix, float scalar) - { - return Matrix4.Add(matrix, scalar); - } - /// - /// Adds a matrix and a scalar. - /// - /// A instance. - /// A single-precision floating-point number. - /// A new instance containing the sum. - public static Matrix4 operator +(float scalar, Matrix4 matrix) - { - return Matrix4.Add(matrix, scalar); - } - /// - /// Subtracts a matrix from a matrix. - /// - /// A instance. - /// A instance. - /// A new instance containing the difference. - public static Matrix4 operator -(Matrix4 left, Matrix4 right) - { - return Matrix4.Subtract(left, right); - } - /// - /// Subtracts a scalar from a matrix. - /// - /// A instance. - /// A single-precision floating-point number. - /// A new instance containing the difference. - public static Matrix4 operator -(Matrix4 matrix, float scalar) - { - return Matrix4.Subtract(matrix, scalar); - } - /// - /// Multiplies two matrices. - /// - /// A instance. - /// A instance. - /// A new instance containing the result. - public static Matrix4 operator *(Matrix4 left, Matrix4 right) - { - return Matrix4.Multiply(left, right); - } - /// - /// Transforms a given vector by a matrix. - /// - /// A instance. - /// A instance. - /// A new instance containing the result. - public static Vector4 operator *(Matrix4 matrix, Vector4 vector) - { - Vector4 result = new(); - for (int r = 0; r < 4; ++r) - { - for (int c = 0; c < 4; ++c) - { - result[r] += matrix[r, c] * vector[c]; - } - } - - return result; - } - #endregion - - #region Indexing Operators - /// - /// Indexer allowing to access the matrix elements by an index - /// where index = 2*row + column. - /// - public unsafe float this[int index] - { - get - { - if (index < 0 || index >= 16) - throw new IndexOutOfRangeException("Invalid matrix index!"); - - fixed (float* f = &_m11) - { - return *(f + index); - } - } - set - { - if (index < 0 || index >= 16) - throw new IndexOutOfRangeException("Invalid matrix index!"); - - fixed (float* f = &_m11) - { - *(f + index) = value; - } - } - } - /// - /// Indexer allowing to access the matrix elements by row and column. - /// - public float this[int row, int column] - { - get - { - return this[(row) * 4 + (column)]; - } - set - { - this[(row) * 4 + (column)] = value; - } - } - #endregion - } -} diff --git a/Source/Framework/GameMath/Plane.cs b/Source/Framework/GameMath/Plane.cs deleted file mode 100644 index 4c832f498..000000000 --- a/Source/Framework/GameMath/Plane.cs +++ /dev/null @@ -1,260 +0,0 @@ -/* - * Copyright (C) 2012-2020 CypherCore - * Copyright (C) 2003-2004 Eran Kampf eran@ekampf.com http://www.ekampf.com - * - * 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 . - */ - -using System; -using System.Runtime.Serialization; -using System.Security.Permissions; - -namespace Framework.GameMath -{ - /// - /// An enumeration representing the sides of the plane. - /// - public enum PlaneSide - { - /// - /// Represents the plane itself. - /// - None, - /// - /// Represents the positive halfspace of the plane (the side where the normal points to). - /// - Positive, - /// - /// Represents the negative halfspace of the plane - /// - Negative - } - - /// - /// Represents a plane in 3D space. - /// - /// - /// The plane is described by a normal and a constant (N,D) which - /// denotes that the plane is consisting of points Q that - /// satisfies (N dot Q)+D = 0. - /// - [Serializable] - public struct Plane : ISerializable, ICloneable - { - #region Private Fields - private Vector3 _normal; - private float _const; - #endregion - - #region Constructors - /// - /// Initializes a new instance of the class using given normal and constant values. - /// - /// The plane's normal vector. - /// The plane's constant value. - public Plane(Vector3 normal, float constant) - { - _normal = normal; - _const = constant; - } - - /// - /// Initializes a new instance of the class using given normal and a point. - /// - /// The plane's normal vector. - /// A point on the plane in 3D space. - public Plane(Vector3 normal, Vector3 point) - { - _normal = normal; - _const = Vector3.DotProduct(normal, point); - } - - /// - /// Initializes a new instance of the class using 3 given points. - /// - /// A point on the plane in 3D space. - /// A point on the plane in 3D space. - /// A point on the plane in 3D space. - public Plane(Vector3 p0, Vector3 p1, Vector3 p2) - { - _normal = Vector3.CrossProduct(p2 - p1, p0 - p1); - _normal.Normalize(); - _const = Vector3.DotProduct(_normal, p0); - } - - /// - /// Initializes a new instance of the class using given a plane to assign values from. - /// - /// A 3D plane to assign values from. - public Plane(Plane p) - { - _normal = p.Normal; - _const = p.Constant; - } - - /// - /// Initializes a new instance of the class with serialized data. - /// - /// The object that holds the serialized object data. - /// The contextual information about the source or destination. - private Plane(SerializationInfo info, StreamingContext context) - { - _normal = (Vector3)info.GetValue("Normal", typeof(Vector3)); - _const = info.GetSingle("Constant"); - } - #endregion - - #region Constants - /// - /// Plane on the X axis. - /// - public static readonly Plane XPlane = new(Vector3.XAxis, Vector3.Zero); - /// - /// Plane on the Y axis. - /// - public static readonly Plane YPlane = new(Vector3.YAxis, Vector3.Zero); - /// - /// Plane on the Z axis. - /// - public static readonly Plane ZPlane = new(Vector3.ZAxis, Vector3.Zero); - #endregion - - #region Public Properties - /// - /// Gets or sets the plane's normal vector. - /// - public Vector3 Normal - { - get { return _normal; } - set { _normal = value; } - } - /// - /// Gets or sets the plane's constant value. - /// - public float Constant - { - get { return _const; } - set { _const = value; } - } - #endregion - - #region ICloneable Members - /// - /// Creates an exact copy of this object. - /// - /// The object this method creates, cast as an object. - object ICloneable.Clone() - { - return new Plane(this); - } - /// - /// Creates an exact copy of this object. - /// - /// The object this method creates. - public Plane Clone() - { - return new Plane(this); - } - #endregion - - #region ISerializable Members - /// - /// Populates a with the data needed to serialize the target object. - /// - /// The to populate with data. - /// The destination (see ) for this serialization. - //[SecurityPermissionAttribute(SecurityAction.Demand, SerializationFormatter = true)] - public void GetObjectData(SerializationInfo info, StreamingContext context) - { - info.AddValue("Normal", _normal, typeof(Vector3)); - info.AddValue("Constant", _const); - } - #endregion - - #region Public Methods - /// - /// Flip the plane. - /// - public void Flip() - { - _normal = -_normal; - } - /// - /// Creates a new flipped plane (-normal, constant). - /// - /// A new instance. - public Plane GetFlipped() - { - return new Plane(-_normal, _const); - } - /// - /// Get the shortest distance from a 3D vector to the plane. - /// - /// A instance. - /// A float representing the shortest distance from the given vector to the plane. - public float GetDistanceToPlane(Vector3 p) - { - return Vector3.DotProduct(p, Normal) - Constant; - } - - public void getEquation(ref Vector3 n, out float d) - { - double _d; - getEquation(ref n, out _d); - d = (float)_d; - } - - void getEquation(ref Vector3 n, out double d) - { - n = _normal; - d = -_const; - } - #endregion - - #region Overrides - /// - /// Returns the hashcode for this instance. - /// - /// A 32-bit signed integer hash code. - public override int GetHashCode() - { - return _normal.GetHashCode() ^ _const.GetHashCode(); - } - /// - /// Returns a value indicating whether this instance is equal to - /// the specified object. - /// - /// An object to compare to this instance. - /// True if is a and has the same values as this instance; otherwise, False. - public override bool Equals(object obj) - { - if (obj is Plane) - { - Plane p = (Plane)obj; - return (_normal == p.Normal) && (_const == p.Constant); - } - return false; - } - - /// - /// Returns a string representation of this object. - /// - /// A string representation of this object. - public override string ToString() - { - return $"Plane[n={_normal}, c={_const}]"; - } - #endregion - } -} diff --git a/Source/Framework/GameMath/Quaternion.cs b/Source/Framework/GameMath/Quaternion.cs deleted file mode 100644 index f466918e0..000000000 --- a/Source/Framework/GameMath/Quaternion.cs +++ /dev/null @@ -1,825 +0,0 @@ -/* - * Copyright (C) 2012-2020 CypherCore - * Copyright (C) 2003-2004 Eran Kampf eran@ekampf.com http://www.ekampf.com - * - * 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 . - */ - -using System; -using System.Runtime.InteropServices; -using System.Text.RegularExpressions; - -namespace Framework.GameMath -{ - /// - /// Represents a double-precision floating-point quaternion. - /// - /// - /// - /// A quaternion can be thought of as a 4-Dimentional vector of form: - /// q = [w, x, y, z] = w + xi + yj +zk. - /// - /// - /// A Quaternion is often written as q = s + V where S represents - /// the scalar part (w component) and V is a 3D vector representing - /// the imaginery coefficients (x,y,z components). - /// - /// - /// Check out http://mathworld.wolfram.com/Quaternion.html for further details. - /// - /// - [Serializable] - [StructLayout(LayoutKind.Sequential)] - public struct Quaternion : ICloneable - { - #region Private Fields - private float _w; - private float _x; - private float _y; - private float _z; - #endregion - - #region Constructors - /// - /// Initializes a new instance of the class with the specified coordinates. - /// - /// The quaternions's X coordinate. - /// The quaternions's Y coordinate. - /// The quaternions's Z coordinate. - /// /// The quaternions's W coordinate. - public Quaternion(float x, float y, float z, float w) - { - _w = w; - _x = x; - _y = y; - _z = z; - } - /// - /// Initializes a new instance of the class using coordinates from a given instance. - /// - /// A instance to copy the coordinates from. - public Quaternion(Quaternion quaternion) - { - _x = quaternion.X; - _y = quaternion.Y; - _z = quaternion.Z; - _w = quaternion.W; - } - public Quaternion(Matrix3 rot) : this(Zero) - { - int[] plus1mod3 = { 1, 2, 0 }; - - // Find the index of the largest diagonal component - // These ? operations hopefully compile to conditional - // move instructions instead of branches. - int i = (rot[1, 1] > rot[0, 0]) ? 2 : 1; - i = (rot[2, 2] > rot[i, i]) ? 2 : i; - - // Find the indices of the other elements - int j = plus1mod3[i]; - int k = plus1mod3[j]; - - // If we attempted to pre-normalize and trusted the matrix to be - // perfectly orthonormal, the result would be: - // - // double c = sqrt((rot[i][i] - (rot[j][j] + rot[k][k])) + 1.0) - // v[i] = -c * 0.5 - // v[j] = -(rot[i][j] + rot[j][i]) * 0.5 / c - // v[k] = -(rot[i][k] + rot[k][i]) * 0.5 / c - // w = (rot[j][k] - rot[k][j]) * 0.5 / c - // - // Since we're going to pay the sqrt anyway, we perform a post normalization, which also - // fixes any poorly normalized input. Multiply all elements by 2*c in the above, giving: - - // nc2 = -c^2 - double nc2 = ((rot[j, j] + rot[k, k]) - rot[i, i]) - 1.0; - this[i] = (float)nc2; - W = (rot[j, k] - rot[k, j]); - this[j] = -(rot[i, j] + rot[j, i]); - this[k] = -(rot[i, k] + rot[k, i]); - - // We now have the correct result with the wrong magnitude, so normalize it: - float s = (float)Math.Sqrt(X * X + Y * Y + Z * Z + W * W); - if (s > 0.00001f) - { - s = 1.0f / s; - X *= s; - Y *= s; - Z *= s; - W *= s; - } - else - { - // The quaternion is nearly zero. Make it 0 0 0 1 - X = 0.0f; - Y = 0.0f; - Z = 0.0f; - W = 1.0f; - } - } - #endregion - - #region Constants - /// - /// Double-precision floating point zero quaternion. - /// - public static readonly Quaternion Zero = new(0, 0, 0, 0); - /// - /// Double-precision floating point identity quaternion. - /// - public static readonly Quaternion Identity = new(0, 0, 0, 1); - /// - /// Double-precision floating point X-Axis quaternion. - /// - public static readonly Quaternion XAxis = new(1, 0, 0, 0); - /// - /// Double-precision floating point Y-Axis quaternion. - /// - public static readonly Quaternion YAxis = new(0, 1, 0, 0); - /// - /// Double-precision floating point Z-Axis quaternion. - /// - public static readonly Quaternion ZAxis = new(0, 0, 1, 0); - /// - /// Double-precision floating point W-Axis quaternion. - /// - public static readonly Quaternion WAxis = new(0, 0, 0, 1); - #endregion - - #region Public Properties - /// - /// Gets or sets the x-coordinate of this quaternion. - /// - /// The x-coordinate of this quaternion. - public float X - { - get { return _x; } - set { _x = value; } - } - /// - /// Gets or sets the y-coordinate of this quaternion. - /// - /// The y-coordinate of this quaternion. - public float Y - { - get { return _y; } - set { _y = value; } - } - /// - /// Gets or sets the z-coordinate of this quaternion. - /// - /// The z-coordinate of this quaternion. - public float Z - { - get { return _z; } - set { _z = value; } - } - /// - /// Gets or sets the w-coordinate of this quaternion. - /// - /// The w-coordinate of this quaternion. - public float W - { - get { return _w; } - set { _w = value; } - } - - /// - /// Gets the the modulus of the quaternion. - /// - /// A double-precision floating-point number. - public float Modulus - { - get - { - return (float)System.Math.Sqrt(_w * _w + _x * _x + _y * _y + _z * _z); - } - } - /// - /// Gets the the squared modulus of the quaternion. - /// - /// A double-precision floating-point number. - public float ModulusSquared - { - get - { - return (_w * _w + _x * _x + _y * _y + _z * _z); - } - } - /// - /// Gets or sets the conjugate of the quaternion. - /// - /// A instance. - public Quaternion Conjugate - { - get - { - return new Quaternion(-_x, -_y, -_z, _w); - } - set - { - this = value.Conjugate; - } - } - #endregion - - #region ICloneable Members - /// - /// Creates an exact copy of this object. - /// - /// The object this method creates, cast as an object. - object ICloneable.Clone() - { - return new Quaternion(this); - } - /// - /// Creates an exact copy of this object. - /// - /// The object this method creates. - public Quaternion Clone() - { - return new Quaternion(this); - } - #endregion - - #region Public Static Parse Methods - /// - /// Converts the specified string to its equivalent. - /// - /// A string representation of a - /// A that represents the vector specified by the parameter. - public static Quaternion Parse(string value) - { - Regex r = new(@"\((?.*),(?.*),(?.*),(?.*)\)", RegexOptions.None); - Match m = r.Match(value); - if (m.Success) - { - return new Quaternion( - float.Parse(m.Result("${x}")), - float.Parse(m.Result("${y}")), - float.Parse(m.Result("${z}")), - float.Parse(m.Result("${w}")) - ); - } - else - { - throw new Exception("Unsuccessful Match."); - } - } - /// - /// Converts the specified string to its equivalent. - /// A return value indicates whether the conversion succeeded or failed. - /// - /// A string representation of a . - /// - /// When this method returns, if the conversion succeeded, - /// contains a representing the vector specified by . - /// - /// if value was converted successfully; otherwise, . - public static bool TryParse(string value, out Quaternion result) - { - Regex r = new(@"\((?.*),(?.*),(?.*),(?.*)\)", RegexOptions.None); - Match m = r.Match(value); - if (m.Success) - { - result = new Quaternion( - float.Parse(m.Result("${x}")), - float.Parse(m.Result("${y}")), - float.Parse(m.Result("${z}")), - float.Parse(m.Result("${w}")) - ); - - return true; - } - - result = Quaternion.Zero; - return false; - } - #endregion - - #region Public Static Quaternion Arithmetics - /// - /// Adds two quaternions. - /// - /// A instance. - /// A instance. - /// A new instance containing the sum. - public static Quaternion Add(Quaternion left, Quaternion right) - { - return new Quaternion(left.X + right.X, left.Y + right.Y, left.Z + right.Z, left.W + right.W); - } - /// - /// Adds two quaternions and put the result in the third quaternion. - /// - /// A instance. - /// A instance. - /// A instance to hold the result. - public static void Add(Quaternion left, Quaternion right, ref Quaternion result) - { - result.X = left.X + right.X; - result.Y = left.Y + right.Y; - result.Z = left.Z + right.Z; - result.W = left.W + right.W; - } - - /// - /// Subtracts a quaternion from a quaternion. - /// - /// A instance. - /// A instance. - /// A new instance containing the difference. - public static Quaternion Subtract(Quaternion left, Quaternion right) - { - return new Quaternion(left.X - right.X, left.Y - right.Y, left.Z - right.Z, left.W - right.W); - } - /// - /// Subtracts a quaternion from a quaternion and puts the result into a third quaternion. - /// - /// A instance. - /// A instance. - /// A instance to hold the result. - public static void Subtract(Quaternion left, Quaternion right, ref Quaternion result) - { - result.X = left.X - right.X; - result.Y = left.Y - right.Y; - result.Z = left.Z - right.Z; - result.W = left.W - right.W; - } - - /// - /// Multiplies quaternion by quaternion . - /// - /// A instance. - /// A instance. - /// A new containing the result. - public static Quaternion Multiply(Quaternion left, Quaternion right) - { - Quaternion result = new(); - result.X = left.W * right.X + left.X * right.W + left.Y * right.Z - left.Z * right.Y; - result.Y = left.W * right.Y + left.Y * right.W + left.Z * right.X - left.X * right.Z; - result.Z = left.W * right.Z + left.Z * right.W + left.X * right.Y - left.Y * right.X; - result.W = left.W * right.W - left.X * right.X - left.Y * right.Y - left.Z * right.Z; - - return result; - } - /// - /// Multiplies quaternion by quaternion and put the result in a third quaternion. - /// - /// A instance. - /// A instance. - /// A instance to hold the result. - public static void Multiply(Quaternion left, Quaternion right, ref Quaternion result) - { - result.X = left.W * right.X + left.X * right.W + left.Y * right.Z - left.Z * right.Y; - result.Y = left.W * right.Y + left.Y * right.W + left.Z * right.X - left.X * right.Z; - result.Z = left.W * right.Z + left.Z * right.W + left.X * right.Y - left.Y * right.X; - result.W = left.W * right.W - left.X * right.X - left.Y * right.Y - left.Z * right.Z; - } - /// - /// Multiplies a quaternion by a scalar. - /// - /// A instance. - /// A scalar. - /// A instance to hold the result. - public static Quaternion Multiply(Quaternion quaternion, float scalar) - { - Quaternion result = new(quaternion); - result.X *= scalar; - result.Y *= scalar; - result.Z *= scalar; - result.W *= scalar; - - return result; - } - /// - /// Multiplies a quaternion by a scalar and put the result in a third quaternion. - /// - /// A instance. - /// A scalar. - /// A instance to hold the result. - public static void Multiply(Quaternion quaternion, float scalar, ref Quaternion result) - { - result.X = quaternion.X * scalar; - result.Y = quaternion.Y * scalar; - result.Z = quaternion.Z * scalar; - result.W = quaternion.W * scalar; - } - - /// - /// Divides a quaternion by a scalar. - /// - /// A instance. - /// A scalar. - /// A instance to hold the result. - public static Quaternion Divide(Quaternion quaternion, float scalar) - { - if (scalar == 0) - { - throw new DivideByZeroException("Dividing quaternion by zero"); - } - - Quaternion result = new(quaternion); - result.X /= scalar; - result.Y /= scalar; - result.Z /= scalar; - result.W /= scalar; - - return result; - } - /// - /// Divides a quaternion by a scalar and put the result in a third quaternion. - /// - /// A instance. - /// A scalar. - /// A instance to hold the result. - public static void Divide(Quaternion quaternion, float scalar, ref Quaternion result) - { - if (scalar == 0) - { - throw new DivideByZeroException("Dividing quaternion by zero"); - } - - result.X = quaternion.X / scalar; - result.Y = quaternion.Y / scalar; - result.Z = quaternion.Z / scalar; - result.W = quaternion.W / scalar; - } - - /// - /// Calculates the dot product of two quaternions. - /// - /// A instance. - /// A instance. - /// The dot product value. - public static double DotProduct(Quaternion left, Quaternion right) - { - return left.X * right.X + left.Y * right.Y + left.Z * right.Z + left.W * right.W; - } - #endregion - - public bool isUnit(double tolerance = 1e-5) - { - return Math.Abs(dot(this) - 1.0f) < tolerance; - } - - public Quaternion ToUnit() - { - Quaternion copyOfThis = this; - copyOfThis.unitize(); - return copyOfThis; - } - - public void unitize() - { - this *= rsq(dot(this)); - } - - float dot(Quaternion other) - { - return (float)((X * other.X) + (Y * other.Y) + (Z * other.Z) + (W * other.W)); - } - - float rsq(float x) - { - return 1.0f / (float)Math.Sqrt(x); - } - - public static Quaternion fromEulerAnglesZYX(float z, float y, float x) - { - return new Quaternion(Matrix3.fromEulerAnglesZYX(z, y, x)); - } - - #region Public Static Complex Special Functions - /// - /// Calculates the logarithm of a given quaternion. - /// - /// A instance. - /// The quaternion's logarithm. - public static Quaternion Log(Quaternion quaternion) - { - Quaternion result = new(0, 0, 0, 0); - - if (Math.Abs(quaternion.W) < 1.0) - { - float angle = (float)System.Math.Acos(quaternion.W); - float sin = (float)System.Math.Sin(angle); - - if (Math.Abs(sin) >= 0) - { - float coeff = angle / sin; - result.X = coeff * quaternion.X; - result.Y = coeff * quaternion.Y; - result.Z = coeff * quaternion.Z; - } - else - { - result.X = quaternion.X; - result.Y = quaternion.Y; - result.Z = quaternion.Z; - } - } - - return result; - } - /// - /// Calculates the exponent of a quaternion. - /// - /// A instance. - /// The quaternion's exponent. - public Quaternion Exp(Quaternion quaternion) - { - Quaternion result = new(0, 0, 0, 0); - - float angle = (float)System.Math.Sqrt(quaternion.X * quaternion.X + quaternion.Y * quaternion.Y + quaternion.Z * quaternion.Z); - float sin = (float)System.Math.Sin(angle); - - if (Math.Abs(sin) > 0) - { - float coeff = angle / sin; - result.X = coeff * quaternion.X; - result.Y = coeff * quaternion.Y; - result.Z = coeff * quaternion.Z; - } - else - { - result.X = quaternion.X; - result.Y = quaternion.Y; - result.Z = quaternion.Z; - } - - return result; - } - #endregion - - #region Public Methods - /// - /// Inverts the quaternion. - /// - public void Inverse() - { - float norm = ModulusSquared; - if (norm > 0) - { - float invNorm = 1.0f / norm; - _w *= invNorm; - _x *= -invNorm; - _y *= -invNorm; - _z *= -invNorm; - } - else - { - throw new Exception("Quaternion " + ToString() + " is not invertable"); - } - } - /// - /// Normelizes the quaternion. - /// - public void Normalize() - { - float norm = Modulus; - if (norm == 0) - { - throw new DivideByZeroException("Trying to normalize a quaternion with modulus of zero."); - } - - _w /= norm; - _x /= norm; - _y /= norm; - _z /= norm; - } - /// - /// Clamps quaternion values to zero using a given tolerance value. - /// - /// The tolerance to use. - /// - /// The quaternion values that are close to zero within the given tolerance are set to zero. - /// - public void ClampZero(float tolerance) - { - _x = MathFunctions.Clamp(_x, 0, tolerance); - _y = MathFunctions.Clamp(_y, 0, tolerance); - _z = MathFunctions.Clamp(_z, 0, tolerance); - _w = MathFunctions.Clamp(_w, 0, tolerance); - } - /// - /// Clamps quaternion values to zero using the default tolerance value. - /// - /// - /// The quaternion values that are close to zero within the given tolerance are set to zero. - /// The tolerance value used is - /// - public void ClampZero() - { - _x = MathFunctions.Clamp(_x, 0); - _y = MathFunctions.Clamp(_y, 0); - _z = MathFunctions.Clamp(_z, 0); - _w = MathFunctions.Clamp(_w, 0); - } - #endregion - - #region System.Object Overrides - /// - /// Returns the hashcode for this instance. - /// - /// A 32-bit signed integer hash code. - public override int GetHashCode() - { - return _w.GetHashCode() ^ _x.GetHashCode() ^ _y.GetHashCode() ^ _z.GetHashCode(); - } - /// - /// Returns a value indicating whether this instance is equal to - /// the specified object. - /// - /// An object to compare to this instance. - /// if is a and has the same values as this instance; otherwise, . - public override bool Equals(object obj) - { - if (obj is Quaternion) - { - Quaternion quaternion = (Quaternion)obj; - return (_w == quaternion.W) && (_x == quaternion.X) && (_y == quaternion.Y) && (_z == quaternion.Z); - } - return false; - } - /// - /// Returns a string representation of this object. - /// - /// A string representation of this object. - public override string ToString() - { - return $"({_w}, {_x}, {_y}, {_z})"; - } - #endregion - - #region Comparison Operators - /// - /// Tests whether two specified quaternions are equal. - /// - /// The left-hand quaternion. - /// The right-hand quaternion. - /// if the two quaternions are equal; otherwise, . - public static bool operator ==(Quaternion left, Quaternion right) - { - return ValueType.Equals(left, right); - } - /// - /// Tests whether two specified quaternions are not equal. - /// - /// The left-hand quaternion. - /// The right-hand quaternion. - /// if the two quaternions are not equal; otherwise, . - public static bool operator !=(Quaternion left, Quaternion right) - { - return !ValueType.Equals(left, right); - } - #endregion - - #region Binary Operators - /// - /// Adds two quaternions. - /// - /// A instance. - /// A instance. - /// A new instance containing the sum. - public static Quaternion operator +(Quaternion left, Quaternion right) - { - return Quaternion.Add(left, right); - } - /// - /// Subtracts a quaternion from a quaternion. - /// - /// A instance. - /// A instance. - /// A new instance containing the difference. - public static Quaternion operator -(Quaternion left, Quaternion right) - { - return Quaternion.Subtract(left, right); - } - /// - /// Multiplies quaternion by quaternion . - /// - /// A instance. - /// A instance. - /// A new containing the result. - public static Quaternion operator *(Quaternion left, Quaternion right) - { - return Quaternion.Multiply(left, right); - } - /// - /// Multiplies a quaternion by a scalar. - /// - /// A instance. - /// A scalar. - /// A instance to hold the result. - public static Quaternion operator *(Quaternion quaternion, float scalar) - { - return Quaternion.Multiply(quaternion, scalar); - } - /// - /// Multiplies a quaternion by a scalar. - /// - /// A instance. - /// A scalar. - /// A instance to hold the result. - public static Quaternion operator *(float scalar, Quaternion quaternion) - { - return Quaternion.Multiply(quaternion, scalar); - } - /// - /// Divides a quaternion by a scalar. - /// - /// A instance. - /// A scalar. - /// A instance to hold the result. - public static Quaternion operator /(Quaternion quaternion, float scalar) - { - return Quaternion.Divide(quaternion, scalar); - } - /// - /// Divides a scalar by a quaternion. - /// - /// A instance. - /// A scalar. - /// A instance to hold the result. - public static Quaternion operator /(float scalar, Quaternion quaternion) - { - return Quaternion.Multiply(quaternion, (1.0f / scalar)); - } - #endregion - - #region Array Indexing Operator - /// - /// Indexer ( [w, x, y, z] ). - /// - public float this[int index] - { - get - { - switch (index) - { - case 0: - return _x; - case 1: - return _y; - case 2: - return _z; - case 3: - return _w; - default: - throw new IndexOutOfRangeException(); - } - } - set - { - switch (index) - { - case 0: - _x = value; - break; - case 1: - _y = value; - break; - case 2: - _z = value; - break; - case 3: - _w = value; - break; - default: - throw new IndexOutOfRangeException(); - } - return; - } - } - #endregion - - #region Conversion Operators - /// - /// Converts the quaternion to an array of double-precision floating point numbers. - /// - /// A instance. - /// An array of double-precision floating point numbers. - /// The array is [w, x, y, z]. - public static explicit operator double[] (Quaternion quaternion) - { - double[] doubles = new double[4]; - doubles[1] = quaternion.X; - doubles[2] = quaternion.Y; - doubles[3] = quaternion.Z; - doubles[0] = quaternion.W; - return doubles; - } - #endregion - } -} diff --git a/Source/Framework/GameMath/Ray.cs b/Source/Framework/GameMath/Ray.cs index 9d880dace..9879554f6 100644 --- a/Source/Framework/GameMath/Ray.cs +++ b/Source/Framework/GameMath/Ray.cs @@ -18,6 +18,7 @@ using System; using System.ComponentModel; +using System.Numerics; using System.Text.RegularExpressions; namespace Framework.GameMath @@ -33,6 +34,8 @@ namespace Framework.GameMath public struct Ray : ICloneable { #region Private Fields + private static Vector3 _inf = new(float.PositiveInfinity, float.PositiveInfinity, float.PositiveInfinity); + private Vector3 _origin; private Vector3 _direction; #endregion @@ -110,8 +113,8 @@ namespace Framework.GameMath if (m.Success) { return new Ray( - Vector3.Parse(m.Result("${origin}")), - Vector3.Parse(m.Result("${direction}")) + m.Result("${origin}").ParseVector3(), + m.Result("${direction}").ParseVector3() ); } else @@ -194,18 +197,14 @@ namespace Framework.GameMath public Vector3 intersection(Plane plane) { - float d; - Vector3 normal = plane.Normal; - plane.getEquation(ref normal, out d); - float rate = Direction.dot(normal); - + float rate = Vector3.Dot(Direction, plane.Normal); if (rate >= 0.0f) { - return Vector3.Inf; + return _inf; } else { - float t = -(d + Origin.dot(normal)) / rate; + float t = -(-plane.D + Vector3.Dot(Origin, plane.Normal)) / rate; return Origin + Direction * t; } } diff --git a/Source/Framework/GameMath/Vector2.cs b/Source/Framework/GameMath/Vector2.cs deleted file mode 100644 index d34f8cde8..000000000 --- a/Source/Framework/GameMath/Vector2.cs +++ /dev/null @@ -1,947 +0,0 @@ -/* - * Copyright (C) 2012-2020 CypherCore - * Copyright (C) 2003-2004 Eran Kampf eran@ekampf.com http://www.ekampf.com - * - * 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 . - */ - -using System; -using System.Collections.Generic; -using System.ComponentModel; -using System.Diagnostics; -using System.Runtime.InteropServices; -using System.Text.RegularExpressions; - -namespace Framework.GameMath -{ - /// - /// Represents 2-Dimentional vector of single-precision floating point numbers. - /// - [Serializable] - [StructLayout(LayoutKind.Sequential)] - [TypeConverter(typeof(Vector2FConverter))] - public struct Vector2 : ICloneable - { - #region Private fields - private float _x; - private float _y; - #endregion - - #region Constructors - /// - /// Initializes a new instance of the class with the specified coordinates. - /// - /// The vector's X coordinate. - /// The vector's Y coordinate. - public Vector2(float x, float y) - { - _x = x; - _y = y; - } - /// - /// Initializes a new instance of the class with the specified coordinates. - /// - /// An array containing the coordinate parameters. - public Vector2(float[] coordinates) - { - Debug.Assert(coordinates != null); - Debug.Assert(coordinates.Length >= 2); - - _x = coordinates[0]; - _y = coordinates[1]; - } - /// - /// Initializes a new instance of the class with the specified coordinates. - /// - /// An array containing the coordinate parameters. - public Vector2(List coordinates) - { - Debug.Assert(coordinates != null); - Debug.Assert(coordinates.Count >= 2); - - _x = coordinates[0]; - _y = coordinates[1]; - } - /// - /// Initializes a new instance of the class using coordinates from a given instance. - /// - /// A to get the coordinates from. - public Vector2(Vector2 vector) - { - _x = vector.X; - _y = vector.Y; - } - #endregion - - #region Constants - /// - /// 4-Dimentional single-precision floating point zero vector. - /// - public static readonly Vector2 Zero = new(0.0f, 0.0f); - /// - /// 4-Dimentional single-precision floating point X-Axis vector. - /// - public static readonly Vector2 XAxis = new(1.0f, 0.0f); - /// - /// 4-Dimentional single-precision floating point Y-Axis vector. - /// - public static readonly Vector2 YAxis = new(0.0f, 1.0f); - #endregion - - #region Public properties - /// - /// Gets or sets the x-coordinate of this vector. - /// - /// The x-coordinate of this vector. - public float X - { - get { return _x; } - set { _x = value; } - } - /// - /// Gets or sets the y-coordinate of this vector. - /// - /// The y-coordinate of this vector. - public float Y - { - get { return _y; } - set { _y = value; } - } - #endregion - - #region ICloneable Members - /// - /// Creates an exact copy of this object. - /// - /// The object this method creates, cast as an object. - object ICloneable.Clone() - { - return new Vector2(this); - } - /// - /// Creates an exact copy of this object. - /// - /// The object this method creates. - public Vector2 Clone() - { - return new Vector2(this); - } - #endregion - - #region Public Static Parse Methods - /// - /// Converts the specified string to its equivalent. - /// - /// A string representation of a . - /// A that represents the vector specified by the parameter. - public static Vector2 Parse(string value) - { - Regex r = new(@"\((?.*),(?.*)\)", RegexOptions.Singleline); - Match m = r.Match(value); - if (m.Success) - { - return new Vector2( - float.Parse(m.Result("${x}")), - float.Parse(m.Result("${y}")) - ); - } - else - { - throw new Exception("Unsuccessful Match."); - } - } - /// - /// Converts the specified string to its equivalent. - /// A return value indicates whether the conversion succeeded or failed. - /// - /// A string representation of a . - /// - /// When this method returns, if the conversion succeeded, - /// contains a representing the vector specified by . - /// - /// if value was converted successfully; otherwise, . - public static bool TryParse(string value, out Vector2 result) - { - Regex r = new(@"\((?.*),(?.*)\)", RegexOptions.Singleline); - Match m = r.Match(value); - if (m.Success) - { - result = new Vector2( - float.Parse(m.Result("${x}")), - float.Parse(m.Result("${y}")) - ); - - return true; - } - - result = Vector2.Zero; - return false; - } - #endregion - - #region Public Static Vector Arithmetics - /// - /// Adds two vectors. - /// - /// A instance. - /// A instance. - /// A new instance containing the sum. - public static Vector2 Add(Vector2 left, Vector2 right) - { - return new Vector2(left.X + right.X, left.Y + right.Y); - } - /// - /// Adds a vector and a scalar. - /// - /// A instance. - /// A single-precision floating-point number. - /// A new instance containing the sum. - public static Vector2 Add(Vector2 vector, float scalar) - { - return new Vector2(vector.X + scalar, vector.Y + scalar); - } - /// - /// Adds two vectors and put the result in the third vector. - /// - /// A instance. - /// A instance - /// A instance to hold the result. - public static void Add(Vector2 left, Vector2 right, ref Vector2 result) - { - result.X = left.X + right.X; - result.Y = left.Y + right.Y; - } - /// - /// Adds a vector and a scalar and put the result into another vector. - /// - /// A instance. - /// A single-precision floating-point number. - /// A instance to hold the result. - public static void Add(Vector2 vector, float scalar, ref Vector2 result) - { - result.X = vector.X + scalar; - result.Y = vector.Y + scalar; - } - /// - /// Subtracts a vector from a vector. - /// - /// A instance. - /// A instance. - /// A new instance containing the difference. - /// - /// result[i] = left[i] - right[i]. - /// - public static Vector2 Subtract(Vector2 left, Vector2 right) - { - return new Vector2(left.X - right.X, left.Y - right.Y); - } - /// - /// Subtracts a scalar from a vector. - /// - /// A instance. - /// A single-precision floating-point number. - /// A new instance containing the difference. - /// - /// result[i] = vector[i] - scalar - /// - public static Vector2 Subtract(Vector2 vector, float scalar) - { - return new Vector2(vector.X - scalar, vector.Y - scalar); - } - /// - /// Subtracts a vector from a scalar. - /// - /// A instance. - /// A single-precision floating-point number. - /// A new instance containing the difference. - /// - /// result[i] = scalar - vector[i] - /// - public static Vector2 Subtract(float scalar, Vector2 vector) - { - return new Vector2(scalar - vector.X, scalar - vector.Y); - } - /// - /// Subtracts a vector from a second vector and puts the result into a third vector. - /// - /// A instance. - /// A instance - /// A instance to hold the result. - /// - /// result[i] = left[i] - right[i]. - /// - public static void Subtract(Vector2 left, Vector2 right, ref Vector2 result) - { - result.X = left.X - right.X; - result.Y = left.Y - right.Y; - } - /// - /// Subtracts a vector from a scalar and put the result into another vector. - /// - /// A instance. - /// A single-precision floating-point number. - /// A instance to hold the result. - /// - /// result[i] = vector[i] - scalar - /// - public static void Subtract(Vector2 vector, float scalar, ref Vector2 result) - { - result.X = vector.X - scalar; - result.Y = vector.Y - scalar; - } - /// - /// Subtracts a scalar from a vector and put the result into another vector. - /// - /// A instance. - /// A single-precision floating-point number. - /// A instance to hold the result. - /// - /// result[i] = scalar - vector[i] - /// - public static void Subtract(float scalar, Vector2 vector, ref Vector2 result) - { - result.X = scalar - vector.X; - result.Y = scalar - vector.Y; - } - /// - /// Divides a vector by another vector. - /// - /// A instance. - /// A instance. - /// A new containing the quotient. - /// - /// result[i] = left[i] / right[i]. - /// - public static Vector2 Divide(Vector2 left, Vector2 right) - { - return new Vector2(left.X / right.X, left.Y / right.Y); - } - /// - /// Divides a vector by a scalar. - /// - /// A instance. - /// A scalar - /// A new containing the quotient. - /// - /// result[i] = vector[i] / scalar; - /// - public static Vector2 Divide(Vector2 vector, float scalar) - { - return new Vector2(vector.X / scalar, vector.Y / scalar); - } - /// - /// Divides a scalar by a vector. - /// - /// A instance. - /// A scalar - /// A new containing the quotient. - /// - /// result[i] = scalar / vector[i] - /// - public static Vector2 Divide(float scalar, Vector2 vector) - { - return new Vector2(scalar / vector.X, scalar / vector.Y); - } - /// - /// Divides a vector by another vector. - /// - /// A instance. - /// A instance. - /// A instance to hold the result. - /// - /// result[i] = left[i] / right[i] - /// - public static void Divide(Vector2 left, Vector2 right, ref Vector2 result) - { - result.X = left.X / right.X; - result.Y = left.Y / right.Y; - } - /// - /// Divides a vector by a scalar. - /// - /// A instance. - /// A scalar - /// A instance to hold the result. - /// - /// result[i] = vector[i] / scalar - /// - public static void Divide(Vector2 vector, float scalar, ref Vector2 result) - { - result.X = vector.X / scalar; - result.Y = vector.Y / scalar; - } - /// - /// Divides a scalar by a vector. - /// - /// A instance. - /// A scalar - /// A instance to hold the result. - /// - /// result[i] = scalar / vector[i] - /// - public static void Divide(float scalar, Vector2 vector, ref Vector2 result) - { - result.X = scalar / vector.X; - result.Y = scalar / vector.Y; - } - /// - /// Multiplies a vector by a scalar. - /// - /// A instance. - /// A single-precision floating-point number. - /// A new containing the result. - public static Vector2 Multiply(Vector2 vector, float scalar) - { - return new Vector2(vector.X * scalar, vector.Y * scalar); - } - /// - /// Multiplies a vector by a scalar and put the result in another vector. - /// - /// A instance. - /// A single-precision floating-point number. - /// A instance to hold the result. - public static void Multiply(Vector2 vector, float scalar, ref Vector2 result) - { - result.X = vector.X * scalar; - result.Y = vector.Y * scalar; - } - /// - /// Calculates the dot product of two vectors. - /// - /// A instance. - /// A instance. - /// The dot product value. - public static float DotProduct(Vector2 left, Vector2 right) - { - return (left.X * right.X) + (left.Y * right.Y); - } - /// - /// Calculates the Kross product of two vectors. - /// - /// A instance. - /// A instance. - /// The Kross product value. - /// - ///

- /// The Kross product is defined as: - /// Kross(u,v) = u.X*v.Y - u.Y*v.X. - ///

- ///

- /// The operation is related to the cross product in 3D given by (x0, y0, 0) X (x1, y1, 0) = (0, 0, Kross((x0, y0), (x1, y1))). - /// The operation has the property that Kross(u, v) = -Kross(v, u). - ///

- ///
- public static float KrossProduct(Vector2 left, Vector2 right) - { - return (left.X * right.Y) - (left.Y * right.X); - } - /// - /// Negates a vector. - /// - /// A instance. - /// A new instance containing the negated values. - public static Vector2 Negate(Vector2 vector) - { - return new Vector2(-vector.X, -vector.Y); - } - /// - /// Tests whether two vectors are approximately equal using default tolerance value. - /// - /// A instance. - /// A instance. - /// if the two vectors are approximately equal; otherwise, . - public static bool ApproxEqual(Vector2 left, Vector2 right) - { - return ApproxEqual(left, right, MathFunctions.Epsilon); - } - /// - /// Tests whether two vectors are approximately equal given a tolerance value. - /// - /// A instance. - /// A instance. - /// The tolerance value used to test approximate equality. - /// if the two vectors are approximately equal; otherwise, . - public static bool ApproxEqual(Vector2 left, Vector2 right, float tolerance) - { - return - ( - (System.Math.Abs(left.X - right.X) <= tolerance) && - (System.Math.Abs(left.Y - right.Y) <= tolerance) - ); - } - #endregion - - #region Public Methods - /// - /// Scale the vector so that its length is 1. - /// - public void Normalize() - { - float length = GetLength(); - if (length == 0) - { - throw new DivideByZeroException("Trying to normalize a vector with length of zero."); - } - - _x /= length; - _y /= length; - } - /// - /// Calculates the length of the vector. - /// - /// Returns the length of the vector. (Sqrt(X*X + Y*Y)) - public float GetLength() - { - return (float)System.Math.Sqrt(_x * _x + _y * _y); - } - /// - /// Calculates the squared length of the vector. - /// - /// Returns the squared length of the vector. (X*X + Y*Y) - public float GetLengthSquared() - { - return (_x * _x + _y * _y); - } - /// - /// Clamps vector values to zero using a given tolerance value. - /// - /// The tolerance to use. - /// - /// The vector values that are close to zero within the given tolerance are set to zero. - /// - public void ClampZero(float tolerance) - { - _x = MathFunctions.Clamp(_x, 0, tolerance); - _y = MathFunctions.Clamp(_y, 0, tolerance); - } - /// - /// Clamps vector values to zero using the default tolerance value. - /// - /// - /// The vector values that are close to zero within the given tolerance are set to zero. - /// The tolerance value used is - /// - public void ClampZero() - { - _x = MathFunctions.Clamp(_x, 0); - _y = MathFunctions.Clamp(_y, 0); - } - #endregion - - #region System.Object Overrides - /// - /// Returns the hashcode for this instance. - /// - /// A 32-bit signed integer hash code. - public override int GetHashCode() - { - return _x.GetHashCode() ^ _y.GetHashCode(); - } - /// - /// Returns a value indicating whether this instance is equal to - /// the specified object. - /// - /// An object to compare to this instance. - /// if is a and has the same values as this instance; otherwise, . - public override bool Equals(object obj) - { - if (obj is Vector2) - { - Vector2 v = (Vector2)obj; - return (_x == v.X) && (_y == v.Y); - } - return false; - } - /// - /// Returns a string representation of this object. - /// - /// A string representation of this object. - public override string ToString() - { - return $"({_x}, {_y})"; - } - #endregion - - #region Comparison Operators - /// - /// Tests whether two specified vectors are equal. - /// - /// A instance. - /// A instance. - /// if the two vectors are equal; otherwise, . - public static bool operator ==(Vector2 left, Vector2 right) - { - return ValueType.Equals(left, right); - } - /// - /// Tests whether two specified vectors are not equal. - /// - /// A instance. - /// A instance. - /// if the two vectors are not equal; otherwise, . - public static bool operator !=(Vector2 left, Vector2 right) - { - return !ValueType.Equals(left, right); - } - - /// - /// Tests if a vector's components are greater than another vector's components. - /// - /// A instance. - /// A instance. - /// if the left-hand vector's components are greater than the right-hand vector's component; otherwise, . - public static bool operator >(Vector2 left, Vector2 right) - { - return ( - (left._x > right._x) && - (left._y > right._y)); - } - /// - /// Tests if a vector's components are smaller than another vector's components. - /// - /// A instance. - /// A instance. - /// if the left-hand vector's components are smaller than the right-hand vector's component; otherwise, . - public static bool operator <(Vector2 left, Vector2 right) - { - return ( - (left._x < right._x) && - (left._y < right._y)); - } - /// - /// Tests if a vector's components are greater or equal than another vector's components. - /// - /// A instance. - /// A instance. - /// if the left-hand vector's components are greater or equal than the right-hand vector's component; otherwise, . - public static bool operator >=(Vector2 left, Vector2 right) - { - return ( - (left._x >= right._x) && - (left._y >= right._y)); - } - /// - /// Tests if a vector's components are smaller or equal than another vector's components. - /// - /// A instance. - /// A instance. - /// if the left-hand vector's components are smaller or equal than the right-hand vector's component; otherwise, . - public static bool operator <=(Vector2 left, Vector2 right) - { - return ( - (left._x <= right._x) && - (left._y <= right._y)); - } - #endregion - - #region Unary Operators - /// - /// Negates the values of the given vector. - /// - /// A instance. - /// A new instance containing the negated values. - public static Vector2 operator -(Vector2 vector) - { - return Vector2.Negate(vector); - } - #endregion - - #region Binary Operators - /// - /// Adds two vectors. - /// - /// A instance. - /// A instance. - /// A new instance containing the sum. - public static Vector2 operator +(Vector2 left, Vector2 right) - { - return Vector2.Add(left, right); - } - /// - /// Adds a vector and a scalar. - /// - /// A instance. - /// A single-precision floating-point number. - /// A new instance containing the sum. - public static Vector2 operator +(Vector2 vector, float scalar) - { - return Vector2.Add(vector, scalar); - } - /// - /// Adds a vector and a scalar. - /// - /// A instance. - /// A single-precision floating-point number. - /// A new instance containing the sum. - public static Vector2 operator +(float scalar, Vector2 vector) - { - return Vector2.Add(vector, scalar); - } - /// - /// Subtracts a vector from a vector. - /// - /// A instance. - /// A instance. - /// A new instance containing the difference. - /// - /// result[i] = left[i] - right[i]. - /// - public static Vector2 operator -(Vector2 left, Vector2 right) - { - return Vector2.Subtract(left, right); - } - /// - /// Subtracts a scalar from a vector. - /// - /// A instance. - /// A single-precision floating-point number. - /// A new instance containing the difference. - /// - /// result[i] = vector[i] - scalar - /// - public static Vector2 operator -(Vector2 vector, float scalar) - { - return Vector2.Subtract(vector, scalar); - } - /// - /// Subtracts a vector from a scalar. - /// - /// A instance. - /// A single-precision floating-point number. - /// A new instance containing the difference. - /// - /// result[i] = scalar - vector[i] - /// - public static Vector2 operator -(float scalar, Vector2 vector) - { - return Vector2.Subtract(scalar, vector); - } - /// - /// Multiplies a vector by a scalar. - /// - /// A instance. - /// A single-precision floating-point number. - /// A new containing the result. - public static Vector2 operator *(Vector2 vector, float scalar) - { - return Vector2.Multiply(vector, scalar); - } - /// - /// Multiplies a vector by a scalar. - /// - /// A instance. - /// A single-precision floating-point number. - /// A new containing the result. - public static Vector2 operator *(float scalar, Vector2 vector) - { - return Vector2.Multiply(vector, scalar); - } - /// - /// Divides a vector by a scalar. - /// - /// A instance. - /// A scalar - /// A new containing the quotient. - /// - /// result[i] = vector[i] / scalar; - /// - public static Vector2 operator /(Vector2 vector, float scalar) - { - return Vector2.Divide(vector, scalar); - } - /// - /// Divides a scalar by a vector. - /// - /// A instance. - /// A scalar - /// A new containing the quotient. - /// - /// result[i] = scalar / vector[i] - /// - public static Vector2 operator /(float scalar, Vector2 vector) - { - return Vector2.Divide(scalar, vector); - } - #endregion - - #region Array Indexing Operator - /// - /// Indexer ( [x, y] ). - /// - public float this[int index] - { - get - { - switch (index) - { - case 0: - return _x; - case 1: - return _y; - default: - throw new IndexOutOfRangeException(); - } - } - set - { - switch (index) - { - case 0: - _x = value; - break; - case 1: - _y = value; - break; - default: - throw new IndexOutOfRangeException(); - } - } - - } - #endregion - - #region Conversion Operators - /// - /// Converts the vector to an array of single-precision floating point values. - /// - /// A instance. - /// An array of single-precision floating point values. - public static explicit operator float[] (Vector2 vector) - { - float[] array = new float[2]; - array[0] = vector.X; - array[1] = vector.Y; - return array; - } - /// - /// Converts the vector to a of single-precision floating point values. - /// - /// A instance. - /// A of single-precision floating point values. - public static explicit operator List(Vector2 vector) - { - List list = new(); - list.Add(vector.X); - list.Add(vector.Y); - - return list; - } - /// - /// Converts the vector to a of single-precision floating point values. - /// - /// A instance. - /// A of single-precision floating point values. - public static explicit operator LinkedList(Vector2 vector) - { - LinkedList list = new(); - list.AddLast(vector.X); - list.AddLast(vector.Y); - - return list; - } - #endregion - } - - #region Vector2FConverter class - /// - /// Converts a to and from string representation. - /// - public class Vector2FConverter : ExpandableObjectConverter - { - /// - /// Returns whether this converter can convert an object of the given type to the type of this converter, using the specified context. - /// - /// An that provides a format context. - /// A that represents the type you want to convert from. - /// true if this converter can perform the conversion; otherwise, false. - public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType) - { - if (sourceType == typeof(string)) - return true; - - return base.CanConvertFrom(context, sourceType); - } - /// - /// Returns whether this converter can convert the object to the specified type, using the specified context. - /// - /// An that provides a format context. - /// A that represents the type you want to convert to. - /// true if this converter can perform the conversion; otherwise, false. - public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType) - { - if (destinationType == typeof(string)) - return true; - - return base.CanConvertTo(context, destinationType); - } - /// - /// Converts the given value object to the specified type, using the specified context and culture information. - /// - /// An that provides a format context. - /// A object. If a null reference (Nothing in Visual Basic) is passed, the current culture is assumed. - /// The to convert. - /// The Type to convert the parameter to. - /// An that represents the converted value. - public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destinationType) - { - if ((destinationType == typeof(string)) && (value is Vector2)) - { - Vector2 v = (Vector2)value; - return v.ToString(); - } - - return base.ConvertTo(context, culture, value, destinationType); - } - /// - /// Converts the given object to the type of this converter, using the specified context and culture information. - /// - /// An that provides a format context. - /// The to use as the current culture. - /// The to convert. - /// An that represents the converted value. - public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) - { - if (value.GetType() == typeof(string)) - { - return Vector2.Parse((string)value); - } - - return base.ConvertFrom(context, culture, value); - } - - /// - /// Returns whether this object supports a standard set of values that can be picked from a list. - /// - /// An that provides a format context. - /// true if should be called to find a common set of values the object supports; otherwise, false. - public override bool GetStandardValuesSupported(ITypeDescriptorContext context) - { - return true; - } - - /// - /// Returns a collection of standard values for the data type this type converter is designed for when provided with a format context. - /// - /// An that provides a format context that can be used to extract additional information about the environment from which this converter is invoked. This parameter or properties of this parameter can be a null reference. - /// A that holds a standard set of valid values, or a null reference (Nothing in Visual Basic) if the data type does not support a standard set of values. - public override System.ComponentModel.TypeConverter.StandardValuesCollection GetStandardValues(ITypeDescriptorContext context) - { - StandardValuesCollection svc = - new(new object[3] { Vector2.Zero, Vector2.XAxis, Vector2.YAxis }); - - return svc; - } - } - #endregion -} diff --git a/Source/Framework/GameMath/Vector3.cs b/Source/Framework/GameMath/Vector3.cs deleted file mode 100644 index 3131dfe90..000000000 --- a/Source/Framework/GameMath/Vector3.cs +++ /dev/null @@ -1,1098 +0,0 @@ -/* - * Copyright (C) 2012-2020 CypherCore - * Copyright (C) 2003-2004 Eran Kampf eran@ekampf.com http://www.ekampf.com - * - * 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 . - */ - -using System; -using System.Collections.Generic; -using System.ComponentModel; -using System.Diagnostics; -using System.Runtime.InteropServices; -using System.Text.RegularExpressions; - -namespace Framework.GameMath -{ - /// - /// Represents 3-Dimentional vector of single-precision floating point numbers. - /// - [Serializable] - [StructLayout(LayoutKind.Sequential)] - //[TypeConverter(typeof(Vector3Converter))] - public struct Vector3 : ICloneable - { - #region Constructors - /// - /// Initializes a new instance of the class with the specified coordinates. - /// - /// The vector's X coordinate. - /// The vector's Y coordinate. - /// The vector's Z coordinate. - public Vector3(float x, float y, float z) - { - X = x; - Y = y; - Z = z; - } - /// - /// Initializes a new instance of the class with the specified coordinates. - /// - /// An array containing the coordinate parameters. - public Vector3(float[] coordinates) - { - Debug.Assert(coordinates != null); - Debug.Assert(coordinates.Length >= 3); - - X = coordinates[0]; - Y = coordinates[1]; - Z = coordinates[2]; - } - /// - /// Initializes a new instance of the class with the specified coordinates. - /// - /// An array containing the coordinate parameters. - public Vector3(List coordinates) - { - Debug.Assert(coordinates != null); - Debug.Assert(coordinates.Count >= 3); - - X = coordinates[0]; - Y = coordinates[1]; - Z = coordinates[2]; - } - /// - /// Initializes a new instance of the class using coordinates from a given instance. - /// - /// A to get the coordinates from. - public Vector3(Vector3 vector) - { - X = vector.X; - Y = vector.Y; - Z = vector.Z; - } - #endregion - - #region Constants - /// - /// 4-Dimentional single-precision floating point zero vector. - /// - public static readonly Vector3 Zero = new(0.0f, 0.0f, 0.0f); - - public static readonly Vector3 One = new(1.0f, 1.0f, 1.0f); - - public static readonly Vector3 Inf = new(float.PositiveInfinity, float.PositiveInfinity, float.PositiveInfinity); - /// - /// 4-Dimentional single-precision floating point X-Axis vector. - /// - public static readonly Vector3 XAxis = new(1.0f, 0.0f, 0.0f); - /// - /// 4-Dimentional single-precision floating point Y-Axis vector. - /// - public static readonly Vector3 YAxis = new(0.0f, 1.0f, 0.0f); - /// - /// 4-Dimentional single-precision floating point Y-Axis vector. - /// - public static readonly Vector3 ZAxis = new(0.0f, 0.0f, 1.0f); - #endregion - - #region Public properties - /// - /// Gets or sets the x-coordinate of this vector. - /// - /// The x-coordinate of this vector. - public float X; - /// - /// Gets or sets the y-coordinate of this vector. - /// - /// The y-coordinate of this vector. - public float Y; - /// - /// Gets or sets the z-coordinate of this vector. - /// - /// The z-coordinate of this vector. - public float Z; - #endregion - - #region ICloneable Members - /// - /// Creates an exact copy of this object. - /// - /// The object this method creates, cast as an object. - object ICloneable.Clone() - { - return new Vector3(this); - } - /// - /// Creates an exact copy of this object. - /// - /// The object this method creates. - public Vector3 Clone() - { - return new Vector3(this); - } - #endregion - - #region Public Static Parse Methods - /// - /// Converts the specified string to its equivalent. - /// - /// A string representation of a . - /// A that represents the vector specified by the parameter. - public static Vector3 Parse(string value) - { - Regex r = new(@"\((?.*),(?.*),(?.*)\)", RegexOptions.Singleline); - Match m = r.Match(value); - if (m.Success) - { - return new Vector3( - float.Parse(m.Result("${x}")), - float.Parse(m.Result("${y}")), - float.Parse(m.Result("${z}")) - ); - } - else - { - throw new Exception("Unsuccessful Match."); - } - } - /// - /// Converts the specified string to its equivalent. - /// A return value indicates whether the conversion succeeded or failed. - /// - /// A string representation of a . - /// - /// When this method returns, if the conversion succeeded, - /// contains a representing the vector specified by . - /// - /// if value was converted successfully; otherwise, . - public static bool TryParse(string value, out Vector3 result) - { - Regex r = new(@"\((?.*),(?.*),(?.*)\)", RegexOptions.Singleline); - Match m = r.Match(value); - if (m.Success) - { - result = new Vector3( - float.Parse(m.Result("${x}")), - float.Parse(m.Result("${y}")), - float.Parse(m.Result("${z}")) - ); - - return true; - } - - result = Vector3.Zero; - return false; - } - #endregion - - #region Public Static Vector Arithmetics - /// - /// Adds two vectors. - /// - /// A instance. - /// A instance. - /// A new instance containing the sum. - public static Vector3 Add(Vector3 left, Vector3 right) - { - return new Vector3(left.X + right.X, left.Y + right.Y, left.Z + right.Z); - } - /// - /// Adds a vector and a scalar. - /// - /// A instance. - /// A single-precision floating-point number. - /// A new instance containing the sum. - public static Vector3 Add(Vector3 vector, float scalar) - { - return new Vector3(vector.X + scalar, vector.Y + scalar, vector.Z + scalar); - } - /// - /// Adds two vectors and put the result in the third vector. - /// - /// A instance. - /// A instance - /// A instance to hold the result. - public static void Add(Vector3 left, Vector3 right, ref Vector3 result) - { - result.X = left.X + right.X; - result.Y = left.Y + right.Y; - result.Z = left.Z + right.Z; - } - /// - /// Adds a vector and a scalar and put the result into another vector. - /// - /// A instance. - /// A single-precision floating-point number. - /// A instance to hold the result. - public static void Add(Vector3 vector, float scalar, ref Vector3 result) - { - result.X = vector.X + scalar; - result.Y = vector.Y + scalar; - result.Z = vector.Z + scalar; - } - /// - /// Subtracts a vector from a vector. - /// - /// A instance. - /// A instance. - /// A new instance containing the difference. - /// - /// result[i] = left[i] - right[i]. - /// - public static Vector3 Subtract(Vector3 left, Vector3 right) - { - return new Vector3(left.X - right.X, left.Y - right.Y, left.Z - right.Z); - } - /// - /// Subtracts a scalar from a vector. - /// - /// A instance. - /// A single-precision floating-point number. - /// A new instance containing the difference. - /// - /// result[i] = vector[i] - scalar - /// - public static Vector3 Subtract(Vector3 vector, float scalar) - { - return new Vector3(vector.X - scalar, vector.Y - scalar, vector.Z - scalar); - } - /// - /// Subtracts a vector from a scalar. - /// - /// A instance. - /// A single-precision floating-point number. - /// A new instance containing the difference. - /// - /// result[i] = scalar - vector[i] - /// - public static Vector3 Subtract(float scalar, Vector3 vector) - { - return new Vector3(scalar - vector.X, scalar - vector.Y, scalar - vector.Z); - } - /// - /// Subtracts a vector from a second vector and puts the result into a third vector. - /// - /// A instance. - /// A instance - /// A instance to hold the result. - /// - /// result[i] = left[i] - right[i]. - /// - public static void Subtract(Vector3 left, Vector3 right, ref Vector3 result) - { - result.X = left.X - right.X; - result.Y = left.Y - right.Y; - result.Z = left.Z - right.Z; - } - /// - /// Subtracts a vector from a scalar and put the result into another vector. - /// - /// A instance. - /// A single-precision floating-point number. - /// A instance to hold the result. - /// - /// result[i] = vector[i] - scalar - /// - public static void Subtract(Vector3 vector, float scalar, ref Vector3 result) - { - result.X = vector.X - scalar; - result.Y = vector.Y - scalar; - result.Z = vector.Z - scalar; - } - /// - /// Subtracts a scalar from a vector and put the result into another vector. - /// - /// A instance. - /// A single-precision floating-point number. - /// A instance to hold the result. - /// - /// result[i] = scalar - vector[i] - /// - public static void Subtract(float scalar, Vector3 vector, ref Vector3 result) - { - result.X = scalar - vector.X; - result.Y = scalar - vector.Y; - result.Z = scalar - vector.Z; - } - /// - /// Divides a vector by another vector. - /// - /// A instance. - /// A instance. - /// A new containing the quotient. - /// - /// result[i] = left[i] / right[i]. - /// - public static Vector3 Divide(Vector3 left, Vector3 right) - { - return new Vector3(left.X / right.X, left.Y / right.Y, left.Z / right.Z); - } - /// - /// Divides a vector by a scalar. - /// - /// A instance. - /// A scalar - /// A new containing the quotient. - /// - /// result[i] = vector[i] / scalar; - /// - public static Vector3 Divide(Vector3 vector, float scalar) - { - return new Vector3(vector.X / scalar, vector.Y / scalar, vector.Z / scalar); - } - /// - /// Divides a scalar by a vector. - /// - /// A instance. - /// A scalar - /// A new containing the quotient. - /// - /// result[i] = scalar / vector[i] - /// - public static Vector3 Divide(float scalar, Vector3 vector) - { - return new Vector3(scalar / vector.X, scalar / vector.Y, scalar / vector.Z); - } - /// - /// Divides a vector by another vector. - /// - /// A instance. - /// A instance. - /// A instance to hold the result. - /// - /// result[i] = left[i] / right[i] - /// - public static void Divide(Vector3 left, Vector3 right, ref Vector3 result) - { - result.X = left.X / right.X; - result.Y = left.Y / right.Y; - result.Z = left.Z / right.Z; - } - /// - /// Divides a vector by a scalar. - /// - /// A instance. - /// A scalar - /// A instance to hold the result. - /// - /// result[i] = vector[i] / scalar - /// - public static void Divide(Vector3 vector, float scalar, ref Vector3 result) - { - result.X = vector.X / scalar; - result.Y = vector.Y / scalar; - result.Z = vector.Z / scalar; - } - /// - /// Divides a scalar by a vector. - /// - /// A instance. - /// A scalar - /// A instance to hold the result. - /// - /// result[i] = scalar / vector[i] - /// - public static void Divide(float scalar, Vector3 vector, ref Vector3 result) - { - result.X = scalar / vector.X; - result.Y = scalar / vector.Y; - result.Z = scalar / vector.Z; - } - /// - /// Multiplies a vector by a scalar. - /// - /// A instance. - /// A single-precision floating-point number. - /// A new containing the result. - public static Vector3 Multiply(Vector3 vector, float scalar) - { - return new Vector3(vector.X * scalar, vector.Y * scalar, vector.Z * scalar); - } - /// - /// Multiplies a vector by a scalar and put the result in another vector. - /// - /// A instance. - /// A single-precision floating-point number. - /// A instance to hold the result. - public static void Multiply(Vector3 vector, float scalar, ref Vector3 result) - { - result.X = vector.X * scalar; - result.Y = vector.Y * scalar; - result.Z = vector.Z * scalar; - } - /// - /// Calculates the dot product of two vectors. - /// - /// A instance. - /// A instance. - /// The dot product value. - public static float DotProduct(Vector3 left, Vector3 right) - { - return (left.X * right.X) + (left.Y * right.Y) + (left.Z * right.Z); - } - /// - /// Calculates the cross product of two vectors. - /// - /// A instance. - /// A instance. - /// A new containing the cross product result. - public static Vector3 CrossProduct(Vector3 left, Vector3 right) - { - return new Vector3( - left.Y * right.Z - left.Z * right.Y, - left.Z * right.X - left.X * right.Z, - left.X * right.Y - left.Y * right.X); - } - /// - /// Calculates the cross product of two vectors. - /// - /// A instance. - /// A instance. - /// A instance to hold the cross product result. - public static void CrossProduct(Vector3 left, Vector3 right, ref Vector3 result) - { - result.X = left.Y * right.Z - left.Z * right.Y; - result.Y = left.Z * right.X - left.X * right.Z; - result.Z = left.X * right.Y - left.Y * right.X; - } - /// - /// Negates a vector. - /// - /// A instance. - /// A new instance containing the negated values. - public static Vector3 Negate(Vector3 vector) - { - return new Vector3(-vector.X, -vector.Y, -vector.Z); - } - /// - /// Tests whether two vectors are approximately equal using default tolerance value. - /// - /// A instance. - /// A instance. - /// if the two vectors are approximately equal; otherwise, . - public static bool ApproxEqual(Vector3 left, Vector3 right) - { - return ApproxEqual(left, right, MathFunctions.Epsilon); - } - /// - /// Tests whether two vectors are approximately equal given a tolerance value. - /// - /// A instance. - /// A instance. - /// The tolerance value used to test approximate equality. - /// if the two vectors are approximately equal; otherwise, . - public static bool ApproxEqual(Vector3 left, Vector3 right, float tolerance) - { - return - ( - (System.Math.Abs(left.X - right.X) <= tolerance) && - (System.Math.Abs(left.Y - right.Y) <= tolerance) && - (System.Math.Abs(left.Z - right.Z) <= tolerance) - ); - } - #endregion - - #region Public Methods - /// - /// Scale the vector so that its length is 1. - /// - public void Normalize() - { - float length = GetLength(); - if (length == 0) - { - throw new DivideByZeroException("Trying to normalize a vector with length of zero."); - } - - X /= length; - Y /= length; - Z /= length; - } - /// - /// Calculates the length of the vector. - /// - /// Returns the length of the vector. (Sqrt(X*X + Y*Y)) - public float GetLength() - { - return (float)System.Math.Sqrt(X * X + Y * Y + Z * Z); - } - /// - /// Calculates the squared length of the vector. - /// - /// Returns the squared length of the vector. (X*X + Y*Y) - public float GetLengthSquared() - { - return (X * X + Y * Y + Z * Z); - } - /// - /// Clamps vector values to zero using a given tolerance value. - /// - /// The tolerance to use. - /// - /// The vector values that are close to zero within the given tolerance are set to zero. - /// - public void ClampZero(float tolerance) - { - X = MathFunctions.Clamp(X, 0, tolerance); - Y = MathFunctions.Clamp(Y, 0, tolerance); - Z = MathFunctions.Clamp(Z, 0, tolerance); - } - /// - /// Clamps vector values to zero using the default tolerance value. - /// - /// - /// The vector values that are close to zero within the given tolerance are set to zero. - /// The tolerance value used is - /// - public void ClampZero() - { - X = MathFunctions.Clamp(X, 0); - Y = MathFunctions.Clamp(Y, 0); - Z = MathFunctions.Clamp(Z, 0); - } - #endregion - - #region System.Object Overrides - /// - /// Returns the hashcode for this instance. - /// - /// A 32-bit signed integer hash code. - public override int GetHashCode() - { - return X.GetHashCode() ^ Y.GetHashCode() ^ Z.GetHashCode(); - } - /// - /// Returns a value indicating whether this instance is equal to - /// the specified object. - /// - /// An object to compare to this instance. - /// if is a and has the same values as this instance; otherwise, . - public override bool Equals(object obj) - { - if (obj is Vector3) - { - Vector3 v = (Vector3)obj; - return (X == v.X) && (Y == v.Y) && (Z == v.Z); - } - return false; - } - /// - /// Returns a string representation of this object. - /// - /// A string representation of this object. - public override string ToString() - { - return $"({X}, {Y}, {Z})"; - } - #endregion - - #region Comparison Operators - /// - /// Tests whether two specified vectors are equal. - /// - /// A instance. - /// A instance. - /// if the two vectors are equal; otherwise, . - public static bool operator ==(Vector3 left, Vector3 right) - { - return ValueType.Equals(left, right); - } - /// - /// Tests whether two specified vectors are not equal. - /// - /// A instance. - /// A instance. - /// if the two vectors are not equal; otherwise, . - public static bool operator !=(Vector3 left, Vector3 right) - { - return !ValueType.Equals(left, right); - } - - /// - /// Tests if a vector's components are greater than another vector's components. - /// - /// A instance. - /// A instance. - /// if the left-hand vector's components are greater than the right-hand vector's component; otherwise, . - public static bool operator >(Vector3 left, Vector3 right) - { - return ( - (left.X > right.X) && - (left.Y > right.Y) && - (left.Z > right.Z)); - } - /// - /// Tests if a vector's components are smaller than another vector's components. - /// - /// A instance. - /// A instance. - /// if the left-hand vector's components are smaller than the right-hand vector's component; otherwise, . - public static bool operator <(Vector3 left, Vector3 right) - { - return ( - (left.X < right.X) && - (left.Y < right.Y) && - (left.Z < right.Z)); - } - /// - /// Tests if a vector's components are greater or equal than another vector's components. - /// - /// A instance. - /// A instance. - /// if the left-hand vector's components are greater or equal than the right-hand vector's component; otherwise, . - public static bool operator >=(Vector3 left, Vector3 right) - { - return ( - (left.X >= right.X) && - (left.Y >= right.Y) && - (left.Z >= right.Z)); - } - /// - /// Tests if a vector's components are smaller or equal than another vector's components. - /// - /// A instance. - /// A instance. - /// if the left-hand vector's components are smaller or equal than the right-hand vector's component; otherwise, . - public static bool operator <=(Vector3 left, Vector3 right) - { - return ( - (left.X <= right.X) && - (left.Y <= right.Y) && - (left.Z <= right.Z)); - } - #endregion - - #region Unary Operators - /// - /// Negates the values of the given vector. - /// - /// A instance. - /// A new instance containing the negated values. - public static Vector3 operator -(Vector3 vector) - { - return Vector3.Negate(vector); - } - #endregion - - #region Binary Operators - /// - /// Adds two vectors. - /// - /// A instance. - /// A instance. - /// A new instance containing the sum. - public static Vector3 operator +(Vector3 left, Vector3 right) - { - return Vector3.Add(left, right); - } - /// - /// Adds a vector and a scalar. - /// - /// A instance. - /// A single-precision floating-point number. - /// A new instance containing the sum. - public static Vector3 operator +(Vector3 vector, float scalar) - { - return Vector3.Add(vector, scalar); - } - /// - /// Adds a vector and a scalar. - /// - /// A instance. - /// A single-precision floating-point number. - /// A new instance containing the sum. - public static Vector3 operator +(float scalar, Vector3 vector) - { - return Vector3.Add(vector, scalar); - } - /// - /// Subtracts a vector from a vector. - /// - /// A instance. - /// A instance. - /// A new instance containing the difference. - /// - /// result[i] = left[i] - right[i]. - /// - public static Vector3 operator -(Vector3 left, Vector3 right) - { - return Vector3.Subtract(left, right); - } - /// - /// Subtracts a scalar from a vector. - /// - /// A instance. - /// A single-precision floating-point number. - /// A new instance containing the difference. - /// - /// result[i] = vector[i] - scalar - /// - public static Vector3 operator -(Vector3 vector, float scalar) - { - return Vector3.Subtract(vector, scalar); - } - /// - /// Subtracts a vector from a scalar. - /// - /// A instance. - /// A single-precision floating-point number. - /// A new instance containing the difference. - /// - /// result[i] = scalar - vector[i] - /// - public static Vector3 operator -(float scalar, Vector3 vector) - { - return Vector3.Subtract(scalar, vector); - } - /// - /// Multiplies a vector by a scalar. - /// - /// A instance. - /// A single-precision floating-point number. - /// A new containing the result. - public static Vector3 operator *(Vector3 vector, float scalar) - { - return Vector3.Multiply(vector, scalar); - } - /// - /// Multiplies a vector by a scalar. - /// - /// A instance. - /// A single-precision floating-point number. - /// A new containing the result. - public static Vector3 operator *(float scalar, Vector3 vector) - { - return Vector3.Multiply(vector, scalar); - } - /// - /// Divides a vector by a scalar. - /// - /// A instance. - /// A scalar - /// A new containing the quotient. - /// - /// result[i] = vector[i] / scalar; - /// - public static Vector3 operator /(Vector3 vector, float scalar) - { - return Vector3.Divide(vector, scalar); - } - /// - /// Divides a scalar by a vector. - /// - /// A instance. - /// A scalar - /// A new containing the quotient. - /// - /// result[i] = scalar / vector[i] - /// - public static Vector3 operator /(float scalar, Vector3 vector) - { - return Vector3.Divide(scalar, vector); - } - #endregion - - #region Array Indexing Operator - /// - /// Indexer ( [x, y, z] ). - /// - public float this[int index] - { - get - { - switch (index) - { - case 0: - return X; - case 1: - return Y; - case 2: - return Z; - default: - throw new IndexOutOfRangeException(); - } - } - set - { - switch (index) - { - case 0: - X = value; - break; - case 1: - Y = value; - break; - case 2: - Z = value; - break; - default: - throw new IndexOutOfRangeException(); - } - } - - } - public float this[uint index] - { - get - { - switch (index) - { - case 0: - return X; - case 1: - return Y; - case 2: - return Z; - default: - throw new IndexOutOfRangeException(); - } - } - set - { - switch (index) - { - case 0: - X = value; - break; - case 1: - Y = value; - break; - case 2: - Z = value; - break; - default: - throw new IndexOutOfRangeException(); - } - } - - } - #endregion - - #region Conversion Operators - /// - /// Converts the vector to an array of single-precision floating point values. - /// - /// A instance. - /// An array of single-precision floating point values. - public static explicit operator float[] (Vector3 vector) - { - float[] array = new float[3]; - array[0] = vector.X; - array[1] = vector.Y; - array[2] = vector.Z; - return array; - } - /// - /// Converts the vector to a of single-precision floating point values. - /// - /// A instance. - /// A of single-precision floating point values. - public static explicit operator List(Vector3 vector) - { - List list = new(3); - list.Add(vector.X); - list.Add(vector.Y); - list.Add(vector.Z); - - return list; - } - /// - /// Converts the vector to a of single-precision floating point values. - /// - /// A instance. - /// A of single-precision floating point values. - public static explicit operator LinkedList(Vector3 vector) - { - LinkedList list = new(); - list.AddLast(vector.X); - list.AddLast(vector.Y); - list.AddLast(vector.Z); - - return list; - } - #endregion - - public float magnitude() - { - return (float)Math.Sqrt(X * X + Y * Y + Z * Z); - } - public float dot(Vector3 rkVector) - { - return X * rkVector.X + Y * rkVector.Y + Z * rkVector.Z; - } - - public Vector3 cross(Vector3 rkVector) - { - return new Vector3(Y * rkVector.Z - Z * rkVector.Y, Z * rkVector.X - X * rkVector.Z, - X * rkVector.Y - Y * rkVector.X); - } - - public Vector3 Min(Vector3 v) - { - return new Vector3(Math.Min(v.X, X), Math.Min(v.Y, Y), Math.Min(v.Z, Z)); - } - - public Vector3 Max(Vector3 v) - { - return new Vector3(Math.Max(v.X, X), Math.Max(v.Y, Y), Math.Max(v.Z, Z)); - } - - public Axis primaryAxis() - { - Axis a = Axis.X; - - double nx = Math.Abs(X); - double ny = Math.Abs(Y); - double nz = Math.Abs(Z); - - if (nx > ny) - { - if (nx > nz) - a = Axis.X; - else - a = Axis.Z; - } - else - { - if (ny > nz) - a = Axis.Y; - else - a = Axis.Z; - } - - return a; - } - - public enum Axis { X = 0, Y = 1, Z = 2, Detect = -1 }; - - public Vector3 lerp(Vector3 v, float alpha) - { - return (this) + (v - this) * alpha; - } - - /// - /// - /// - /// Vector3.Zero if the length is nearly zero, otherwise returns a unit vector - public Vector3 directionOrZero() - { - float mag = magnitude(); - if (mag < 0.0000001f) - { - return Zero; - } - else if (mag < 1.00001f && mag > 0.99999f) - { - return this; - } - else - { - return this * (1.0f / mag); - } - } - } - - #region Vector3Converter class - /// - /// Converts a to and from string representation. - /// - public class Vector3Converter : ExpandableObjectConverter - { - /// - /// Returns whether this converter can convert an object of the given type to the type of this converter, using the specified context. - /// - /// An that provides a format context. - /// A that represents the type you want to convert from. - /// true if this converter can perform the conversion; otherwise, false. - public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType) - { - if (sourceType == typeof(string)) - return true; - - return base.CanConvertFrom(context, sourceType); - } - /// - /// Returns whether this converter can convert the object to the specified type, using the specified context. - /// - /// An that provides a format context. - /// A that represents the type you want to convert to. - /// true if this converter can perform the conversion; otherwise, false. - public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType) - { - if (destinationType == typeof(string)) - return true; - - return base.CanConvertTo(context, destinationType); - } - /// - /// Converts the given value object to the specified type, using the specified context and culture information. - /// - /// An that provides a format context. - /// A object. If a null reference (Nothing in Visual Basic) is passed, the current culture is assumed. - /// The to convert. - /// The Type to convert the parameter to. - /// An that represents the converted value. - public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destinationType) - { - if ((destinationType == typeof(string)) && (value is Vector3)) - { - Vector3 v = (Vector3)value; - return v.ToString(); - } - - return base.ConvertTo(context, culture, value, destinationType); - } - /// - /// Converts the given object to the type of this converter, using the specified context and culture information. - /// - /// An that provides a format context. - /// The to use as the current culture. - /// The to convert. - /// An that represents the converted value. - public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) - { - if (value.GetType() == typeof(string)) - { - return Vector3.Parse((string)value); - } - - return base.ConvertFrom(context, culture, value); - } - - /// - /// Returns whether this object supports a standard set of values that can be picked from a list. - /// - /// An that provides a format context. - /// true if should be called to find a common set of values the object supports; otherwise, false. - public override bool GetStandardValuesSupported(ITypeDescriptorContext context) - { - return true; - } - - /// - /// Returns a collection of standard values for the data type this type converter is designed for when provided with a format context. - /// - /// An that provides a format context that can be used to extract additional information about the environment from which this converter is invoked. This parameter or properties of this parameter can be a null reference. - /// A that holds a standard set of valid values, or a null reference (Nothing in Visual Basic) if the data type does not support a standard set of values. - public override System.ComponentModel.TypeConverter.StandardValuesCollection GetStandardValues(ITypeDescriptorContext context) - { - StandardValuesCollection svc = - new(new object[4] { Vector3.Zero, Vector3.XAxis, Vector3.YAxis, Vector3.ZAxis }); - - return svc; - } - } - #endregion -} diff --git a/Source/Framework/GameMath/Vector4.cs b/Source/Framework/GameMath/Vector4.cs deleted file mode 100644 index 380734a8c..000000000 --- a/Source/Framework/GameMath/Vector4.cs +++ /dev/null @@ -1,1034 +0,0 @@ -/* - * Copyright (C) 2012-2020 CypherCore - * Copyright (C) 2003-2004 Eran Kampf eran@ekampf.com http://www.ekampf.com - * - * 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 . - */ - -using System; -using System.Collections.Generic; -using System.ComponentModel; -using System.Diagnostics; -using System.Runtime.InteropServices; -using System.Text.RegularExpressions; - -namespace Framework.GameMath -{ - /// - /// Represents 4-Dimentional vector of single-precision floating point numbers. - /// - [Serializable] - [StructLayout(LayoutKind.Sequential)] - [TypeConverter(typeof(Vector4Converter))] - public struct Vector4 : ICloneable - { - #region Private fields - private float _x; - private float _y; - private float _z; - private float _w; - #endregion - - #region Constructors - /// - /// Initializes a new instance of the class with the specified coordinates. - /// - /// The vector's X coordinate. - /// The vector's Y coordinate. - /// The vector's Z coordinate. - /// The vector's W coordinate. - public Vector4(float x, float y, float z, float w) - { - _x = x; - _y = y; - _z = z; - _w = w; - } - /// - /// Initializes a new instance of the class with the specified coordinates. - /// - /// An array containing the coordinate parameters. - public Vector4(float[] coordinates) - { - Debug.Assert(coordinates != null); - Debug.Assert(coordinates.Length >= 4); - - _x = coordinates[0]; - _y = coordinates[1]; - _z = coordinates[2]; - _w = coordinates[3]; - } - /// - /// Initializes a new instance of the class with the specified coordinates. - /// - /// An array containing the coordinate parameters. - public Vector4(List coordinates) - { - Debug.Assert(coordinates != null); - Debug.Assert(coordinates.Count >= 4); - - _x = coordinates[0]; - _y = coordinates[1]; - _z = coordinates[2]; - _w = coordinates[3]; - } - /// - /// Initializes a new instance of the class using coordinates from a given instance. - /// - /// A to get the coordinates from. - public Vector4(Vector4 vector) - { - _x = vector.X; - _y = vector.Y; - _z = vector.Z; - _w = vector.W; - } - #endregion - - #region Constants - /// - /// 4-Dimentional single-precision floating point zero vector. - /// - public static readonly Vector4 Zero = new(0.0f, 0.0f, 0.0f, 0.0f); - /// - /// 4-Dimentional single-precision floating point X-Axis vector. - /// - public static readonly Vector4 XAxis = new(1.0f, 0.0f, 0.0f, 0.0f); - /// - /// 4-Dimentional single-precision floating point Y-Axis vector. - /// - public static readonly Vector4 YAxis = new(0.0f, 1.0f, 0.0f, 0.0f); - /// - /// 4-Dimentional single-precision floating point Y-Axis vector. - /// - public static readonly Vector4 ZAxis = new(0.0f, 0.0f, 1.0f, 0.0f); - /// - /// 4-Dimentional single-precision floating point Y-Axis vector. - /// - public static readonly Vector4 WAxis = new(0.0f, 0.0f, 0.0f, 1.0f); - #endregion - - #region Public properties - /// - /// Gets or sets the x-coordinate of this vector. - /// - /// The x-coordinate of this vector. - public float X - { - get { return _x; } - set { _x = value; } - } - /// - /// Gets or sets the y-coordinate of this vector. - /// - /// The y-coordinate of this vector. - public float Y - { - get { return _y; } - set { _y = value; } - } - /// - /// Gets or sets the z-coordinate of this vector. - /// - /// The z-coordinate of this vector. - public float Z - { - get { return _z; } - set { _z = value; } - } - /// - /// Gets or sets the w-coordinate of this vector. - /// - /// The w-coordinate of this vector. - public float W - { - get { return _w; } - set { _w = value; } - } - #endregion - - #region ICloneable Members - /// - /// Creates an exact copy of this object. - /// - /// The object this method creates, cast as an object. - object ICloneable.Clone() - { - return new Vector4(this); - } - /// - /// Creates an exact copy of this object. - /// - /// The object this method creates. - public Vector4 Clone() - { - return new Vector4(this); - } - #endregion - - #region Public Static Parse Methods - /// - /// Converts the specified string to its equivalent. - /// - /// A string representation of a . - /// A that represents the vector specified by the parameter. - public static Vector4 Parse(string value) - { - Regex r = new(@"\((?.*),(?.*),(?.*),(?.*)\)", RegexOptions.Singleline); - Match m = r.Match(value); - if (m.Success) - { - return new Vector4( - float.Parse(m.Result("${x}")), - float.Parse(m.Result("${y}")), - float.Parse(m.Result("${z}")), - float.Parse(m.Result("${w}")) - ); - } - else - { - throw new Exception("Unsuccessful Match."); - } - } - /// - /// Converts the specified string to its equivalent. - /// A return value indicates whether the conversion succeeded or failed. - /// - /// A string representation of a . - /// - /// When this method returns, if the conversion succeeded, - /// contains a representing the vector specified by . - /// - /// if value was converted successfully; otherwise, . - public static bool TryParse(string value, out Vector4 result) - { - Regex r = new(@"\((?.*),(?.*),(?.*),(?.*)\)", RegexOptions.Singleline); - Match m = r.Match(value); - if (m.Success) - { - result = new Vector4( - float.Parse(m.Result("${x}")), - float.Parse(m.Result("${y}")), - float.Parse(m.Result("${z}")), - float.Parse(m.Result("${w}")) - ); - - return true; - } - - result = Vector4.Zero; - return false; - } - #endregion - - #region Public Static Vector Arithmetics - /// - /// Adds two vectors. - /// - /// A instance. - /// A instance. - /// A new instance containing the sum. - public static Vector4 Add(Vector4 left, Vector4 right) - { - return new Vector4(left.X + right.X, left.Y + right.Y, left.Z + right.Z, left.W + right.W); - } - /// - /// Adds a vector and a scalar. - /// - /// A instance. - /// A single-precision floating-point number. - /// A new instance containing the sum. - public static Vector4 Add(Vector4 vector, float scalar) - { - return new Vector4(vector.X + scalar, vector.Y + scalar, vector.Z + scalar, vector.W + scalar); - } - /// - /// Adds two vectors and put the result in the third vector. - /// - /// A instance. - /// A instance - /// A instance to hold the result. - public static void Add(Vector4 left, Vector4 right, ref Vector4 result) - { - result.X = left.X + right.X; - result.Y = left.Y + right.Y; - result.Z = left.Z + right.Z; - result.W = left.W + right.W; - } - /// - /// Adds a vector and a scalar and put the result into another vector. - /// - /// A instance. - /// A single-precision floating-point number. - /// A instance to hold the result. - public static void Add(Vector4 vector, float scalar, ref Vector4 result) - { - result.X = vector.X + scalar; - result.Y = vector.Y + scalar; - result.Z = vector.Z + scalar; - result.W = vector.W + scalar; - } - /// - /// Subtracts a vector from a vector. - /// - /// A instance. - /// A instance. - /// A new instance containing the difference. - /// - /// result[i] = left[i] - right[i]. - /// - public static Vector4 Subtract(Vector4 left, Vector4 right) - { - return new Vector4(left.X - right.X, left.Y - right.Y, left.Z - right.Z, left.W - right.W); - } - /// - /// Subtracts a scalar from a vector. - /// - /// A instance. - /// A single-precision floating-point number. - /// A new instance containing the difference. - /// - /// result[i] = vector[i] - scalar - /// - public static Vector4 Subtract(Vector4 vector, float scalar) - { - return new Vector4(vector.X - scalar, vector.Y - scalar, vector.Z - scalar, vector.W - scalar); - } - /// - /// Subtracts a vector from a scalar. - /// - /// A instance. - /// A single-precision floating-point number. - /// A new instance containing the difference. - /// - /// result[i] = scalar - vector[i] - /// - public static Vector4 Subtract(float scalar, Vector4 vector) - { - return new Vector4(scalar - vector.X, scalar - vector.Y, scalar - vector.Z, scalar - vector.W); - } - /// - /// Subtracts a vector from a second vector and puts the result into a third vector. - /// - /// A instance. - /// A instance - /// A instance to hold the result. - /// - /// result[i] = left[i] - right[i]. - /// - public static void Subtract(Vector4 left, Vector4 right, ref Vector4 result) - { - result.X = left.X - right.X; - result.Y = left.Y - right.Y; - result.Z = left.Z - right.Z; - result.W = left.W - right.W; - } - /// - /// Subtracts a vector from a scalar and put the result into another vector. - /// - /// A instance. - /// A single-precision floating-point number. - /// A instance to hold the result. - /// - /// result[i] = vector[i] - scalar - /// - public static void Subtract(Vector4 vector, float scalar, ref Vector4 result) - { - result.X = vector.X - scalar; - result.Y = vector.Y - scalar; - result.Z = vector.Z - scalar; - result.W = vector.W - scalar; - } - /// - /// Subtracts a scalar from a vector and put the result into another vector. - /// - /// A instance. - /// A single-precision floating-point number. - /// A instance to hold the result. - /// - /// result[i] = scalar - vector[i] - /// - public static void Subtract(float scalar, Vector4 vector, ref Vector4 result) - { - result.X = scalar - vector.X; - result.Y = scalar - vector.Y; - result.Z = scalar - vector.Z; - result.W = scalar - vector.W; - } - /// - /// Divides a vector by another vector. - /// - /// A instance. - /// A instance. - /// A new containing the quotient. - /// - /// result[i] = left[i] / right[i]. - /// - public static Vector4 Divide(Vector4 left, Vector4 right) - { - return new Vector4(left.X / right.X, left.Y / right.Y, left.Z / right.Z, left.W / right.W); - } - /// - /// Divides a vector by a scalar. - /// - /// A instance. - /// A scalar - /// A new containing the quotient. - /// - /// result[i] = vector[i] / scalar; - /// - public static Vector4 Divide(Vector4 vector, float scalar) - { - return new Vector4(vector.X / scalar, vector.Y / scalar, vector.Z / scalar, vector.W / scalar); - } - /// - /// Divides a scalar by a vector. - /// - /// A instance. - /// A scalar - /// A new containing the quotient. - /// - /// result[i] = scalar / vector[i] - /// - public static Vector4 Divide(float scalar, Vector4 vector) - { - return new Vector4(scalar / vector.X, scalar / vector.Y, scalar / vector.Z, scalar / vector.W); - } - /// - /// Divides a vector by another vector. - /// - /// A instance. - /// A instance. - /// A instance to hold the result. - /// - /// result[i] = left[i] / right[i] - /// - public static void Divide(Vector4 left, Vector4 right, ref Vector4 result) - { - result.X = left.X / right.X; - result.Y = left.Y / right.Y; - result.Z = left.Z / right.Z; - result.W = left.W / right.W; - } - /// - /// Divides a vector by a scalar. - /// - /// A instance. - /// A scalar - /// A instance to hold the result. - /// - /// result[i] = vector[i] / scalar - /// - public static void Divide(Vector4 vector, float scalar, ref Vector4 result) - { - result.X = vector.X / scalar; - result.Y = vector.Y / scalar; - result.Z = vector.Z / scalar; - result.W = vector.W / scalar; - } - /// - /// Divides a scalar by a vector. - /// - /// A instance. - /// A scalar - /// A instance to hold the result. - /// - /// result[i] = scalar / vector[i] - /// - public static void Divide(float scalar, Vector4 vector, ref Vector4 result) - { - result.X = scalar / vector.X; - result.Y = scalar / vector.Y; - result.Z = scalar / vector.Z; - result.W = scalar / vector.W; - } - /// - /// Multiplies a vector by a scalar. - /// - /// A instance. - /// A single-precision floating-point number. - /// A new containing the result. - public static Vector4 Multiply(Vector4 vector, float scalar) - { - return new Vector4(vector.X * scalar, vector.Y * scalar, vector.Z * scalar, vector.W * scalar); - } - /// - /// Multiplies a vector by a scalar and put the result in another vector. - /// - /// A instance. - /// A single-precision floating-point number. - /// A instance to hold the result. - public static void Multiply(Vector4 vector, float scalar, ref Vector4 result) - { - result.X = vector.X * scalar; - result.Y = vector.Y * scalar; - result.Z = vector.Z * scalar; - result.W = vector.W * scalar; - } - /// - /// Calculates the dot product of two vectors. - /// - /// A instance. - /// A instance. - /// The dot product value. - public static float DotProduct(Vector4 left, Vector4 right) - { - return (left.X * right.X) + (left.Y * right.Y) + (left.Z * right.Z) + (left.W * right.W); - } - /// - /// Negates a vector. - /// - /// A instance. - /// A new instance containing the negated values. - public static Vector4 Negate(Vector4 vector) - { - return new Vector4(-vector.X, -vector.Y, -vector.Z, -vector.W); - } - /// - /// Tests whether two vectors are approximately equal using default tolerance value. - /// - /// A instance. - /// A instance. - /// if the two vectors are approximately equal; otherwise, . - public static bool ApproxEqual(Vector4 left, Vector4 right) - { - return ApproxEqual(left, right, MathFunctions.Epsilon); - } - /// - /// Tests whether two vectors are approximately equal given a tolerance value. - /// - /// A instance. - /// A instance. - /// The tolerance value used to test approximate equality. - /// if the two vectors are approximately equal; otherwise, . - public static bool ApproxEqual(Vector4 left, Vector4 right, float tolerance) - { - return - ( - (System.Math.Abs(left.X - right.X) <= tolerance) && - (System.Math.Abs(left.Y - right.Y) <= tolerance) && - (System.Math.Abs(left.Z - right.Z) <= tolerance) && - (System.Math.Abs(left.W - right.W) <= tolerance) - ); - } - #endregion - - #region Public Methods - /// - /// Scale the vector so that its length is 1. - /// - public void Normalize() - { - float length = GetLength(); - if (length == 0) - { - throw new DivideByZeroException("Trying to normalize a vector with length of zero."); - } - - _x /= length; - _y /= length; - _z /= length; - _w /= length; - } - /// - /// Calculates the length of the vector. - /// - /// Returns the length of the vector. (Sqrt(X*X + Y*Y)) - public float GetLength() - { - return (float)System.Math.Sqrt(_x * _x + _y * _y + _z * _z + _w * _w); - } - /// - /// Calculates the squared length of the vector. - /// - /// Returns the squared length of the vector. (X*X + Y*Y) - public float GetLengthSquared() - { - return (_x * _x + _y * _y + _z * _z + _w * _w); - } - /// - /// Clamps vector values to zero using a given tolerance value. - /// - /// The tolerance to use. - /// - /// The vector values that are close to zero within the given tolerance are set to zero. - /// - public void ClampZero(float tolerance) - { - _x = MathFunctions.Clamp(_x, 0, tolerance); - _y = MathFunctions.Clamp(_y, 0, tolerance); - _z = MathFunctions.Clamp(_z, 0, tolerance); - _w = MathFunctions.Clamp(_w, 0, tolerance); - } - /// - /// Clamps vector values to zero using the default tolerance value. - /// - /// - /// The vector values that are close to zero within the given tolerance are set to zero. - /// The tolerance value used is - /// - public void ClampZero() - { - _x = MathFunctions.Clamp(_x, 0); - _y = MathFunctions.Clamp(_y, 0); - _z = MathFunctions.Clamp(_z, 0); - _w = MathFunctions.Clamp(_w, 0); - } - #endregion - - #region Overrides - /// - /// Returns the hashcode for this instance. - /// - /// A 32-bit signed integer hash code. - public override int GetHashCode() - { - return _x.GetHashCode() ^ _y.GetHashCode() ^ _z.GetHashCode() ^ _w.GetHashCode(); - } - /// - /// Returns a value indicating whether this instance is equal to - /// the specified object. - /// - /// An object to compare to this instance. - /// if is a and has the same values as this instance; otherwise, . - public override bool Equals(object obj) - { - if (obj is Vector4) - { - Vector4 v = (Vector4)obj; - return (_x == v.X) && (_y == v.Y) && (_z == v.Z) && (_w == v.W); - } - return false; - } - /// - /// Returns a string representation of this object. - /// - /// A string representation of this object. - public override string ToString() - { - return $"({_x}, {_y}, {_z}, {_w})"; - } - #endregion - - #region Comparison Operators - /// - /// Tests whether two specified vectors are equal. - /// - /// A instance. - /// A instance. - /// if the two vectors are equal; otherwise, . - public static bool operator ==(Vector4 left, Vector4 right) - { - return ValueType.Equals(left, right); - } - /// - /// Tests whether two specified vectors are not equal. - /// - /// A instance. - /// A instance. - /// if the two vectors are not equal; otherwise, . - public static bool operator !=(Vector4 left, Vector4 right) - { - return !ValueType.Equals(left, right); - } - - /// - /// Tests if a vector's components are greater than another vector's components. - /// - /// A instance. - /// A instance. - /// if the left-hand vector's components are greater than the right-hand vector's component; otherwise, . - public static bool operator >(Vector4 left, Vector4 right) - { - return ( - (left._x > right._x) && - (left._y > right._y) && - (left._z > right._z) && - (left._w > right._w)); - } - /// - /// Tests if a vector's components are smaller than another vector's components. - /// - /// A instance. - /// A instance. - /// if the left-hand vector's components are smaller than the right-hand vector's component; otherwise, . - public static bool operator <(Vector4 left, Vector4 right) - { - return ( - (left._x < right._x) && - (left._y < right._y) && - (left._z < right._z) && - (left._w < right._w)); - } - /// - /// Tests if a vector's components are greater or equal than another vector's components. - /// - /// A instance. - /// A instance. - /// if the left-hand vector's components are greater or equal than the right-hand vector's component; otherwise, . - public static bool operator >=(Vector4 left, Vector4 right) - { - return ( - (left._x >= right._x) && - (left._y >= right._y) && - (left._z >= right._z) && - (left._w >= right._w)); - } - /// - /// Tests if a vector's components are smaller or equal than another vector's components. - /// - /// A instance. - /// A instance. - /// if the left-hand vector's components are smaller or equal than the right-hand vector's component; otherwise, . - public static bool operator <=(Vector4 left, Vector4 right) - { - return ( - (left._x <= right._x) && - (left._y <= right._y) && - (left._z <= right._z) && - (left._w <= right._w)); - } - #endregion - - #region Unary Operators - /// - /// Negates the values of the given vector. - /// - /// A instance. - /// A new instance containing the negated values. - public static Vector4 operator -(Vector4 vector) - { - return Vector4.Negate(vector); - } - #endregion - - #region Binary Operators - /// - /// Adds two vectors. - /// - /// A instance. - /// A instance. - /// A new instance containing the sum. - public static Vector4 operator +(Vector4 left, Vector4 right) - { - return Vector4.Add(left, right); - } - /// - /// Adds a vector and a scalar. - /// - /// A instance. - /// A single-precision floating-point number. - /// A new instance containing the sum. - public static Vector4 operator +(Vector4 vector, float scalar) - { - return Vector4.Add(vector, scalar); - } - /// - /// Adds a vector and a scalar. - /// - /// A instance. - /// A single-precision floating-point number. - /// A new instance containing the sum. - public static Vector4 operator +(float scalar, Vector4 vector) - { - return Vector4.Add(vector, scalar); - } - /// - /// Subtracts a vector from a vector. - /// - /// A instance. - /// A instance. - /// A new instance containing the difference. - /// - /// result[i] = left[i] - right[i]. - /// - public static Vector4 operator -(Vector4 left, Vector4 right) - { - return Vector4.Subtract(left, right); - } - /// - /// Subtracts a scalar from a vector. - /// - /// A instance. - /// A single-precision floating-point number. - /// A new instance containing the difference. - /// - /// result[i] = vector[i] - scalar - /// - public static Vector4 operator -(Vector4 vector, float scalar) - { - return Vector4.Subtract(vector, scalar); - } - /// - /// Subtracts a vector from a scalar. - /// - /// A instance. - /// A single-precision floating-point number. - /// A new instance containing the difference. - /// - /// result[i] = scalar - vector[i] - /// - public static Vector4 operator -(float scalar, Vector4 vector) - { - return Vector4.Subtract(scalar, vector); - } - /// - /// Multiplies a vector by a scalar. - /// - /// A instance. - /// A single-precision floating-point number. - /// A new containing the result. - public static Vector4 operator *(Vector4 vector, float scalar) - { - return Vector4.Multiply(vector, scalar); - } - /// - /// Multiplies a vector by a scalar. - /// - /// A instance. - /// A single-precision floating-point number. - /// A new containing the result. - public static Vector4 operator *(float scalar, Vector4 vector) - { - return Vector4.Multiply(vector, scalar); - } - - public static Vector4 operator *(Vector4 vector, Matrix4 M) - { - Vector4 result = new(); - for (int i = 0; i < 4; ++i) - { - result[i] = 0.0f; - for (int j = 0; j < 4; ++j) - { - result[i] += vector[j] * M[j, i]; - } - } - return result; - } - - /// - /// Divides a vector by a scalar. - /// - /// A instance. - /// A scalar - /// A new containing the quotient. - /// - /// result[i] = vector[i] / scalar; - /// - public static Vector4 operator /(Vector4 vector, float scalar) - { - return Vector4.Divide(vector, scalar); - } - /// - /// Divides a scalar by a vector. - /// - /// A instance. - /// A scalar - /// A new containing the quotient. - /// - /// result[i] = scalar / vector[i] - /// - public static Vector4 operator /(float scalar, Vector4 vector) - { - return Vector4.Divide(scalar, vector); - } - #endregion - - #region Array Indexing Operator - /// - /// Indexer ( [x, y, z, w] ). - /// - public float this[int index] - { - get - { - switch (index) - { - case 0: - return _x; - case 1: - return _y; - case 2: - return _z; - case 3: - return _w; - default: - throw new IndexOutOfRangeException(); - } - } - set - { - switch (index) - { - case 0: - _x = value; - break; - case 1: - _y = value; - break; - case 2: - _z = value; - break; - case 3: - _w = value; - break; - default: - throw new IndexOutOfRangeException(); - } - } - - } - #endregion - - #region Conversion Operators - /// - /// Converts the vector to an array of single-precision floating point values. - /// - /// A instance. - /// An array of single-precision floating point values. - public static explicit operator float[] (Vector4 vector) - { - float[] array = new float[4]; - array[0] = vector.X; - array[1] = vector.Y; - array[2] = vector.Z; - array[3] = vector.W; - return array; - } - /// - /// Converts the vector to a of single-precision floating point values. - /// - /// A instance. - /// A of single-precision floating point values. - public static explicit operator List(Vector4 vector) - { - List list = new(4); - list.Add(vector.X); - list.Add(vector.Y); - list.Add(vector.Z); - list.Add(vector.W); - - return list; - } - /// - /// Converts the vector to a of single-precision floating point values. - /// - /// A instance. - /// A of single-precision floating point values. - public static explicit operator LinkedList(Vector4 vector) - { - LinkedList list = new(); - list.AddLast(vector.X); - list.AddLast(vector.Y); - list.AddLast(vector.Z); - list.AddLast(vector.W); - - return list; - } - #endregion - } - - #region Vector4Converter class - /// - /// Converts a to and from string representation. - /// - public class Vector4Converter : ExpandableObjectConverter - { - /// - /// Returns whether this converter can convert an object of the given type to the type of this converter, using the specified context. - /// - /// An that provides a format context. - /// A that represents the type you want to convert from. - /// true if this converter can perform the conversion; otherwise, false. - public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType) - { - if (sourceType == typeof(string)) - return true; - - return base.CanConvertFrom(context, sourceType); - } - /// - /// Returns whether this converter can convert the object to the specified type, using the specified context. - /// - /// An that provides a format context. - /// A that represents the type you want to convert to. - /// true if this converter can perform the conversion; otherwise, false. - public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType) - { - if (destinationType == typeof(string)) - return true; - - return base.CanConvertTo(context, destinationType); - } - /// - /// Converts the given value object to the specified type, using the specified context and culture information. - /// - /// An that provides a format context. - /// A object. If a null reference (Nothing in Visual Basic) is passed, the current culture is assumed. - /// The to convert. - /// The Type to convert the parameter to. - /// An that represents the converted value. - public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destinationType) - { - if ((destinationType == typeof(string)) && (value is Vector4)) - { - Vector4 v = (Vector4)value; - return v.ToString(); - } - - return base.ConvertTo(context, culture, value, destinationType); - } - /// - /// Converts the given object to the type of this converter, using the specified context and culture information. - /// - /// An that provides a format context. - /// The to use as the current culture. - /// The to convert. - /// An that represents the converted value. - public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) - { - if (value.GetType() == typeof(string)) - { - return Vector4.Parse((string)value); - } - - return base.ConvertFrom(context, culture, value); - } - - /// - /// Returns whether this object supports a standard set of values that can be picked from a list. - /// - /// An that provides a format context. - /// true if should be called to find a common set of values the object supports; otherwise, false. - public override bool GetStandardValuesSupported(ITypeDescriptorContext context) - { - return true; - } - - /// - /// Returns a collection of standard values for the data type this type converter is designed for when provided with a format context. - /// - /// An that provides a format context that can be used to extract additional information about the environment from which this converter is invoked. This parameter or properties of this parameter can be a null reference. - /// A that holds a standard set of valid values, or a null reference (Nothing in Visual Basic) if the data type does not support a standard set of values. - public override System.ComponentModel.TypeConverter.StandardValuesCollection GetStandardValues(ITypeDescriptorContext context) - { - StandardValuesCollection svc = - new(new object[5] { Vector4.Zero, Vector4.XAxis, Vector4.YAxis, Vector4.ZAxis, Vector4.WAxis }); - - return svc; - } - } - #endregion -} diff --git a/Source/Framework/IO/ByteBuffer.cs b/Source/Framework/IO/ByteBuffer.cs index 35f380edd..d544658dd 100644 --- a/Source/Framework/IO/ByteBuffer.cs +++ b/Source/Framework/IO/ByteBuffer.cs @@ -15,9 +15,9 @@ * along with this program. If not, see . */ -using Framework.GameMath; using System; using System.IO; +using System.Numerics; using System.Text; namespace Framework.IO diff --git a/Source/Framework/IO/SocketBuffer.cs b/Source/Framework/IO/SocketBuffer.cs index fca72b2cc..a34bf87c4 100644 --- a/Source/Framework/IO/SocketBuffer.cs +++ b/Source/Framework/IO/SocketBuffer.cs @@ -1,6 +1,4 @@ using System; -using System.Collections.Generic; -using System.Text; namespace Framework.IO { diff --git a/Source/Framework/Util/Extensions.cs b/Source/Framework/Util/Extensions.cs index d682e0bb9..5f37df1d7 100644 --- a/Source/Framework/Util/Extensions.cs +++ b/Source/Framework/Util/Extensions.cs @@ -182,6 +182,102 @@ namespace System return list; } + public static float GetAt(this Vector3 vector, long index) + { + switch (index) + { + case 0: + return vector.X; + case 1: + return vector.Y; + case 2: + return vector.Z; + default: + throw new IndexOutOfRangeException(); + } + } + + public static void SetAt(this ref Vector3 vector, float value, long index) + { + switch (index) + { + case 0: + vector.X = value; + break; + case 1: + vector.Y = value; + break; + case 2: + vector.Z = value; + break; + default: + throw new IndexOutOfRangeException(); + } + } + + public static int primaryAxis(this Vector3 vector) + { + var a = 0; + + double nx = Math.Abs(vector.X); + double ny = Math.Abs(vector.Y); + double nz = Math.Abs(vector.Z); + + if (nx > ny) + { + if (nx > nz) + a = 0; + else + a = 2; + } + else + { + if (ny > nz) + a = 1; + else + a = 2; + } + + return a; + } + + public static Vector3 directionOrZero(this Vector3 vector) + { + float mag = vector.Length(); + if (mag < 0.0000001f) + { + return Vector3.Zero; + } + else if (mag < 1.00001f && mag > 0.99999f) + { + return vector; + } + else + { + return vector * (1.0f / mag); + } + } + + public static Matrix4x4 fromEulerAnglesZYX(float fYAngle, float fPAngle, float fRAngle) + { + float fCos = (float)Math.Cos(fYAngle); + float fSin = (float)Math.Sin(fYAngle); + + Matrix4x4 kZMat = new(fCos, -fSin, 0, 0, fSin, fCos, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0); + + fCos = (float)Math.Cos(fPAngle); + fSin = (float)Math.Sin(fPAngle); + + Matrix4x4 kYMat = new(fCos, 0, fSin, 0, 0, 1, 0, 0, -fSin, 0, fCos, 0, 0, 0, 0, 0); + + fCos = (float)Math.Cos(fRAngle); + fSin = (float)Math.Sin(fRAngle); + + Matrix4x4 kXMat = new(1, 0, 0, 0, 0, fCos, -fSin, 0, 0, fSin, fCos, 0, 0, 0, 0, 0); + + return (kZMat * (kYMat * kXMat)); + } + #region Strings public static bool IsEmpty(this string str) { @@ -255,6 +351,24 @@ namespace System return true; return false; } + + public static Vector3 ParseVector3(this string value) + { + Regex r = new Regex(@"\((?.*),(?.*),(?.*)\)", RegexOptions.Singleline); + Match m = r.Match(value); + if (m.Success) + { + return new Vector3( + float.Parse(m.Result("${x}")), + float.Parse(m.Result("${y}")), + float.Parse(m.Result("${z}")) + ); + } + else + { + throw new Exception("Unsuccessful Match."); + } + } #endregion #region BinaryReader diff --git a/Source/Framework/Web/API/ApiRequest.cs b/Source/Framework/Web/API/ApiRequest.cs index f4d33afda..dabf03d14 100644 --- a/Source/Framework/Web/API/ApiRequest.cs +++ b/Source/Framework/Web/API/ApiRequest.cs @@ -1,6 +1,4 @@ using System; -using System.Collections.Generic; -using System.Text; namespace Framework.Web.API { diff --git a/Source/Game/AI/CoreAI/PassiveAI.cs b/Source/Game/AI/CoreAI/PassiveAI.cs index a93f5ec49..367e3ca21 100644 --- a/Source/Game/AI/CoreAI/PassiveAI.cs +++ b/Source/Game/AI/CoreAI/PassiveAI.cs @@ -18,7 +18,6 @@ using Framework.Constants; using Game.Entities; using Game.Spells; -using System.Collections.Generic; namespace Game.AI { diff --git a/Source/Game/AI/SmartScripts/SmartScript.cs b/Source/Game/AI/SmartScripts/SmartScript.cs index 77138fc92..483618477 100644 --- a/Source/Game/AI/SmartScripts/SmartScript.cs +++ b/Source/Game/AI/SmartScripts/SmartScript.cs @@ -16,7 +16,6 @@ */ using Framework.Constants; -using Framework.GameMath; using Game.Chat; using Game.DataStorage; using Game.Entities; @@ -27,6 +26,7 @@ using Game.Spells; using System; using System.Collections.Generic; using System.Linq; +using System.Numerics; namespace Game.AI { @@ -1195,20 +1195,20 @@ namespace Game.AI if (!summoner) break; - foreach (var target in targets) - { - Position pos = target.GetPositionWithOffset(new Position(e.Target.x, e.Target.y, e.Target.z, e.Target.o)); - Quaternion rot = Quaternion.fromEulerAnglesZYX(pos.GetOrientation(), 0f, 0f); - summoner.SummonGameObject(e.Action.summonGO.entry, pos, rot, e.Action.summonGO.despawnTime, (GameObjectSummonType)e.Action.summonGO.summonType); - } + foreach (var target in targets) + { + Position pos = target.GetPositionWithOffset(new Position(e.Target.x, e.Target.y, e.Target.z, e.Target.o)); + Quaternion rot = Quaternion.CreateFromRotationMatrix(Extensions.fromEulerAnglesZYX(pos.GetOrientation(), 0f, 0f)); + summoner.SummonGameObject(e.Action.summonGO.entry, pos, rot, e.Action.summonGO.despawnTime, (GameObjectSummonType)e.Action.summonGO.summonType); + } - if (e.GetTargetType() != SmartTargets.Position) + if (e.GetTargetType() != SmartTargets.Position) + break; + + Quaternion _rot = Quaternion.CreateFromRotationMatrix(Extensions.fromEulerAnglesZYX(e.Target.o, 0f, 0f)); + summoner.SummonGameObject(e.Action.summonGO.entry, new Position(e.Target.x, e.Target.y, e.Target.z, e.Target.o), _rot, e.Action.summonGO.despawnTime, (GameObjectSummonType)e.Action.summonGO.summonType); break; - - Quaternion _rot = Quaternion.fromEulerAnglesZYX(e.Target.o, 0f, 0f); - summoner.SummonGameObject(e.Action.summonGO.entry, new Position(e.Target.x, e.Target.y, e.Target.z, e.Target.o), _rot, e.Action.summonGO.despawnTime, (GameObjectSummonType)e.Action.summonGO.summonType); - break; - } + } case SmartActions.KillUnit: { foreach (var target in targets) diff --git a/Source/Game/Achievements/CriteriaHandler.cs b/Source/Game/Achievements/CriteriaHandler.cs index 14e165b6b..a7c466c01 100644 --- a/Source/Game/Achievements/CriteriaHandler.cs +++ b/Source/Game/Achievements/CriteriaHandler.cs @@ -18,7 +18,6 @@ using Framework.Constants; using Framework.Database; using Game.Arenas; -using Game.BattleFields; using Game.BattleGrounds; using Game.DataStorage; using Game.Entities; diff --git a/Source/Game/AuctionHouse/AuctionManager.cs b/Source/Game/AuctionHouse/AuctionManager.cs index e4d3abeb8..08a76aad8 100644 --- a/Source/Game/AuctionHouse/AuctionManager.cs +++ b/Source/Game/AuctionHouse/AuctionManager.cs @@ -24,9 +24,7 @@ using Game.Mails; using Game.Networking.Packets; using System; using System.Collections.Generic; -using System.Collections.Concurrent; using System.Linq; -using Framework.Networking; using Framework.IO; using System.Collections; diff --git a/Source/Game/BattleFields/BattleField.cs b/Source/Game/BattleFields/BattleField.cs index 29c0c2645..02c2a4be2 100644 --- a/Source/Game/BattleFields/BattleField.cs +++ b/Source/Game/BattleFields/BattleField.cs @@ -16,8 +16,6 @@ */ using Framework.Constants; -using Framework.GameMath; -using Game.DataStorage; using Game.Entities; using Game.Groups; using Game.Maps; @@ -25,6 +23,7 @@ using Game.Networking; using Game.Networking.Packets; using System; using System.Collections.Generic; +using System.Numerics; namespace Game.BattleFields { diff --git a/Source/Game/BattleFields/Zones/WinterGraspConst.cs b/Source/Game/BattleFields/Zones/WinterGraspConst.cs index dc1e52f7b..07ba6e25b 100644 --- a/Source/Game/BattleFields/Zones/WinterGraspConst.cs +++ b/Source/Game/BattleFields/Zones/WinterGraspConst.cs @@ -16,8 +16,8 @@ */ using Framework.Constants; -using Framework.GameMath; using Game.Entities; +using System.Numerics; namespace Game.BattleFields { diff --git a/Source/Game/BattleGrounds/BattleGround.cs b/Source/Game/BattleGrounds/BattleGround.cs index a4583f1da..2d7ae4f15 100644 --- a/Source/Game/BattleGrounds/BattleGround.cs +++ b/Source/Game/BattleGrounds/BattleGround.cs @@ -17,7 +17,6 @@ using Framework.Constants; using Framework.Database; -using Framework.GameMath; using Game.Chat; using Game.DataStorage; using Game.Entities; @@ -30,6 +29,7 @@ using Game.Spells; using System; using System.Collections.Generic; using System.Linq; +using System.Numerics; namespace Game.BattleGrounds { @@ -1353,7 +1353,7 @@ namespace Game.BattleGrounds Log.outDebug(LogFilter.Battleground, $"Battleground.AddObject: gameoobject [entry: {entry}, object type: {type}] for BG (map: {GetMapId()}) has zeroed rotation fields, " + "orientation used temporally, but please fix the spawn"); - rotation = Quaternion.fromEulerAnglesZYX(o, 0.0f, 0.0f); + rotation = Quaternion.CreateFromRotationMatrix(Extensions.fromEulerAnglesZYX(o, 0.0f, 0.0f)); } // Must be created this way, adding to godatamap would add it to the base map of the instance diff --git a/Source/Game/BattleGrounds/Zones/ArathiBasin.cs b/Source/Game/BattleGrounds/Zones/ArathiBasin.cs index 08b115604..48acec17f 100644 --- a/Source/Game/BattleGrounds/Zones/ArathiBasin.cs +++ b/Source/Game/BattleGrounds/Zones/ArathiBasin.cs @@ -16,7 +16,6 @@ */ using Framework.Constants; -using Game.DataStorage; using Game.Entities; using Game.Networking.Packets; using System; diff --git a/Source/Game/BattleGrounds/Zones/EyeofStorm.cs b/Source/Game/BattleGrounds/Zones/EyeofStorm.cs index 80d07cc0e..3b14e3c4b 100644 --- a/Source/Game/BattleGrounds/Zones/EyeofStorm.cs +++ b/Source/Game/BattleGrounds/Zones/EyeofStorm.cs @@ -20,7 +20,6 @@ using Framework.Constants; using System.Collections.Generic; using Game.Networking.Packets; using Game.DataStorage; -using Game.BattleGrounds; namespace Game.BattleGrounds.Zones { diff --git a/Source/Game/BattleGrounds/Zones/StrandofAncients.cs b/Source/Game/BattleGrounds/Zones/StrandofAncients.cs index f935c1325..523390eea 100644 --- a/Source/Game/BattleGrounds/Zones/StrandofAncients.cs +++ b/Source/Game/BattleGrounds/Zones/StrandofAncients.cs @@ -16,11 +16,10 @@ */ using Framework.Constants; -using Game.DataStorage; using Game.Entities; using Game.Networking.Packets; using System.Collections.Generic; -using Framework.GameMath; +using System.Numerics; namespace Game.BattleGrounds.Zones { diff --git a/Source/Game/BattleGrounds/Zones/WarsongGluch.cs b/Source/Game/BattleGrounds/Zones/WarsongGluch.cs index 695ecf9bd..2061d93d4 100644 --- a/Source/Game/BattleGrounds/Zones/WarsongGluch.cs +++ b/Source/Game/BattleGrounds/Zones/WarsongGluch.cs @@ -16,10 +16,8 @@ */ using Framework.Constants; -using Game.DataStorage; using Game.Entities; using Game.Networking.Packets; -using System.Collections.Generic; namespace Game.BattleGrounds.Zones { diff --git a/Source/Game/Chat/Channels/ChannelAppenders.cs b/Source/Game/Chat/Channels/ChannelAppenders.cs index 98978b258..dc5e15b6e 100644 --- a/Source/Game/Chat/Channels/ChannelAppenders.cs +++ b/Source/Game/Chat/Channels/ChannelAppenders.cs @@ -19,7 +19,6 @@ using Framework.Constants; using Game.Cache; using Game.Entities; using Game.Maps; -using Game.Networking; using Game.Networking.Packets; namespace Game.Chat diff --git a/Source/Game/Chat/Commands/BNetAccountCommands.cs b/Source/Game/Chat/Commands/BNetAccountCommands.cs index 8bbec9938..39af1c890 100644 --- a/Source/Game/Chat/Commands/BNetAccountCommands.cs +++ b/Source/Game/Chat/Commands/BNetAccountCommands.cs @@ -19,7 +19,6 @@ using Framework.Constants; using Framework.Database; using Framework.IO; using System; -using System.Linq; namespace Game.Chat.Commands { diff --git a/Source/Game/Chat/Commands/GameObjectCommands.cs b/Source/Game/Chat/Commands/GameObjectCommands.cs index aa9cb4b06..2168d1d2f 100644 --- a/Source/Game/Chat/Commands/GameObjectCommands.cs +++ b/Source/Game/Chat/Commands/GameObjectCommands.cs @@ -17,13 +17,13 @@ using Framework.Constants; using Framework.Database; -using Framework.GameMath; using Framework.IO; using Game.DataStorage; using Game.Entities; using Game.Maps; using System; using System.Collections.Generic; +using System.Numerics; using System.Text; namespace Game.Chat @@ -614,7 +614,7 @@ namespace Game.Chat Player player = handler.GetPlayer(); Map map = player.GetMap(); - GameObject obj = GameObject.CreateGameObject(objectInfo.entry, map, player, Quaternion.fromEulerAnglesZYX(player.GetOrientation(), 0.0f, 0.0f), 255, GameObjectState.Ready); + GameObject obj = GameObject.CreateGameObject(objectInfo.entry, map, player, Quaternion.CreateFromRotationMatrix(Extensions.fromEulerAnglesZYX(player.GetOrientation(), 0.0f, 0.0f)), 255, GameObjectState.Ready); if (!obj) return false; @@ -658,7 +658,7 @@ namespace Game.Chat if (spawntime != 0) spawntm = spawntime; - Quaternion rotation = Quaternion.fromEulerAnglesZYX(player.GetOrientation(), 0.0f, 0.0f); + Quaternion rotation = Quaternion.CreateFromRotationMatrix(Extensions.fromEulerAnglesZYX(player.GetOrientation(), 0.0f, 0.0f)); if (Global.ObjectMgr.GetGameObjectTemplate(id) == null) { diff --git a/Source/Game/Chat/LanguageManager.cs b/Source/Game/Chat/LanguageManager.cs index dfa27d907..b2694619e 100644 --- a/Source/Game/Chat/LanguageManager.cs +++ b/Source/Game/Chat/LanguageManager.cs @@ -19,12 +19,8 @@ using System; using System.Collections.Generic; using System.Linq; -using System.Text; -using System.Threading.Tasks; using Game.DataStorage; using Framework.Constants; -using Game.Spells; -using Framework.Dynamic; using Framework.Collections; namespace Game.Chat diff --git a/Source/Game/Collision/BoundingIntervalHierarchy.cs b/Source/Game/Collision/BoundingIntervalHierarchy.cs index b7e16f646..05ef6128a 100644 --- a/Source/Game/Collision/BoundingIntervalHierarchy.cs +++ b/Source/Game/Collision/BoundingIntervalHierarchy.cs @@ -19,6 +19,7 @@ using Framework.GameMath; using System; using System.Collections.Generic; using System.IO; +using System.Numerics; using System.Runtime.InteropServices; namespace Game.Collision @@ -76,12 +77,12 @@ namespace Game.Collision Vector3 d = gridBox.hi - gridBox.lo; for (int i = 0; i < 3; i++) { - if (nodeBox.hi[i] < gridBox.lo[i] || nodeBox.lo[i] > gridBox.hi[i]) + if (nodeBox.hi.GetAt(i) < gridBox.lo.GetAt(i) || nodeBox.lo.GetAt(i) > gridBox.hi.GetAt(i)) Log.outError(LogFilter.Server, "Reached tree area in error - discarding node with: {0} objects", right - left + 1); } // find longest axis axis = (int)d.primaryAxis(); - split = 0.5f * (gridBox.lo[axis] + gridBox.hi[axis]); + split = 0.5f * (gridBox.lo.GetAt(axis) + gridBox.hi.GetAt(axis)); // partition L/R subsets clipL = float.NegativeInfinity; clipR = float.PositiveInfinity; @@ -91,8 +92,8 @@ namespace Game.Collision for (int i = left; i <= right; ) { int obj = (int)dat.indices[i]; - float minb = dat.primBound[obj].Lo[axis]; - float maxb = dat.primBound[obj].Hi[axis]; + float minb = dat.primBound[obj].Lo.GetAt(axis); + float maxb = dat.primBound[obj].Hi.GetAt(axis); float center = (minb + maxb) * 0.5f; if (center <= split) { @@ -115,9 +116,9 @@ namespace Game.Collision nodeR = Math.Max(nodeR, maxb); } // check for empty space - if (nodeL > nodeBox.lo[axis] && nodeR < nodeBox.hi[axis]) + if (nodeL > nodeBox.lo.GetAt(axis) && nodeR < nodeBox.hi.GetAt(axis)) { - float nodeBoxW = nodeBox.hi[axis] - nodeBox.lo[axis]; + float nodeBoxW = nodeBox.hi.GetAt(axis) - nodeBox.lo.GetAt(axis); float nodeNewW = nodeR - nodeL; // node box is too big compare to space occupied by primitives? if (1.3f * nodeNewW < nodeBoxW) @@ -134,8 +135,8 @@ namespace Game.Collision tempTree[nodeIndex + 1] = FloatToRawIntBits(nodeL); tempTree[nodeIndex + 2] = FloatToRawIntBits(nodeR); // update nodebox and recurse - nodeBox.lo[axis] = nodeL; - nodeBox.hi[axis] = nodeR; + nodeBox.lo.SetAt(nodeL, axis); + nodeBox.hi.SetAt(nodeR, axis); Subdivide(left, rightOrig, tempTree, dat, gridBox, nodeBox, nextIndex1, depth + 1, stats); return; } @@ -154,12 +155,12 @@ namespace Game.Collision if (clipL <= split) { // keep looping on left half - gridBox.hi[axis] = split; + gridBox.hi.SetAt(split, axis); prevClip = clipL; wasLeft = true; continue; } - gridBox.hi[axis] = split; + gridBox.hi.SetAt(split, axis); prevClip = float.NaN; } else if (left > right) @@ -177,12 +178,12 @@ namespace Game.Collision if (clipR >= split) { // keep looping on right half - gridBox.lo[axis] = split; + gridBox.lo.SetAt(split, axis); prevClip = clipR; wasLeft = false; continue; } - gridBox.lo[axis] = split; + gridBox.lo.SetAt(split, axis); prevClip = float.NaN; } else @@ -254,9 +255,12 @@ namespace Game.Collision AABound gridBoxR = gridBox; AABound nodeBoxL = nodeBox; AABound nodeBoxR = nodeBox; - gridBoxL.hi[axis] = gridBoxR.lo[axis] = split; - nodeBoxL.hi[axis] = clipL; - nodeBoxR.lo[axis] = clipR; + + gridBoxR.lo.SetAt(split, axis); + gridBoxL.hi.SetAt(split, axis); + nodeBoxL.hi.SetAt(clipL, axis); + nodeBoxR.lo.SetAt(clipR, axis); + // recurse if (nl > 0) Subdivide(left, right, tempTree, dat, gridBoxL, nodeBoxL, nextIndex, depth + 1, stats); @@ -325,11 +329,11 @@ namespace Game.Collision Vector3 invDir = new(); for (int i = 0; i < 3; ++i) { - invDir[i] = 1.0f / dir[i]; - if (MathFunctions.fuzzyNe(dir[i], 0.0f)) + invDir.SetAt(1.0f / dir.GetAt(i), i); + if (MathFunctions.fuzzyNe(dir.GetAt(i), 0.0f)) { - float t1 = (bounds.Lo[i] - org[i]) * invDir[i]; - float t2 = (bounds.Hi[i] - org[i]) * invDir[i]; + float t1 = (bounds.Lo.GetAt(i) - org.GetAt(i)) * invDir.GetAt(i); + float t2 = (bounds.Hi.GetAt(i) - org.GetAt(i)) * invDir.GetAt(i); if (t1 > t2) MathFunctions.Swap(ref t1, ref t2); if (t1 > intervalMin) @@ -356,7 +360,7 @@ namespace Game.Collision for (int i = 0; i < 3; ++i) { - offsetFront[i] = FloatToRawIntBits(dir[i]) >> 31; + offsetFront[i] = FloatToRawIntBits(dir.GetAt(i)) >> 31; offsetBack[i] = offsetFront[i] ^ 1; offsetFront3[i] = offsetFront[i] * 3; offsetBack3[i] = offsetBack[i] * 3; @@ -383,8 +387,8 @@ namespace Game.Collision if (axis < 3) { // "normal" interior node - float tf = (IntBitsToFloat(tree[(int)(node + offsetFront[axis])]) - org[axis]) * invDir[axis]; - float tb = (IntBitsToFloat(tree[(int)(node + offsetBack[axis])]) - org[axis]) * invDir[axis]; + float tf = (IntBitsToFloat(tree[(int)(node + offsetFront[axis])]) - org.GetAt(axis)) * invDir.GetAt(axis); + float tb = (IntBitsToFloat(tree[(int)(node + offsetBack[axis])]) - org.GetAt(axis)) * invDir.GetAt(axis); // ray passes between clip zones if (tf < intervalMin && tb > intervalMax) break; @@ -432,8 +436,8 @@ namespace Game.Collision { if (axis > 2) return; // should not happen - float tf = (IntBitsToFloat(tree[(int)(node + offsetFront[axis])]) - org[axis]) * invDir[axis]; - float tb = (IntBitsToFloat(tree[(int)(node + offsetBack[axis])]) - org[axis]) * invDir[axis]; + float tf = (IntBitsToFloat(tree[(int)(node + offsetFront[axis])]) - org.GetAt(axis)) * invDir.GetAt(axis); + float tb = (IntBitsToFloat(tree[(int)(node + offsetBack[axis])]) - org.GetAt(axis)) * invDir.GetAt(axis); node = offset; intervalMin = (tf >= intervalMin) ? tf : intervalMin; intervalMax = (tb <= intervalMax) ? tb : intervalMax; @@ -484,18 +488,18 @@ namespace Game.Collision float tl = IntBitsToFloat(tree[node + 1]); float tr = IntBitsToFloat(tree[node + 2]); // point is between clip zones - if (tl < p[(int)axis] && tr > p[axis]) + if (tl < p.GetAt(axis) && tr > p.GetAt(axis)) break; int right = offset + 3; node = right; // point is in right node only - if (tl < p[(int)axis]) + if (tl < p.GetAt(axis)) { continue; } node = offset; // left // point is in left node only - if (tr > p[axis]) + if (tr > p.GetAt(axis)) { continue; } @@ -525,7 +529,7 @@ namespace Game.Collision float tl = IntBitsToFloat(tree[node + 1]); float tr = IntBitsToFloat(tree[node + 2]); node = offset; - if (tl > p[axis] || tr < p[axis]) + if (tl > p.GetAt(axis) || tr < p.GetAt(axis)) break; continue; } diff --git a/Source/Game/Collision/BoundingIntervalHierarchyWrap.cs b/Source/Game/Collision/BoundingIntervalHierarchyWrap.cs index f5bdc1ef1..55f7a8ae9 100644 --- a/Source/Game/Collision/BoundingIntervalHierarchyWrap.cs +++ b/Source/Game/Collision/BoundingIntervalHierarchyWrap.cs @@ -17,6 +17,7 @@ using Framework.GameMath; using System.Collections.Generic; +using System.Numerics; namespace Game.Collision { diff --git a/Source/Game/Collision/Callbacks.cs b/Source/Game/Collision/Callbacks.cs index e2760b20e..e2c060967 100644 --- a/Source/Game/Collision/Callbacks.cs +++ b/Source/Game/Collision/Callbacks.cs @@ -19,6 +19,7 @@ using Framework.Constants; using Framework.GameMath; using System; using System.Collections.Generic; +using System.Numerics; namespace Game.Collision { @@ -43,8 +44,8 @@ namespace Game.Collision Vector3 lo = vertices[(int)tri.idx0]; Vector3 hi = lo; - lo = (lo.Min(vertices[(int)tri.idx1])).Min(vertices[(int)tri.idx2]); - hi = (hi.Max(vertices[(int)tri.idx1])).Max(vertices[(int)tri.idx2]); + lo = Vector3.Min(Vector3.Min(lo, vertices[(int)tri.idx1]), vertices[(int)tri.idx2]); + hi = Vector3.Max(Vector3.Max(hi, vertices[(int)tri.idx1]), vertices[(int)tri.idx2]); value = new AxisAlignedBox(lo, hi); } @@ -119,8 +120,8 @@ namespace Game.Collision Vector3 e1 = points[(int)tri.idx1] - points[(int)tri.idx0]; Vector3 e2 = points[(int)tri.idx2] - points[(int)tri.idx0]; - Vector3 p = new(ray.Direction.cross(e2)); - float a = e1.dot(p); + Vector3 p = Vector3.Cross(ray.Direction, e2); + float a = Vector3.Dot(e1, p); if (Math.Abs(a) < EPS) { @@ -129,8 +130,8 @@ namespace Game.Collision } float f = 1.0f / a; - Vector3 s = new(ray.Origin - points[(int)tri.idx0]); - float u = f * s.dot(p); + Vector3 s = ray.Origin - points[(int)tri.idx0]; + float u = f * Vector3.Dot(s, p); if ((u < 0.0f) || (u > 1.0f)) { @@ -138,8 +139,8 @@ namespace Game.Collision return false; } - Vector3 q = new(s.cross(e1)); - float v = f * ray.Direction.dot(q); + Vector3 q = Vector3.Cross(s, e1); + float v = f * Vector3.Dot(ray.Direction, q); if ((v < 0.0f) || ((u + v) > 1.0f)) { @@ -147,7 +148,7 @@ namespace Game.Collision return false; } - float t = f * e2.dot(q); + float t = f * Vector3.Dot(e2, q); if ((t > 0.0f) && (t < distance)) { diff --git a/Source/Game/Collision/DynamicTree.cs b/Source/Game/Collision/DynamicTree.cs index 798753c8a..8f517b685 100644 --- a/Source/Game/Collision/DynamicTree.cs +++ b/Source/Game/Collision/DynamicTree.cs @@ -16,6 +16,7 @@ */ using Framework.GameMath; +using System.Numerics; namespace Game.Collision { @@ -64,7 +65,7 @@ namespace Game.Collision public bool GetObjectHitPos(Vector3 startPos, Vector3 endPos, ref Vector3 resultHitPos, float modifyDist, PhaseShift phaseShift) { bool result; - float maxDist = (endPos - startPos).magnitude(); + float maxDist = (endPos - startPos).Length(); // valid map coords should *never ever* produce float overflow, but this would produce NaNs too Cypher.Assert(maxDist < float.MaxValue); // prevent NaN values which can cause BIH intersection to enter infinite loop @@ -81,7 +82,7 @@ namespace Game.Collision resultHitPos = startPos + dir * dist; if (modifyDist < 0) { - if ((resultHitPos - startPos).magnitude() > -modifyDist) + if ((resultHitPos - startPos).Length() > -modifyDist) resultHitPos += dir * modifyDist; else resultHitPos = startPos; @@ -101,7 +102,7 @@ namespace Game.Collision public bool IsInLineOfSight(Vector3 startPos, Vector3 endPos, PhaseShift phaseShift) { - float maxDist = (endPos - startPos).magnitude(); + float maxDist = (endPos - startPos).Length(); if (!MathFunctions.fuzzyGt(maxDist, 0)) return true; diff --git a/Source/Game/Collision/Management/VMapManager.cs b/Source/Game/Collision/Management/VMapManager.cs index 298be46b4..f65d82c82 100644 --- a/Source/Game/Collision/Management/VMapManager.cs +++ b/Source/Game/Collision/Management/VMapManager.cs @@ -16,10 +16,10 @@ */ using Framework.Constants; -using Framework.GameMath; +using Framework.Dynamic; using System; using System.Collections.Generic; -using Framework.Dynamic; +using System.Numerics; namespace Game.Collision { diff --git a/Source/Game/Collision/Maps/MapTree.cs b/Source/Game/Collision/Maps/MapTree.cs index 2cbfcd06c..76035465b 100644 --- a/Source/Game/Collision/Maps/MapTree.cs +++ b/Source/Game/Collision/Maps/MapTree.cs @@ -20,6 +20,7 @@ using Framework.GameMath; using System; using System.Collections.Generic; using System.IO; +using System.Numerics; namespace Game.Collision { @@ -346,7 +347,7 @@ namespace Game.Collision public bool GetObjectHitPos(Vector3 pPos1, Vector3 pPos2, out Vector3 pResultHitPos, float pModifyDist) { bool result; - float maxDist = (pPos2 - pPos1).magnitude(); + float maxDist = (pPos2 - pPos1).Length(); // valid map coords should *never ever* produce float overflow, but this would produce NaNs too Cypher.Assert(maxDist < float.MaxValue); // prevent NaN values which can cause BIH intersection to enter infinite loop @@ -363,7 +364,7 @@ namespace Game.Collision pResultHitPos = pPos1 + dir * dist; if (pModifyDist < 0) { - if ((pResultHitPos - pPos1).magnitude() > -pModifyDist) + if ((pResultHitPos - pPos1).Length() > -pModifyDist) { pResultHitPos += dir * pModifyDist; } @@ -388,7 +389,7 @@ namespace Game.Collision public bool IsInLineOfSight(Vector3 pos1, Vector3 pos2, ModelIgnoreFlags ignoreFlags) { - float maxDist = (pos2 - pos1).magnitude(); + float maxDist = (pos2 - pos1).Length(); // return false if distance is over max float, in case of cheater teleporting to the end of the universe if (maxDist == float.MaxValue || maxDist == float.PositiveInfinity) diff --git a/Source/Game/Collision/Models/GameObjectModel.cs b/Source/Game/Collision/Models/GameObjectModel.cs index 9f834e6fa..0bdb93f6a 100644 --- a/Source/Game/Collision/Models/GameObjectModel.cs +++ b/Source/Game/Collision/Models/GameObjectModel.cs @@ -15,11 +15,12 @@ * along with this program. If not, see . */ +using Framework.Constants; using Framework.GameMath; using System; using System.Collections.Generic; using System.IO; -using Framework.Constants; +using System.Numerics; namespace Game.Collision { @@ -64,13 +65,13 @@ namespace Game.Collision iScale = modelOwner.GetScale(); iInvScale = 1.0f / iScale; - Matrix3 iRotation = Matrix3.fromEulerAnglesZYX(modelOwner.GetOrientation(), 0, 0); - iInvRot = iRotation.inverse(); + Matrix4x4 iRotation = Extensions.fromEulerAnglesZYX(modelOwner.GetOrientation(), 0, 0); + Matrix4x4.Invert(iRotation, out iInvRot); // transform bounding box: mdl_box = new AxisAlignedBox(mdl_box.Lo * iScale, mdl_box.Hi * iScale); AxisAlignedBox rotated_bounds = new(); for (int i = 0; i < 8; ++i) - rotated_bounds.merge(iRotation * mdl_box.corner(i)); + rotated_bounds.merge(Vector3.Transform(mdl_box.corner(i), iRotation)); iBound = rotated_bounds + iPos; owner = modelOwner; @@ -100,8 +101,8 @@ namespace Game.Collision return false; // child bounds are defined in object space: - Vector3 p = iInvRot * (ray.Origin - iPos) * iInvScale; - Ray modRay = new(p, iInvRot * ray.Direction); + Vector3 p = Vector3.Transform((ray.Origin - iPos) * iInvScale, iInvRot); + Ray modRay = new Ray(p, Vector3.Transform(ray.Direction, iInvRot)); float distance = maxDist * iInvScale; bool hit = iModel.IntersectRay(modRay, ref distance, stopAtFirstHit, ignoreFlags); if (hit) @@ -124,13 +125,13 @@ namespace Game.Collision return; // child bounds are defined in object space: - Vector3 pModel = iInvRot * (point - iPos) * iInvScale; - Vector3 zDirModel = iInvRot * new Vector3(0.0f, 0.0f, -1.0f); + Vector3 pModel = Vector3.Transform((point - iPos) * iInvScale, iInvRot); + Vector3 zDirModel = Vector3.Transform(new Vector3(0.0f, 0.0f, -1.0f), iInvRot); float zDist; if (iModel.IntersectPoint(pModel, zDirModel, out zDist, info)) { Vector3 modelGround = pModel + zDist * zDirModel; - float world_Z = ((modelGround * iInvRot) * iScale + iPos).Z; + float world_Z = (Vector3.Transform(modelGround, iInvRot) * iScale + iPos).Z; if (info.ground_Z < world_Z) { info.ground_Z = world_Z; @@ -151,13 +152,13 @@ namespace Game.Collision return false; // child bounds are defined in object space: - Vector3 pModel = iInvRot * (point - iPos) * iInvScale; - Vector3 zDirModel = iInvRot * new Vector3(0.0f, 0.0f, -1.0f); + Vector3 pModel = Vector3.Transform((point - iPos) * iInvScale, iInvRot); + Vector3 zDirModel = Vector3.Transform(new Vector3(0.0f, 0.0f, -1.0f), iInvRot); float zDist; if (iModel.GetLocationInfo(pModel, zDirModel, out zDist, info)) { Vector3 modelGround = pModel + zDist * zDirModel; - float world_Z = ((modelGround * iInvRot) * iScale + iPos).Z; + float world_Z = (Vector3.Transform(modelGround, iInvRot) * iScale + iPos).Z; if (info.ground_Z < world_Z) { info.ground_Z = world_Z; @@ -171,7 +172,7 @@ namespace Game.Collision public bool GetLiquidLevel(Vector3 point, LocationInfo info, ref float liqHeight) { // child bounds are defined in object space: - Vector3 pModel = iInvRot * (point - iPos) * iInvScale; + Vector3 pModel = Vector3.Transform((point - iPos) * iInvScale, iInvRot); //Vector3 zDirModel = iInvRot * Vector3(0.f, 0.f, -1.f); float zDist; if (info.hitModel.GetLiquidLevel(pModel, out zDist)) @@ -203,13 +204,13 @@ namespace Game.Collision iPos = owner.GetPosition(); - Matrix3 iRotation = Matrix3.fromEulerAnglesZYX(owner.GetOrientation(), 0, 0); - iInvRot = iRotation.inverse(); + Matrix4x4 iRotation = Extensions.fromEulerAnglesZYX(owner.GetOrientation(), 0, 0); + Matrix4x4.Invert(iRotation, out iInvRot); // transform bounding box: mdl_box = new AxisAlignedBox(mdl_box.Lo * iScale, mdl_box.Hi * iScale); AxisAlignedBox rotated_bounds = new(); for (int i = 0; i < 8; ++i) - rotated_bounds.merge(iRotation * mdl_box.corner(i)); + rotated_bounds.merge(Vector3.Transform(mdl_box.corner(i), iRotation)); iBound = rotated_bounds + iPos; @@ -269,7 +270,7 @@ namespace Game.Collision bool _collisionEnabled; AxisAlignedBox iBound; - Matrix3 iInvRot; + Matrix4x4 iInvRot; Vector3 iPos; float iInvScale; float iScale; diff --git a/Source/Game/Collision/Models/IModel.cs b/Source/Game/Collision/Models/IModel.cs index 38e024fb5..6ce625ac6 100644 --- a/Source/Game/Collision/Models/IModel.cs +++ b/Source/Game/Collision/Models/IModel.cs @@ -15,8 +15,9 @@ * along with this program. If not, see . */ -using Framework.GameMath; using Framework.Constants; +using Framework.GameMath; +using System.Numerics; namespace Game.Collision { diff --git a/Source/Game/Collision/Models/ModelInstance.cs b/Source/Game/Collision/Models/ModelInstance.cs index cf99b6f6f..7b1b3f3a1 100644 --- a/Source/Game/Collision/Models/ModelInstance.cs +++ b/Source/Game/Collision/Models/ModelInstance.cs @@ -19,6 +19,7 @@ using Framework.Constants; using Framework.GameMath; using System; using System.IO; +using System.Numerics; namespace Game.Collision { @@ -85,7 +86,7 @@ namespace Game.Collision public class ModelInstance : ModelMinimalData { - Matrix3 iInvRot; + Matrix4x4 iInvRot; float iInvScale; WorldModel iModel; @@ -107,7 +108,8 @@ namespace Game.Collision iModel = model; - iInvRot = Matrix3.fromEulerAnglesZYX(MathFunctions.PI * spawn.iRot.Y / 180.0f, MathFunctions.PI * spawn.iRot.X / 180.0f, MathFunctions.PI * spawn.iRot.Z / 180.0f).inverse(); + Matrix4x4.Invert(Extensions.fromEulerAnglesZYX(MathFunctions.PI * spawn.iRot.Y / 180.0f, MathFunctions.PI * spawn.iRot.X / 180.0f, MathFunctions.PI * spawn.iRot.Z / 180.0f), out iInvRot); + iInvScale = 1.0f / iScale; } @@ -121,8 +123,8 @@ namespace Game.Collision return false; // child bounds are defined in object space: - Vector3 p = iInvRot * (pRay.Origin - iPos) * iInvScale; - Ray modRay = new(p, iInvRot * pRay.Direction); + Vector3 p = Vector3.Transform((pRay.Origin - iPos) * iInvScale, iInvRot); + Ray modRay = new Ray(p, Vector3.Transform(pRay.Direction, iInvRot)); float distance = pMaxDist * iInvScale; bool hit = iModel.IntersectRay(modRay, ref distance, pStopAtFirstHit, ignoreFlags); if (hit) @@ -144,8 +146,8 @@ namespace Game.Collision if (!iBound.contains(p)) return; // child bounds are defined in object space: - Vector3 pModel = iInvRot * (p - iPos) * iInvScale; - Vector3 zDirModel = iInvRot * new Vector3(0.0f, 0.0f, -1.0f); + Vector3 pModel = Vector3.Transform((p - iPos) * iInvScale, iInvRot); + Vector3 zDirModel = Vector3.Transform(new Vector3(0.0f, 0.0f, -1.0f), iInvRot); float zDist; if (iModel.IntersectPoint(pModel, zDirModel, out zDist, info)) { @@ -153,7 +155,7 @@ namespace Game.Collision // Transform back to world space. Note that: // Mat * vec == vec * Mat.transpose() // and for rotation matrices: Mat.inverse() == Mat.transpose() - float world_Z = ((modelGround * iInvRot) * iScale + iPos).Z; + float world_Z = ((Vector3.Transform(modelGround, iInvRot)) * iScale + iPos).Z; if (info.ground_Z < world_Z) { info.ground_Z = world_Z; @@ -165,7 +167,7 @@ namespace Game.Collision public bool GetLiquidLevel(Vector3 p, LocationInfo info, ref float liqHeight) { // child bounds are defined in object space: - Vector3 pModel = iInvRot * (p - iPos) * iInvScale; + Vector3 pModel = Vector3.Transform((p - iPos) * iInvScale, iInvRot); //Vector3 zDirModel = iInvRot * Vector3(0.f, 0.f, -1.f); float zDist; if (info.hitModel.GetLiquidLevel(pModel, out zDist)) @@ -189,8 +191,8 @@ namespace Game.Collision if (!iBound.contains(p)) return false; // child bounds are defined in object space: - Vector3 pModel = iInvRot * (p - iPos) * iInvScale; - Vector3 zDirModel = iInvRot * new Vector3(0.0f, 0.0f, -1.0f); + Vector3 pModel = Vector3.Transform((p - iPos) * iInvScale, iInvRot); + Vector3 zDirModel = Vector3.Transform(new Vector3(0.0f, 0.0f, -1.0f), iInvRot); float zDist; if (iModel.GetLocationInfo(pModel, zDirModel, out zDist, info)) { @@ -198,7 +200,7 @@ namespace Game.Collision // Transform back to world space. Note that: // Mat * vec == vec * Mat.transpose() // and for rotation matrices: Mat.inverse() == Mat.transpose() - float world_Z = ((modelGround * iInvRot) * iScale + iPos).Z; + float world_Z = (Vector3.Transform(modelGround, iInvRot) * iScale + iPos).Z; if (info.ground_Z < world_Z) // hm...could it be handled automatically with zDist at intersection? { info.ground_Z = world_Z; diff --git a/Source/Game/Collision/Models/WorldModel.cs b/Source/Game/Collision/Models/WorldModel.cs index 5cb650c39..122a763b7 100644 --- a/Source/Game/Collision/Models/WorldModel.cs +++ b/Source/Game/Collision/Models/WorldModel.cs @@ -20,6 +20,7 @@ using Framework.GameMath; using System; using System.Collections.Generic; using System.IO; +using System.Numerics; namespace Game.Collision { diff --git a/Source/Game/Collision/RegularGrid2D.cs b/Source/Game/Collision/RegularGrid2D.cs index 0f4a9be51..26d146f6c 100644 --- a/Source/Game/Collision/RegularGrid2D.cs +++ b/Source/Game/Collision/RegularGrid2D.cs @@ -18,6 +18,7 @@ using Framework.GameMath; using System; using System.Collections.Generic; +using System.Numerics; namespace Game.Collision { diff --git a/Source/Game/DataStorage/AreaTriggerDataStorage.cs b/Source/Game/DataStorage/AreaTriggerDataStorage.cs index 4efc180ee..c60f4eb52 100644 --- a/Source/Game/DataStorage/AreaTriggerDataStorage.cs +++ b/Source/Game/DataStorage/AreaTriggerDataStorage.cs @@ -17,10 +17,10 @@ using Framework.Constants; using Framework.Database; -using Framework.GameMath; using Game.Entities; using Game.Maps; using System.Collections.Generic; +using System.Numerics; namespace Game.DataStorage { diff --git a/Source/Game/DataStorage/ClientReader/DB6Storage.cs b/Source/Game/DataStorage/ClientReader/DB6Storage.cs index 9bbaaf318..a02f0d984 100644 --- a/Source/Game/DataStorage/ClientReader/DB6Storage.cs +++ b/Source/Game/DataStorage/ClientReader/DB6Storage.cs @@ -18,12 +18,12 @@ using Framework.Constants; using Framework.Database; using Framework.Dynamic; -using Framework.GameMath; using Framework.IO; using System; -using System.Collections.Generic; -using System.Reflection; using System.Collections; +using System.Collections.Generic; +using System.Numerics; +using System.Reflection; namespace Game.DataStorage { diff --git a/Source/Game/DataStorage/ClientReader/DBReader.cs b/Source/Game/DataStorage/ClientReader/DBReader.cs index 598775b3c..ce2056c31 100644 --- a/Source/Game/DataStorage/ClientReader/DBReader.cs +++ b/Source/Game/DataStorage/ClientReader/DBReader.cs @@ -19,15 +19,15 @@ using Framework.Collections; using Framework.Constants; using Framework.Database; using Framework.Dynamic; -using Framework.GameMath; using System; +using System.Collections; using System.Collections.Generic; using System.IO; using System.Linq; +using System.Numerics; using System.Runtime.CompilerServices; using System.Runtime.InteropServices; using System.Text; -using System.Collections; namespace Game.DataStorage { diff --git a/Source/Game/DataStorage/DB2Manager.cs b/Source/Game/DataStorage/DB2Manager.cs index 8ed0d7c04..36c238230 100644 --- a/Source/Game/DataStorage/DB2Manager.cs +++ b/Source/Game/DataStorage/DB2Manager.cs @@ -17,13 +17,12 @@ using Framework.Constants; using Framework.Database; -using Framework.GameMath; using Game.Networking; using System; +using System.Collections; using System.Collections.Generic; using System.Linq; -using System.Collections; -using Framework.Dynamic; +using System.Numerics; namespace Game.DataStorage { diff --git a/Source/Game/DataStorage/M2Storage.cs b/Source/Game/DataStorage/M2Storage.cs index 6b43bcba0..105a65a23 100644 --- a/Source/Game/DataStorage/M2Storage.cs +++ b/Source/Game/DataStorage/M2Storage.cs @@ -15,10 +15,10 @@ * along with this program. If not, see . */ -using Framework.GameMath; using System; using System.Collections.Generic; using System.IO; +using System.Numerics; namespace Game.DataStorage { diff --git a/Source/Game/DataStorage/Structs/A_Records.cs b/Source/Game/DataStorage/Structs/A_Records.cs index 5004bc506..34b123fb5 100644 --- a/Source/Game/DataStorage/Structs/A_Records.cs +++ b/Source/Game/DataStorage/Structs/A_Records.cs @@ -16,8 +16,8 @@ */ using Framework.Constants; -using Framework.GameMath; using System; +using System.Numerics; namespace Game.DataStorage { diff --git a/Source/Game/DataStorage/Structs/C_Records.cs b/Source/Game/DataStorage/Structs/C_Records.cs index 5a39ae44d..0b0be4ac1 100644 --- a/Source/Game/DataStorage/Structs/C_Records.cs +++ b/Source/Game/DataStorage/Structs/C_Records.cs @@ -16,7 +16,7 @@ */ using Framework.Constants; -using Framework.GameMath; +using System.Numerics; namespace Game.DataStorage { diff --git a/Source/Game/DataStorage/Structs/G_Records.cs b/Source/Game/DataStorage/Structs/G_Records.cs index ae98e3699..28655096a 100644 --- a/Source/Game/DataStorage/Structs/G_Records.cs +++ b/Source/Game/DataStorage/Structs/G_Records.cs @@ -16,7 +16,7 @@ */ using Framework.Constants; -using Framework.GameMath; +using System.Numerics; namespace Game.DataStorage { diff --git a/Source/Game/DataStorage/Structs/J_Records.cs b/Source/Game/DataStorage/Structs/J_Records.cs index 280b3f76b..e192ba22d 100644 --- a/Source/Game/DataStorage/Structs/J_Records.cs +++ b/Source/Game/DataStorage/Structs/J_Records.cs @@ -15,8 +15,7 @@ * along with this program. If not, see . */ -using Framework.Constants; -using Framework.GameMath; +using System.Numerics; namespace Game.DataStorage { diff --git a/Source/Game/DataStorage/Structs/L_Records.cs b/Source/Game/DataStorage/Structs/L_Records.cs index c2e87c8dd..10885ca5a 100644 --- a/Source/Game/DataStorage/Structs/L_Records.cs +++ b/Source/Game/DataStorage/Structs/L_Records.cs @@ -16,7 +16,7 @@ */ using Framework.Constants; -using Framework.GameMath; +using System.Numerics; namespace Game.DataStorage { diff --git a/Source/Game/DataStorage/Structs/M2Structure.cs b/Source/Game/DataStorage/Structs/M2Structure.cs index 2a8b3a389..20eaa9e6d 100644 --- a/Source/Game/DataStorage/Structs/M2Structure.cs +++ b/Source/Game/DataStorage/Structs/M2Structure.cs @@ -17,6 +17,7 @@ using Framework.GameMath; using System.IO; +using System.Numerics; namespace Game.DataStorage { diff --git a/Source/Game/DataStorage/Structs/M_Records.cs b/Source/Game/DataStorage/Structs/M_Records.cs index 21178b460..c802ddcb6 100644 --- a/Source/Game/DataStorage/Structs/M_Records.cs +++ b/Source/Game/DataStorage/Structs/M_Records.cs @@ -14,8 +14,9 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ + using Framework.Constants; -using Framework.GameMath; +using System.Numerics; namespace Game.DataStorage { diff --git a/Source/Game/DataStorage/Structs/T_Records.cs b/Source/Game/DataStorage/Structs/T_Records.cs index 6f9e48420..bb6ca52cb 100644 --- a/Source/Game/DataStorage/Structs/T_Records.cs +++ b/Source/Game/DataStorage/Structs/T_Records.cs @@ -16,7 +16,7 @@ */ using Framework.Constants; -using Framework.GameMath; +using System.Numerics; namespace Game.DataStorage { diff --git a/Source/Game/DataStorage/Structs/U_Records.cs b/Source/Game/DataStorage/Structs/U_Records.cs index 0a1829a4b..43c8eff64 100644 --- a/Source/Game/DataStorage/Structs/U_Records.cs +++ b/Source/Game/DataStorage/Structs/U_Records.cs @@ -14,8 +14,9 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ -using Framework.GameMath; + using Framework.Constants; +using System.Numerics; namespace Game.DataStorage { diff --git a/Source/Game/DataStorage/Structs/V_Records.cs b/Source/Game/DataStorage/Structs/V_Records.cs index c89df1050..6bfd0a9a8 100644 --- a/Source/Game/DataStorage/Structs/V_Records.cs +++ b/Source/Game/DataStorage/Structs/V_Records.cs @@ -16,8 +16,8 @@ */ using Framework.Constants; -using Framework.GameMath; using System; +using System.Numerics; namespace Game.DataStorage { diff --git a/Source/Game/DataStorage/Structs/W_Records.cs b/Source/Game/DataStorage/Structs/W_Records.cs index 28de4a002..3456b5c67 100644 --- a/Source/Game/DataStorage/Structs/W_Records.cs +++ b/Source/Game/DataStorage/Structs/W_Records.cs @@ -16,7 +16,6 @@ */ using Framework.Constants; -using Framework.GameMath; namespace Game.DataStorage { diff --git a/Source/Game/Entities/AreaTrigger/AreaTrigger.cs b/Source/Game/Entities/AreaTrigger/AreaTrigger.cs index e3a17fd14..cd1f6aac6 100644 --- a/Source/Game/Entities/AreaTrigger/AreaTrigger.cs +++ b/Source/Game/Entities/AreaTrigger/AreaTrigger.cs @@ -17,7 +17,6 @@ using Framework.Constants; using Framework.Dynamic; -using Framework.GameMath; using Game.AI; using Game.Maps; using Game.Movement; @@ -26,6 +25,7 @@ using Game.Networking.Packets; using Game.Spells; using System; using System.Collections.Generic; +using System.Numerics; namespace Game.Entities { diff --git a/Source/Game/Entities/AreaTrigger/AreaTriggerTemplate.cs b/Source/Game/Entities/AreaTrigger/AreaTriggerTemplate.cs index 7b733cb30..4ec9491d5 100644 --- a/Source/Game/Entities/AreaTrigger/AreaTriggerTemplate.cs +++ b/Source/Game/Entities/AreaTrigger/AreaTriggerTemplate.cs @@ -17,10 +17,10 @@ using Framework.Constants; using Framework.Dynamic; -using Framework.GameMath; using Game.Networking; using System; using System.Collections.Generic; +using System.Numerics; using System.Runtime.InteropServices; namespace Game.Entities diff --git a/Source/Game/Entities/Creature/Creature.cs b/Source/Game/Entities/Creature/Creature.cs index e854b3368..2849f1dc6 100644 --- a/Source/Game/Entities/Creature/Creature.cs +++ b/Source/Game/Entities/Creature/Creature.cs @@ -19,7 +19,6 @@ using Framework.Constants; using Framework.Database; using Framework.Dynamic; using Game.AI; -using Game.Combat; using Game.DataStorage; using Game.Groups; using Game.Loots; diff --git a/Source/Game/Entities/Creature/CreatureData.cs b/Source/Game/Entities/Creature/CreatureData.cs index ef6e9dca6..f12b3e5ae 100644 --- a/Source/Game/Entities/Creature/CreatureData.cs +++ b/Source/Game/Entities/Creature/CreatureData.cs @@ -19,7 +19,6 @@ using Framework.Collections; using Framework.Constants; using System; using System.Collections.Generic; -using Framework.Dynamic; using Game.Networking.Packets; using Game.Maps; diff --git a/Source/Game/Entities/Creature/Gossip.cs b/Source/Game/Entities/Creature/Gossip.cs index 123f2f19b..59c45f6df 100644 --- a/Source/Game/Entities/Creature/Gossip.cs +++ b/Source/Game/Entities/Creature/Gossip.cs @@ -17,13 +17,13 @@ using Framework.Collections; using Framework.Constants; -using Framework.GameMath; using Game.Conditions; using Game.DataStorage; using Game.Entities; using Game.Networking.Packets; using Game.Spells; using System.Collections.Generic; +using System.Numerics; namespace Game.Misc { diff --git a/Source/Game/Entities/DynamicObject.cs b/Source/Game/Entities/DynamicObject.cs index dd3a5b36e..86b26039a 100644 --- a/Source/Game/Entities/DynamicObject.cs +++ b/Source/Game/Entities/DynamicObject.cs @@ -18,7 +18,6 @@ using Framework.Constants; using Game.Spells; using Game.Networking; -using Game.Networking.Packets; namespace Game.Entities { diff --git a/Source/Game/Entities/GameObject/GameObject.cs b/Source/Game/Entities/GameObject/GameObject.cs index 2c6009cb2..801af5331 100644 --- a/Source/Game/Entities/GameObject/GameObject.cs +++ b/Source/Game/Entities/GameObject/GameObject.cs @@ -17,7 +17,6 @@ using Framework.Constants; using Framework.Database; -using Framework.GameMath; using Game.AI; using Game.BattleGrounds; using Game.Collision; @@ -25,12 +24,13 @@ using Game.DataStorage; using Game.Groups; using Game.Loots; using Game.Maps; +using Game.Networking; using Game.Networking.Packets; using Game.Spells; using System; using System.Collections.Generic; using System.Linq; -using Game.Networking; +using System.Numerics; namespace Game.Entities { @@ -246,7 +246,7 @@ namespace Game.Entities GameObjectAddon gameObjectAddon = Global.ObjectMgr.GetGameObjectAddon(GetSpawnId()); // For most of gameobjects is (0, 0, 0, 1) quaternion, there are only some transports with not standard rotation - Quaternion parentRotation = Quaternion.WAxis; + Quaternion parentRotation = Quaternion.Identity; if (gameObjectAddon != null) parentRotation = gameObjectAddon.ParentRotation; @@ -2191,8 +2191,9 @@ namespace Game.Entities public void SetWorldRotation(float qx, float qy, float qz, float qw) { - Quaternion rotation = new(qx, qy, qz, qw); - rotation.unitize(); + Quaternion rotation = new Quaternion(qx, qy, qz, qw); + rotation = Quaternion.Multiply(rotation, 1.0f / MathF.Sqrt(Quaternion.Dot(rotation, rotation))); + m_worldRotation.X = rotation.X; m_worldRotation.Y = rotation.Y; m_worldRotation.Z = rotation.Z; @@ -2207,7 +2208,7 @@ namespace Game.Entities public void SetWorldRotationAngles(float z_rot, float y_rot, float x_rot) { - Quaternion quat = Quaternion.fromEulerAnglesZYX(z_rot, y_rot, x_rot); + Quaternion quat = Quaternion.CreateFromRotationMatrix(Extensions.fromEulerAnglesZYX(z_rot, y_rot, x_rot)); SetWorldRotation(quat.X, quat.Y, quat.Z, quat.W); } diff --git a/Source/Game/Entities/GameObject/GameObjectData.cs b/Source/Game/Entities/GameObject/GameObjectData.cs index ca598893d..533230593 100644 --- a/Source/Game/Entities/GameObject/GameObjectData.cs +++ b/Source/Game/Entities/GameObject/GameObjectData.cs @@ -17,11 +17,10 @@ using Framework.Collections; using Framework.Constants; -using Framework.GameMath; -using System.Collections.Generic; -using System.Runtime.InteropServices; -using Game.Networking.Packets; using Game.Maps; +using Game.Networking.Packets; +using System.Numerics; +using System.Runtime.InteropServices; namespace Game.Entities { diff --git a/Source/Game/Entities/Item/AzeriteEmpoweredItem.cs b/Source/Game/Entities/Item/AzeriteEmpoweredItem.cs index 736138b8d..442b3aa38 100644 --- a/Source/Game/Entities/Item/AzeriteEmpoweredItem.cs +++ b/Source/Game/Entities/Item/AzeriteEmpoweredItem.cs @@ -15,13 +15,10 @@ * along with this program. If not, see . */ -using System; using System.Collections.Generic; -using System.Text; using Game.Networking; using Framework.Constants; using Framework.Database; -using Game.Networking.Packets; using Game.DataStorage; using System.Linq; diff --git a/Source/Game/Entities/Item/AzeriteItem.cs b/Source/Game/Entities/Item/AzeriteItem.cs index e0f7ebf87..bc0c90491 100644 --- a/Source/Game/Entities/Item/AzeriteItem.cs +++ b/Source/Game/Entities/Item/AzeriteItem.cs @@ -17,7 +17,6 @@ using System; using System.Collections.Generic; -using System.Text; using Game.Networking; using Framework.Constants; using Framework.Database; diff --git a/Source/Game/Entities/Object/Position.cs b/Source/Game/Entities/Object/Position.cs index 250e2834b..f285c56a3 100644 --- a/Source/Game/Entities/Object/Position.cs +++ b/Source/Game/Entities/Object/Position.cs @@ -15,9 +15,9 @@ * along with this program. If not, see . */ -using Framework.GameMath; using Game.Maps; using System; +using System.Numerics; namespace Game.Entities { diff --git a/Source/Game/Entities/Object/Update/UpdateFields.cs b/Source/Game/Entities/Object/Update/UpdateFields.cs index c37ce8403..1e78db046 100644 --- a/Source/Game/Entities/Object/Update/UpdateFields.cs +++ b/Source/Game/Entities/Object/Update/UpdateFields.cs @@ -17,13 +17,13 @@ using Framework.Constants; using Framework.Dynamic; -using Framework.GameMath; using Game.DataStorage; using Game.Networking; using Game.Networking.Packets; using Game.Spells; using System; using System.Collections.Generic; +using System.Numerics; namespace Game.Entities { diff --git a/Source/Game/Entities/Object/WorldObject.cs b/Source/Game/Entities/Object/WorldObject.cs index d9fe7a01f..36db6a4eb 100644 --- a/Source/Game/Entities/Object/WorldObject.cs +++ b/Source/Game/Entities/Object/WorldObject.cs @@ -17,7 +17,6 @@ using Framework.Constants; using Framework.Dynamic; -using Framework.GameMath; using Game.AI; using Game.BattleFields; using Game.DataStorage; @@ -28,6 +27,7 @@ using Game.Scenarios; using Game.Spells; using System; using System.Collections.Generic; +using System.Numerics; namespace Game.Entities { diff --git a/Source/Game/Entities/Player/Player.Items.cs b/Source/Game/Entities/Player/Player.Items.cs index a7ca7bd1a..4963eb554 100644 --- a/Source/Game/Entities/Player/Player.Items.cs +++ b/Source/Game/Entities/Player/Player.Items.cs @@ -17,7 +17,6 @@ using Framework.Constants; using Framework.Database; -using Game.Arenas; using Game.BattleGrounds; using Game.DataStorage; using Game.Groups; @@ -32,7 +31,6 @@ using System.Collections.Generic; using System.Linq; using System.Text; using Game.BattleFields; -using Framework.Dynamic; namespace Game.Entities { diff --git a/Source/Game/Entities/Player/Player.Spells.cs b/Source/Game/Entities/Player/Player.Spells.cs index 406b70209..6761f39e6 100644 --- a/Source/Game/Entities/Player/Player.Spells.cs +++ b/Source/Game/Entities/Player/Player.Spells.cs @@ -17,7 +17,6 @@ using Framework.Constants; using Framework.Dynamic; -using Game.BattlePets; using Game.DataStorage; using Game.Networking.Packets; using Game.Spells; diff --git a/Source/Game/Entities/Taxi/TaxiPathGraph.cs b/Source/Game/Entities/Taxi/TaxiPathGraph.cs index 781b31b2d..c13e7e0bc 100644 --- a/Source/Game/Entities/Taxi/TaxiPathGraph.cs +++ b/Source/Game/Entities/Taxi/TaxiPathGraph.cs @@ -18,10 +18,10 @@ using Framework.Algorithms; using Framework.Collections; using Framework.Constants; -using Framework.GameMath; using Game.DataStorage; using System; using System.Collections.Generic; +using System.Numerics; namespace Game.Entities { diff --git a/Source/Game/Entities/Transport.cs b/Source/Game/Entities/Transport.cs index 6c3f2fada..6c146d60e 100644 --- a/Source/Game/Entities/Transport.cs +++ b/Source/Game/Entities/Transport.cs @@ -16,12 +16,12 @@ */ using Framework.Constants; -using Framework.GameMath; using Game.DataStorage; using Game.Maps; using System; using System.Collections.Generic; using System.Linq; +using System.Numerics; namespace Game.Entities { @@ -132,7 +132,7 @@ namespace Game.Entities SetGoAnimProgress(animprogress); SetName(goinfo.name); SetWorldRotation(0.0f, 0.0f, 0.0f, 1.0f); - SetParentRotation(Quaternion.WAxis); + SetParentRotation(Quaternion.Identity); CreateModel(); return true; diff --git a/Source/Game/Entities/Unit/CharmInfo.cs b/Source/Game/Entities/Unit/CharmInfo.cs index 415b369c1..08c18dc22 100644 --- a/Source/Game/Entities/Unit/CharmInfo.cs +++ b/Source/Game/Entities/Unit/CharmInfo.cs @@ -17,10 +17,10 @@ using Framework.Collections; using Framework.Constants; -using Framework.GameMath; using Game.Networking; using Game.Spells; using System; +using System.Numerics; namespace Game.Entities { diff --git a/Source/Game/Entities/Unit/Unit.Fields.cs b/Source/Game/Entities/Unit/Unit.Fields.cs index 49d8e227c..caf43fa2c 100644 --- a/Source/Game/Entities/Unit/Unit.Fields.cs +++ b/Source/Game/Entities/Unit/Unit.Fields.cs @@ -17,13 +17,10 @@ using Framework.Collections; using Framework.Constants; -using Framework.Dynamic; using Game.AI; using Game.Combat; using Game.DataStorage; -using Game.Maps; using Game.Movement; -using Game.Networking; using Game.Networking.Packets; using Game.Spells; using System; diff --git a/Source/Game/Entities/Unit/Unit.Movement.cs b/Source/Game/Entities/Unit/Unit.Movement.cs index f924d2d3c..cc7210681 100644 --- a/Source/Game/Entities/Unit/Unit.Movement.cs +++ b/Source/Game/Entities/Unit/Unit.Movement.cs @@ -17,7 +17,6 @@ using Framework.Constants; using Framework.Dynamic; -using Framework.GameMath; using Game.BattleGrounds; using Game.DataStorage; using Game.Maps; @@ -26,6 +25,7 @@ using Game.Networking.Packets; using Game.Spells; using System; using System.Collections.Generic; +using System.Numerics; namespace Game.Entities { diff --git a/Source/Game/Entities/Unit/Unit.Pets.cs b/Source/Game/Entities/Unit/Unit.Pets.cs index 98ebf87f0..29a07ce06 100644 --- a/Source/Game/Entities/Unit/Unit.Pets.cs +++ b/Source/Game/Entities/Unit/Unit.Pets.cs @@ -17,7 +17,6 @@ using Framework.Constants; using Game.AI; -using Game.Movement; using Game.Networking.Packets; using Game.Spells; using System.Collections.Generic; diff --git a/Source/Game/Entities/Unit/Unit.cs b/Source/Game/Entities/Unit/Unit.cs index 4ce154884..f9167c9b7 100644 --- a/Source/Game/Entities/Unit/Unit.cs +++ b/Source/Game/Entities/Unit/Unit.cs @@ -16,8 +16,6 @@ */ using Framework.Constants; -using Framework.Dynamic; -using Framework.GameMath; using Game.AI; using Game.BattleGrounds; using Game.Chat; @@ -32,6 +30,7 @@ using Game.Spells; using System; using System.Collections.Generic; using System.Linq; +using System.Numerics; namespace Game.Entities { diff --git a/Source/Game/Garrisons/Garrisons.cs b/Source/Game/Garrisons/Garrisons.cs index a9ca1169e..b69f5eb85 100644 --- a/Source/Game/Garrisons/Garrisons.cs +++ b/Source/Game/Garrisons/Garrisons.cs @@ -18,7 +18,6 @@ using Framework.Constants; using Framework.Database; using Framework.Dynamic; -using Framework.GameMath; using Game.DataStorage; using Game.Entities; using Game.Maps; @@ -26,6 +25,7 @@ using Game.Networking.Packets; using System; using System.Collections.Generic; using System.Linq; +using System.Numerics; namespace Game.Garrisons { @@ -735,7 +735,7 @@ namespace Game.Garrisons if (finalizeInfo != null) { Position pos2 = finalizeInfo.factionInfo[faction].Pos; - GameObject finalizer = GameObject.CreateGameObject(finalizeInfo.factionInfo[faction].GameObjectId, map, pos2, Quaternion.fromEulerAnglesZYX(pos2.GetOrientation(), 0.0f, 0.0f), 255, GameObjectState.Ready); + GameObject finalizer = GameObject.CreateGameObject(finalizeInfo.factionInfo[faction].GameObjectId, map, pos2, Quaternion.CreateFromRotationMatrix(Extensions.fromEulerAnglesZYX(pos2.GetOrientation(), 0.0f, 0.0f)), 255, GameObjectState.Ready); if (finalizer) { // set some spell id to make the object delete itself after use diff --git a/Source/Game/Globals/ObjectManager.cs b/Source/Game/Globals/ObjectManager.cs index dee6570d1..502a2aad7 100644 --- a/Source/Game/Globals/ObjectManager.cs +++ b/Source/Game/Globals/ObjectManager.cs @@ -19,7 +19,6 @@ using Framework.Collections; using Framework.Constants; using Framework.Database; using Framework.Dynamic; -using Framework.GameMath; using Framework.IO; using Game.Conditions; using Game.DataStorage; @@ -32,6 +31,7 @@ using Game.Spells; using System; using System.Collections.Generic; using System.Linq; +using System.Numerics; using System.Runtime.InteropServices; namespace Game @@ -4257,10 +4257,10 @@ namespace Game gameObjectAddon.invisibilityValue = 1; } - if (!gameObjectAddon.ParentRotation.isUnit()) + if (!(Math.Abs(Quaternion.Dot(gameObjectAddon.ParentRotation, gameObjectAddon.ParentRotation) - 1) < 1e-5)) { Log.outError(LogFilter.Sql, $"GameObject (GUID: {guid}) has invalid parent rotation in `gameobject_addon`, set to default"); - gameObjectAddon.ParentRotation = Quaternion.WAxis; + gameObjectAddon.ParentRotation = Quaternion.Identity; } if (gameObjectAddon.WorldEffectID != 0 && !CliDB.WorldEffectStorage.ContainsKey(gameObjectAddon.WorldEffectID)) diff --git a/Source/Game/Handlers/AdventureJournalHandler.cs b/Source/Game/Handlers/AdventureJournalHandler.cs index d32d957de..14b8a2277 100644 --- a/Source/Game/Handlers/AdventureJournalHandler.cs +++ b/Source/Game/Handlers/AdventureJournalHandler.cs @@ -16,20 +16,10 @@ */ using Framework.Constants; -using Framework.IO; -using Game.BattleGrounds; using Game.DataStorage; -using Game.Entities; -using Game.Groups; -using Game.Guilds; -using Game.Maps; -using Game.Misc; using Game.Networking; using Game.Networking.Packets; -using Game.PvP; -using System; using System.Collections.Generic; -using System.Text; namespace Game { diff --git a/Source/Game/Handlers/AdventureMapHandler.cs b/Source/Game/Handlers/AdventureMapHandler.cs index d43de62b5..8d7c16cbc 100644 --- a/Source/Game/Handlers/AdventureMapHandler.cs +++ b/Source/Game/Handlers/AdventureMapHandler.cs @@ -16,20 +16,9 @@ */ using Framework.Constants; -using Framework.IO; -using Game.BattleGrounds; using Game.DataStorage; -using Game.Entities; -using Game.Groups; -using Game.Guilds; -using Game.Maps; -using Game.Misc; using Game.Networking; using Game.Networking.Packets; -using Game.PvP; -using System; -using System.Collections.Generic; -using System.Text; using System.Linq; namespace Game diff --git a/Source/Game/Handlers/CombatHandler.cs b/Source/Game/Handlers/CombatHandler.cs index a3e934adb..c65b0e2b4 100644 --- a/Source/Game/Handlers/CombatHandler.cs +++ b/Source/Game/Handlers/CombatHandler.cs @@ -20,7 +20,6 @@ using Game.DataStorage; using Game.Entities; using Game.Networking; using Game.Networking.Packets; -using System; namespace Game { diff --git a/Source/Game/Handlers/HotfixHandler.cs b/Source/Game/Handlers/HotfixHandler.cs index d4651c7bd..c6a78e64e 100644 --- a/Source/Game/Handlers/HotfixHandler.cs +++ b/Source/Game/Handlers/HotfixHandler.cs @@ -19,7 +19,6 @@ using Framework.Constants; using Game.DataStorage; using Game.Networking; using Game.Networking.Packets; -using System.Collections.Generic; namespace Game { diff --git a/Source/Game/Handlers/PetitionsHandler.cs b/Source/Game/Handlers/PetitionsHandler.cs index 5b0e4a103..aa0321a53 100644 --- a/Source/Game/Handlers/PetitionsHandler.cs +++ b/Source/Game/Handlers/PetitionsHandler.cs @@ -22,7 +22,6 @@ using Game.Guilds; using Game.Networking; using Game.Networking.Packets; using System.Collections.Generic; -using System.Text; namespace Game { diff --git a/Source/Game/Handlers/RAFHandler.cs b/Source/Game/Handlers/RAFHandler.cs index 798d04cb1..7a29a03d4 100644 --- a/Source/Game/Handlers/RAFHandler.cs +++ b/Source/Game/Handlers/RAFHandler.cs @@ -15,11 +15,6 @@ * along with this program. If not, see . */ -using Framework.Constants; -using Game.Entities; -using Game.Networking; -using Game.Networking.Packets; - namespace Game { public partial class WorldSession diff --git a/Source/Game/Loot/Loot.cs b/Source/Game/Loot/Loot.cs index 5a8673e9b..04785fc6a 100644 --- a/Source/Game/Loot/Loot.cs +++ b/Source/Game/Loot/Loot.cs @@ -16,7 +16,6 @@ */ using Framework.Constants; -using Framework.Database; using Framework.Dynamic; using Game.Conditions; using Game.Entities; diff --git a/Source/Game/Maps/GridMap.cs b/Source/Game/Maps/GridMap.cs index 6b0b27f14..ac79fc4e5 100644 --- a/Source/Game/Maps/GridMap.cs +++ b/Source/Game/Maps/GridMap.cs @@ -17,11 +17,12 @@ using Framework.Constants; using Framework.GameMath; +using Game.Collision; using Game.DataStorage; using System; using System.Collections.Generic; using System.IO; -using Game.Collision; +using System.Numerics; namespace Game.Maps { @@ -175,7 +176,7 @@ namespace Game.Maps _minHeightPlanes = new Plane[8]; for (uint quarterIndex = 0; quarterIndex < 8; ++quarterIndex) - _minHeightPlanes[quarterIndex] = new Plane( + _minHeightPlanes[quarterIndex] = Plane.CreateFromVertices( new Vector3(boundGridCoords[indices[quarterIndex][0]][0], boundGridCoords[indices[quarterIndex][0]][1], minHeights[indices[quarterIndex][0]]), new Vector3(boundGridCoords[indices[quarterIndex][1]][0], boundGridCoords[indices[quarterIndex][1]][1], minHeights[indices[quarterIndex][1]]), new Vector3(boundGridCoords[indices[quarterIndex][2]][0], boundGridCoords[indices[quarterIndex][2]][1], minHeights[indices[quarterIndex][2]]) @@ -500,8 +501,7 @@ namespace Game.Maps else quarterIndex = gx > gy ? 1u : 0; - - Ray ray = new(new Vector3(gx, gy, 0.0f), Vector3.ZAxis); + Ray ray = new(new Vector3(gx, gy, 0.0f), Vector3.UnitZ); return ray.intersection(_minHeightPlanes[quarterIndex]).Z; } diff --git a/Source/Game/Maps/Map.cs b/Source/Game/Maps/Map.cs index 74615d44c..97b19dd15 100644 --- a/Source/Game/Maps/Map.cs +++ b/Source/Game/Maps/Map.cs @@ -18,7 +18,6 @@ using Framework.Constants; using Framework.Database; using Framework.Dynamic; -using Framework.GameMath; using Game.BattleGrounds; using Game.Collision; using Game.DataStorage; @@ -32,6 +31,7 @@ using System.Collections; using System.Collections.Generic; using System.IO; using System.Linq; +using System.Numerics; namespace Game.Maps { diff --git a/Source/Game/Maps/TransportManager.cs b/Source/Game/Maps/TransportManager.cs index d34206479..55c915de0 100644 --- a/Source/Game/Maps/TransportManager.cs +++ b/Source/Game/Maps/TransportManager.cs @@ -17,13 +17,13 @@ using Framework.Constants; using Framework.Database; -using Framework.GameMath; using Game.DataStorage; using Game.Entities; using Game.Movement; using System; using System.Collections.Generic; using System.Linq; +using System.Numerics; namespace Game.Maps { @@ -186,9 +186,9 @@ namespace Game.Maps allPoints.Add(new Vector3(path[i].Loc.X, path[i].Loc.Y, path[i].Loc.Z)); // Add extra points to allow derivative calculations for all path nodes - allPoints.Insert(0, allPoints.First().lerp(allPoints[1], -0.2f)); - allPoints.Add(allPoints.Last().lerp(allPoints[^2], -0.2f)); - allPoints.Add(allPoints.Last().lerp(allPoints[^2], -1.0f)); + allPoints.Insert(0, Vector3.Lerp(allPoints.First(), allPoints[1], -0.2f)); + allPoints.Add(Vector3.Lerp(allPoints.Last(), allPoints[^2], -0.2f)); + allPoints.Add(Vector3.Lerp(allPoints.Last(), allPoints[^2], -1.0f)); SplineRawInitializer initer = new(allPoints); Spline orientationSpline = new(); diff --git a/Source/Game/Movement/AbstractFollower.cs b/Source/Game/Movement/AbstractFollower.cs index 52721f1fb..59ce08cb2 100644 --- a/Source/Game/Movement/AbstractFollower.cs +++ b/Source/Game/Movement/AbstractFollower.cs @@ -15,11 +15,6 @@ * along with this program. If not, see . */ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; using Game.Entities; namespace Game.Movement diff --git a/Source/Game/Movement/Generators/FlightPathMovementGenerator.cs b/Source/Game/Movement/Generators/FlightPathMovementGenerator.cs index f2642af72..03ca2d9e9 100644 --- a/Source/Game/Movement/Generators/FlightPathMovementGenerator.cs +++ b/Source/Game/Movement/Generators/FlightPathMovementGenerator.cs @@ -16,13 +16,13 @@ */ using Framework.Constants; -using Framework.GameMath; using Game.DataStorage; using Game.Entities; using Game.Maps; using System; using System.Collections.Generic; using System.Linq; +using System.Numerics; namespace Game.Movement { diff --git a/Source/Game/Movement/Generators/GenericMovementGenerator.cs b/Source/Game/Movement/Generators/GenericMovementGenerator.cs index 7bd6dd92f..26046bd89 100644 --- a/Source/Game/Movement/Generators/GenericMovementGenerator.cs +++ b/Source/Game/Movement/Generators/GenericMovementGenerator.cs @@ -17,7 +17,6 @@ using Framework.Constants; using Game.Entities; -using System; namespace Game.Movement { diff --git a/Source/Game/Movement/Generators/PathGenerator.cs b/Source/Game/Movement/Generators/PathGenerator.cs index ad21b85bf..04251fad3 100644 --- a/Source/Game/Movement/Generators/PathGenerator.cs +++ b/Source/Game/Movement/Generators/PathGenerator.cs @@ -16,10 +16,10 @@ */ using Framework.Constants; -using Framework.GameMath; using Game.Entities; using Game.Maps; using System; +using System.Numerics; namespace Game.Movement { @@ -481,7 +481,7 @@ namespace Game.Movement Vector3 endVec = new(endPoint[0], endPoint[1], endPoint[2]); Vector3 diffVec = (endVec - startVec); Vector3 prevVec = startVec; - float len = diffVec.GetLength(); + float len = diffVec.Length(); diffVec *= 4.0f / len; while (len > 4.0f) { @@ -861,7 +861,7 @@ namespace Game.Movement float Dist3DSqr(Vector3 p1, Vector3 p2) { - return (p1 - p2).GetLengthSquared(); + return (p1 - p2).LengthSquared(); } public void ShortenPathUntilDist(Position pos, float dist) { ShortenPathUntilDist(new Vector3(pos.posX, pos.posY, pos.posZ), dist); } @@ -878,11 +878,11 @@ namespace Game.Movement // the first point of the path must be outside the specified range // (this should have really been checked by the caller...) - if ((_pathPoints[0] - target).GetLengthSquared() < distSq) + if ((_pathPoints[0] - target).LengthSquared() < distSq) return; // check if we even need to do anything - if ((_pathPoints[0] - target).GetLengthSquared() >= distSq) + if ((_pathPoints[0] - target).LengthSquared() >= distSq) return; int i = _pathPoints.Length - 1; @@ -893,7 +893,7 @@ namespace Game.Movement while (true) { // we know that pathPoints[i] is too close already (from the previous iteration) - if ((_pathPoints[i - 1] - target).GetLengthSquared() >= distSq) + if ((_pathPoints[i - 1] - target).LengthSquared() >= distSq) break; // bingo! if (--i == 0) @@ -908,7 +908,7 @@ namespace Game.Movement // ok, _pathPoints[i] is too close, _pathPoints[i-1] is not, so our target point is somewhere between the two... // ... settle for a guesstimate since i'm not confident in doing trig on every chase motion tick... // (@todo review this) - _pathPoints[i] += (_pathPoints[i - 1] - _pathPoints[i]).directionOrZero() * (dist - (_pathPoints[i] - target).GetLength()); + _pathPoints[i] += (_pathPoints[i - 1] - _pathPoints[i]).directionOrZero() * (dist - (_pathPoints[i] - target).Length()); Array.Resize(ref _pathPoints, i + 1); } diff --git a/Source/Game/Movement/Generators/RandomMovement.cs b/Source/Game/Movement/Generators/RandomMovement.cs index 28b50924c..0f5a79c8c 100644 --- a/Source/Game/Movement/Generators/RandomMovement.cs +++ b/Source/Game/Movement/Generators/RandomMovement.cs @@ -17,7 +17,6 @@ using Framework.Constants; using Game.Entities; -using Game.Maps; using System; namespace Game.Movement diff --git a/Source/Game/Movement/Generators/SplineChainMovementGenerator.cs b/Source/Game/Movement/Generators/SplineChainMovementGenerator.cs index 65c474412..031bb60f9 100644 --- a/Source/Game/Movement/Generators/SplineChainMovementGenerator.cs +++ b/Source/Game/Movement/Generators/SplineChainMovementGenerator.cs @@ -16,11 +16,11 @@ */ using Framework.Constants; -using Framework.GameMath; using Game.AI; using Game.Entities; using System; using System.Collections.Generic; +using System.Numerics; namespace Game.Movement { diff --git a/Source/Game/Movement/MotionMaster.cs b/Source/Game/Movement/MotionMaster.cs index 19cc950f7..db68cf5db 100644 --- a/Source/Game/Movement/MotionMaster.cs +++ b/Source/Game/Movement/MotionMaster.cs @@ -16,13 +16,13 @@ */ using Framework.Constants; -using Framework.GameMath; using Game.AI; using Game.DataStorage; using Game.Entities; using System; using System.Collections.Generic; using System.Linq; +using System.Numerics; namespace Game.Movement { diff --git a/Source/Game/Movement/MoveSpline.cs b/Source/Game/Movement/MoveSpline.cs index 507e7ac29..8c9d25f4c 100644 --- a/Source/Game/Movement/MoveSpline.cs +++ b/Source/Game/Movement/MoveSpline.cs @@ -17,9 +17,9 @@ using Framework.Constants; using Framework.Dynamic; -using Framework.GameMath; using System; using System.Collections.Generic; +using System.Numerics; namespace Game.Movement { @@ -52,7 +52,7 @@ namespace Game.Movement effect_start_time = 0; spell_effect_extra = args.spellEffectExtra; anim_tier = args.animTier; - splineIsFacingOnly = args.path.Length == 2 && args.facing.type != MonsterMoveType.Normal && ((args.path[1] - args.path[0]).GetLength() < 0.1f); + splineIsFacingOnly = args.path.Length == 2 && args.facing.type != MonsterMoveType.Normal && ((args.path[1] - args.path[0]).Length() < 0.1f); // Check if its a stop spline if (args.flags.HasFlag(SplineFlag.Done)) diff --git a/Source/Game/Movement/MoveSplineInit.cs b/Source/Game/Movement/MoveSplineInit.cs index 729464ac4..58a496b70 100644 --- a/Source/Game/Movement/MoveSplineInit.cs +++ b/Source/Game/Movement/MoveSplineInit.cs @@ -16,10 +16,10 @@ */ using Framework.Constants; -using Framework.GameMath; using Game.Entities; using Game.Networking.Packets; using System; +using System.Numerics; namespace Game.Movement { diff --git a/Source/Game/Movement/MoveSplineInitArgs.cs b/Source/Game/Movement/MoveSplineInitArgs.cs index 9ec1e6834..de9db7c60 100644 --- a/Source/Game/Movement/MoveSplineInitArgs.cs +++ b/Source/Game/Movement/MoveSplineInitArgs.cs @@ -16,9 +16,8 @@ */ using Framework.Dynamic; -using Framework.GameMath; using Game.Entities; -using System; +using System.Numerics; namespace Game.Movement { @@ -80,7 +79,7 @@ namespace Game.Movement { if (path.Length > 2 || facing.type == Framework.Constants.MonsterMoveType.Normal) for (uint i = 0; i < path.Length - 1; ++i) - if ((path[i + 1] - path[i]).GetLength() < 0.1f) + if ((path[i + 1] - path[i]).Length() < 0.1f) return false; return true; } diff --git a/Source/Game/Movement/Spline.cs b/Source/Game/Movement/Spline.cs index 50341070e..7a9b1aae2 100644 --- a/Source/Game/Movement/Spline.cs +++ b/Source/Game/Movement/Spline.cs @@ -16,10 +16,10 @@ */ using Framework.Constants; -using Framework.GameMath; using Game.Entities; using Game.Maps; using System; +using System.Numerics; namespace Game.Movement { @@ -139,14 +139,14 @@ namespace Game.Movement if (cyclic_point == 0) points[0] = controls[count - 1]; else - points[0] = controls[0].lerp(controls[1], -1); + points[0] = Vector3.Lerp(controls[0], controls[1], -1); points[high_index + 1] = controls[cyclic_point]; points[high_index + 2] = controls[cyclic_point + 1]; } else { - points[0] = controls[0].lerp(controls[1], -1); + points[0] = Vector3.Lerp(controls[0], controls[1], -1); points[high_index + 1] = controls[count - 1]; } @@ -219,7 +219,7 @@ namespace Game.Movement } float SegLengthLinear(int index) { - return (points[index] - points[index + 1]).GetLength(); + return (points[index] - points[index + 1]).Length(); } float SegLengthCatmullRom(int index) { @@ -232,7 +232,7 @@ namespace Game.Movement while (i <= 3) { C_Evaluate(p, i / (float)3, s_catmullRomCoeffs, out nextPos); - length += (nextPos - curPos).GetLength(); + length += (nextPos - curPos).Length(); curPos = nextPos; ++i; } @@ -253,7 +253,7 @@ namespace Game.Movement while (i <= 3) { C_Evaluate(p, i / (float)3, s_Bezier3Coeffs, out nextPos); - length += (nextPos - curPos).GetLength(); + length += (nextPos - curPos).Length(); curPos = nextPos; ++i; } @@ -296,25 +296,25 @@ namespace Game.Movement return i; } - private static readonly Matrix4 s_catmullRomCoeffs = new(-0.5f, 1.5f, -1.5f, 0.5f, 1.0f, -2.5f, 2.0f, -0.5f, -0.5f, 0.0f, 0.5f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f); + private static readonly Matrix4x4 s_catmullRomCoeffs = new(-0.5f, 1.5f, -1.5f, 0.5f, 1.0f, -2.5f, 2.0f, -0.5f, -0.5f, 0.0f, 0.5f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f); - private static readonly Matrix4 s_Bezier3Coeffs = new(-1.0f, 3.0f, -3.0f, 1.0f, 3.0f, -6.0f, 3.0f, 0.0f, -3.0f, 3.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f); + private static readonly Matrix4x4 s_Bezier3Coeffs = new(-1.0f, 3.0f, -3.0f, 1.0f, 3.0f, -6.0f, 3.0f, 0.0f, -3.0f, 3.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f); - void C_Evaluate(Span vertice, float t, Matrix4 matr, out Vector3 result) + void C_Evaluate(Span vertice, float t, Matrix4x4 matr, out Vector3 result) { Vector4 tvec = new(t * t * t, t * t, t, 1.0f); - Vector4 weights = (tvec * matr); + Vector4 weights = Vector4.Transform(tvec, matr); - result = vertice[0] * weights[0] + vertice[1] * weights[1] - + vertice[2] * weights[2] + vertice[3] * weights[3]; + result = vertice[0] * weights.X + vertice[1] * weights.Y + + vertice[2] * weights.Z + vertice[3] * weights.W; } - void C_Evaluate_Derivative(Span vertice, float t, Matrix4 matr, out Vector3 result) + void C_Evaluate_Derivative(Span vertice, float t, Matrix4x4 matr, out Vector3 result) { Vector4 tvec = new(3.0f * t * t, 2.0f * t, 1.0f, 0.0f); - Vector4 weights = (tvec * matr); + Vector4 weights = Vector4.Transform(tvec, matr); - result = vertice[0] * weights[0] + vertice[1] * weights[1] - + vertice[2] * weights[2] + vertice[3] * weights[3]; + result = vertice[0] * weights.X + vertice[1] * weights.Y + + vertice[2] * weights.Z + vertice[3] * weights.W; } public int Length() { return lengths[index_hi];} diff --git a/Source/Game/Networking/Packets/AddonPackets.cs b/Source/Game/Networking/Packets/AddonPackets.cs index 9a5f86908..71cb2ac1f 100644 --- a/Source/Game/Networking/Packets/AddonPackets.cs +++ b/Source/Game/Networking/Packets/AddonPackets.cs @@ -15,12 +15,6 @@ * along with this program. If not, see . */ -using Framework.Constants; -using Game.Entities; -using System; -using System.Collections.Generic; -using Framework.Dynamic; - namespace Game.Networking.Packets { public struct AddOnInfo diff --git a/Source/Game/Networking/Packets/AdventureJournalPackets.cs b/Source/Game/Networking/Packets/AdventureJournalPackets.cs index 196bb872a..e972c1fcf 100644 --- a/Source/Game/Networking/Packets/AdventureJournalPackets.cs +++ b/Source/Game/Networking/Packets/AdventureJournalPackets.cs @@ -16,10 +16,6 @@ */ using Framework.Constants; -using Framework.Dynamic; -using Framework.GameMath; -using Game.Entities; -using System; using System.Collections.Generic; namespace Game.Networking.Packets diff --git a/Source/Game/Networking/Packets/AdventureMapPackets.cs b/Source/Game/Networking/Packets/AdventureMapPackets.cs index 7a41f3ee6..40fb58c92 100644 --- a/Source/Game/Networking/Packets/AdventureMapPackets.cs +++ b/Source/Game/Networking/Packets/AdventureMapPackets.cs @@ -15,13 +15,6 @@ * along with this program. If not, see . */ -using Framework.Constants; -using Framework.Dynamic; -using Framework.GameMath; -using Game.Entities; -using System; -using System.Collections.Generic; - namespace Game.Networking.Packets { class AdventureMapStartQuest : ClientPacket diff --git a/Source/Game/Networking/Packets/AreaTriggerPackets.cs b/Source/Game/Networking/Packets/AreaTriggerPackets.cs index 5ed87bef5..6aaba891d 100644 --- a/Source/Game/Networking/Packets/AreaTriggerPackets.cs +++ b/Source/Game/Networking/Packets/AreaTriggerPackets.cs @@ -17,10 +17,9 @@ using Framework.Constants; using Framework.Dynamic; -using Framework.GameMath; using Game.Entities; using System.Collections.Generic; - +using System.Numerics; namespace Game.Networking.Packets { diff --git a/Source/Game/Networking/Packets/AzeritePackets.cs b/Source/Game/Networking/Packets/AzeritePackets.cs index 19dae1a58..25c4c01b1 100644 --- a/Source/Game/Networking/Packets/AzeritePackets.cs +++ b/Source/Game/Networking/Packets/AzeritePackets.cs @@ -17,7 +17,6 @@ using Framework.Constants; using Game.Entities; -using Framework.Dynamic; namespace Game.Networking.Packets { diff --git a/Source/Game/Networking/Packets/BattleGroundPackets.cs b/Source/Game/Networking/Packets/BattleGroundPackets.cs index 2106f8743..cb81e6df0 100644 --- a/Source/Game/Networking/Packets/BattleGroundPackets.cs +++ b/Source/Game/Networking/Packets/BattleGroundPackets.cs @@ -17,10 +17,10 @@ using Framework.Constants; using Framework.Dynamic; -using Framework.GameMath; using Game.Entities; using System; using System.Collections.Generic; +using System.Numerics; namespace Game.Networking.Packets { diff --git a/Source/Game/Networking/Packets/CharacterPackets.cs b/Source/Game/Networking/Packets/CharacterPackets.cs index b1fe4e6ee..5d16725a9 100644 --- a/Source/Game/Networking/Packets/CharacterPackets.cs +++ b/Source/Game/Networking/Packets/CharacterPackets.cs @@ -18,11 +18,11 @@ using Framework.Constants; using Framework.Database; using Framework.Dynamic; -using Framework.GameMath; using Framework.IO; using Game.Entities; using System; using System.Collections.Generic; +using System.Numerics; namespace Game.Networking.Packets { diff --git a/Source/Game/Networking/Packets/ClientConfigPackets.cs b/Source/Game/Networking/Packets/ClientConfigPackets.cs index f985ea4df..6f8b8499b 100644 --- a/Source/Game/Networking/Packets/ClientConfigPackets.cs +++ b/Source/Game/Networking/Packets/ClientConfigPackets.cs @@ -18,7 +18,6 @@ using Framework.Constants; using Framework.IO; using Game.Entities; -using System.Collections.Generic; namespace Game.Networking.Packets { diff --git a/Source/Game/Networking/Packets/HotfixPackets.cs b/Source/Game/Networking/Packets/HotfixPackets.cs index ac1b3b3fc..24b59afaa 100644 --- a/Source/Game/Networking/Packets/HotfixPackets.cs +++ b/Source/Game/Networking/Packets/HotfixPackets.cs @@ -16,11 +16,8 @@ */ using Framework.Constants; -using Framework.Dynamic; using Framework.IO; -using Game.Entities; using System.Collections.Generic; -using System; using Game.DataStorage; namespace Game.Networking.Packets diff --git a/Source/Game/Networking/Packets/MiscPackets.cs b/Source/Game/Networking/Packets/MiscPackets.cs index c127884bd..db4535581 100644 --- a/Source/Game/Networking/Packets/MiscPackets.cs +++ b/Source/Game/Networking/Packets/MiscPackets.cs @@ -17,10 +17,10 @@ using Framework.Constants; using Framework.Dynamic; -using Framework.GameMath; using Game.Entities; using System; using System.Collections.Generic; +using System.Numerics; namespace Game.Networking.Packets { diff --git a/Source/Game/Networking/Packets/MovementPackets.cs b/Source/Game/Networking/Packets/MovementPackets.cs index 1bf9da8ed..2bb04a047 100644 --- a/Source/Game/Networking/Packets/MovementPackets.cs +++ b/Source/Game/Networking/Packets/MovementPackets.cs @@ -17,11 +17,11 @@ using Framework.Constants; using Framework.Dynamic; -using Framework.GameMath; using Game.Entities; using Game.Movement; -using System.Collections.Generic; using System; +using System.Collections.Generic; +using System.Numerics; namespace Game.Networking.Packets { diff --git a/Source/Game/Networking/Packets/NPCPackets.cs b/Source/Game/Networking/Packets/NPCPackets.cs index 3afcc8819..e82c56374 100644 --- a/Source/Game/Networking/Packets/NPCPackets.cs +++ b/Source/Game/Networking/Packets/NPCPackets.cs @@ -16,11 +16,11 @@ */ using Framework.Constants; -using Framework.GameMath; +using Framework.Dynamic; using Game.Entities; using System; using System.Collections.Generic; -using Framework.Dynamic; +using System.Numerics; namespace Game.Networking.Packets { diff --git a/Source/Game/Networking/Packets/PetPackets.cs b/Source/Game/Networking/Packets/PetPackets.cs index 2ccb367c8..189f353fa 100644 --- a/Source/Game/Networking/Packets/PetPackets.cs +++ b/Source/Game/Networking/Packets/PetPackets.cs @@ -16,10 +16,10 @@ */ using Framework.Constants; -using Framework.GameMath; using Game.Entities; using System; using System.Collections.Generic; +using System.Numerics; namespace Game.Networking.Packets { diff --git a/Source/Game/Networking/Packets/QueryPackets.cs b/Source/Game/Networking/Packets/QueryPackets.cs index 46051f9ba..3e96e8a10 100644 --- a/Source/Game/Networking/Packets/QueryPackets.cs +++ b/Source/Game/Networking/Packets/QueryPackets.cs @@ -18,12 +18,12 @@ using Framework.Collections; using Framework.Constants; using Framework.Dynamic; -using Framework.GameMath; using Framework.IO; using Game.Cache; using Game.Entities; using System; using System.Collections.Generic; +using System.Numerics; namespace Game.Networking.Packets { diff --git a/Source/Game/Networking/Packets/ReferAFriendPackets.cs b/Source/Game/Networking/Packets/ReferAFriendPackets.cs index 3646c369a..37ba9d51e 100644 --- a/Source/Game/Networking/Packets/ReferAFriendPackets.cs +++ b/Source/Game/Networking/Packets/ReferAFriendPackets.cs @@ -16,7 +16,6 @@ */ using Framework.Constants; -using Game.Entities; using System; namespace Game.Networking.Packets diff --git a/Source/Game/Networking/Packets/SpellPackets.cs b/Source/Game/Networking/Packets/SpellPackets.cs index be357e44d..bdacbc28e 100644 --- a/Source/Game/Networking/Packets/SpellPackets.cs +++ b/Source/Game/Networking/Packets/SpellPackets.cs @@ -17,11 +17,11 @@ using Framework.Constants; using Framework.Dynamic; -using Framework.GameMath; using Game.Entities; using Game.Spells; using System; using System.Collections.Generic; +using System.Numerics; namespace Game.Networking.Packets { diff --git a/Source/Game/Networking/Packets/TicketPackets.cs b/Source/Game/Networking/Packets/TicketPackets.cs index bb7c909eb..142d3c0ca 100644 --- a/Source/Game/Networking/Packets/TicketPackets.cs +++ b/Source/Game/Networking/Packets/TicketPackets.cs @@ -17,10 +17,10 @@ using Framework.Constants; using Framework.Dynamic; -using Framework.GameMath; using Game.Entities; using System; using System.Collections.Generic; +using System.Numerics; namespace Game.Networking.Packets { diff --git a/Source/Game/OutdoorPVP/OutdoorPvP.cs b/Source/Game/OutdoorPVP/OutdoorPvP.cs index 69f7080f9..340e5e146 100644 --- a/Source/Game/OutdoorPVP/OutdoorPvP.cs +++ b/Source/Game/OutdoorPVP/OutdoorPvP.cs @@ -17,7 +17,6 @@ using Framework.Constants; using Framework.Database; -using Framework.GameMath; using Game.Chat; using Game.DataStorage; using Game.Entities; @@ -29,6 +28,7 @@ using Game.Networking.Packets; using System; using System.Collections.Generic; using System.Linq; +using System.Numerics; namespace Game.PvP { diff --git a/Source/Game/Scenarios/ScenarioManager.cs b/Source/Game/Scenarios/ScenarioManager.cs index 1a9a2b373..a214de361 100644 --- a/Source/Game/Scenarios/ScenarioManager.cs +++ b/Source/Game/Scenarios/ScenarioManager.cs @@ -17,7 +17,6 @@ using Framework.Constants; using Framework.Database; -using Framework.GameMath; using Game.Achievements; using Game.DataStorage; using Game.Maps; diff --git a/Source/Game/Scripting/ScriptManager.cs b/Source/Game/Scripting/ScriptManager.cs index e8d46f5f0..fa025d51e 100644 --- a/Source/Game/Scripting/ScriptManager.cs +++ b/Source/Game/Scripting/ScriptManager.cs @@ -17,7 +17,6 @@ using Framework.Constants; using Framework.Database; -using Framework.GameMath; using Game.AI; using Game.BattleGrounds; using Game.Chat; @@ -35,6 +34,7 @@ using System.Collections; using System.Collections.Generic; using System.IO; using System.Linq; +using System.Numerics; using System.Reflection; namespace Game.Scripting diff --git a/Source/Game/Spells/Auras/AuraEffect.cs b/Source/Game/Spells/Auras/AuraEffect.cs index e645c2ab2..8429a0703 100644 --- a/Source/Game/Spells/Auras/AuraEffect.cs +++ b/Source/Game/Spells/Auras/AuraEffect.cs @@ -21,7 +21,6 @@ using Game.BattleFields; using Game.BattleGrounds; using Game.DataStorage; using Game.Entities; -using Game.Loots; using Game.Maps; using Game.Networking.Packets; using System; diff --git a/Source/Game/Spells/SpellEffects.cs b/Source/Game/Spells/SpellEffects.cs index ce1c0a484..112ea508f 100644 --- a/Source/Game/Spells/SpellEffects.cs +++ b/Source/Game/Spells/SpellEffects.cs @@ -17,7 +17,6 @@ using Framework.Constants; using Framework.Dynamic; -using Framework.GameMath; using Game.BattleGrounds; using Game.BattlePets; using Game.Combat; @@ -33,6 +32,7 @@ using Game.Networking.Packets; using System; using System.Collections.Generic; using System.Linq; +using System.Numerics; namespace Game.Spells { @@ -2705,7 +2705,7 @@ namespace Game.Spells Map map = target.GetMap(); Position pos = new(x, y, z, target.GetOrientation()); - Quaternion rotation = Quaternion.fromEulerAnglesZYX(target.GetOrientation(), 0.0f, 0.0f); + Quaternion rotation = Quaternion.CreateFromRotationMatrix(Extensions.fromEulerAnglesZYX(target.GetOrientation(), 0.0f, 0.0f)); GameObject go = GameObject.CreateGameObject((uint)effectInfo.MiscValue, map, pos, rotation, 255, GameObjectState.Ready); if (!go) return; @@ -3263,7 +3263,7 @@ namespace Game.Spells posZ = caster.GetPositionZ(), Orientation = caster.GetOrientation() }; - Quaternion rotation = Quaternion.fromEulerAnglesZYX(pos.GetOrientation(), 0.0f, 0.0f); + Quaternion rotation = Quaternion.CreateFromRotationMatrix(Extensions.fromEulerAnglesZYX(pos.GetOrientation(), 0.0f, 0.0f)); GameObject go = GameObject.CreateGameObject((uint)effectInfo.MiscValue, map, pos, rotation, 0, GameObjectState.Ready); if (!go) @@ -3620,7 +3620,7 @@ namespace Game.Spells Map map = m_caster.GetMap(); Position pos = new(x, y, z, m_caster.GetOrientation()); - Quaternion rotation = Quaternion.fromEulerAnglesZYX(m_caster.GetOrientation(), 0.0f, 0.0f); + Quaternion rotation = Quaternion.CreateFromRotationMatrix(Extensions.fromEulerAnglesZYX(m_caster.GetOrientation(), 0.0f, 0.0f)); GameObject go = GameObject.CreateGameObject((uint)effectInfo.MiscValue, map, pos, rotation, 255, GameObjectState.Ready); if (!go) return; @@ -4404,7 +4404,7 @@ namespace Game.Spells unitCaster.GetPosition(out fx, out fy, out fz); Position pos = new(fx, fy, fz, unitCaster.GetOrientation()); - Quaternion rotation = Quaternion.fromEulerAnglesZYX(unitCaster.GetOrientation(), 0.0f, 0.0f); + Quaternion rotation = Quaternion.CreateFromRotationMatrix(Extensions.fromEulerAnglesZYX(unitCaster.GetOrientation(), 0.0f, 0.0f)); GameObject go = GameObject.CreateGameObject(name_id, cMap, pos, rotation, 255, GameObjectState.Ready); if (!go) @@ -5201,7 +5201,7 @@ namespace Game.Spells Map map = m_caster.GetMap(); Position pos = new(x, y, z, m_caster.GetOrientation()); - Quaternion rot = Quaternion.fromEulerAnglesZYX(m_caster.GetOrientation(), 0.0f, 0.0f); + Quaternion rot = Quaternion.CreateFromRotationMatrix(Extensions.fromEulerAnglesZYX(m_caster.GetOrientation(), 0.0f, 0.0f)); GameObject go = GameObject.CreateGameObject(goId, map, pos, rot, 255, GameObjectState.Ready); if (!go) diff --git a/Source/Game/SupportSystem/SupportTickets.cs b/Source/Game/SupportSystem/SupportTickets.cs index df31b3475..f46d700c4 100644 --- a/Source/Game/SupportSystem/SupportTickets.cs +++ b/Source/Game/SupportSystem/SupportTickets.cs @@ -17,10 +17,10 @@ using Framework.Constants; using Framework.Database; -using Framework.GameMath; using Game.Chat; using Game.Entities; using Game.Networking.Packets; +using System.Numerics; using System.Text; namespace Game.SupportSystem diff --git a/Source/Scripts/Pets/Mage.cs b/Source/Scripts/Pets/Mage.cs index 3511ea6a8..d70565381 100644 --- a/Source/Scripts/Pets/Mage.cs +++ b/Source/Scripts/Pets/Mage.cs @@ -18,10 +18,7 @@ using Framework.Constants; using Game.AI; using Game.Entities; -using Game.Maps; using Game.Scripting; -using System.Collections.Generic; -using System; namespace Scripts.Pets { diff --git a/Source/Scripts/Spells/Hunter.cs b/Source/Scripts/Spells/Hunter.cs index ec313ac27..df3551bed 100644 --- a/Source/Scripts/Spells/Hunter.cs +++ b/Source/Scripts/Spells/Hunter.cs @@ -17,7 +17,6 @@ using Framework.Constants; using Game.Entities; -using Game.Maps; using Game.Scripting; using Game.Spells; using System; diff --git a/Source/Scripts/Spells/Items.cs b/Source/Scripts/Spells/Items.cs index 0d2a61c85..ffbb59ff7 100644 --- a/Source/Scripts/Spells/Items.cs +++ b/Source/Scripts/Spells/Items.cs @@ -16,7 +16,6 @@ */ using Framework.Constants; -using Framework.GameMath; using Game.BattleGrounds; using Game.DataStorage; using Game.Entities; @@ -26,6 +25,7 @@ using Game.Spells; using System; using System.Collections.Generic; using System.Linq; +using System.Numerics; namespace Scripts.Spells.Items { @@ -2345,7 +2345,7 @@ namespace Scripts.Spells.Items { if (target.IsDead() && !target.IsPet()) { - GetCaster().SummonGameObject(ObjectIds.ImprisonedDoomguard, target, Quaternion.fromEulerAnglesZYX(target.GetOrientation(), 0.0f, 0.0f), (uint)(target.GetRespawnTime() - GameTime.GetGameTime())); + GetCaster().SummonGameObject(ObjectIds.ImprisonedDoomguard, target, Quaternion.CreateFromRotationMatrix(Extensions.fromEulerAnglesZYX(target.GetOrientation(), 0.0f, 0.0f)), (uint)(target.GetRespawnTime() - GameTime.GetGameTime())); target.DespawnOrUnsummon(); } } diff --git a/Source/Scripts/Spells/Priest.cs b/Source/Scripts/Spells/Priest.cs index 94af1fe6c..fa76fb03e 100644 --- a/Source/Scripts/Spells/Priest.cs +++ b/Source/Scripts/Spells/Priest.cs @@ -16,7 +16,6 @@ */ using Framework.Constants; -using Framework.Dynamic; using Game.AI; using Game.Entities; using Game.Scripting; diff --git a/Source/Scripts/Spells/Warrior.cs b/Source/Scripts/Spells/Warrior.cs index 0f026140f..cc8c5ea85 100644 --- a/Source/Scripts/Spells/Warrior.cs +++ b/Source/Scripts/Spells/Warrior.cs @@ -16,13 +16,13 @@ */ using Framework.Constants; -using Framework.GameMath; using Game.Entities; using Game.Movement; using Game.Scripting; using Game.Spells; using System; using System.Collections.Generic; +using System.Numerics; namespace Scripts.Spells.Warrior { diff --git a/Source/Scripts/World/ItemScripts.cs b/Source/Scripts/World/ItemScripts.cs index 644de7685..50a5588b6 100644 --- a/Source/Scripts/World/ItemScripts.cs +++ b/Source/Scripts/World/ItemScripts.cs @@ -16,11 +16,12 @@ */ using Framework.Constants; -using Framework.GameMath; using Game.Entities; using Game.Scripting; using Game.Spells; +using System; using System.Collections.Generic; +using System.Numerics; namespace Scripts.World.ItemScripts { @@ -235,7 +236,7 @@ namespace Scripts.World.ItemScripts return true; go.GetClosePoint(out float x, out float y, out float z, go.GetCombatReach() / 3, 7.0f); - go.SummonGameObject(GameObjectIds.HighQualityFur, go, Quaternion.fromEulerAnglesZYX(go.GetOrientation(), 0.0f, 0.0f), 1); + go.SummonGameObject(GameObjectIds.HighQualityFur, go, Quaternion.CreateFromRotationMatrix(Extensions.fromEulerAnglesZYX(go.GetOrientation(), 0.0f, 0.0f)), 1); TempSummon summon = player.SummonCreature(CreatureIds.NesingwaryTrapper, x, y, z, go.GetOrientation(), TempSummonType.DeadDespawn, 1000); if (summon) { diff --git a/Source/Scripts/World/NpcSpecial.cs b/Source/Scripts/World/NpcSpecial.cs index 5c4761f62..1f3e250f9 100644 --- a/Source/Scripts/World/NpcSpecial.cs +++ b/Source/Scripts/World/NpcSpecial.cs @@ -16,18 +16,18 @@ */ using Framework.Constants; -using Framework.GameMath; +using Framework.Dynamic; using Game; using Game.AI; using Game.Entities; using Game.Maps; +using Game.Movement; using Game.Scripting; using Game.Spells; using System; using System.Collections.Generic; using System.Linq; -using Framework.Dynamic; -using Game.Movement; +using System.Numerics; namespace Scripts.World.NpcSpecial { @@ -1929,7 +1929,7 @@ namespace Scripts.World.NpcSpecial float displacement = 0.7f; for (byte i = 0; i < 4; i++) - me.SummonGameObject(GetFireworkGameObjectId(), me.GetPositionX() + (i % 2 == 0 ? displacement : -displacement), me.GetPositionY() + (i > 1 ? displacement : -displacement), me.GetPositionZ() + 4.0f, me.GetOrientation(), Quaternion.fromEulerAnglesZYX(me.GetOrientation(), 0.0f, 0.0f), 1); + me.SummonGameObject(GetFireworkGameObjectId(), me.GetPositionX() + (i % 2 == 0 ? displacement : -displacement), me.GetPositionY() + (i > 1 ? displacement : -displacement), me.GetPositionZ() + 4.0f, me.GetOrientation(), Quaternion.CreateFromRotationMatrix(Extensions.fromEulerAnglesZYX(me.GetOrientation(), 0.0f, 0.0f)), 1); } else //me.CastSpell(me, GetFireworkSpell(me.GetEntry()), true);