void Update() { Init(); if (lastContacts.Count < 1) { guiText.text = "Contact: null"; } else { string guiOutput = ""; float weight = 0f; foreach (Contact lastContact in lastContacts) { // get stats! bool isTouching = lastContact.IsTouching(); FarseerPhysics.Common.FixedArray2 <FVector2> contactPoints; FVector2 normal; string cps = "none"; string cn = "none"; string lpm = "none"; float dot = 0f; //float dot2 = 0f; if (isTouching) { lastContact.GetWorldManifold(out normal, out contactPoints); cps = string.Format("p0[ {0}, {1} ] p1[ {2}, {3} ]", contactPoints[0].X, contactPoints[0].Y, contactPoints[1].X, contactPoints[1].Y); cn = string.Format("[ {0}, {1} ]", normal.X, normal.Y); FarseerPhysics.Common.FixedArray2 <FarseerPhysics.Collision.ManifoldPoint> lpp = lastContact.Manifold.Points; lpm = string.Format("nimpulse[ {0}, {1} ] tanimpulse[ {2}, {3} ]", lpp[0].NormalImpulse / Time.fixedDeltaTime, lpp[1].NormalImpulse / Time.fixedDeltaTime, lpp[0].TangentImpulse / Time.fixedDeltaTime, lpp[1].TangentImpulse / Time.fixedDeltaTime); dot = FVector2.Dot(FVector2.Normalize(-AttachedBody.PhysicsBody.Position + contactPoints[0]), normal); //dot2 = FVector2.Dot(FVector2.Normalize(-AttachedBody.PhysicsBody.Position+contactPoints[1]), normal); weight += (1f * (lpp[0].NormalImpulse / Time.fixedDeltaTime) / 9.8f); weight += (1f * (lpp[1].NormalImpulse / Time.fixedDeltaTime) / 9.8f); } guiOutput += string.Format(contactInfoBase, lastContact.Restitution, lastContact.TangentSpeed, cps, cn, lpm, dot); } for (int i = 0; i < lastContacts.Count; i++) { if (!lastContacts[i].IsTouching()) { lastContacts.RemoveAt(i); i = Mathf.Max(0, i - 1); } } float ownmass = AttachedBody.PhysicsBody.Mass; weight -= ownmass; weight *= 0.5f; weight += ownmass; guiText.text = "TOTAL WEIGHT: " + weight.ToString() + "Kg"; guiText.text += guiOutput; } }
private void GetWeight(ref float weight) { if (lastContacts.Count < 1) { return; } float ownWeight = weight; weight = 0f; foreach (Contact lastContact in lastContacts) { bool isTouching = lastContact.IsTouching(); if (isTouching) { FarseerPhysics.Common.FixedArray2 <FarseerPhysics.Collision.ManifoldPoint> localManifoldPoints = lastContact.Manifold.Points; // gravity = 9.8f (hard coded here just for testing purposes) // Time.fixedDeltaTime is the FPE timeStep weight += (1f * (localManifoldPoints[0].NormalImpulse / Time.fixedDeltaTime) / 9.8f); weight += (1f * (localManifoldPoints[1].NormalImpulse / Time.fixedDeltaTime) / 9.8f); } } // remove inactive contacts for (int i = 0; i < lastContacts.Count; i++) { if (!lastContacts[i].IsTouching()) { lastContacts.RemoveAt(i); i = Mathf.Max(0, i - 1); } } // calc weight weight -= ownWeight; weight *= 0.5f; weight += ownWeight; }