private Rectangle CalculateBoundingBox(IHasTransform parent, IPerPixelCollisionComponent collisionComponent)
        {
            var transform =
                Matrix.CreateTranslation(-new Vector3(collisionComponent.RotationAnchor ?? Vector2.Zero, 0)) *
                Matrix.CreateFromQuaternion(parent.FinalTransform.AbsoluteRotation) *
                //Matrix.CreateTranslation(new Vector3(collisionComponent.RotationAnchor ?? Vector2.Zero, 0)) *
                Matrix.CreateTranslation(parent.FinalTransform.AbsolutePosition);

            var width  = collisionComponent.GetPixelWidth();
            var height = collisionComponent.GetPixelHeight();

            var tl = Vector2.Transform(Vector2.Zero, transform);
            var tr = Vector2.Transform(new Vector2(width, 0), transform);
            var bl = Vector2.Transform(new Vector2(0, height), transform);
            var br = Vector2.Transform(new Vector2(width, height), transform);

            var minX = Math.Min(Math.Min(tl.X, tr.X), Math.Min(bl.X, br.X));
            var minY = Math.Min(Math.Min(tl.Y, tr.Y), Math.Min(bl.Y, br.Y));
            var maxX = Math.Max(Math.Max(tl.X, tr.X), Math.Max(bl.X, br.X));
            var maxY = Math.Max(Math.Max(tl.Y, tr.Y), Math.Max(bl.Y, br.Y));

            return(new Rectangle(
                       (int)Math.Floor(minX),
                       (int)Math.Floor(minY),
                       (int)Math.Ceiling(maxX) - (int)Math.Floor(minX),
                       (int)Math.Ceiling(maxY) - (int)Math.Floor(minY)));
        }
Beispiel #2
0
        public void UnregisterRigidBodyForHasMatrix(RigidBody rigidBody, IHasTransform hasTransform)
        {
            Func <RigidBodyMapping, bool> predicate1 = x =>
            {
                IHasTransform hasTransform2;
                if (x.HasTransform.TryGetTarget(out hasTransform2))
                {
                    return(x.RigidBody != rigidBody || hasTransform2 != hasTransform);
                }
                return(true);
            };

            if (_rigidBodyMappings.All(predicate1))
            {
                throw new InvalidOperationException();
            }

            Predicate <RigidBodyMapping> predicate2 = x =>
            {
                IHasTransform hasTransform2;
                if (x.HasTransform.TryGetTarget(out hasTransform2))
                {
                    return(x.RigidBody == rigidBody && hasTransform2 == hasTransform);
                }
                return(false);
            };

            _rigidBodyMappings.RemoveAll(predicate2);
            _physicsWorld.RemoveBody(rigidBody);
        }
Beispiel #3
0
        public virtual bool SetParent(IHasTransform transform)
        {
            bool done = true;

            if (transform == null)
            {
                IHasTransform obj = null;

                if (Parent != null)
                {
                    obj = ((Transform)Parent.Transform).Children.SingleOrDefault(t => t.Transform == this);
                }

                if (obj != null)
                {
                    ((Transform)Parent.Transform).Children.Remove(obj);
                }

                Parent = null;
            }
            else
            {
                if (Parent != null && !((Transform)Parent.Transform).Children.Contains(transform))
                {
                    ((Transform)Parent.Transform).Children.Add(transform);
                }
                Parent = transform;
            }

            return(done);
        }
 public RigidBodyMapping(RigidBody rigidBody, IHasTransform hasTransform, bool staticAndImmovable)
 {
     RigidBody            = rigidBody;
     HasTransform         = hasTransform;
     StaticAndImmovable   = staticAndImmovable;
     PerformedInitialSync = false;
 }
Beispiel #5
0
 public RigidBodyMapping(RigidBody rigidBody, IHasTransform hasTransform, bool staticAndImmovable)
 {
     RigidBody            = rigidBody;
     HasTransform         = new WeakReference <IHasTransform>(hasTransform);
     StaticAndImmovable   = staticAndImmovable;
     PerformedInitialSync = false;
 }
Beispiel #6
0
        public static Point TransformPos(this IHasTransform self, double x, double y)
        {
            var s = self.ScaleTransform.ScaleX;

            return(new Point(
                       (x - self.TranslateTransform.X) / s,
                       (y - self.TranslateTransform.Y) / s));
        }
