Extended math lib
added GetRotacionFromNormal AlignRotacionToNormalAndSnapToGrid SnapToRotatedGrid SnapToRotatedGridWithOffset
This commit is contained in:
@@ -1479,6 +1479,47 @@ namespace FlaxEngine
|
||||
return results;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets rotacion for normal in relation to transform<br/>
|
||||
/// Funcion especially created for aligned with axis aligned faces
|
||||
/// use full with <seealso cref="Physics.RayCast"/>
|
||||
///
|
||||
/// <example><para><b>Example code:</b></para>
|
||||
/// <code>
|
||||
/// <see langword="public" /> <see langword="class" /> GetRotacionFromNormalExample : <see cref="Script"/><br/>
|
||||
/// <see langword="public" /> <see cref="Actor"/> RayOrgin;<br/>
|
||||
/// <see langword="public" /> <see cref="Actor"/> SomeObject;<br/>
|
||||
/// <see langword="public" /> <see langword="override" /> <see langword="void" /> <see cref="Script.OnFixedUpdate"/><br/>
|
||||
/// {<br/>
|
||||
/// <see langword="if" /> (<see cref="Physics"/>.RayCast(RayOrgin.Position, RayOrgin.Transform.Forward, out <see cref="RayCastHit"/> Hit)
|
||||
/// {<br/>
|
||||
/// <see cref="Vector3"/> position = Hit.Collider.Position;
|
||||
/// <see cref="Transform"/> transform = Hit.Collider.Transform;
|
||||
/// <see cref="Vector3"/> point = Hit.Point;
|
||||
/// <see cref="Vector3"/> normal = Hit.Normal;
|
||||
/// <see cref="Quaternion"/> rot = <see cref="Quaternion"/>.GetRotacionFromNormal(normal,transform);
|
||||
/// SomeObject.Position = point;
|
||||
/// SomeObject.Orientation = rot;
|
||||
/// }
|
||||
/// }
|
||||
/// }
|
||||
/// </code>
|
||||
/// </example>
|
||||
/// </summary>
|
||||
/// <param name="InNormal">the normal vector</param>
|
||||
/// <param name="InRefrenceTransform">relative to</param>
|
||||
/// <returns>normal as rotacion</returns>
|
||||
public static Quaternion GetRotacionFromNormal(Vector3 InNormal, Transform InRefrenceTransform)
|
||||
{
|
||||
Float3 up = InRefrenceTransform.Up;
|
||||
var dot = Vector3.Dot(InNormal, up);
|
||||
if (Mathf.NearEqual(Math.Abs(dot), 1))
|
||||
{
|
||||
up = InRefrenceTransform.Right;
|
||||
}
|
||||
return Quaternion.LookRotation(InNormal, up);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Adds two quaternions.
|
||||
/// </summary>
|
||||
|
||||
@@ -660,6 +660,27 @@ public:
|
||||
// @param roll The roll of rotation (in radians)
|
||||
// @param result When the method completes, contains the newly created quaternion
|
||||
static void RotationYawPitchRoll(float yaw, float pitch, float roll, Quaternion& result);
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Gets rotacion for normal in relation to transform<br/>
|
||||
/// Funcion especially created for aligned with axis aligned faces
|
||||
/// use full with <seealso cref="Physics::RayCast"/>
|
||||
/// </summary>
|
||||
/// <param name="InNormal">the normal vector</param>
|
||||
/// <param name="InRefrenceTransform">relative to</param>
|
||||
/// <returns>normal as rotacion</returns>
|
||||
static Quaternion GetRotacionFromNormal(const Vector3& InNormal, const Transform& InRefrenceTransform)
|
||||
{
|
||||
Float3 up = InRefrenceTransform.GetUp();
|
||||
auto dot = Vector3::Dot(InNormal, up);
|
||||
if (Math::NearEqual(Math::Abs(dot), 1))
|
||||
{
|
||||
up = InRefrenceTransform.GetRight();
|
||||
}
|
||||
return Quaternion::LookRotation(InNormal, up);
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
/// <summary>
|
||||
|
||||
@@ -477,6 +477,187 @@ namespace FlaxEngine
|
||||
Quaternion.Slerp(ref start.Orientation, ref end.Orientation, amount, out result.Orientation);
|
||||
Float3.Lerp(ref start.Scale, ref end.Scale, amount, out result.Scale);
|
||||
}
|
||||
/// <summary>
|
||||
/// combines funcions <br/>
|
||||
/// <see cref="Vector3.SnapToRotatedGridWithOffset"/>,<br/>
|
||||
/// <see cref="Quaternion.GetRotacionFromNormal"/>
|
||||
/// <example><para><b>Example code:</b></para>
|
||||
/// <code>
|
||||
/// <see langword="public" /> <see langword="class" /> AlignRotacionToObjectAndSnapToGridExample : <see cref="Script"/><br/>
|
||||
/// <see langword="public" /> <see cref="float"/> Offset = 50.0f;<br/>
|
||||
/// <see langword="public" /> <see cref="Vector3"/> GridSize = <see cref="Vector3.One"/> * 20.0f;<br/>
|
||||
/// <see langword="public" /> <see cref="Actor"/> RayOrgin;<br/>
|
||||
/// <see langword="public" /> <see cref="Actor"/> SomeObject;<br/>
|
||||
/// <see langword="public" /> <see langword="override" /> <see langword="void" /> <see cref="Script.OnFixedUpdate"/><br/>
|
||||
/// {<br/>
|
||||
/// <see langword="if" /> (<see cref="Physics"/>.RayCast(RayOrgin.Position, RayOrgin.Transform.Forward, out <see cref="RayCastHit"/> Hit)
|
||||
/// {<br/>
|
||||
/// <see cref="Transform"/> transform = Hit.Collider.Transform;
|
||||
/// <see cref="Vector3"/> point = Hit.Point;
|
||||
/// <see cref="Vector3"/> normal = Hit.Normal;
|
||||
/// SomeObject.Transform = <see cref="Transform"/>.AlignRotacionToNormalAndSnapToGrid
|
||||
/// (
|
||||
/// point,
|
||||
/// normal,
|
||||
/// Offset,
|
||||
/// transform,
|
||||
/// SomeObject.Scale,
|
||||
/// GridSize
|
||||
/// );
|
||||
/// }
|
||||
/// }
|
||||
/// }
|
||||
/// </code>
|
||||
/// </example>
|
||||
/// </summary>
|
||||
/// <param name="InPoint">The position to snap.</param>
|
||||
/// <param name="InGridSize">The size of the grid.</param>
|
||||
/// <param name="InNormalOffset">The local Z grid offset to applay after snaping</param>
|
||||
/// <param name="InNormal">Normal vector</param>
|
||||
/// <param name="InRelativeTo">Realative transform</param>
|
||||
/// <param name="InReturnScale">return scale</param>
|
||||
/// InRefrenceTransform
|
||||
/// <returns>rotated and snaped transform</returns>
|
||||
public static Transform AlignRotacionToNormalAndSnapToGrid(Vector3 InPoint, Vector3 InNormal, float InNormalOffset, Transform InRelativeTo, Float3 InReturnScale, Vector3 InGridSize)
|
||||
{
|
||||
Quaternion rot = Quaternion.GetRotacionFromNormal(InNormal, InRelativeTo);
|
||||
return new Transform(Vector3.SnapToRotatedGridWithOffset(InPoint, InRelativeTo.Translation, new Vector3(0, 0, InNormalOffset), rot, InGridSize), rot, InReturnScale);
|
||||
}
|
||||
/// <summary>
|
||||
/// combines funcions <br/>
|
||||
/// <see cref="Vector3.SnapToRotatedGridWithOffset"/>,<br/>
|
||||
/// <see cref="Quaternion.GetRotacionFromNormal"/>
|
||||
/// <example><para><b>Example code:</b></para>
|
||||
/// <code>
|
||||
/// <see langword="public" /> <see langword="class" /> AlignRotacionToObjectAndSnapToGridExample : <see cref="Script"/><br/>
|
||||
/// <see langword="public" /> <see cref="float"/> Offset = 50.0f;<br/>
|
||||
/// <see langword="public" /> <see cref="Vector3"/> GridSize = <see cref="Vector3.One"/> * 20.0f;<br/>
|
||||
/// <see langword="public" /> <see cref="Actor"/> RayOrgin;<br/>
|
||||
/// <see langword="public" /> <see cref="Actor"/> SomeObject;<br/>
|
||||
/// <see langword="public" /> <see langword="override" /> <see langword="void" /> <see cref="Script.OnFixedUpdate"/><br/>
|
||||
/// {<br/>
|
||||
/// <see langword="if" /> (<see cref="Physics"/>.RayCast(RayOrgin.Position, RayOrgin.Transform.Forward, out <see cref="RayCastHit"/> Hit)
|
||||
/// {<br/>
|
||||
/// <see cref="Transform"/> transform = Hit.Collider.Transform;
|
||||
/// <see cref="Vector3"/> point = Hit.Point;
|
||||
/// <see cref="Vector3"/> normal = Hit.Normal;
|
||||
/// SomeObject.Transform = <see cref="Transform"/>.AlignRotacionToNormalAndSnapToGrid
|
||||
/// (
|
||||
/// point,
|
||||
/// normal,
|
||||
/// Offset,
|
||||
/// transform,
|
||||
/// GridSize
|
||||
/// );
|
||||
/// }
|
||||
/// }
|
||||
/// }
|
||||
/// </code>
|
||||
/// </example>
|
||||
/// </summary>
|
||||
/// <param name="InPoint">The position to snap.</param>
|
||||
/// <param name="InGridSize">The size of the grid.</param>
|
||||
/// <param name="InNormalOffset">The local Z grid offset to applay after snaping</param>
|
||||
/// <param name="InNormal">Normal vector</param>
|
||||
/// <param name="InRelativeTo">Realative transform</param>
|
||||
/// InRefrenceTransform
|
||||
/// <returns>rotated and snaped transform with scale <see cref="Float3.One"/> </returns>
|
||||
public static Transform AlignRotacionToNormalAndSnapToGrid(Vector3 InPoint, Vector3 InNormal, float InNormalOffset, Transform InRelativeTo, Vector3 InGridSize)
|
||||
{
|
||||
Quaternion rot = Quaternion.GetRotacionFromNormal(InNormal, InRelativeTo);
|
||||
return new Transform(Vector3.SnapToRotatedGridWithOffset(InPoint, InRelativeTo.Translation, new Vector3(0, 0, InNormalOffset), rot, InGridSize), rot, Float3.One);
|
||||
}
|
||||
/// <summary>
|
||||
/// combines funcions <br/>
|
||||
/// <see cref="Vector3.SnapToRotatedGridWithOffset"/>,<br/>
|
||||
/// <see cref="Quaternion.GetRotacionFromNormal"/>
|
||||
/// <example><para><b>Example code:</b></para>
|
||||
/// <code>
|
||||
/// <see langword="public" /> <see langword="class" /> AlignRotacionToObjectAndSnapToGridExample : <see cref="Script"/><br/>
|
||||
/// <see langword="public" /> <see cref="Vector3"/> Offset = new Vector3(0, 0, 50f);<br/>
|
||||
/// <see langword="public" /> <see cref="Vector3"/> GridSize = <see cref="Vector3.One"/> * 20.0f;<br/>
|
||||
/// <see langword="public" /> <see cref="Actor"/> RayOrgin;<br/>
|
||||
/// <see langword="public" /> <see cref="Actor"/> SomeObject;<br/>
|
||||
/// <see langword="public" /> <see langword="override" /> <see langword="void" /> <see cref="Script.OnFixedUpdate"/><br/>
|
||||
/// {<br/>
|
||||
/// <see langword="if" /> (<see cref="Physics"/>.RayCast(RayOrgin.Position, RayOrgin.Transform.Forward, out <see cref="RayCastHit"/> Hit)
|
||||
/// {<br/>
|
||||
/// <see cref="Transform"/> transform = Hit.Collider.Transform;
|
||||
/// <see cref="Vector3"/> point = Hit.Point;
|
||||
/// <see cref="Vector3"/> normal = Hit.Normal;
|
||||
/// SomeObject.Transform = <see cref="Transform"/>.AlignRotacionToNormalAndSnapToGrid
|
||||
/// (
|
||||
/// point,
|
||||
/// normal,
|
||||
/// Offset,
|
||||
/// transform,
|
||||
/// SomeObject.Scale,
|
||||
/// GridSize
|
||||
/// );
|
||||
/// }
|
||||
/// }
|
||||
/// }
|
||||
/// </code>
|
||||
/// </example>
|
||||
/// </summary>
|
||||
/// <param name="InPoint">The position to snap.</param>
|
||||
/// <param name="InGridSize">The size of the grid.</param>
|
||||
/// <param name="InNormalOffset">The local Z grid offset to applay after snaping</param>
|
||||
/// <param name="InNormal">Normal vector</param>
|
||||
/// <param name="InRelativeTo">Realative transform</param>
|
||||
/// <param name="InReturnScale">return scale</param>
|
||||
/// InRefrenceTransform
|
||||
/// <returns>rotated and snaped transform</returns>
|
||||
public static Transform AlignRotacionToNormalAndSnapToGrid(Vector3 InPoint, Vector3 InNormal, Vector3 InNormalOffset, Transform InRelativeTo, Float3 InReturnScale, Vector3 InGridSize)
|
||||
{
|
||||
Quaternion rot = Quaternion.GetRotacionFromNormal(InNormal, InRelativeTo);
|
||||
return new Transform(Vector3.SnapToRotatedGridWithOffset(InPoint, InRelativeTo.Translation, InNormalOffset, rot, InGridSize), rot, InReturnScale);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// combines funcions <br/>
|
||||
/// <see cref="Vector3.SnapToRotatedGridWithOffset"/>,<br/>
|
||||
/// <see cref="Quaternion.GetRotacionFromNormal"/>
|
||||
/// <example><para><b>Example code:</b></para>
|
||||
/// <code>
|
||||
/// <see langword="public" /> <see langword="class" /> AlignRotacionToObjectAndSnapToGridExample : <see cref="Script"/><br/>
|
||||
/// <see langword="public" /> <see cref="Vector3"/> Offset = new Vector3(0, 0, 50f);<br/>
|
||||
/// <see langword="public" /> <see cref="Vector3"/> GridSize = <see cref="Vector3.One"/> * 20.0f;<br/>
|
||||
/// <see langword="public" /> <see cref="Actor"/> RayOrgin;<br/>
|
||||
/// <see langword="public" /> <see cref="Actor"/> SomeObject;<br/>
|
||||
/// <see langword="public" /> <see langword="override" /> <see langword="void" /> <see cref="Script.OnFixedUpdate"/><br/>
|
||||
/// {<br/>
|
||||
/// <see langword="if" /> (<see cref="Physics"/>.RayCast(RayOrgin.Position, RayOrgin.Transform.Forward, out <see cref="RayCastHit"/> Hit)
|
||||
/// {<br/>
|
||||
/// <see cref="Transform"/> transform = Hit.Collider.Transform;
|
||||
/// <see cref="Vector3"/> point = Hit.Point;
|
||||
/// <see cref="Vector3"/> normal = Hit.Normal;
|
||||
/// SomeObject.Transform = <see cref="Transform"/>.AlignRotacionToNormalAndSnapToGrid
|
||||
/// (
|
||||
/// point,
|
||||
/// normal,
|
||||
/// Offset,
|
||||
/// transform,
|
||||
/// GridSize
|
||||
/// );
|
||||
/// }
|
||||
/// }
|
||||
/// }
|
||||
/// </code>
|
||||
/// </example>
|
||||
/// </summary>
|
||||
/// <param name="InPoint">The position to snap.</param>
|
||||
/// <param name="InGridSize">The size of the grid.</param>
|
||||
/// <param name="InNormalOffset">The local grid offset to applay after snaping</param>
|
||||
/// <param name="InNormal">Normal vector</param>
|
||||
/// <param name="InRelativeTo">Realative transform</param>
|
||||
/// InRefrenceTransform
|
||||
/// <returns>rotated and snaped transform with scale <see cref="Float3.One"/> </returns>
|
||||
public static Transform AlignRotacionToNormalAndSnapToGrid(Vector3 InPoint, Vector3 InNormal, Vector3 InNormalOffset, Transform InRelativeTo, Vector3 InGridSize)
|
||||
{
|
||||
Quaternion rot = Quaternion.GetRotacionFromNormal(InNormal, InRelativeTo);
|
||||
return new Transform(Vector3.SnapToRotatedGridWithOffset(InPoint, InRelativeTo.Translation, InNormalOffset, rot, InGridSize), rot, Float3.One);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Tests for equality between two objects.
|
||||
|
||||
@@ -291,6 +291,77 @@ public:
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// combines funcions <br/>
|
||||
/// <see cref="SnapToRotatedGridWithOffset"/>,<br/>
|
||||
/// <see cref="GetRotacionFromNormal"/>
|
||||
/// </summary>
|
||||
/// <param name="InPoint">The position to snap.</param>
|
||||
/// <param name="InGridSize">The size of the grid.</param>
|
||||
/// <param name="InNormalOffset">The local Z grid offset to applay after snaping</param>
|
||||
/// <param name="InNormal">Normal vector</param>
|
||||
/// <param name="InRelativeTo">Realative transform</param>
|
||||
/// <param name="InReturnScale">return scale</param>
|
||||
/// InRefrenceTransform
|
||||
/// <returns>rotated and snaped transform</returns>
|
||||
static Transform AlignRotacionToNormalAndSnapToGrid(const Vector3& InPoint, const Vector3& InNormal, float InNormalOffset, const Transform& InRelativeTo, const Float3& InReturnScale, const Vector3& InGridSize)
|
||||
{
|
||||
Quaternion rot = Quaternion::GetRotacionFromNormal(InNormal, InRelativeTo);
|
||||
return Transform(Vector3::SnapToRotatedGridWithOffset(InPoint, InRelativeTo.Translation, Vector3(0, 0, InNormalOffset), rot, InGridSize), rot, InReturnScale);
|
||||
}
|
||||
/// <summary>
|
||||
/// combines funcions <br/>
|
||||
/// <see cref="SnapToRotatedGridWithOffset"/>,<br/>
|
||||
/// <see cref="GetRotacionFromNormal"/>
|
||||
/// </summary>
|
||||
/// <param name="InPoint">The position to snap.</param>
|
||||
/// <param name="InGridSize">The size of the grid.</param>
|
||||
/// <param name="InNormalOffset">The local Z grid offset to applay after snaping</param>
|
||||
/// <param name="InNormal">Normal vector</param>
|
||||
/// <param name="InRelativeTo">Realative transform</param>
|
||||
/// InRefrenceTransform
|
||||
/// <returns>rotated and snaped transform with scale <see cref="Float3.One"/> </returns>
|
||||
static Transform AlignRotacionToNormalAndSnapToGrid(const Vector3& InPoint, const Vector3& InNormal, float InNormalOffset, const Transform& InRelativeTo, const Vector3& InGridSize)
|
||||
{
|
||||
Quaternion rot = Quaternion::GetRotacionFromNormal(InNormal, InRelativeTo);
|
||||
return Transform(Vector3::SnapToRotatedGridWithOffset(InPoint, InRelativeTo.Translation, Vector3(0, 0, InNormalOffset), rot, InGridSize), rot, Float3::One);
|
||||
}
|
||||
/// <summary>
|
||||
/// combines funcions <br/>
|
||||
/// <see cref="SnapToRotatedGridWithOffset"/>,<br/>
|
||||
/// <see cref="GetRotacionFromNormal"/>
|
||||
/// </summary>
|
||||
/// <param name="InPoint">The position to snap.</param>
|
||||
/// <param name="InGridSize">The size of the grid.</param>
|
||||
/// <param name="InNormalOffset">The local Z grid offset to applay after snaping</param>
|
||||
/// <param name="InNormal">Normal vector</param>
|
||||
/// <param name="InRelativeTo">Realative transform</param>
|
||||
/// <param name="InReturnScale">return scale</param>
|
||||
/// InRefrenceTransform
|
||||
/// <returns>rotated and snaped transform</returns>
|
||||
static Transform AlignRotacionToNormalAndSnapToGrid(const Vector3& InPoint, const Vector3& InNormal, const Vector3& InNormalOffset, const Transform& InRelativeTo,const Float3& InReturnScale,const Vector3& InGridSize)
|
||||
{
|
||||
Quaternion rot = Quaternion::GetRotacionFromNormal(InNormal, InRelativeTo);
|
||||
return Transform(Vector3::SnapToRotatedGridWithOffset(InPoint, InRelativeTo.Translation, InNormalOffset, rot, InGridSize), rot, InReturnScale);
|
||||
}
|
||||
/// <summary>
|
||||
/// combines funcions <br/>
|
||||
/// <see cref="SnapToRotatedGridWithOffset"/>,<br/>
|
||||
/// <see cref="GetRotacionFromNormal"/>
|
||||
/// </summary>
|
||||
/// <param name="InPoint">The position to snap.</param>
|
||||
/// <param name="InGridSize">The size of the grid.</param>
|
||||
/// <param name="InNormalOffset">The local grid offset to applay after snaping</param>
|
||||
/// <param name="InNormal">Normal vector</param>
|
||||
/// <param name="InRelativeTo">Realative transform</param>
|
||||
/// InRefrenceTransform
|
||||
/// <returns>rotated and snaped transform with scale <see cref="Float3.One"/> </returns>
|
||||
static Transform AlignRotacionToNormalAndSnapToGrid(const Vector3& InPoint,const Vector3& InNormal,const Vector3& InNormalOffset, const Transform& InRelativeTo,const Vector3& InGridSize)
|
||||
{
|
||||
Quaternion rot = Quaternion::GetRotacionFromNormal(InNormal, InRelativeTo);
|
||||
return Transform(Vector3::SnapToRotatedGridWithOffset(InPoint, InRelativeTo.Translation, InNormalOffset, rot, InGridSize), rot, Float3::One);
|
||||
}
|
||||
|
||||
public:
|
||||
FORCE_INLINE Transform operator*(const Transform& other) const
|
||||
{
|
||||
|
||||
@@ -1685,6 +1685,80 @@ namespace FlaxEngine
|
||||
return pos;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Snaps the <paramref name="InPoint"/> on to rotate grid.<br/>
|
||||
/// for world aligned grid snapping use <b><see cref="Vector3.SnapToGrid"/></b> instead
|
||||
/// <example><para><b>Example code:</b></para>
|
||||
/// <code>
|
||||
/// <see langword="public" /> <see langword="class" /> SnapToRotatedGridExample : <see cref="Script"/><br/>
|
||||
/// <see langword="public" /> <see cref="Vector3"/> GridSize = <see cref="Vector3.One"/> * 20.0f;<br/>
|
||||
/// <see langword="public" /> <see cref="Actor"/> RayOrgin;<br/>
|
||||
/// <see langword="public" /> <see cref="Actor"/> SomeObject;<br/>
|
||||
/// <see langword="public" /> <see langword="override" /> <see langword="void" /> <see cref="Script.OnFixedUpdate"/><br/>
|
||||
/// {<br/>
|
||||
/// <see langword="if" /> (<see cref="Physics"/>.RayCast(RayOrgin.Position, RayOrgin.Transform.Forward, out <see cref="RayCastHit"/> Hit)
|
||||
/// {<br/>
|
||||
/// <see cref="Vector3"/> position = Hit.Collider.Position;
|
||||
/// <see cref="FlaxEngine.Transform"/> transform = Hit.Collider.Transform;
|
||||
/// <see cref="Vector3"/> point = Hit.Point;
|
||||
/// <see cref="Vector3"/> normal = Hit.Normal;
|
||||
/// //Get rotation from normal relative to collider transform
|
||||
/// <see cref="Quaternion"/> rot = <see cref="Quaternion"/>.GetRotacionFromNormal(normal,transform);
|
||||
/// point = <see cref="Vector3"/>.SnapToRotatedGrid(point,position,rot,GridSize);
|
||||
/// SomeObject.Position = point;
|
||||
/// }
|
||||
/// }
|
||||
/// }
|
||||
/// </code>
|
||||
/// </example>
|
||||
/// </summary>
|
||||
/// <param name="InPoint">The position to snap.</param>
|
||||
/// <param name="InCenterPoint">The center point.</param>
|
||||
/// <param name="InOrientation">The rotation of the grid.</param>
|
||||
/// <param name="InGridSize">The size of the grid.</param>
|
||||
/// <returns>The position snapped to the grid.</returns>
|
||||
public static Vector3 SnapToRotatedGrid(Vector3 InPoint, Vector3 InCenterPoint, Quaternion InOrientation, Vector3 InGridSize)
|
||||
{
|
||||
Vector3 p = (InPoint - InCenterPoint) * InOrientation.Conjugated();
|
||||
return (SnapToGrid(p, InGridSize) * InOrientation) + InCenterPoint;
|
||||
}
|
||||
/// <summary>
|
||||
/// The same as <see cref="SnapToRotatedGrid"/> but with local offset applied after point is spapend
|
||||
/// <example><para><b>Example code:</b></para>
|
||||
/// <code>
|
||||
/// <see langword="public" /> <see langword="class" /> SnapToRotatedGridWithOffsetExample : <see cref="Script"/><br/>
|
||||
/// <see langword="public" /> <see cref="Vector3"/> Offset = new Vector3(0, 0, 50f);<br/>
|
||||
/// <see langword="public" /> <see cref="Vector3"/> GridSize = <see cref="Vector3.One"/> * 20.0f;<br/>
|
||||
/// <see langword="public" /> <see cref="Actor"/> RayOrgin;<br/>
|
||||
/// <see langword="public" /> <see cref="Actor"/> SomeObject;<br/>
|
||||
/// <see langword="public" /> <see langword="override" /> <see langword="void" /> <see cref="Script.OnFixedUpdate"/><br/>
|
||||
/// {<br/>
|
||||
/// <see langword="if" /> (<see cref="Physics"/>.RayCast(RayOrgin.Position, RayOrgin.Transform.Forward, out <see cref="RayCastHit"/> Hit)
|
||||
/// {<br/>
|
||||
/// <see cref="Vector3"/> position = Hit.Collider.Position;
|
||||
/// <see cref="FlaxEngine.Transform"/> transform = Hit.Collider.Transform;
|
||||
/// <see cref="Vector3"/> point = Hit.Point;
|
||||
/// <see cref="Vector3"/> normal = Hit.Normal;
|
||||
/// <see cref="Quaternion"/> rot = <see cref="Quaternion"/>.GetRotacionFromNormal(normal,transform);
|
||||
/// point = <see cref="Vector3"/>.SnapToRotatedGridWithOffset(point,position,Offset,rot,GridSize);
|
||||
/// SomeObject.Position = point;
|
||||
/// }
|
||||
/// }
|
||||
/// }
|
||||
/// </code>
|
||||
/// </example>
|
||||
/// </summary>
|
||||
/// <param name="InPoint">The position to snap.</param>
|
||||
/// <param name="InCenterPoint">The center point.</param>
|
||||
/// <param name="InOrientation">The rotation of the grid.</param>
|
||||
/// <param name="InGridSize">The size of the grid.</param>
|
||||
/// <param name="InOffset">The local grid offset to applay after snaping</param>
|
||||
/// <returns></returns>
|
||||
public static Vector3 SnapToRotatedGridWithOffset(Vector3 InPoint, Vector3 InCenterPoint, Vector3 InOffset, Quaternion InOrientation, Vector3 InGridSize)
|
||||
{
|
||||
return ((SnapToGrid((InPoint - InCenterPoint) * InOrientation.Conjugated(), InGridSize) + InOffset) * InOrientation) + InCenterPoint;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Adds two vectors.
|
||||
/// </summary>
|
||||
|
||||
@@ -927,6 +927,47 @@ public:
|
||||
/// <param name="to">The second vector.</param>
|
||||
/// <returns>The angle (in radians).</returns>
|
||||
static FLAXENGINE_API T Angle(const Vector3Base& from, const Vector3Base& to);
|
||||
|
||||
/// <summary>
|
||||
/// Snaps the input position into the grid.
|
||||
/// </summary>
|
||||
/// <param name="pos">The position to snap.</param>
|
||||
/// <param name="gridSize">The size of the grid.</param>
|
||||
/// <returns>The position snapped to the grid.</returns>
|
||||
static Vector3 SnapToGrid(const Vector3& pos, Vector3 gridSize)
|
||||
{
|
||||
pos.X = Math::Ceil((pos.X - (gridSize.X * 0.5f)) / gridSize.X) * gridSize.X;
|
||||
pos.Y = Math::Ceil((pos.Y - (gridSize.Y * 0.5f)) / gridSize.Y) * gridSize.Y;
|
||||
pos.Z = Math::Ceil((pos.Z - (gridSize.Z * 0.5f)) / gridSize.Z) * gridSize.Z;
|
||||
return pos;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Snaps the <paramref name="InPoint"/> on to rotate grid.<br/>
|
||||
/// for world aligned grid snapping use <b><see cref="Vector3::SnapToGrid"/></b> instead
|
||||
/// </summary>
|
||||
/// <param name="InPoint">The position to snap.</param>
|
||||
/// <param name="InCenterPoint">The center point.</param>
|
||||
/// <param name="InOrientation">The rotation of the grid.</param>
|
||||
/// <param name="InGridSize">The size of the grid.</param>
|
||||
/// <returns>The position snapped to the grid.</returns>
|
||||
static Vector3 SnapToRotatedGrid(const Vector3& InPoint, const Vector3& InCenterPoint, const Quaternion& InOrientation, const Vector3& InGridSize)
|
||||
{
|
||||
return (Vector3::SnapToGrid((InPoint - InCenterPoint) * InOrientation.Conjugated(), InGridSize) * InOrientation) + InCenterPoint;
|
||||
}
|
||||
/// <summary>
|
||||
/// The same as <see cref="SnapToRotatedGrid"/> but with local offset applied after point is spapend
|
||||
/// </summary>
|
||||
/// <param name="InPoint">The position to snap.</param>
|
||||
/// <param name="InCenterPoint">The center point.</param>
|
||||
/// <param name="InOrientation">The rotation of the grid.</param>
|
||||
/// <param name="InGridSize">The size of the grid.</param>
|
||||
/// <param name="InOffset">The local grid offset to applay after snaping</param>
|
||||
/// <returns></returns>
|
||||
static Vector3 SnapToRotatedGridWithOffset(const Vector3& InPoint, const Vector3& InCenterPoint, const Vector3& InOffset, const Quaternion& InOrientation, const Vector3& InGridSize)
|
||||
{
|
||||
return ((Vector3::SnapToGrid((InPoint - InCenterPoint) * InOrientation.Conjugated(), InGridSize) + InOffset) * InOrientation) + InCenterPoint;
|
||||
}
|
||||
};
|
||||
|
||||
template<typename T>
|
||||
|
||||
Reference in New Issue
Block a user