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


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

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

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

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

            return(result);
        }
        // INTERNALS

        private void CreateBodyFixture(Physics2D.Body i_Body, Physics2D.Shape i_Shape, FP i_Friction, FP i_Restitution)
        {
            Physics2D.Fixture fixture = i_Body.CreateFixture(i_Shape);

            fixture.Friction    = i_Friction;
            fixture.Restitution = i_Restitution;
        }
Пример #3
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;
            }
        }
Пример #4
0
        private void CreateBody(Physics2D.World world)
        {
            Physics2D.Body body = Physics2D.BodyFactory.CreateBody(world);

            if (tsMaterial == null)
            {
                tsMaterial = GetComponent <TSMaterial>();
            }

            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 (tsRigidBody == null)
            {
                body.BodyType = Physics2D.BodyType.Static;
            }
            else
            {
                if (tsRigidBody.isKinematic)
                {
                    body.BodyType = TrueSync.Physics2D.BodyType.Kinematic;
                }
                else
                {
                    body.BodyType      = Physics2D.BodyType.Dynamic;
                    body.IgnoreGravity = !tsRigidBody.useGravity;
                }

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

                body.FixedRotation = tsRigidBody.freezeZAxis;
                body.Mass          = tsRigidBody.mass;
                body.TSLinearDrag  = tsRigidBody.drag;
                body.TSAngularDrag = tsRigidBody.angularDrag;
            }

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

            _body = body;
        }
        private static object OverlapGeneric(Physics2D.Shape i_Shape, TSVector2 i_Position, Physics2D.BodySpecialSensor i_SensorType, int i_Mask)
        {
            Physics2D.World world = (Physics2D.World)PhysicsManager.GetWorld();

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

            body.BodyType = Physics2D.BodyType.Static;

            body.CollisionCategories = Physics2D.Category.All; // Category.All is used for sweep test objects.
            body.CollidesWith        = (Physics2D.Category)i_Mask;

            body.CollisionGroup = 0;

            body.IsSensor          = true;
            body.SpecialSensor     = i_SensorType;
            body.SpecialSensorMask = (int)Physics2D.Category.All;

            body.Position = i_Position;

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

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

                    return(result);
                }
            }

            return(null);
        }
Пример #6
0
        public void Restore(TrueSync.Physics2D.Shape sh)
        {
            sh._density = this._density;
            sh._radius  = this._radius;
            sh._2radius = this._2radius;
            sh.MassData = this.massData;
            bool flag = sh is PolygonShape;

            if (flag)
            {
                this.RestorePolygon((PolygonShape)sh);
            }
            else
            {
                bool flag2 = sh is CircleShape;
                if (flag2)
                {
                    this.RestoreCircle((CircleShape)sh);
                }
            }
        }
Пример #7
0
        public void Clone(TrueSync.Physics2D.Shape sh)
        {
            this._density = sh._density;
            this._radius  = sh._radius;
            this._2radius = sh._2radius;
            this.massData = sh.MassData;
            bool flag = sh is PolygonShape;

            if (flag)
            {
                this.ClonePolygon((PolygonShape)sh);
            }
            else
            {
                bool flag2 = sh is CircleShape;
                if (flag2)
                {
                    this.CloneCircle((CircleShape)sh);
                }
            }
        }
Пример #8
0
        private static object OverlapGeneric(Physics2D.Shape shape, TSVector2 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 <TSCollider2D>());
                }
                else
                {
                    TSCollider2D[] result = new TSCollider2D[body._specialSensorResults.Count];
                    for (int i = 0; i < body._specialSensorResults.Count; i++)
                    {
                        result[i] = Physics2DWorldManager.instance.GetGameObject(body._specialSensorResults[i]).GetComponent <TSCollider2D>();
                    }

                    return(result);
                }
            }

            return(null);
        }
        /**
         *  @brief Create the internal shape used to represent a TSBoxCollider.
         **/
        public override Physics2D.Shape[] CreateShapes()
        {
            if (m_Points == null || m_Points.Length == 0)
            {
                return(null);
            }

            Physics2D.Vertices v = new Physics2D.Vertices();
            for (int index = 0, length = m_Points.Length; index < length; index++)
            {
                v.Add(m_Points[index]);
            }

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

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

            return(result);
        }
        private void CreateBody(Physics2D.World i_World)
        {
            Physics2D.Body body = Physics2D.BodyFactory.CreateBody(i_World);

            // Get collision/restitution data from physics material.

            FP materialFriction    = FP.Zero;
            FP materialRestitution = FP.Zero;

            if (m_PhysicsMaterial != null)
            {
                materialFriction    = m_PhysicsMaterial.friction;
                materialRestitution = m_PhysicsMaterial.restitution;
            }
            else
            {
                // Check for a TSMaterial2D. Note: this is deprecated.

                m_TSMaterial = GetComponent <TSMaterial>();

                if (m_TSMaterial != null)
                {
                    materialFriction    = m_TSMaterial.friction;
                    materialRestitution = m_TSMaterial.restitution;
                }
            }

            // Create collision shape(s).

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

            // Setup RigidBody.

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

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

                body.FixedRotation = m_TSRigidBody.freezeZAxis;
                body.Mass          = m_TSRigidBody.mass;

                body.TSLinearDrag  = m_TSRigidBody.linearDrag;
                body.TSAngularDrag = m_TSRigidBody.angularDrag;
            }

            m_Body  = body;
            m_Shape = shape;

            SetIsSensor(m_IsTrigger);
            SetCollisionLayer(gameObject.layer);
        }