Beispiel #1
0
        public Box(World world, Vector2 position, Vector2 size, string texture, bool isStatic, Player player, float health = 100)
        {
            ObjectType = EObjectType.Box;
            mHealth = health;
            mStartHealth = health;
            mIsDestroyed = false;
            mSize = size;
            mWorld = world;
            mTexture = texture;
            mPlayer = player;
            DestroyTime = null;
            PolygonShape polygonShape = new PolygonShape();
            polygonShape.SetAsBox(size.X / 2f, size.Y / 2f);

            BodyDef bodyDef = new BodyDef();
            bodyDef.position = position;
            bodyDef.bullet = true;
            if (isStatic)
            {
                bodyDef.type = BodyType.Static;
            }
            else
            {
                bodyDef.type = BodyType.Dynamic;
            }
            mBody = world.CreateBody(bodyDef);

            FixtureDef fixtureDef = new FixtureDef();
            fixtureDef.shape = polygonShape;//Форма
            fixtureDef.density = 0.1f;//Плотность
            fixtureDef.friction = 0.3f;//Сила трения
            fixtureDef.restitution = 0f;//Отскок

            Filter filter = new Filter();
            filter.maskBits = (ushort)(EntityCategory.Player1 | EntityCategory.Player2);
            filter.categoryBits = (ushort)player.PlayerType;

            var fixture = mBody.CreateFixture(fixtureDef);
            fixture.SetFilterData(ref filter);
            MassData data = new MassData();
            data.mass = 0.01f;
            mBody.SetUserData(this);
        }
