Example #1
0
    void OnDrawGizmos()
    {
        if (lastContacts == null)
        {
            return;
        }
        foreach (Contact lastContact in lastContacts)
        {
            if (!lastContact.IsTouching())
            {
                return;
            }

            FarseerPhysics.Common.FixedArray2 <FVector2> contactPoints;
            FVector2 normal;
            lastContact.GetWorldManifold(out normal, out contactPoints);

            Vector3 p0 = FSHelper.FVector2ToVector3(contactPoints[0]);
            Vector3 p1 = FSHelper.FVector2ToVector3(contactPoints[1]);
            Vector3 pn = FSHelper.FVector2ToVector3(normal);
            Gizmos.color = Color.red;
            Gizmos.DrawWireSphere(p0, 0.15f);
            Gizmos.DrawLine(p0, p0 + pn * 2f);
            Gizmos.color = Color.yellow;
            Gizmos.DrawWireSphere(p1, 0.15f);
            Gizmos.DrawLine(p1, p1 + pn * 2f);
        }
    }
Example #2
0
    protected virtual void ConvertToConvex(FSConcaveShapeComponent targetCSC)
    {
        FSShapeComponent[] childcomps = targetCSC.GetComponentsInChildren <FSShapeComponent>();
        if (childcomps != null)
        {
            if (childcomps.Length > 0)
            {
                for (int i = 0; i < childcomps.Length; i++)
                {
                    if (childcomps[i] == null)
                    {
                        continue;
                    }
                    if (childcomps[i].gameObject == null)
                    {
                        continue;
                    }
                    DestroyImmediate(childcomps[i].gameObject);
                }
            }
        }
        // convert vertices
        FarseerPhysics.Common.Vertices concaveVertices = new FarseerPhysics.Common.Vertices();

        if (targetCSC.PointInput == FSShapePointInput.Transform)
        {
            for (int i = 0; i < targetCSC.TransformPoints.Length; i++)
            {
                concaveVertices.Add(FSHelper.Vector3ToFVector2(targetCSC.TransformPoints[i].localPosition));
            }
        }
        List <FarseerPhysics.Common.Vertices> convexShapeVs = FarseerPhysics.Common.Decomposition.BayazitDecomposer.ConvexPartition(concaveVertices);

        for (int i = 0; i < convexShapeVs.Count; i++)
        {
            GameObject newConvShape = new GameObject("convexShape" + i.ToString());
            newConvShape.transform.parent        = targetCSC.transform;
            newConvShape.transform.localPosition = Vector3.zero;
            newConvShape.transform.localRotation = Quaternion.Euler(Vector3.zero);
            newConvShape.transform.localScale    = Vector3.one;
            FSShapeComponent shape0 = newConvShape.AddComponent <FSShapeComponent>();
            shape0.CollidesWith     = targetCSC.CollidesWith;
            shape0.CollisionFilter  = targetCSC.CollisionFilter;
            shape0.BelongsTo        = targetCSC.BelongsTo;
            shape0.CollisionGroup   = targetCSC.CollisionGroup;
            shape0.Friction         = targetCSC.Friction;
            shape0.Restitution      = targetCSC.Restitution;
            shape0.Density          = targetCSC.Density;
            shape0.UseUnityCollider = false;
            shape0.PolygonPoints    = new Transform[convexShapeVs[i].Count];
            for (int j = 0; j < convexShapeVs[i].Count; j++)
            {
                GameObject pnew = new GameObject("p" + j.ToString());
                pnew.transform.parent        = shape0.transform;
                pnew.transform.localPosition = FSHelper.FVector2ToVector3(convexShapeVs[i][j]);
                shape0.PolygonPoints[j]      = pnew.transform;
            }
        }
    }