Beispiel #7
0
        internal FrameworkElementBag(IHasTransform transform)
        {
            var g = new TransformGroup();

            g.Children.Add(transform.ScaleTransform);
            g.Children.Add(transform.TranslateTransform);
            RenderTransform = g;
        }
        public static IFinalTransform Create(IHasTransform detachedChild)
        {
            var attachedTransform = new DefaultFinalTransform
            {
                _child = detachedChild
            };

            return(attachedTransform);
        }
        public static IFinalTransform Create(IHasTransform parent, IHasTransform child)
        {
            var attachedTransform = new DefaultFinalTransform
            {
                _parent = parent,
                _child  = child
            };

            return(attachedTransform);
        }
        public void RegisterRigidBodyForHasMatrix(RigidBody rigidBody, IHasTransform hasTransform, bool staticAndImmovable)
        {
            if (_rigidBodyMappings.Any(x => x.RigidBody == rigidBody && x.HasTransform == hasTransform))
            {
                throw new InvalidOperationException();
            }

            _rigidBodyMappings.Add(new RigidBodyMapping(rigidBody, hasTransform, staticAndImmovable));
            _physicsWorld.AddBody(rigidBody);
        }
        public void UnregisterRigidBodyForHasMatrix(RigidBody rigidBody, IHasTransform hasTransform)
        {
            if (_rigidBodyMappings.All(x => x.RigidBody != rigidBody || x.HasTransform != hasTransform))
            {
                throw new InvalidOperationException();
            }

            _rigidBodyMappings.RemoveAll(x => x.RigidBody == rigidBody && x.HasTransform == hasTransform);
            _physicsWorld.RemoveBody(rigidBody);
        }
Beispiel #12
0
        public static ImmutableRect_double TransformRect(this IHasTransform self, double w, double h)
        {
            var s = self.ScaleTransform.ScaleX;

            return(new ImmutableRect_double(
                       -self.TranslateTransform.X / s,
                       -self.TranslateTransform.Y / s,
                       w / s,
                       h / s));
        }
Beispiel #13
0
        /// <summary>
        /// Returns a final transform by combining the final transform of the parent in the hierarchy (if the parent
        /// node has a transform), and the local transform of this object.  You should use this method to implement
        /// <see cref="IHasTransform.FinalTransform"/> if your object resides in the hierarchy.
        /// </summary>
        /// <param name="hasTransform">The current object.</param>
        /// <param name="node">
        /// The node in the dependency injection hierarchy that points to this object.  This value
        /// can be obtained by injecting <see cref="INode"/> into the constructor of your object.
        /// </param>
        /// <returns>A final computed transform.</returns>
        public static IFinalTransform GetAttachedFinalTransformImplementation(this IHasTransform hasTransform, INode node)
        {
            var parentHasTransform = node?.Parent?.UntypedValue as IHasTransform;

            if (parentHasTransform != null)
            {
                return(DefaultFinalTransform.Create(parentHasTransform, hasTransform));
            }

            return(DefaultFinalTransform.Create(hasTransform));
        }
Beispiel #14
0
        public void RegisterRigidBodyForHasMatrix(RigidBody rigidBody, IHasTransform hasTransform, bool staticAndImmovable)
        {
            if (_rigidBodyMappings.Any(x =>
            {
                IHasTransform hasTransform2;
                if (x.HasTransform.TryGetTarget(out hasTransform2))
                {
                    return(x.RigidBody == rigidBody && hasTransform2 == hasTransform);
                }
                return(false);
            }))
            {
                throw new InvalidOperationException();
            }

            _rigidBodyMappings.Add(new RigidBodyMapping(rigidBody, hasTransform, staticAndImmovable));
            _physicsWorld.AddBody(rigidBody);
        }
 public void UnregisterRigidBodyForHasMatrixInCurrentWorld(RigidBody rigidBody, IHasTransform hasTransform)
 {
     _shadowWorld.UnregisterRigidBodyForHasMatrix(rigidBody, hasTransform);
 }
 public void RegisterRigidBodyForHasMatrixInCurrentWorld(RigidBody rigidBody, IHasTransform hasTransform, bool staticAndImmovable)
 {
     _shadowWorld.RegisterRigidBodyForHasMatrix(rigidBody, hasTransform, staticAndImmovable);
 }
