コード例 #1
0
        /**
         *  @brief Create the internal shape used to represent a PolygonCollider.
         **/
        public override Spax.Physics2D.Shape[] CreateShapes()
        {
            if (_points == null || _points.Length == 0)
            {
                return(null);
            }


            FPVector2 lossy2D = new FPVector2(lossyScale.x, lossyScale.y);

            Spax.Physics2D.Vertices v = new Physics2D.Vertices();
            for (int index = 0, length = _points.Length; index < length; index++)
            {
                v.Add(FPVector2.Scale(_points[index], lossy2D));
            }

            List <Spax.Physics2D.Vertices> convexShapeVs = Spax.Physics2D.BayazitDecomposer.ConvexPartition(v);

            Spax.Physics2D.Shape[] result = new Physics2D.Shape[convexShapeVs.Count];
            for (int index = 0, length = result.Length; index < length; index++)
            {
                result[index] = new Spax.Physics2D.PolygonShape(convexShapeVs[index], 1);
            }

            return(result);
        }
コード例 #2
0
        private void CreateBodyFixture(Physics2D.Body body, Physics2D.Shape shape)
        {
            Physics2D.Fixture fixture = body.CreateFixture(shape);

            if (FPMaterial != null)
            {
                fixture.Friction    = FPMaterial.friction;
                fixture.Restitution = FPMaterial.restitution;
            }
        }
コード例 #3
0
        private void CreateBody(Physics2D.World world)
        {
            Physics2D.Body body = Physics2D.BodyFactory.CreateBody(world);

            if (FPMaterial == null)
            {
                FPMaterial = GetComponent <FPMaterial>();
            }

            Physics2D.Shape shape = Shape;
            if (shape != null)
            {
                CreateBodyFixture(body, shape);
            }
            else
            {
                Physics2D.Shape[] shapes = CreateShapes();
                for (int index = 0, length = shapes.Length; index < length; index++)
                {
                    CreateBodyFixture(body, shapes[index]);
                }
            }

            if (FPRigidBody == null)
            {
                body.BodyType = Physics2D.BodyType.Static;
            }
            else
            {
                if (FPRigidBody.isKinematic)
                {
                    body.BodyType = Spax.Physics2D.BodyType.Kinematic;
                }
                else
                {
                    body.BodyType      = Physics2D.BodyType.Dynamic;
                    body.IgnoreGravity = !FPRigidBody.useGravity;
                }

                if (FPRigidBody.mass <= 0)
                {
                    FPRigidBody.mass = 1;
                }

                body.FixedRotation = FPRigidBody.freezeZAxis;
                body.Mass          = FPRigidBody.mass;
                body.FPLinearDrag  = FPRigidBody.drag;
                body.FPAngularDrag = FPRigidBody.angularDrag;
            }

            body.IsSensor     = isTrigger;
            body.CollidesWith = Physics2D.Category.All;

            _body = body;
        }
コード例 #4
0
        private static object OverlapGeneric(Physics2D.Shape shape, FPVector2 position, Physics2D.BodySpecialSensor sensorType, int layerMask)
        {
            Physics2D.World world = (Physics2D.World)Physics2DWorldManager.instance.GetWorld();

            Physics2D.Body body = Physics2D.BodyFactory.CreateBody(world);
            body.CreateFixture(shape);

            body.BodyType     = Physics2D.BodyType.Static;
            body.IsSensor     = true;
            body.CollidesWith = Physics2D.Category.All;

            body.SpecialSensor     = sensorType;
            body.SpecialSensorMask = layerMask;
            body.Position          = position;

            world.RemoveBody(body);
            world.ProcessRemovedBodies();

            if (body._specialSensorResults.Count > 0)
            {
                if (sensorType == Physics2D.BodySpecialSensor.ActiveOnce)
                {
                    return(Physics2DWorldManager.instance.GetGameObject(body._specialSensorResults[0]).GetComponent <FPCollider2D>());
                }
                else
                {
                    FPCollider2D[] result = new FPCollider2D[body._specialSensorResults.Count];
                    for (int i = 0; i < body._specialSensorResults.Count; i++)
                    {
                        result[i] = Physics2DWorldManager.instance.GetGameObject(body._specialSensorResults[i]).GetComponent <FPCollider2D>();
                    }

                    return(result);
                }
            }

            return(null);
        }