Beispiel #2
0
 public Bullet(World world, Vector2 position, float maxDamageValue, Player player,int bulletType)
 {
     mPlayer = player;
     ObjectType = EObjectType.Bullet;
     switch (bulletType)
     {
         case 1:
             mDamageRadius = 10;
             break;
         case 2:
             mDamageRadius = 20;
             break;
     }
     mMaxDamageValue = maxDamageValue;
     Vector2 size = new Vector2(5, 5);
     mBox = new Box(world, position, size, "bullet", true, player);
     MassData massData = new MassData();
     massData.mass = 1000;
     mBox.mBody.SetMassData(ref massData);
     mIsActive = false;
     mIsDestoyed = false;
     Body.SetUserData(this);
 }
        /// <summary>
        ///  @see Shape.ComputeMass
        /// </summary>
        /// <param name="massData"></param>
        /// <param name="density"></param>
        public override void ComputeMass(out MassData massData, float density)
        {
            // Polygon mass, centroid, and inertia.
            // Let rho be the polygon density in mass per unit area.
            // Then:
            // mass = rho * int(dA)
            // centroid.X = (1/mass) * rho * int(x * dA)
            // centroid.Y = (1/mass) * rho * int(y * dA)
            // I = rho * int((x*x + y*y) * dA)
            //
            // We can compute these integrals by summing all the integrals
            // for each triangle of the polygon. To evaluate the integral
            // for a single triangle, we make a change of variables to
            // the (u,v) coordinates of the triangle:
            // x = x0 + e1x * u + e2x * v
            // y = y0 + e1y * u + e2y * v
            // where 0 <= u && 0 <= v && u + v <= 1.
            //
            // We integrate u from [0,1-v] and then v from [0,1].
            // We also need to use the Jacobian of the transformation:
            // D = cross(e1, e2)
            //
            // Simplification: triangle centroid = (1/3) * (p1 + p2 + p3)
            //
            // The rest of the derivation is handled by computer algebra.

            Debug.Assert(_vertexCount >= 2);

            // A line segment has zero mass.
            if (_vertexCount == 2)
            {
                massData.center = 0.5f * (_vertices[0] + _vertices[1]);
                massData.mass = 0.0f;
                massData.I = 0.0f;
                return;
            }

            Vector2 center = new Vector2(0.0f, 0.0f);
            float area = 0.0f;
            float I = 0.0f;

            // pRef is the reference point for forming triangles.
            // It's location doesn't change the result (except for rounding error).
            Vector2 pRef = new Vector2(0.0f, 0.0f);

            const float k_inv3 = 1.0f / 3.0f;

            for (int i = 0; i < _vertexCount; ++i)
            {
                // Triangle vertices.
                Vector2 p1 = pRef;
                Vector2 p2 = _vertices[i];
                Vector2 p3 = i + 1 < _vertexCount ? _vertices[i + 1] : _vertices[0];

                Vector2 e1 = p2 - p1;
                Vector2 e2 = p3 - p1;

                float D = MathUtils.Cross(e1, e2);

                float triangleArea = 0.5f * D;
                area += triangleArea;

                // Area weighted centroid
                center += triangleArea * k_inv3 * (p1 + p2 + p3);

                float px = p1.X, py = p1.Y;
                float ex1 = e1.X, ey1 = e1.Y;
                float ex2 = e2.X, ey2 = e2.Y;

                float intx2 = k_inv3 * (0.25f * (ex1 * ex1 + ex2 * ex1 + ex2 * ex2) + (px * ex1 + px * ex2)) + 0.5f * px * px;
                float inty2 = k_inv3 * (0.25f * (ey1 * ey1 + ey2 * ey1 + ey2 * ey2) + (py * ey1 + py * ey2)) + 0.5f * py * py;

                I += D * (intx2 + inty2);
            }

            // Total mass
            massData.mass = density * area;

            // Center of mass
            Debug.Assert(area > Settings.b2_epsilon);
            center *= 1.0f / area;
            massData.center = center;

            // Inertia tensor relative to the local origin.
            massData.I = density * I;
        }
        /// <summary>
        /// Set the mass properties to override the mass properties of the fixtures.
        /// Note that this changes the center of mass position.
        /// Note that creating or destroying fixtures can also alter the mass.
        /// This function has no effect if the body isn't dynamic.
        /// </summary>
        /// <param name="massData">the mass properties.</param>
        public void SetMassData(ref MassData massData)
        {
            Debug.Assert(_world.IsLocked == false);
            if (_world.IsLocked == true)
            {
                return;
            }

            if (_type != BodyType.Dynamic)
            {
                return;
            }

            _invMass = 0.0f;
            _I = 0.0f;
            _invI = 0.0f;

            _mass = massData.mass;

            if (_mass <= 0.0f)
            {
                _mass = 1.0f;
            }

            _invMass = 1.0f / _mass;

            if (massData.I > 0.0f && (_flags & BodyFlags.FixedRotation) == 0)
            {
                _I = massData.I - _mass * Vector2.Dot(massData.center, massData.center);
                Debug.Assert(_I > 0.0f);
                _invI = 1.0f / _I;
            }

            // Move center of mass.
            Vector2 oldCenter = _sweep.c;
            _sweep.localCenter = massData.center;
            _sweep.c0 = _sweep.c = MathUtils.Multiply(ref _xf, _sweep.localCenter);

            // Update center of mass velocity.
            _linearVelocity += MathUtils.Cross(_angularVelocity, _sweep.c - oldCenter);
        }
 /// <summary>
 /// Get the mass data of the body.
 /// </summary>
 /// <param name="massData">a struct containing the mass, inertia and center of the body.</param>
 public void GetMassData(out MassData massData)
 {
     massData = new MassData();
     massData.mass = _mass;
     massData.I = _I + _mass * Vector2.Dot(_sweep.localCenter, _sweep.localCenter);
     massData.center = _sweep.localCenter;
 }
Beispiel #6
0
	    /// Get the mass data for this fixture. The mass data is based on the density and
	    /// the Shape. The rotational inertia is about the Shape's origin.
	    public void GetMassData(out MassData massData)
        {
            _shape.ComputeMass(out massData, _density);
        }
Beispiel #7
0
 /// Chains have zero mass.
 /// @see Shape::ComputeMass
 public override void ComputeMass(out MassData massData, float density)
 {
     massData = new MassData();
     massData.mass = 0.0f;
     massData.center = Vector2.Zero;
     massData.I = 0.0f;
 }
Beispiel #8
0
 /// @see Shape::ComputeMass
 public override void ComputeMass(out MassData massData, float density)
 {
     massData = new MassData();
     massData.mass = 0.0f;
     massData.center = 0.5f * (_vertex1 + _vertex2);
     massData.I = 0.0f;
 }
