Skip to content

Infinitely looping CP_BODY_FOREACH_ARBITER(body, arb) in FloodFillComponent(cpBody *root, cpBody *body) #260

@padmadevd

Description

@padmadevd

`
#include <raylib/raylib.h>
#include <chipmunk/chipmunk.h>

int main()
{
InitWindow(600, 600, "chipmunk test");
InitAudioDevice();

cpSpace *space = cpSpaceNew();
cpSpaceSetIterations(space, 30);
cpSpaceSetGravity(space, cpv(0, -100));
cpSpaceSetSleepTimeThreshold(space, 0.5f);

cpBody *staticBody = cpSpaceGetStaticBody(space);

// Create segments around the edge of the screen.
cpShape *shape;
shape = cpSpaceAddShape(space, cpSegmentShapeNew(staticBody, cpv(-300,-300), cpv(-300,300), 0.0f));
cpShapeSetElasticity(shape, 1.0f);
cpShapeSetFriction(shape, 1.0f);

shape = cpSpaceAddShape(space, cpSegmentShapeNew(staticBody, cpv(300,-300), cpv(300,300), 0.0f));
cpShapeSetElasticity(shape, 1.0f);
cpShapeSetFriction(shape, 1.0f);

shape = cpSpaceAddShape(space, cpSegmentShapeNew(staticBody, cpv(-300,-300), cpv(300,-300), 0.0f));
cpShapeSetElasticity(shape, 1.0f);
cpShapeSetFriction(shape, 1.0f);

shape = cpSpaceAddShape(space, cpSegmentShapeNew(staticBody, cpv(-300,300), cpv(300,300), 0.0f));
cpShapeSetElasticity(shape, 1.0f);
cpShapeSetFriction(shape, 1.0f);

cpFloat radius = 10;
cpFloat mass = 4;

// The moment of inertia is like mass for rotation
// Use the cpMomentFor*() functions to help you approximate it.
cpFloat moment = cpMomentForCircle(mass, 0, radius, cpvzero);

// The cpSpaceAdd*() functions return the thing that you are adding.
// It's convenient to create and add an object in one line.
cpBody *ballBody = cpSpaceAddBody(space, cpBodyNew(mass, moment));
cpBodySetPosition(ballBody, cpv(0, 15));

// Now we create the collision shape for the ball.
// You can create multiple collision shapes that point to the same body.
// They will all be attached to the body and move around to follow it.
cpShape *ballShape = cpSpaceAddShape(space, cpCircleShapeNew(ballBody, radius, cpvzero));
cpShapeSetFriction(ballShape, 0.7);

Camera2D cam = {0};
cam.offset = {300, 300};
cam.zoom = 1;

double timeStep = 1/60.0;
double lastTime = GetTime();
double deltaTime = GetTime();
while(!WindowShouldClose())
{
    PollInputEvents();
    ClearBackground(BLUE);

    deltaTime = GetTime() - lastTime;
    if(deltaTime < 1/60.0)
    {
        WaitTime(1/60.0-deltaTime);
        deltaTime = GetTime() - lastTime;
    }
    lastTime = GetTime();

    cpVect pos = cpBodyGetPosition(ballBody);
    cpVect vel = cpBodyGetVelocity(ballBody);

    BeginMode2D(cam);
        DrawCircle(pos.x, pos.y, radius, WHITE);
    EndMode2D();

    cpSpaceStep(space, timeStep);

    SwapScreenBuffer();
}

// Clean up our objects and exit!
cpShapeFree(ballShape);
cpBodyFree(ballBody);
cpBodyFree(staticBody);
cpSpaceFree(space);

CloseAudioDevice();
CloseWindow();

}
`

this is a simple chipmunk program with static body as border all around the screen and a simple ball.
but when the ball falls and hit the border, the app freezes. then, i tried to trace where it happens.
i found out that the cpSpaceStep calls cpSpaceProcessComponents (line : 376) that calls FloodFillComponent(body, body); (line : 303)
then inside FloodFillComponent there is a statement to traverse all the arbiter CP_BODY_FOREACH_ARBITER(body, arb) , but i found that this loop goes infinitely.

i can't figure it out by myself why this happens. because, i don't know what are the things inside the arbiter structure and in what point of the program the arbiter is created and who sets the next arbiter pointer so on...

if any one knows why the loop goes infinitely please leave a comment.

Note : the demo program also freezes as soon as a collision happens in any demo. i suspect this problem links to that.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions