[Solved] Integrating Box3D into Raylib. How to use b3DebugDraw in Raylib? #79
|
(It is from Discord) Hello. I try to use Click to see a codestatic inline b3DebugDraw CreateDebugDrawer(LineDrawer *drawer)
{
b3DebugDraw dd = {0};
dd.DrawSegmentFcn = drawSegment;
dd.DrawTransformFcn = drawTransform;
// Add others as needed: dd.DrawBoxFcn = drawBox;
dd.context = drawer;
// Enable features
dd.drawShapes = true;
dd.drawJoints = true;
return dd;
}But these functions are not called: Click to see a codestatic void drawSegment(b3Pos p1, b3Pos p2, b3HexColor color, void *context)
{
printf("hello");
}
static void drawTransform(b3WorldTransform transform, void *context)
{
printf("hello");
}I call uint64_t debugMask = 0xFFFFFFFFFFFFFFFF;
b3World_Draw(app->worldId, &app->debugDrawer, debugMask);I have created a ground body: Click to see a code app->lineDrawer = LineDrawer_getInstance();
mat4 dummyMatrix = GLM_MAT4_IDENTITY_INIT;
LineDrawer_init(app->lineDrawer, app->shaderProgram, dummyMatrix);
b3WorldDef worldDef = b3DefaultWorldDef();
worldDef.gravity = (b3Vec3) { 0.f, -9.8f, 0.f };
app->worldId = b3CreateWorld(&worldDef);
app->debugDrawer = CreateDebugDrawer(app->lineDrawer);
// b3World_SetDebugDraw(app->worldId, &app->debugDrawer);
b3BodyDef groundBodyDef = b3DefaultBodyDef();
groundBodyDef.position = (b3Vec3){ 0.f, -0.f, 0.f };
b3BodyId groundId = b3CreateBody(app->worldId, &groundBodyDef);
b3BoxHull groundBox = b3MakeBoxHull(5.f, 0.5f, 5.f);
b3ShapeDef groundShapeDef = b3DefaultShapeDef();
b3CreateHullShape(groundId, &groundShapeDef, &groundBox.base);I call Click to see a codeSDL_AppResult SDL_AppIterate(void *appState)
{
App *app = (App *)appState;
int winW, winH;
SDL_GetWindowSizeInPixels(app->window, &winW, &winH);
// Update projection every frame with the window's actual aspect ratio
update_projection(app, winW, winH);
// Fill the entire window
glViewport(0, 0, winW, winH);
b3World_Step(app->worldId, 1.0f / 60.0f, 5);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// Use 0xFFFFFFFFFFFFFFFF to enable all flags,
// or specify a more granular mask if you only want to see specific things
uint64_t debugMask = 0xFFFFFFFFFFFFFFFF;
b3World_Draw(app->worldId, &app->debugDrawer, debugMask);
// Update the screen
SDL_GL_SwapWindow(app->window);
return SDL_APP_CONTINUE;
} |
Replies: 2 comments 6 replies
|
There are two separate things mixed together here. First, initialize the debug draw from Second, b3WorldDef worldDef = b3DefaultWorldDef();
// set worldDef.createDebugShape / destroyDebugShape here if your renderer uses cached shape objects
b3WorldId worldId = b3CreateWorld(&worldDef);
b3DebugDraw dd = b3DefaultDebugDraw();
dd.DrawShapeFcn = drawShape; // needed for normal shape rendering
dd.DrawSegmentFcn = drawSegment; // used for joints/contacts/extra lines
dd.DrawTransformFcn = drawTransform;
dd.DrawBoundsFcn = drawBounds;
dd.context = drawer;
dd.drawShapes = true;
dd.drawJoints = true;
dd.drawMass = true; // this is the flag that can make transforms useful
// dd.drawBounds = true; // if you want AABBs
b3World_Draw(worldId, &dd, B3_DEFAULT_MASK_BITS);With only a static ground body and no joints, So the quickest test is:
If this fixes the missing callbacks, please mark it as the answer so future Box3D debug-draw integrations can find the callback split quickly. |
|
I should publish
// Used to create debug draw shapes. This is called when a shape is
// first drawn using b3DebugDraw.
b3CreateDebugShapeCallback* createDebugShape;
|

A simple example using Raylib to show the solution:
View WebAssemlby-demo in the Browser
GitHub repositories for C and C++ projects for the demo above (Desktop and WebAssembly):
main.c (full code)