예제 #1
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;
            }
        }
    }
예제 #2
0
    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]));
                }
            }
        }
    }
예제 #3
0
        private static List <FarseerPhysics.Common.Vertices> GetTris(IList <Atom.Math.IndexedTriangle> tris, FarseerPhysics.Common.Vertices vertices)
        {
            List <FarseerPhysics.Common.Vertices> res = new List <Vertices>();

            foreach (var tri in tris)
            {
                var v = new Vertices(new Microsoft.Xna.Framework.Vector2[] { vertices[tri.IndexA], vertices[tri.IndexB], vertices[tri.IndexC] });

                bool b = v.IsCounterClockWise();

                if (!v.IsCounterClockWise())
                {
                    v.Reverse();
                }

                res.Add(v);
            }

            return(res);
        }
예제 #4
0
        public ObjPolygon(List<Vector2> vertlist, BodyType bodyType, World worldPhysic)
        {
            objType = ObjectType.Polygon;
            this.vertlist = vertlist;
            FarseerPhysics.Common.Vertices vert = new FarseerPhysics.Common.Vertices(vertlist);
            List<FarseerPhysics.Common.Vertices> verts = EarclipDecomposer.ConvexPartition(vert);
            ObjectPhysic = FixtureFactory.CreateCompoundPolygon(worldPhysic, verts, 1.0f);
            Body_Type = bodyType;

            foreach (Fixture fixt in ObjectPhysic)
            {
                fixt.Body.BodyType = bodyType;
                if (bodyType != BodyType.Kinematic)
                    fixt.CollisionFilter.CollisionCategories = PolygonCategory;
                else
                {
                    fixt.CollisionFilter.CollisionCategories = PolygonCategory;
                    fixt.CollisionFilter.CollidesWith = PointCategory;
                }
            }
        }