Beispiel #9
0
	    /// Compute the mass properties of this shape using its dimensions and density.
	    /// The inertia tensor is computed about the local origin, not the centroid.
	    /// @param massData returns the mass data for this shape.
	    /// @param density the density in kilograms per meter squared.
	    public abstract void ComputeMass(out MassData massData, float density);
        /// <summary>
        /// @see Shape.ComputeMass
        /// </summary>
        /// <param name="massData"></param>
        /// <param name="density"></param>
        public override void ComputeMass(out MassData massData, float density)
        {
            massData.mass = density * Settings.b2_pi * _radius * _radius;
            massData.center = _p;

            // inertia about the local origin
            massData.I = massData.mass * (0.5f * _radius * _radius + Vector2.Dot(_p, _p));
        }
Beispiel #11
0
 /// <summary> Compute the mass properties of this shape using its dimensions and density.
 /// The inertia tensor is computed about the local origin, not the centroid.
 /// </summary> <param name="massData"> returns the mass data for this shape.
 /// </param> <param name="density"> the density in kilograms per meter squared.
 /// </param>
 public abstract void ComputeMass(out MassData massData, float density);
Beispiel #12
0
        /// <summary>
        ///  @see Shape.ComputeMass
        /// </summary>
        /// <param name="massData"></param>
        /// <param name="density"></param>
        public override void ComputeMass(out MassData massData, float density)
        {
            // Polygon mass, centroid, and inertia.
            // Let rho be the polygon density in mass per unit area.
            // Then:
            // mass = rho * int(dA)
            // centroid.X = (1/mass) * rho * int(x * dA)
            // centroid.Y = (1/mass) * rho * int(y * dA)
            // I = rho * int((x*x + y*y) * dA)
            //
            // We can compute these integrals by summing all the integrals
            // for each triangle of the polygon. To evaluate the integral
            // for a single triangle, we make a change of variables to
            // the (u,v) coordinates of the triangle:
            // x = x0 + e1x * u + e2x * v
            // y = y0 + e1y * u + e2y * v
            // where 0 <= u && 0 <= v && u + v <= 1.
            //
            // We integrate u from [0,1-v] and then v from [0,1].
            // We also need to use the Jacobian of the transformation:
            // D = cross(e1, e2)
            //
            // Simplification: triangle centroid = (1/3) * (p1 + p2 + p3)
            //
            // The rest of the derivation is handled by computer algebra.

            Debug.Assert(_vertexCount >= 2);

            // A line segment has zero mass.
            if (_vertexCount == 2)
            {
                massData.center = 0.5f * (_vertices[0] + _vertices[1]);
                massData.mass   = 0.0f;
                massData.I      = 0.0f;
                return;
            }

            Vector2 center = new Vector2(0.0f, 0.0f);
            float   area   = 0.0f;
            float   I      = 0.0f;

            // pRef is the reference point for forming triangles.
            // It's location doesn't change the result (except for rounding error).
            Vector2 pRef = new Vector2(0.0f, 0.0f);

            const float k_inv3 = 1.0f / 3.0f;

            for (int i = 0; i < _vertexCount; ++i)
            {
                // Triangle vertices.
                Vector2 p1 = pRef;
                Vector2 p2 = _vertices[i];
                Vector2 p3 = i + 1 < _vertexCount ? _vertices[i + 1] : _vertices[0];

                Vector2 e1 = p2 - p1;
                Vector2 e2 = p3 - p1;

                float D = MathUtils.Cross(e1, e2);

                float triangleArea = 0.5f * D;
                area += triangleArea;

                // Area weighted centroid
                center += triangleArea * k_inv3 * (p1 + p2 + p3);

                float px = p1.X, py = p1.Y;
                float ex1 = e1.X, ey1 = e1.Y;
                float ex2 = e2.X, ey2 = e2.Y;

                float intx2 = k_inv3 * (0.25f * (ex1 * ex1 + ex2 * ex1 + ex2 * ex2) + (px * ex1 + px * ex2)) + 0.5f * px * px;
                float inty2 = k_inv3 * (0.25f * (ey1 * ey1 + ey2 * ey1 + ey2 * ey2) + (py * ey1 + py * ey2)) + 0.5f * py * py;

                I += D * (intx2 + inty2);
            }

            // Total mass
            massData.mass = density * area;

            // Center of mass
            Debug.Assert(area > Settings.b2_epsilon);
            center         *= 1.0f / area;
            massData.center = center;

            // Inertia tensor relative to the local origin.
            massData.I = density * I;
        }