/
EugenyCh7
/
Puppet2D.Net
Обзор
Документация
Войти
/
EugenyCh7
/
Puppet2D.Net
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
Puppet2D.Engine/Common/Transform.cs
125 строк
4 KB
EugenyCh7
Transform fix
25 авг 2025, 22:13
25 авг 2025, 22:13
453f1b0
Код
Авторство
О чём код?
using Puppet2D.System; namespace Puppet2D.Engine.Common { /// <summary> /// Represents a 2D transformation consisting of position and rotation /// </summary> public struct Transform { /// <summary> /// The position component of the transform /// </summary> public Vector2 Position; /// <summary> /// The rotation component of the transform /// </summary> public Rotation Rotation; /// <summary> /// Initializes a new instance of the Transform struct with specified position and rotation /// </summary> /// <param name="position">The position component</param> /// <param name="rotation">The rotation component</param> public Transform(Vector2 position, Rotation rotation) { Position = position; Rotation = rotation; } /// <summary> /// Initializes a new instance of the Transform struct with specified position and angle /// </summary> /// <param name="position">The position component</param> /// <param name="angle">The rotation angle in radians</param> public Transform(Vector2 position, float angle) { Position = position; Rotation = new Rotation(angle); } /// <summary> /// Gets or sets the rotation angle in radians /// </summary> public float Angle { get => Rotation.Angle; set => Rotation.Angle = value; } /// <summary> /// Gets the identity transform (zero position, no rotation) /// </summary> public static Transform Identity { get => new Transform(Vector2.Zero, Rotation.Identity); } /// <summary> /// Transform a point (e.g. local space to world space) /// </summary> /// <param name="point">Point to transform</param> /// <returns>Transformed point in world space</returns> public Vector2 TransformPoint(Vector2 point) { float x = (Rotation.Cos * point.X - Rotation.Sin * point.Y) + Position.X; float y = (Rotation.Sin * point.X + Rotation.Cos * point.Y) + Position.Y; return new Vector2(x, y); } /// <summary> /// Inverse transform a point (e.g. world space to local space) /// </summary> /// <param name="point">Point to inverse transform</param> /// <returns>Transformed point in local space</returns> public Vector2 InvTransformPoint(Vector2 point) { float vx = point.X - Position.X; float vy = point.Y - Position.Y; return new Vector2( Rotation.Cos * vx + Rotation.Sin * vy, -Rotation.Sin * vx + Rotation.Cos * vy ); } /// <summary> /// Multiply two transforms. If the result is applied to a point p local to frame B, /// the transform would first convert p to a point local to frame A, then into a point /// in the world frame. /// v2 = A.q.Rot(B.q.Rot(v1) + B.p) + A.p /// = (A.q * B.q).Rot(v1) + A.q.Rot(B.p) + A.p /// </summary> /// <param name="A">First transform</param> /// <param name="B">Second transform</param> /// <returns>Combined transform representing A * B</returns> public static Transform MulTransforms(Transform A, Transform B) { Transform C; C.Rotation = A.Rotation.Mul(B.Rotation); C.Position = A.Rotation.RotateVector(B.Position) + A.Position; return C; } /// <summary> /// Creates a transform that converts a local point in frame B to a local point in frame A. /// v2 = A.q' * (B.q * v1 + B.p - A.p) /// = A.q' * B.q * v1 + A.q' * (B.p - A.p) /// </summary> /// <param name="A">First transform</param> /// <param name="B">Second transform</param> /// <returns>Relative transform from frame A to frame B</returns> public static Transform InvMulTransforms(Transform A, Transform B) { Transform C; C.Rotation = A.Rotation.InvMul(B.Rotation); C.Position = A.Rotation.InvRotateVector(B.Position - A.Position); return C; } public override string ToString() { return $"[Transform] Position({Position}) Rotation({Rotation})"; } } }