/// <summary> /// Get the collision force between two bodies /// Remember, bullets go another way /// </summary> /// <param name="selfRb"></param> /// <param name="otherRb"></param> /// <returns></returns> public static float GetCollisionForce(Rigidbody selfRb, Rigidbody otherRb) { // Ponemos esto para evitar errores // Pero esto no dbería ocurrir if (selfRb == null) { return(0); } // RigidbodyExtraInfo selfRbExtraInfo = selfRb.GetComponent <RigidbodyExtraInfo>(); // El otherRb puede ser nulo float otherImpactForce = 0; if (otherRb != null) { RigidbodyExtraInfo otherRbExtraInfo = otherRb.GetComponent <RigidbodyExtraInfo>(); //otherImpactForce = otherRb.velocity.magnitude * otherRb.mass; otherImpactForce = GeneralFunctions.GetBodyKineticEnergy(otherRb, otherRbExtraInfo); } // El propio no puede serlo //float selfImpactForce = selfRb.velocity.magnitude * selfRb.mass; float selfImpactForce = GeneralFunctions.GetBodyKineticEnergy(selfRb, selfRbExtraInfo); // float impactForce = otherImpactForce + selfImpactForce; return(impactForce); }
/// <summary> /// Gives the kinetic energy of a rigidbody /// </summary> /// <param name="rb"></param> /// <returns></returns> public static float GetBodyKineticEnergy(Rigidbody rb, RigidbodyExtraInfo rbExtraInfo) { // if (rbExtraInfo == null) { Debug.Log("Asking kinetic energy of not prepared object: " + rb.name); return(0); } // float bodySpeedVariation = Math.Abs((rb.velocity - rbExtraInfo.PreviousVelocity).magnitude); float bodyMass = rb.mass; float bodyKE = bodyMass * Mathf.Pow(bodySpeedVariation, 2) / 2; Debug.Log("Impact of " + rb.name + ", speed variation of " + bodySpeedVariation + ", impacting with a force of " + bodyKE + " Joules"); return(bodyKE); }