Object disappearing when it collides,Objects disappear on collision
Hello, I'm building a 3D particle simulator, and I've just recently added gravity. I have a loop that goes through all particles in the scene and applies a gravitational force to them that factors in their mass. I've been using Rigidbody's mass field as a way to input AMU (atomic mass units) so that I don't have to type in tons of zeros for the mass. Only recently did I add a method to convert from AMU to KG, and now that I have, the particles still attract correctly, but now they disappear upon collision, and I receive this error:
rigidbody.force assign attempt for 'Particle (1)' is not valid. Input force is { NaN, NaN, NaN }.
UnityEngine.Rigidbody:AddForce(Vector3, ForceMode)
Gravity:Update() (at Assets/Gravity.cs:39)
Here's my gravity code:
for (int i = 0; i < originalParticles.Length; i++)
{
Particle particle = originalParticles[i];
for (int j = 0; j < originalParticles.Length; j++)
{
Particle secondParticle = originalParticles[j];
if (i != j)
{
float dist = Vector3.Distance(particle.transform.position, secondParticle.transform.position);
float magnitude = G * (particle.getMass() * secondParticle.getMass()) / (dist * dist);
Vector3 twoToOne = secondParticle.transform.position - particle.transform.position;
Vector3 oneToTwo = twoToOne.normalized * -magnitude;
finalParticles[i].rb.AddForce(twoToOne, ForceMode.Force);
finalParticles[j].rb.AddForce(oneToTwo, ForceMode.Force);
finalParticles[i].acceleration = oneToTwo / amuToKG(finalParticles[i].getMass());
finalParticles[j].acceleration = twoToOne / amuToKG(finalParticles[j].getMass());
}
}
}
originalParticles and finalParticles just hold all particles in the scene, and math is applied on the final particles, but with the variables of the initial particles so that the gravity calculations are correct.
Thank you!
Your answer
Follow this Question
Related Questions
Kinematic forces under Rigidbody Parents 0 Answers
Road problem. Detect collision without effectively colliding with a rigidBody 1 Answer
How do you fix a Rigidbody stopping quickly after the addforce stops being called? 1 Answer
How to add gravity to imported objects? 1 Answer
[SOLVED] Ball rolling around a planet bounces off? 2 Answers