コード例 #1
0
        public static DistanceConstraint CreateJoint(this World world, Vertex v0, Vertex v1)
        {
            var constraint = new DistanceConstraint(
                v0,
                v1,
                false
                );

            world.AddGlobalConstraint(constraint);
            return(constraint);
        }
コード例 #2
0
        public static bool CheckCollide(Body b0, Body b1)
        {
            if (
                !(0 > Mathf.Abs(b1.center.x - b0.center.x) - (b1.halfExtent.x + b0.halfExtent.x) &&
                  0 > Mathf.Abs(b1.center.y - b0.center.y) - (b1.halfExtent.y + b0.halfExtent.y))
                )
            {
                return(false); // no aabb overlap
            }

            var minDistance = float.MaxValue;
            var n0          = b0.edges.Length;
            var n1          = b1.edges.Length;
            var dist        = float.MaxValue;

            // Iterate through all of the edges of both bodies
            for (var i = 0; i < n1 + n0; i++)
            {
                // get edge
                var edge = i < n0 ? b0.edges[i] : b1.edges[i - n0];

                // Calculate the perpendicular to this edge and normalize it
                m_TestAxis = MathUtils.Normal(edge.v0.position, edge.v1.position);

                // Project both bodies onto the normal
                b0.ProjectAxis(m_TestAxis);
                b1.ProjectAxis(m_TestAxis);

                //Calculate the distance between the two intervals
                dist = b0.min < b1.min ? b1.min - b0.max : b0.min - b1.max;

                // If the intervals don't overlap, return, since there is no collision
                if (dist > 0)
                {
                    return(false);
                }
                else if (Mathf.Abs(dist) < minDistance)
                {
                    minDistance = Mathf.Abs(dist);

                    // Save collision information
                    m_Axis = m_TestAxis;
                    m_Edge = edge;
                }
            }

            m_Depth = minDistance;

            // Ensure collision edge in B1 and collision vertex in B0
            if (m_Edge.parent != b1)
            {
                var t = b1;
                b1 = b0;
                b0 = t;
            }

            // Make sure that the collision normal is pointing at B1
            m_Center = b0.center - b1.center;
            var n = Vector2.Dot(m_Center, m_Axis);

            // Revert the collision normal if it points away from B1
            if (n < 0)
            {
                m_Axis = MathUtils.Neg(m_Axis);
            }

            var smallestDist = float.MaxValue;

            for (var i = 0; i < b0.vertices.Length; i++)
            {
                // Measure the distance of the vertex from the line using the line equation
                var v = b0.vertices[i];
                m_Line = v.position - b1.center;
                dist   = Vector2.Dot(m_Axis, m_Line);

                // Set the smallest distance and the collision vertex
                if (dist < smallestDist)
                {
                    smallestDist = dist;
                    m_Vertex     = v;
                }
            }
            m_Body = b0;

            // There is no separating axis. Report a collision!
            return(true);
        }