5.7 C
New York
Tuesday, April 8, 2025

raylib – Cannot make circles collide utilizing Box2D


I am utterly new to Box2D. I am implementing some assessments with Raylib. I managed to implement the collision between static and dynamic rectangles and now I wish to insert circles into the simulation, however the collision between themselves and between them and the rectangles would not work. I feel I am lacking one thing, however I am unable to discover what it could possibly be. That is my MRE:

#embrace 
#embrace 

#embrace "box2d/box2d.h"
#embrace "box2d/base.h"

#embrace "raylib/raylib.h"
#embrace "raylib/rlgl.h"

#outline WORLD_SIZE 400

int principal( void ) {

    b2WorldDef worldDef = b2DefaultWorldDef();
    worldDef.gravity = (b2Vec2){ 0.0f, -10.0f };
    b2WorldId worldId = b2CreateWorld( &worldDef );

    // floor
    Vector2 gDim = { WORLD_SIZE, 20 };
    b2BodyDef gBodyDef = b2DefaultBodyDef();
    gBodyDef.place = (b2Vec2){ WORLD_SIZE / 2, 10 };
    gBodyDef.sort = b2_staticBody;
    b2BodyId gBodyId = b2CreateBody( worldId, &gBodyDef );
    b2Polygon gPolygon = b2MakeBox( gDim.x/2, gDim.y/2 );
    b2ShapeDef gShapeDef = b2DefaultShapeDef();
    b2ShapeId gShapeId = b2CreatePolygonShape( gBodyId, &gShapeDef, &gPolygon );

    // sq.
    Vector2 sDim = { 20, 20 };
    b2BodyDef sBodyDef = b2DefaultBodyDef();
    sBodyDef.place = (b2Vec2){ WORLD_SIZE / 2 - 40, WORLD_SIZE / 2 };
    sBodyDef.sort = b2_dynamicBody;
    b2BodyId sBodyId = b2CreateBody( worldId, &sBodyDef );
    b2Polygon sPolygon = b2MakeBox( sDim.x/2, sDim.y/2 );
    b2ShapeDef sShapeDef = b2DefaultShapeDef();
    sShapeDef.density = 1.0f;
    sShapeDef.friction = 0.3f;
    b2ShapeId sShapeId = b2CreatePolygonShape( sBodyId, &sShapeDef, &sPolygon );

    // circle
    float cRad = 10.0f;
    b2BodyDef cBodyDef = b2DefaultBodyDef();
    cBodyDef.place = (b2Vec2){ WORLD_SIZE / 2 + 40, WORLD_SIZE / 2 };
    cBodyDef.sort = b2_dynamicBody;
    b2BodyId cBodyId = b2CreateBody( worldId, &cBodyDef );
    b2Circle cCircle = (b2Circle){ { cBodyDef.place.x, cBodyDef.place.y }, cRad };
    b2ShapeDef cShapeDef = b2DefaultShapeDef();
    cShapeDef.density = 1.0f;
    cShapeDef.friction = 0.3f;
    b2ShapeId cShapeId = b2CreateCircleShape( cBodyId, &cShapeDef, &cCircle );

    InitWindow( WORLD_SIZE, WORLD_SIZE, "MRE Box2D" );
    SetTargetFPS( 60 );

    whereas ( !WindowShouldClose() ) {

        b2World_Step( worldId, GetFrameTime(), 4 );

        BeginDrawing();
        ClearBackground( WHITE );

        rlTranslatef( 0, GetScreenHeight(), 0 );
        
        // floor
        DrawRectangle( 
            gBodyDef.place.x - gDim.x / 2, 
            -gBodyDef.place.y - gDim.y / 2, 
            gDim.x, 
            gDim.y, 
            BLACK );

        // sq.
        b2Vec2 sPos = b2Body_GetPosition( sBodyId );
        b2Rot sRot = b2Body_GetRotation( sBodyId );
        Rectangle sRect = (Rectangle){ sPos.x, -sPos.y, sDim.x, sDim.y };

        DrawRectanglePro( 
            sRect, 
            (Vector2) { sRect.width / 2, sRect.top / 2 }, 
            RAD2DEG * atan2( sRot.c, sRot.s ),
            BLUE
        );

        // circle
        b2Vec2 cPos = b2Body_GetPosition( cBodyId );
        DrawCircle( cPos.x, -cPos.y, cRad, ORANGE );

        EndDrawing();

    }

    b2DestroyWorld( worldId );
    CloseWindow();

    return 0;

}

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles