Example #1
0
        public override b2MassData ComputeMass(float density)
        {
            b2MassData massData = new b2MassData();
            massData.mass = density * (float)Math.PI * m_radius * m_radius;
            massData.center = m_p;

            // inertia about the local origin
            massData.I = massData.mass * (0.5f * m_radius * m_radius + b2Math.b2Dot(m_p, m_p));
            return (massData);
        }
Example #2
0
 public override b2MassData ComputeMass(float density)
 {
     b2MassData massData = new b2MassData();
     massData.mass = 0.0f;
     massData.center.SetZero();
     massData.I = 0.0f;
     return (massData);
 }
Example #3
0
 public override b2MassData ComputeMass(float density)
 {
     b2MassData massData = new b2MassData();
     massData.mass = 0.0f;
     massData.center = 0.5f * (m_vertex1 + m_vertex2);
     massData.I = 0.0f;
     return (massData);
 }
Example #4
0
        public virtual void SetMassData(b2MassData massData)
        {
            Debug.Assert(m_world.IsLocked == false);
            if (m_world.IsLocked == true)
            {
                return;
            }

            if (m_type != b2BodyType.b2_dynamicBody)
            {
                return;
            }

            m_invMass = 0.0f;
            m_I = 0.0f;
            m_invI = 0.0f;

            m_mass = massData.mass;
            if (m_mass <= 0.0f)
            {
                m_mass = 1.0f;
            }

            m_invMass = 1.0f / m_mass;

            if (massData.I > 0.0f && (!m_flags.HasFlag(b2BodyFlags.e_fixedRotationFlag)))
            {
                m_I = massData.I - m_mass * b2Math.b2Dot(massData.center, massData.center);
                Debug.Assert(m_I > 0.0f);
                m_invI = 1.0f / m_I;
            }

            // Move center of mass.
            b2Vec2 oldCenter = Sweep.c;
            Sweep.localCenter = massData.center;
            Sweep.c0 = Sweep.c = b2Math.b2Mul(m_xf, Sweep.localCenter);

            // Update center of mass velocity.
            m_linearVelocity += b2Math.b2Cross(m_angularVelocity, Sweep.c - oldCenter);
        }
Example #5
0
public virtual void SetMassData(b2MassData massData)
{
    b2Assert(m_world.IsLocked() == false);
    if (m_world.IsLocked() == true)
    {
        return;
    }

    if (m_type != b2_dynamicBody)
    {
        return;
    }

    m_invMass = 0.0f;
    m_I = 0.0f;
    m_invI = 0.0f;

    m_mass = massData.mass;
    if (m_mass <= 0.0f)
    {
        m_mass = 1.0f;
    }

    m_invMass = 1.0f / m_mass;

    if (massData.I > 0.0f && (m_flags & e_fixedRotationFlag) == 0)
    {
        m_I = massData.I - m_mass * b2Dot(massData.center, massData.center);
        b2Assert(m_I > 0.0f);
        m_invI = 1.0f / m_I;
    }

    // Move center of mass.
    b2Vec2 oldCenter = m_sweep.c;
    m_sweep.localCenter =  massData.center;
    m_sweep.c0 = m_sweep.c = b2Mul(m_xf, m_sweep.localCenter);

    // Update center of mass velocity.
    m_linearVelocity += b2Cross(m_angularVelocity, m_sweep.c - oldCenter);
}
Example #6
0
 public virtual b2MassData GetMassData()
 {
     b2MassData data = new b2MassData();
     data.mass = m_mass;
     data.I = m_I + m_mass * b2Math.b2Dot(m_sweep.localCenter, m_sweep.localCenter);
     data.center = m_sweep.localCenter;
     return (data);
 }
Example #7
0
        public override b2MassData ComputeMass(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.

            b2Vec2 center = new b2Vec2();
            center.Set(0.0f, 0.0f);
            float area = 0.0f;
            float I = 0.0f;

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

            // This code would put the reference point inside the polygon.
            for (int i = 0; i < m_vertexCount; ++i)
            {
                s += Vertices[i];
            }
            s *= 1.0f / m_vertexCount;

            float k_inv3 = 1.0f / 3.0f;

            for (int i = 0; i < m_vertexCount; ++i)
            {
                // Triangle vertices.
                b2Vec2 e1 = Vertices[i] - s;
                b2Vec2 e2 = i + 1 < m_vertexCount ? Vertices[i + 1] - s : Vertices[0] - s;

                float D = b2Math.b2Cross(ref e1, ref e2);

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

                // Area weighted centroid
                center += triangleArea * k_inv3 * (e1 + e2);

                float ex1 = e1.x, ey1 = e1.y;
                float ex2 = e2.x, ey2 = e2.y;

                float intx2 = ex1 * ex1 + ex2 * ex1 + ex2 * ex2;
                float inty2 = ey1 * ey1 + ey2 * ey1 + ey2 * ey2;

                I += (0.25f * k_inv3 * D) * (intx2 + inty2);
            }

            // Total mass
            b2MassData massData = new b2MassData();

            massData.mass = density * area;

            // Center of mass
            if (area <= b2Settings.b2_epsilon)
            {
#if NETFX_CORE
                throw (new OverflowException("Area is zero in mass calculation."));
#else
                throw (new NotFiniteNumberException("Area is zero in mass calculation."));
#endif
            }
            center *= 1.0f / area;
            massData.center = center + s;

            // Inertia tensor relative to the local origin (point s).
            massData.I = density * I;

            // Shift to center of mass then to original body origin.
            massData.I += massData.mass * (b2Math.b2Dot(ref massData.center, ref massData.center) - b2Math.b2Dot(ref center, ref center));
            return (massData);
        }
Example #8
0
        public b2Body N2b2Body(b2World world, JObject bodyValue)
        {
            b2BodyDef bodyDef = new b2BodyDef();

            switch ((int)bodyValue.GetValue("type"))
            {
                case 0:
                    bodyDef.type = b2BodyType.b2_staticBody;
                    break;
                case 1:
                    bodyDef.type = b2BodyType.b2_kinematicBody;
                    break;
                case 2:
                    bodyDef.type = b2BodyType.b2_dynamicBody;
                    break;
            }

            bodyDef.position = jsonToVec("position", bodyValue);
            bodyDef.angle = jsonToFloat("angle", bodyValue);
            bodyDef.linearVelocity = jsonToVec("linearVelocity", bodyValue);
            bodyDef.angularVelocity = jsonToFloat("angularVelocity", bodyValue);
            bodyDef.linearDamping = jsonToFloat("linearDamping", bodyValue, -1, 0);
            bodyDef.angularDamping = jsonToFloat("angularDamping", bodyValue, -1, 0);
            bodyDef.gravityScale = jsonToFloat("gravityScale", bodyValue, -1, 1);

            bodyDef.allowSleep = bodyValue["allowSleep"] == null ? false : (bool)bodyValue["allowSleep"];
            bodyDef.awake = bodyValue["awake"] == null ? false : (bool)bodyValue["awake"];
            bodyDef.fixedRotation = bodyValue["fixedRotation"] == null ? false : (bool)bodyValue["fixedRotation"];
            bodyDef.bullet = bodyValue["bullet"] == null ? false : (bool)bodyValue["bullet"];

            //bodyDef.active = bodyValue["active"] == null ? false : (bool)bodyValue["active"];

            bodyDef.active = true;

            b2Body body = world.CreateBody(bodyDef);
            String bodyName = bodyValue["name"] == null ? "" : (string)bodyValue["active"];

            if ("" != bodyName)
                SetBodyName(body, bodyName);

            int i = 0;

            JArray fixtureValues = (JArray)bodyValue["fixture"];
            if (null != fixtureValues)
            {
                int numFixtureValues = fixtureValues.Count;
                for (i = 0; i < numFixtureValues; i++)
                {
                    JObject fixtureValue = (JObject)fixtureValues[i];
                    b2Fixture fixture = j2b2Fixture(body, fixtureValue);
                    readCustomPropertiesFromJson(fixture, fixtureValue);
                }
            }

            // may be necessary if user has overridden mass characteristics
            b2MassData massData = new b2MassData();
            massData.mass = jsonToFloat("massData-mass", bodyValue);
            massData.center = jsonToVec("massData-center", bodyValue);
            massData.I = jsonToFloat("massData-I", bodyValue);
            body.SetMassData(massData);

            return body;
        }
Example #9
0
        public JObject B2n(b2Body body)
        {
            JObject bodyValue = new JObject();

            string bodyName = GetBodyName(body);
            if (null != bodyName)
                bodyValue["name"] = bodyName;

            switch (body.BodyType)
            {
                case b2BodyType.b2_staticBody:
                    bodyValue["type"] = 0;
                    break;
                case b2BodyType.b2_kinematicBody:
                    bodyValue["type"] = 1;
                    break;
                case b2BodyType.b2_dynamicBody:
                    bodyValue["type"] = 2;
                    break;
            }

            VecToJson("position", body.Position, bodyValue);
            FloatToJson("angle", body.Angle, bodyValue);

            VecToJson("linearVelocity", body.LinearVelocity, bodyValue);
            FloatToJson("angularVelocity", body.AngularVelocity, bodyValue);

            if (body.LinearDamping != 0)
                FloatToJson("linearDamping", body.LinearDamping, bodyValue);
            if (body.AngularDamping != 0)
                FloatToJson("angularDamping", body.AngularDamping, bodyValue);
            if (body.GravityScale != 1)
                FloatToJson("gravityScale", body.GravityScale, bodyValue);

            if (body.IsBullet())
                bodyValue["bullet"] = true;
            if (!body.IsSleepingAllowed())
                bodyValue["allowSleep"] = false;
            if (body.IsAwake())
                bodyValue["awake"] = true;
            if (!body.IsActive())
                bodyValue["active"] = false;
            if (body.IsFixedRotation())
                bodyValue["fixedRotation"] = true;

            b2MassData massData = new b2MassData();
            massData = body.GetMassData();

            if (massData.mass != 0)
                FloatToJson("massData-mass", massData.mass, bodyValue);
            if (massData.center.x != 0 || massData.center.y != 0)
                VecToJson("massData-center", body.LocalCenter, bodyValue);
            if (massData.I != 0)
            {
                FloatToJson("massData-I", massData.I, bodyValue);
            }

            //int i = 0;
            JArray arr = new JArray();
            b2Body tmp = body;
            while (tmp != null)
            {
                bodyValue.Add("fixture", B2n(tmp));
                tmp = body.Next;
            }

            bodyValue["fixture"] = arr;

            JArray customPropertyValue = WriteCustomPropertiesToJson(body);
            if (customPropertyValue.Count > 0)
                bodyValue["customProperties"] = customPropertyValue;

            return bodyValue;
        }
Example #10
0
        public override b2MassData ComputeMass(float density)
        {
            b2MassData massData = new b2MassData();
            massData.mass = density * (float)Math.PI * Radius * Radius;
            massData.center = Position;

            // inertia about the local origin
            massData.I = massData.mass * (0.5f * Radius * Radius + Position.LengthSquared);
            return (massData);
        }
Example #11
0
        public LiquidTest()
        {
            hash = new List<int>[40, 40];
            for (int i = 0; i < 40; ++i)
            {
                for (int j = 0; j < 40; ++j)
                {
                    hash[i, j] = new List<int>();
                }
            }
            hashWidth = 40;
            hashHeight = 40;

            //if (firstTime)
            //{
            //    setCamera(new Vec2(0, 2), 35f);
            //    firstTime = false;
            //}

            //m_getWorld().setGravity(new Vec2(0.0f,0.0f));

            b2Body ground = null;
            {
                b2BodyDef bd = new b2BodyDef();
                bd.position.Set(0.0f, 0.0f);
                ground = m_world.CreateBody(bd);
                b2PolygonShape shape = new b2PolygonShape();
                shape.SetAsBox(5.0f, 0.5f);
                ground.CreateFixture(shape, 0);

                shape.SetAsBox(1.0f, 0.2f, new b2Vec2(0.0f, 4.0f), -0.2f);
                ground.CreateFixture(shape, 0);
                shape.SetAsBox(1.5f, 0.2f, new b2Vec2(-1.2f, 5.2f), -1.5f);
                ground.CreateFixture(shape, 0);
                shape.SetAsBox(0.5f, 50.0f, new b2Vec2(5.0f, 0.0f), 0.0f);
                ground.CreateFixture(shape, 0);

                shape.SetAsBox(0.5f, 3.0f, new b2Vec2(-8.0f, 0.0f), 0.0f);
                ground.CreateFixture(shape, 0);

                shape.SetAsBox(2.0f, 0.1f, new b2Vec2(-6.0f, -2.8f), 0.1f);
                ground.CreateFixture(shape, 0);

                b2CircleShape cd = new b2CircleShape();
                cd.Radius = 0.5f;
                cd.Position.Set(-0.5f, -4.0f);
                ground.CreateFixture(cd, 0);

            }

            liquid = new b2Body[nParticles];
            float massPerParticle = totalMass / nParticles;
            //		PointDef pd = new PointDef();
            //		pd.mass = massPerParticle;
            //		pd.restitution = 0.0f;
            //		pd.filter.groupIndex = -10;
            b2CircleShape pd = new b2CircleShape();
            b2FixtureDef fd = new b2FixtureDef();
            fd.shape = pd;
            fd.density = 1f;
            fd.filter.groupIndex = -10;
            pd.Radius = .05f;
            fd.restitution = 0.4f;
            fd.friction = 0.0f;
            float cx = 0.0f;
            float cy = 25.0f;
            for (int i = 0; i < nParticles; ++i)
            {
                b2BodyDef bd = new b2BodyDef();
                bd.position = new b2Vec2(Rand.RandomFloat(cx - boxWidth * .5f, cx + boxWidth * .5f),
                    Rand.RandomFloat(cy - boxHeight * .5f, cy + boxHeight * .5f));
                bd.fixedRotation = true;
                bd.type = b2BodyType.b2_dynamicBody;
                b2Body b = m_world.CreateBody(bd);

                b.CreateFixture(fd).UserData = LIQUID_INT;

                b2MassData md = new b2MassData();
                md.mass = massPerParticle;
                md.I = 1.0f;
                b.SetMassData(md);
                b.SetSleepingAllowed(false);
                liquid[i] = b;
            }

            b2PolygonShape polyDef = new b2PolygonShape();
            polyDef.SetAsBox(Rand.RandomFloat(0.3f, 0.7f), Rand.RandomFloat(0.3f, 0.7f));
            b2BodyDef bodyDef = new b2BodyDef();
            bodyDef.position = new b2Vec2(0.0f, 25.0f);
            bodyDef.type = b2BodyType.b2_dynamicBody;
            bod = m_world.CreateBody(bodyDef);
            bod.CreateFixture(polyDef, 1f);
        }
Example #12
0
        private void checkBounds()
        {
            for (int i = 0; i < liquid.Length; ++i)
            {
                if (liquid[i].WorldCenter.y < -10.0f)
                {
                    m_world.DestroyBody(liquid[i]);
                    float massPerParticle = totalMass / nParticles;

                    var pd = new b2CircleShape();
                    var fd = new b2FixtureDef();
                    fd.shape = pd;
                    fd.density = 1.0f;
                    fd.filter.groupIndex = -10;
                    pd.Radius = .05f;
                    fd.restitution = 0.4f;
                    fd.friction = 0.0f;
                    float cx = 0.0f + Rand.RandomFloat(-0.6f, 0.6f);
                    float cy = 15.0f + Rand.RandomFloat(-2.3f, 2.0f);
                    var bd = new b2BodyDef();
                    bd.position = new b2Vec2(cx, cy);
                    bd.fixedRotation = true;
                    bd.type = b2BodyType.b2_dynamicBody;
                    var b = m_world.CreateBody(bd);
                    b.CreateFixture(fd).UserData = LIQUID_INT;
                    var md = new b2MassData();
                    md.mass = massPerParticle;
                    md.I = 1.0f;
                    b.SetMassData(md);
                    b.SetSleepingAllowed(false);
                    liquid[i] = b;
                }
            }

            if (bod.WorldCenter.y < -15.0f)
            {
                m_world.DestroyBody(bod);
                var polyDef = new b2PolygonShape();
                polyDef.SetAsBox(Rand.RandomFloat(0.3f, 0.7f), Rand.RandomFloat(0.3f, 0.7f));
                var bodyDef = new b2BodyDef();
                bodyDef.position = new b2Vec2(0.0f, 25.0f);
                bodyDef.type = b2BodyType.b2_dynamicBody;
                bod = m_world.CreateBody(bodyDef);
                bod.CreateFixture(polyDef, 1f);
            }
        }