<commit_before>﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Myre.Extensions;
using Myre.Entities;
using Microsoft.Xna.Framework;
using Myre.Entities.Behaviours;

namespace Myre.Physics
{
    public class Transform
        : Behaviour
    {
        private Property<Vector2> position;
        private Property<float> rotation;
        private Property<Matrix> transform;
        private Property<Matrix> inverseTransform;

        public Vector2 Position
        {
            get { return position.Value; }
            set { position.Value = value; }
        }

        public float Rotation
        {
            get { return rotation.Value; }
            set { rotation.Value = value; }
        }

        public Matrix TransformMatrix
        {
            get { return transform.Value; }
            set { transform.Value = value; }
        }

        public Matrix InverseTransformMatrix
        {
            get { return inverseTransform.Value; }
            set { inverseTransform.Value = value; }
        }

        public override void CreateProperties(Entity.InitialisationContext context)
        {
            this.position = context.CreateProperty<Vector2>(PropertyName.POSITION);
            this.rotation = context.CreateProperty<float>(PropertyName.ROTATION);
            this.transform = context.CreateProperty<Matrix>("transform_matrix");
            this.inverseTransform = context.CreateProperty<Matrix>("inverse_transform_matrix");

            base.CreateProperties(context);
        }

        public override void Initialise()
        {
            position.PropertyChanged += _ => CalculateTransform();
            rotation.PropertyChanged += _ => CalculateTransform();
        }

        public Vector2 ToWorldCoordinates(Vector2 point)
        {
            return Vector2.Transform(point, transform.Value);
        }

        public Vector2 ToLocalCoordinates(Vector2 point)
        {
            return Vector2.Transform(point, inverseTransform.Value);
        }

        private void CalculateTransform()
        {
            var pos = new Vector3(position.Value, 0);
            var rot = rotation.Value;

            Matrix temp1, temp2;
            Matrix.CreateRotationZ(rot, out temp1);
            Matrix.CreateTranslation(ref pos, out temp2);
            Matrix.Multiply(ref temp1, ref temp2, out temp1);
            Matrix.Invert(ref temp1, out temp2);

            transform.Value = temp1;
            inverseTransform.Value = temp2;
        }
    }
}<commit_msg>* Modified Transform to recalculate the transform matrix less often. * Transform now negates the z rotation to simulate a left handed coordinate system.<commit_after>﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Myre.Extensions;
using Myre.Entities;
using Microsoft.Xna.Framework;
using Myre.Entities.Behaviours;
using Myre.Entities.Services;

namespace Myre.Physics
{
    [DefaultManager(typeof(Manager))]
    public class Transform
        : Behaviour
    {
        private Property<Vector2> position;
        private Property<float> rotation;
        private Property<Matrix> transform;
        private Property<Matrix> inverseTransform;
        private bool isDirty;

        public Vector2 Position
        {
            get { return position.Value; }
            set { position.Value = value; }
        }

        public float Rotation
        {
            get { return rotation.Value; }
            set { rotation.Value = value; }
        }

        public Matrix TransformMatrix
        {
            get { return transform.Value; }
            set { transform.Value = value; }
        }

        public Matrix InverseTransformMatrix
        {
            get { return inverseTransform.Value; }
            set { inverseTransform.Value = value; }
        }

        public override void CreateProperties(Entity.InitialisationContext context)
        {
            this.position = context.CreateProperty<Vector2>(PropertyName.POSITION);
            this.rotation = context.CreateProperty<float>(PropertyName.ROTATION);
            this.transform = context.CreateProperty<Matrix>("transform");
            this.inverseTransform = context.CreateProperty<Matrix>("inverse_transform");

            base.CreateProperties(context);
        }

        public override void Initialise()
        {
            position.PropertyChanged += _ => isDirty = true;
            rotation.PropertyChanged += _ => isDirty = true;
        }

        public Vector2 ToWorldCoordinates(Vector2 point)
        {
            return Vector2.Transform(point, transform.Value);
        }

        public Vector2 ToLocalCoordinates(Vector2 point)
        {
            return Vector2.Transform(point, inverseTransform.Value);
        }

        public void CalculateTransform()
        {
            if (!isDirty)
                return;

            var pos = new Vector3(position.Value, 0);
            var rot = rotation.Value;

            Matrix temp1, temp2;
            Matrix.CreateRotationZ(-rot, out temp1);
            Matrix.CreateTranslation(ref pos, out temp2);
            Matrix.Multiply(ref temp1, ref temp2, out temp1);
            Matrix.Invert(ref temp1, out temp2);

            transform.Value = temp1;
            inverseTransform.Value = temp2;
            isDirty = false;
        }

        public class Manager
            : BehaviourManager<Transform>, IProcess
        {
            public bool IsComplete
            {
                get { return false; }
            }

            public Manager(Scene scene)
            {
                scene.GetService<ProcessService>().Add(this);
            }

            public void Update(float time)
            {
                for (int i = 0; i < Behaviours.Count; i++)
                    Behaviours[i].CalculateTransform();
            }
        }
    }
}
{'fraction_non_alphanumeric': 0.08248816768086545, 'fraction_numerical': 0.007606490872210953, 'mean_word_length': 1.9190922545633942, 'pattern_counts': {'":': 0, '<': 22, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4600709', 'n_tokens_mistral': 1474, 'n_tokens_neox': 1422, 'n_words': 344}