#pragma once
#include "bitset.h"
#include "container.h"
#include "table.h"
#include "box3d/collision.h"
#include "box3d/types.h"
typedef struct b3Shape b3Shape;
typedef struct b3MovePair b3MovePair;
typedef struct b3MoveResult b3MoveResult;
typedef struct b3Stack b3Stack;
typedef struct b3World b3World;
#define B3_PROXY_TYPE( KEY ) ( (b3BodyType)( ( KEY ) & 3 ) )
#define B3_PROXY_ID( KEY ) ( ( KEY ) >> 2 )
#define B3_PROXY_KEY( ID, TYPE ) ( ( ( ID ) << 2 ) | ( TYPE ) )
typedef struct b3BroadPhase
{
b3DynamicTree trees[b3_bodyTypeCount];
b3BitSet movedProxies[b3_bodyTypeCount];
b3Array( int ) moveArray;
b3MoveResult* moveResults;
b3MovePair* movePairs;
int movePairCapacity;
b3AtomicInt movePairIndex;
b3HashSet pairSet;
} b3BroadPhase;
void b3CreateBroadPhase( b3BroadPhase* bp, const b3Capacity* capacity );
void b3DestroyBroadPhase( b3BroadPhase* bp );
int b3BroadPhase_CreateProxy( b3BroadPhase* bp, b3BodyType proxyType, b3AABB aabb, uint64_t categoryBits, int shapeIndex,
bool forcePairCreation );
void b3BroadPhase_DestroyProxy( b3BroadPhase* bp, int proxyKey );
void b3BroadPhase_MoveProxy( b3BroadPhase* bp, int proxyKey, b3AABB aabb );
void b3BroadPhase_EnlargeProxy( b3BroadPhase* bp, int proxyKey, b3AABB aabb );
int b3BroadPhase_GetShapeIndex( b3BroadPhase* bp, int proxyKey );
void b3UpdateBroadPhasePairs( b3World* world );
bool b3BroadPhase_TestOverlap( const b3BroadPhase* bp, int proxyKeyA, int proxyKeyB );
void b3ValidateBroadPhase( const b3BroadPhase* bp );
void b3ValidateNoEnlarged( const b3BroadPhase* bp );
static inline void b3BufferMove( b3BroadPhase* bp, int queryProxy )
{
b3BodyType proxyType = B3_PROXY_TYPE( queryProxy );
int proxyId = B3_PROXY_ID( queryProxy );
b3BitSet* set = &bp->movedProxies[proxyType];
if ( b3GetBit( set, proxyId ) == false )
{
b3SetBitGrow( set, proxyId );
b3Array_Push( bp->moveArray, queryProxy );
}
}