Example #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;
            }
        }
    }
Example #2
0
    void Start()
    {
        shapeComponent = GetComponent<FSShapeComponent>() as FSShapeComponent;
        bodyComponent = GetComponent<FSBodyComponent>() as FSBodyComponent;

        Body body = bodyComponent.PhysicsBody;

        fixtures = body.FixtureList;
        isSensor = new List<bool>(fixtures.Count);

        /* Save the initial state of all fixtures. */
        for (int i = 0; i < fixtures.Count; ++i) {
            isSensor.Add(fixtures[i].IsSensor);
        }
    }
Example #3
0
 public virtual void OnEnable()
 {
     target0 = target as FSShapeComponent;
     FSSettings.Load();
     categorySettings = FSSettings.CategorySettings;
 }
Example #4
0
    public virtual void Start()
    {
        if (initialized)
        {
            return;
        }
        initialized = true;
        //body = BodyFactory.CreateRectangle(FSWorldComponent.PhysicsWorld, 1f, 1f, Density);
        body = new Body(FSWorldComponent.PhysicsWorld);
        FSShapeComponent[] shapecs = GetComponentsInChildren <FSShapeComponent>();
        //print("shapes " + name + ": " + shapecs.Length);
        foreach (FSShapeComponent shp in shapecs)
        {
            Fixture f = body.CreateFixture(shp.GetShape());
            f.Friction    = shp.Friction;
            f.Restitution = shp.Restitution;
            if (shp.tag.Length > 0)
            {
                f.UserTag = shp.tag;
            }
            if (shp.CollisionFilter == CollisionGroupDef.Manually)
            {
                f.CollisionCategories = shp.BelongsTo;
                f.CollidesWith        = shp.CollidesWith;
            }
            else if (shp.CollisionFilter == CollisionGroupDef.PresetFile)
            {
                if (shp.CollisionGroup != null)
                {
                    f.CollisionCategories = shp.CollisionGroup.BelongsTo;
                    f.CollidesWith        = shp.CollisionGroup.CollidesWith;
                }
            }
        }
        // try to get a single shape at the same level
        // if theres no children
        if (shapecs.Length < 1)
        {
            FSShapeComponent shape = GetComponent <FSShapeComponent>();
            if (shape != null)
            {
                Fixture f = body.CreateFixture(shape.GetShape());
                f.Friction    = shape.Friction;
                f.Restitution = shape.Restitution;
                if (shape.tag.Length > 0)
                {
                    f.UserTag = shape.tag;
                }
                if (shape.CollisionFilter == CollisionGroupDef.Manually)
                {
                    f.CollisionCategories = shape.BelongsTo;
                    f.CollidesWith        = shape.CollidesWith;
                }
                else if (shape.CollisionFilter == CollisionGroupDef.PresetFile)
                {
                    if (shape.CollisionGroup != null)
                    {
                        f.CollisionCategories = shape.CollisionGroup.BelongsTo;
                        f.CollidesWith        = shape.CollisionGroup.CollidesWith;
                    }
                }
            }
        }

        body.BodyType = Type;
        body.Position = new FVector2(transform.position.x, transform.position.y);
        body.Rotation = transform.rotation.eulerAngles.z * Mathf.Deg2Rad;
        if (this.tag.Length > 0)
        {
            body.UserTag = this.tag;
        }
        body.UserFSBodyComponent = this;
    }
Example #5
0
	public virtual void OnEnable()
	{
		target0 = target as FSShapeComponent;
		FSSettings.Load();
		categorySettings = FSSettings.CategorySettings;
	}