/// <summary>
        /// Gets the anchor which belongs to the passed link target
        /// </summary>
        /// <param name="linkTarget">link target</param>
        /// <returns>anchor instance</returns>
        private AnchorShape GetAnchorFromLink(ShapeLink linkTarget)
        {
            // Get the anchor shape which is connected to the passed link
            AnchorShape anchorShape = linkTarget.OwnerShape as AnchorShape;

            Debug.Assert(anchorShape != null);

            return(anchorShape);
        }
        /// <summary>
        /// Gets the engine instance which belongs to the passed anchor, or NULL if the anchor
        /// is not attached to an entity.
        /// </summary>
        /// <param name="linkTarget">link target</param>
        /// <returns>Entity engine instance, or NULL if anchor is not attached to an entity</returns>
        private EngineInstanceEntity GetEntityFromAnchor(AnchorShape anchorShape)
        {
            // Get the entity shape which holds the anchor
            EntityShape entityShape = anchorShape.Parent as EntityShape;

            if (entityShape == null)
            {
                return(null);
            }

            // Next get the engine instance of the entity shape
            EngineInstanceEntity engineEntityInstance = entityShape._engineInstance as EngineInstanceEntity;

            if (engineEntityInstance == null)
            {
                return(null);
            }

            return(engineEntityInstance);
        }
        /// <summary>
        /// Performs the actual linking on engine instances
        /// </summary>
        /// <param name="src"></param>
        /// <param name="target"></param>
        public override void OnLink(ShapeLink src, ShapeLink target)
        {
            base.OnLink(src, target);

            // Ignore links which don't belong to us
            if (src.OwnerShape != this)
            {
                return;
            }

            // Get the anchor the target link points to
            AnchorShape anchorShape = GetAnchorFromLink(target);

            if (anchorShape == null || !HasEngineInstance())
            {
                return;
            }

            // Get the entity instance the target points to
            EngineInstanceEntity entityInstance = GetEntityFromAnchor(anchorShape);

            // Add the actor to the constraint
            if (entityInstance != null)
            {
                // We have an anchor which belongs to an entity. Add the entity to the constraint
                // and pass the anchor position, relative to the parent entity.
                Vector3F anchorPositionLS = anchorShape.LocalSpacePosition;
                bool     bResult          = EngineConstraintChainInstance.AddEntityAnchor(
                    (long)anchorShape.UniqueID, entityInstance, anchorPositionLS);
                Debug.Assert(bResult == true);
            }
            else
            {
                // Our anchor is statically attached to the world.
                // Add the static world anchor to the constraint. Pass the world space position of the anchor
                // for this purpose.
                Vector3F anchorPositionWS = anchorShape.Position;
                bool     bResult          = EngineConstraintChainInstance.AddWorldAnchor((long)anchorShape.UniqueID, anchorShape);
                Debug.Assert(bResult == true);
            }
        }
        /// <summary>
        /// unlinks a target from a source. Either src or target has this shape as owner
        /// </summary>
        /// <param name="src">the link source</param>
        /// <param name="target">the link target</param>
        public override void OnUnlink(ShapeLink src, ShapeLink target)
        {
            base.OnUnlink(src, target);

            // Ignore links which don't belong to us
            if (src.OwnerShape != this)
            {
                return;
            }

            // Get the anchor the target link points to
            AnchorShape anchorShape = GetAnchorFromLink(target);

            if (anchorShape == null || !HasEngineInstance())
            {
                return;
            }

            bool bResult = EngineConstraintChainInstance.RemoveAnchor((long)anchorShape.UniqueID);

            Debug.Assert(bResult == true);
        }
        /// <summary>
        /// Gets the engine instance which belongs to the passed anchor, or NULL if the anchor
        /// is not attached to an entity.
        /// </summary>
        /// <param name="linkTarget">link target</param>
        /// <returns>Entity engine instance, or NULL if anchor is not attached to an entity</returns>
        private EngineInstanceEntity GetEntityFromAnchor(AnchorShape anchorShape)
        {
            // Get the entity shape which holds the anchor
              EntityShape entityShape = anchorShape.Parent as EntityShape;
              if (entityShape == null)
            return null;

              // Next get the engine instance of the entity shape
              EngineInstanceEntity engineEntityInstance = entityShape._engineInstance as EngineInstanceEntity;
              if (engineEntityInstance == null)
            return null;

              return engineEntityInstance;
        }