Skip to content

Instantly share code, notes, and snippets.

@erincatto
Last active January 28, 2023 05:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save erincatto/edac85d34685a8ff52e3ed4fedd1d45f to your computer and use it in GitHub Desktop.
Save erincatto/edac85d34685a8ff52e3ed4fedd1d45f to your computer and use it in GitHub Desktop.
Data layout choices
struct b2CircleShape
{
// circle stuff
};
struct b2PolygonShape
{
// polygon stuff
};
struct b2Shape
{
int type;
int typeIndex;
int nextShapeIndex;
};
struct b2Body
{
int shapeIndex;
};
struct b2World
{
struct b2Body* bodies;
struct b2Shape* shapes;
struct b2CircleShape* circles;
struct b2PolygonShape* polygons;
};
// =========================================================
struct b2Shape
{
int nextShapeIndex;
int nextShapeType;
};
struct b2CircleShape
{
struct b2Shape shape;
// circle stuff
};
struct b2PolygonShape
{
struct b2Shape shape;
// polygon stuff
};
struct b2Body
{
int shapeType;
int shapeIndex;
};
struct b2World
{
struct b2Body* bodies;
struct b2CircleShape* circleShapes;
struct b2PolygonShape* polygonShapes;
};
// ===========================================================
// =========================================================
struct b2Circle
{
// circle stuff
};
struct b2Polygon
{
// polygon stuff
};
struct b2Shape
{
int next;
int type;
union
{
b2Circle circle;
b2Polygon polygon;
};
};
struct b2Body
{
int shapeIndex;
};
struct b2World
{
struct b2Body* bodies;
struct b2Shape* shapes;
};
// Update all proxies for a body
// Separate shape types
b2Body* body = bodies + index;
int shapeIndex = body->shapeList;
while (shapeIndex != B2_NULL_INDEX)
{
b2Shape* s = world->shapes + shapeIndex;
switch (s->type)
{
case b2_circleShape:
s->aabb = b2ComputeCircleAABB(w->circles + s->typeIndex, b->transform);
break;
case b2_polygonShape:
s->aabb = b2ComputePolygonAABB(w->polygons + s->typeIndex, b->transform);
break;
}
shapeIndex = s->nextShapeIndex;
}
// Merged shape types
b2Body* body = bodies + index;
int shapeIndex = body->shapeList;
int shapeType = body->shapeType;
while (shapeIndex != B2_NULL_INDEX)
{
b2Shape* shape = NULL;
switch (shapeType)
{
case b2_circleShape:
{
b2CircleShape* circle = w->circles + shapeIndex;
shape = &circle->shape;
shape->aabb = b2ComputeCircleAABB(circle, b->transform);
}
break;
case b2_polygonShape:
{
b2PolygonShape* polygon = w->polygons + shapeIndex;
shape = &polygon->shape;
shape->aabb = b2ComputePolygonAABB(polygon, b->transform);
}
break;
}
assert(shape);
shapeIndex = shape->nextShapeIndex;
shapeType = shape->nextShapeType;
}
// Union shape
b2Body* body = bodies + index;
int shapeIndex = body->shapeList;
while (shapeIndex != B2_NULL_INDEX)
{
b2Shape* s = world->shapes + shapeIndex;
switch (s->type)
{
case b2_circleShape:
s->aabb = b2ComputeCircleAABB(s->circle, b->transform);
break;
case b2_polygonShape:
s->aabb = b2ComputePolygonAABB(s->polygon, b->transform);
break;
}
shapeIndex = s->next;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment