コード例 #1
0
ファイル: CircleShape.cs プロジェクト: CloneDeath/Box2D.Net
		/// @see Shape::ComputeMass
		public override void ComputeMass(out MassData massData, float Density) {
			massData = new MassData();
			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 + Utilities.Dot(m_p, m_p));
		}
コード例 #2
0
ファイル: PolygonShape.cs プロジェクト: CloneDeath/Box2D.Net
		/// @see Shape::ComputeMass
		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.

			Utilities.Assert(m_count >= 3);

			Vec2 center = new Vec2(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).
			Vec2 s = new Vec2(0.0f, 0.0f);

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

			const float k_inv3 = 1.0f / 3.0f;

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

			    float D = Utilities.Cross(e1, 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
			massData.mass = Density * area;

			// Center of mass
			Utilities.Assert(area > Single.Epsilon);
			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 * (Utilities.Dot(massData.center, massData.center) - Utilities.Dot(center, center));
		}
コード例 #3
0
ファイル: EdgeShape.cs プロジェクト: CloneDeath/Box2D.Net
		/// @see Shape::ComputeMass
		public override void ComputeMass(out MassData massData, float Density) {
			throw new NotImplementedException();

			//massData.mass = 0.0f;
			//massData.center = 0.5f * (m_vertex1 + m_vertex2);
			//massData.I = 0.0f;
		}
コード例 #4
0
ファイル: ChainShape.cs プロジェクト: CloneDeath/Box2D.Net
		/// 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.SetZero();
			massData.I = 0.0f;
		}
コード例 #5
0
ファイル: Body.cs プロジェクト: CloneDeath/Box2D.Net
		/// 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.
		/// @param massData the mass properties.
		public void SetMassData(MassData data){
			Utilities.Assert(m_world.IsLocked() == false);
			if (m_world.IsLocked() == true) {
				return;
			}

			if (m_type != BodyType._dynamicBody) {
				return;
			}

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

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

			m_invMass = 1.0f / m_mass;

			if (data.I > 0.0f && (m_flags & Body.BodyFlags.e_fixedRotationFlag) == 0) {
				m_I = data.I - m_mass * Utilities.Dot(data.center, data.center);
				Utilities.Assert(m_I > 0.0f);
				m_invI = 1.0f / m_I;
			}

			// Move center of mass.
			Vec2 oldCenter = m_sweep.c;
			m_sweep.localCenter = data.center;
			m_sweep.c0 = m_sweep.c = Utilities.Mul(m_xf, m_sweep.localCenter);

			// Update center of mass velocity.
			m_linearVelocity += Utilities.Cross(m_angularVelocity, m_sweep.c - oldCenter);
		}
コード例 #6
0
ファイル: Body.cs プロジェクト: CloneDeath/Box2D.Net
		/// Get the mass data of the body.
		/// @return a struct containing the mass, inertia and center of the body.
		public MassData GetMassData() {
			MassData data = new MassData();
			data.mass = m_mass;
			data.I = m_I + m_mass * Utilities.Dot(m_sweep.localCenter, m_sweep.localCenter);
			data.center = m_sweep.localCenter;
			return data;
		}
コード例 #7
0
ファイル: Shape.cs プロジェクト: CloneDeath/Box2D.Net
		/// Compute the mass properties of this shape using its dimensions and Density.
		/// The inertia tensor is computed about the local origin.
		/// @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);
コード例 #8
0
ファイル: Fixture.cs プロジェクト: CloneDeath/Box2D.Net
		/// 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. This operation
		/// may be expensive.
		public void GetMassData(out MassData massData){
			m_shape.ComputeMass(out massData, m_Density);
		}
コード例 #9
0
ファイル: Shape.cs プロジェクト: CloneDeath/Box2D.Net
 /// Compute the mass properties of this shape using its dimensions and Density.
 /// The inertia tensor is computed about the local origin.
 /// @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);
コード例 #10
0
ファイル: Fixture.cs プロジェクト: quinsmpang/box2d4unity
 /// 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);
 }