#include "box2d/b2_particle_group.h"
#include "box2d/b2_particle_system.h"
#include "box2d/b2_world.h"
#if LIQUIDFUN_EXTERNAL_LANGUAGE_API
#include <Box2D/Collision/Shapes/b2CircleShape.h>
#endif
b2ParticleGroup::b2ParticleGroup()
{
m_system = nullptr;
m_firstIndex = 0;
m_lastIndex = 0;
m_groupFlags = 0;
m_strength = 1.0f;
m_prev = nullptr;
m_next = nullptr;
m_timestamp = -1;
m_mass = 0;
m_inertia = 0;
m_center = b2Vec2_zero;
m_linearVelocity = b2Vec2_zero;
m_angularVelocity = 0;
m_transform.SetIdentity();
m_userData = nullptr;
}
uint32 b2ParticleGroup::GetAllParticleFlags() const
{
uint32 flags = 0;
for (int32 i = m_firstIndex; i < m_lastIndex; i++)
{
flags |= m_system->m_flagsBuffer.data[i];
}
return flags;
}
void b2ParticleGroup::SetGroupFlags(uint32 flags)
{
b2Assert((flags & b2_particleGroupInternalMask) == 0);
flags |= m_groupFlags & b2_particleGroupInternalMask;
m_system->SetGroupFlags(this, flags);
}
void b2ParticleGroup::UpdateStatistics() const
{
if (m_timestamp != m_system->m_timestamp)
{
float m = m_system->GetParticleMass();
m_mass = 0;
m_center.SetZero();
m_linearVelocity.SetZero();
for (int32 i = m_firstIndex; i < m_lastIndex; i++)
{
m_mass += m;
m_center += m * m_system->m_positionBuffer.data[i];
m_linearVelocity += m * m_system->m_velocityBuffer.data[i];
}
if (m_mass > 0)
{
m_center *= 1 / m_mass;
m_linearVelocity *= 1 / m_mass;
}
m_inertia = 0;
m_angularVelocity = 0;
for (int32 i = m_firstIndex; i < m_lastIndex; i++)
{
b2Vec2 p = m_system->m_positionBuffer.data[i] - m_center;
b2Vec2 v = m_system->m_velocityBuffer.data[i] - m_linearVelocity;
m_inertia += m * b2Dot(p, p);
m_angularVelocity += m * b2Cross(p, v);
}
if (m_inertia > 0)
{
m_angularVelocity *= 1 / m_inertia;
}
m_timestamp = m_system->m_timestamp;
}
}
void b2ParticleGroup::ApplyForce(const b2Vec2& force)
{
m_system->ApplyForce(m_firstIndex, m_lastIndex, force);
}
void b2ParticleGroup::ApplyLinearImpulse(const b2Vec2& impulse)
{
m_system->ApplyLinearImpulse(m_firstIndex, m_lastIndex, impulse);
}
void b2ParticleGroup::DestroyParticles(bool callDestructionListener)
{
b2Assert(m_system->m_world->IsLocked() == false);
if (m_system->m_world->IsLocked())
{
return;
}
for (int32 i = m_firstIndex; i < m_lastIndex; i++) {
m_system->DestroyParticle(i, callDestructionListener);
}
}
#if LIQUIDFUN_EXTERNAL_LANGUAGE_API
void b2ParticleGroupDef::FreeShapesMemory() {
if (circleShapes)
{
delete[] circleShapes;
circleShapes = nullptr;
}
if (ownShapesArray && shapes)
{
delete[] shapes;
shapes = nullptr;
ownShapesArray = false;
}
}
void b2ParticleGroupDef::SetCircleShapesFromVertexList(void* inBuf,
int numShapes,
float radius)
{
float* points = (float*) inBuf;
b2CircleShape* pCircleShapes = new b2CircleShape[numShapes];
b2Shape** pShapes = new b2Shape*[numShapes];
for (int i = 0; i < numShapes; ++i) {
pCircleShapes[i].m_radius = radius;
pCircleShapes[i].m_p = b2Vec2(points[i*2], points[i*2+1]);
pShapes[i] = &pCircleShapes[i];
}
FreeShapesMemory();
ownShapesArray = true;
circleShapes = pCircleShapes;
shapes = pShapes;
shapeCount = numShapes;
}
#endif