I have a 2d group of particles that I'd like to set its angular velocity, for the particles to spin around their group center.
I found this method to get the angular velocity of the group, and I have tried inverting the logic to actually set the angular group velocity.
In my attempts I leave most of the logic intact, by getting the group mass, group center and group linear velocity in the same way. But seems most of my trial and errors are not building up to the expected results.
If you have any knowledge on how I should tackle this logic inversion from getting the angular velocity to setting it, I'd love to hear what you have to say.
getParticleGroupAngularVelocity()
{
particleCount = 30;
particleMass = 1.5;
particleGroupMass = 0;
particleGroupInertia = 0;
particleGroupAngularVelocity = 0;
particleGroupCenter = vector(0, 0);
particleGroupLinearVelocity = vector(0, 0);
for (i = 0; i < particleCount; i++)
{
particleGroupMass += particleMass;
particleGroupCenter += particleMass * particles[i].position;
particleGroupLinearVelocity += particleMass * particles[i].velocity;
}
if (particleGroupMass > 0)
{
particleGroupCenter *= 1 / particleGroupMass;
particleGroupLinearVelocity *= 1 / particleGroupMass;
}
for (i = 0; i < particleCount; i++)
{
pos = particles[i].position - particleGroupCenter;
vel = particles[i].velocity - particleGroupLinearVelocity;
particleGroupInertia += particleMass * (pos.x * pos.x + pos.y * pos.y);
particleGroupAngularVelocity += particleMass * (pos.x * vel.y - pos.y * vel.x);
}
if (particleGroupInertia > 0)
{
particleGroupAngularVelocity *= 1 / particleGroupInertia;
}
return particleGroupAngularVelocity;
}