Пример #1
0
        /// <summary>
        /// builds the Polygon edge normals. These are lazily created and updated only by the edgeNormals getter
        /// </summary>
        void buildEdgeNormals()
        {
            // for boxes we only require 2 edges since the other 2 are parallel
            var totalEdges = isBox ? 2 : points.Length;

            if (_edgeNormals == null || _edgeNormals.Length != totalEdges)
            {
                _edgeNormals = new Vector2[totalEdges];
            }

            Vector2 p2;

            for (var i = 0; i < totalEdges; i++)
            {
                var p1 = points[i];
                if (i + 1 >= points.Length)
                {
                    p2 = points[0];
                }
                else
                {
                    p2 = points[i + 1];
                }

                var perp = Vector2Ext.perpendicular(ref p1, ref p2);
                Vector2Ext.normalize(ref perp);
                _edgeNormals[i] = perp;
            }

            return;
        }
Пример #2
0
        void preparePolygonForCollisionChecks()
        {
            // we need to setup a Polygon with one edge. It needs the center to be in the opposite direction of it's normal.
            // this is necessary so that SAT knows which way to calculate the MTV, which uses Shape positions.
            var perp = Vector2Ext.perpendicular(_particleTwo.position - _particleOne.position);

            perp.Normalize();

            // set our Polygon points
            var midPoint = Vector2.Lerp(_particleOne.position, _particleTwo.position, 0.5f);

            _polygon.position  = midPoint + perp * 50;
            _polygon.points[0] = _particleOne.position - _polygon.position;
            _polygon.points[1] = _particleTwo.position - _polygon.position;
            _polygon.recalculateCenterAndEdgeNormals();
        }