Example #3
0
        public static FDVertex FromLine(FVector2 begin, FVector2 end, Color color)
        {
            FDVertex fv = new FDVertex();

            fv.Position    = new Vector3[2];
            fv.Position[0] = FSHelper.FVector2ToVector3(begin);
            fv.Position[1] = FSHelper.FVector2ToVector3(end);
            fv.Color       = color;
            return(fv);
        }
Example #4
0
        public static FDVertex FromTriangleList(FVector2 p0, FVector2 p1, FVector2 p2, Color color)
        {
            FDVertex fv = new FDVertex();

            fv.Position    = new Vector3[3];
            fv.Position[0] = FSHelper.FVector2ToVector3(p0);
            fv.Position[1] = FSHelper.FVector2ToVector3(p1);
            fv.Position[2] = FSHelper.FVector2ToVector3(p2);
            fv.Color       = color;
            return(fv);
        }
    public void ConvertToConvex()
    {
        FSShapeComponent[] childFsShapes = GetComponentsInChildren <FSShapeComponent>();

        foreach (FSShapeComponent shapeComponent in childFsShapes)
        {
            if (shapeComponent.gameObject == null)
            {
                continue;
            }
            DestroyImmediate(shapeComponent.gameObject);
        }
        // convert vertices
        var concaveVertices = new FarseerPhysics.Common.Vertices();

        if (PointInput == FSShapePointInput.Transform)
        {
            for (int i = 0; i < PointsTransforms.Length; i++)
            {
                concaveVertices.Add(FSHelper.Vector3ToFVector2(PointsTransforms[i].localPosition));
            }
        }

        if (PointInput == FSShapePointInput.Vector2List)
        {
            foreach (var coordinate in PointsCoordinates)
            {
                concaveVertices.Add(FSHelper.Vector2ToFVector2(transform.TransformPoint(coordinate)));
            }
        }

        List <FarseerPhysics.Common.Vertices> convexShapeVs =
            FarseerPhysics.Common.Decomposition.BayazitDecomposer.ConvexPartition(concaveVertices);

        for (int i = 0; i < convexShapeVs.Count; i++)
        {
            var newConvShape = new GameObject("convexShape" + i.ToString());
            newConvShape.transform.parent        = transform;
            newConvShape.transform.localPosition = Vector3.zero;
            newConvShape.transform.localRotation = Quaternion.Euler(Vector3.zero);
            newConvShape.transform.localScale    = Vector3.one;

            var shapeComponent = newConvShape.AddComponent <FSShapeComponent>();
            shapeComponent.CollidesWith     = CollidesWith;
            shapeComponent.CollisionFilter  = CollisionFilter;
            shapeComponent.BelongsTo        = BelongsTo;
            shapeComponent.CollisionGroup   = CollisionGroup;
            shapeComponent.Friction         = Friction;
            shapeComponent.Restitution      = Restitution;
            shapeComponent.Density          = Density;
            shapeComponent.UseUnityCollider = false;
            shapeComponent.UseTransforms    = (PointInput == FSShapePointInput.Transform);

            if (PointInput == FSShapePointInput.Transform)
            {
                shapeComponent.PolygonTransforms = new Transform[convexShapeVs[i].Count];
                for (int j = 0; j < convexShapeVs[i].Count; j++)
                {
                    var pnew = new GameObject("p" + j.ToString(CultureInfo.InvariantCulture));
                    pnew.transform.parent               = shapeComponent.transform;
                    pnew.transform.localPosition        = FSHelper.FVector2ToVector3(convexShapeVs[i][j]);
                    shapeComponent.PolygonTransforms[j] = pnew.transform;
                }
            }
            else
            {
                shapeComponent.PolygonCoordinates = new Vector2[convexShapeVs[i].Count];
                for (int j = 0; j < convexShapeVs[i].Count; j++)
                {
                    shapeComponent.PolygonCoordinates[j] = newConvShape.transform.InverseTransformPoint(FSHelper.FVector2ToVector3(convexShapeVs[i][j]));
                }
            }
        }
    }