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


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

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

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

            KBEngine.Physics2D.Shape[] result = new Physics2D.Shape[convexShapeVs.Count];
            for (int index = 0, length = result.Length; index < length; index++)
            {
                result[index] = new KBEngine.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 (tsMaterial != null)
            {
                fixture.Friction    = tsMaterial.friction;
                fixture.Restitution = tsMaterial.restitution;
            }
        }
예제 #3
0
        private void CreateBody(Physics2D.World world)
        {
            Physics2D.Body body = Physics2D.BodyFactory.CreateBody(world);

            if (tsMaterial == null)
            {
                tsMaterial = 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 = KBEngine.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.TSLinearDrag  = FPRigidBody.drag;
                body.TSAngularDrag = 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);
        }