Esempio n. 1
0
 public void AddBodyToRope(BodyGroup playerGroup, Body body)
 {
     if (playerGroup == BodyGroup.red)
     {
         foreach (BodySpot found in redTeamSpotList)
         {
             if (!found.isTaken)
             {
                 body.transform.position = found.transform.position;
                 body.transform.SetParent(found.transform.parent);
                 found.containedBody = body;
                 found.isTaken       = true;
                 bodysConnected.Add(body);
                 redForce += body.scaleMass * nutonRatio;
                 ApplyForceToRope(true, true, body.scaleMass);
                 break;
             }
             else
             {
                 Debug.Log("ListIsFull");
             }
         }
     }
     else if (playerGroup == BodyGroup.blue)
     {
         foreach (BodySpot found in blueTeamSpotList)
         {
             if (!found.isTaken)
             {
                 body.transform.position = found.transform.position;
                 body.transform.SetParent(found.transform.parent);
                 found.containedBody = body;
                 found.isTaken       = true;
                 bodysConnected.Add(body);
                 blueForce += body.scaleMass * nutonRatio;
                 ApplyForceToRope(true, false, body.scaleMass);
                 break;
             }
             else
             {
                 Debug.Log("ListIsFull");
             }
         }
     }
     else
     {
         Debug.Log("how the f**k did you do that");
     }
 }
Esempio n. 2
0
    public void setup_with_body(FlatBodyObject aBody, bool aUseGravity = true)
    {
        mFlat = aBody;
        //construct the bodies
        foreach (var e in mImportant)
        {
            //note this will cerate 'dummy bodies' representing hand/ankle/head
            BodyGroup bg = new BodyGroup();
            bg.offset             = aBody.mParts[e.Key].transform.rotation.flat_rotation();
            bg.body               = BodyFactory.CreateBody(FSWorldComponent.PhysicsWorld, mFlat.mParts[e.Key].transform.position.toFV2());
            bg.body.Mass          = 25;
            bg.body.Friction      = .5f;
            bg.body.IgnoreGravity = !aUseGravity;
            bg.body.BodyType      = FarseerPhysics.Dynamics.BodyType.Dynamic;
            mBodies[e.Key]        = bg;

            //TODO why do I still have this? can probably delete
            //new GameObject(e.Key.ToString()).transform.position = mFlat.mParts[e.Key].transform.position;
        }

        //now create fixtures
        foreach (var e in mImportant)
        {
            if (e.Value.otherEnds.Count > 0)
            {
                List <FarseerPhysics.Common.Vertices> poly = new List <FarseerPhysics.Common.Vertices>();
                poly.Add(new FarseerPhysics.Common.Vertices());
                if (e.Key != ZgJointId.Torso || e.Value.otherEnds.Count == 1)                //torso does not need this point
                {
                    poly[0].Add(FVector2.Zero);
                }
                if (e.Value.otherEnds.Count == 1)
                {
                    //line version
                    //foreach(var f in e.Value.otherEnds)
                    //	poly[0].Add(mFlat.mParts[f].transform.position.toFV2()-mBodies[e.Key].body.Position);

                    //block version
                    FVector2 diff = mFlat.mParts[e.Value.otherEnds[0]].transform.position.toFV2() - mBodies[e.Key].body.Position;
                    FVector2 perp = new FVector2(diff.Y, -diff.X);
                    perp.Normalize();
                    float stretch = .03f;
                    poly[0].Add(perp * stretch);
                    poly[0].Add(diff + perp * stretch);
                    poly[0].Add(diff - perp * stretch);
                    poly[0].Add(-perp * stretch);
                }
                else
                {
                    foreach (var f in e.Value.otherEnds)
                    {
                        poly[0].Add(mFlat.mParts[f].transform.position.toFV2() - mBodies[e.Key].body.Position);
                    }
                }
                var fixture = FixtureFactory.AttachCompoundPolygon(poly, 1, mBodies[e.Key].body);
                fixture[0].CollisionGroup = -8; //negative indices never self collide
            }
        }

        //create joints
        foreach (var e in mImportant)
        {
            foreach (var f in e.Value.otherEnds)
            {
                if (mImportant[f].otherEnds.Count > 0)
                {
                    var joint = JointFactory.CreateRevoluteJoint(FSWorldComponent.PhysicsWorld, mBodies[e.Key].body, mBodies[f].body, FVector2.Zero);
                    mBodies[f].joint = joint;
                }
            }
        }

        //clean up the dummy bodies
        foreach (var e in mImportant)
        {
            if (e.Value.otherEnds.Count == 0)
            {
                FSWorldComponent.PhysicsWorld.RemoveBody(mBodies[e.Key].body);
                mBodies.Remove(e.Key);
            }
        }
    }