Beispiel #17
0
 /// <summary>
 /// ctor
 /// </summary>
 public Transform(IHasTransform owner) : this()
 {
     Owner = owner;
 }
 public TemporaryFinalTransform(IHasTransform parent, IHasTransform child)
 {
     _parent = parent;
     _child  = child;
 }
Beispiel #19
0
 public static ImmutableRect_double TransformRect(this IHasTransform self, in ImmutableRect_double rect)
 public DefaultFinalTransform(IHasTransform currentObject, INode currentNode)
 {
     _currentObject = currentObject;
     _currentNode   = currentNode;
 }
Beispiel #21
0
 /// <summary>
 /// Gets a final transform which is just representative of the local transform.  This method should be used
 /// sparingly, but is intended when either you know the parent of this object will have no transform (i.e.
 /// you are implementing an entity which resides directly in the world), or when there's no way for the caller
 /// to know it's position in the hierarchy.
 /// </summary>
 /// <param name="hasTransform">The current object.</param>
 /// <returns>A final computed transform.</returns>
 public static IFinalTransform GetDetachedFinalTransformImplementation(this IHasTransform hasTransform)
 {
     return(DefaultFinalTransform.Create(hasTransform));
 }
        private bool IntersectPixels(
            IHasTransform aParent,
            IPerPixelCollisionComponent a,
            IHasTransform bParent,
            IPerPixelCollisionComponent b)
        {
            var transformA =
                Matrix.CreateTranslation(-new Vector3(a.RotationAnchor ?? Vector2.Zero, 0)) *
                Matrix.CreateFromQuaternion(aParent.FinalTransform.AbsoluteRotation) *
                //Matrix.CreateTranslation(new Vector3(a.RotationAnchor ?? Vector2.Zero, 0)) *
                Matrix.CreateTranslation(aParent.FinalTransform.AbsolutePosition);
            var transformB =
                Matrix.CreateTranslation(-new Vector3(b.RotationAnchor ?? Vector2.Zero, 0)) *
                Matrix.CreateFromQuaternion(bParent.FinalTransform.AbsoluteRotation) *
                //Matrix.CreateTranslation(new Vector3(b.RotationAnchor ?? Vector2.Zero, 0)) *
                Matrix.CreateTranslation(bParent.FinalTransform.AbsolutePosition);

            var widthA  = a.GetPixelWidth();
            var heightA = a.GetPixelHeight();
            var widthB  = b.GetPixelWidth();
            var heightB = b.GetPixelHeight();

            var dataA = a.GetPixelData();
            var dataB = b.GetPixelData();

            var transformAToB = transformA * Matrix.Invert(transformB);

            var stepX = Vector2.TransformNormal(Vector2.UnitX, transformAToB);
            var stepY = Vector2.TransformNormal(Vector2.UnitY, transformAToB);

            var yPosInB = Vector2.Transform(Vector2.Zero, transformAToB);

            for (var yA = 0; yA < heightA; yA++)
            {
                var posInB = yPosInB;

                for (var xA = 0; xA < widthA; xA++)
                {
                    var xB = (int)Math.Round(posInB.X);
                    var yB = (int)Math.Round(posInB.Y);

                    if (0 <= xB && xB < widthB &&
                        0 <= yB && yB < heightB)
                    {
                        var colorA = dataA[xA + yA * widthA];
                        var colorB = dataB[xB + yB * widthB];

                        if (colorA.A != 0 && colorB.A != 0)
                        {
                            return(true);
                        }
                    }

                    posInB += stepX;
                }

                yPosInB += stepY;
            }

            return(false);
        }
Beispiel #23
0
 public void RegisterRigidBodyForHasMatrixInCurrentWorld(RigidBody rigidBody, IHasTransform hasTransform, bool staticAndImmovable)
 {
     _shadowWorld?.RegisterRigidBodyForHasMatrix(rigidBody, hasTransform, staticAndImmovable);
 }
Beispiel #24
0
 public void UnregisterRigidBodyForHasMatrixInCurrentWorld(RigidBody rigidBody, IHasTransform hasTransform)
 {
     _shadowWorld?.UnregisterRigidBodyForHasMatrix(rigidBody, hasTransform);
 }