Game with box2d crashes when having huge sprites

by Marc M   Last Updated October 10, 2019 13:13 PM

in my game Im using Box2D for collision detection. I'm also using Ashley as the Entity Component System.

Right now I have several entites for the player, the tree-sprites and for every single gras tile on the floor.

The player entity has a bodycomponent and a transformcomponent. The bodycomponent holds the reference to the Box2D body and such thinks. The transform component is for getting synced with the values of the bodies position so in other systems like the Rendering-System I can read these koordinates and draw the sprite from a sprite-component. When arrow-keys are pressed, the body-system is giving the body (from the body-component) some velocity.

This is how the player can move. The camera updates its position accordently to the trnasform-component of the player (which is - as already descriped - synced with the values from the body-component).

game impression

So far so good.

Anyway when moving the player through the scene and the maps contains also some entities with a simple tree sprite, the game crashed with a hint to Box2D.

The interesting thing is, that right now the only box2d body in the world is the one from the player and it will only crashes if a tree is in the scene. the more trees there are in the scene, the faster the program crashes.

My first though was, that the problem is that the big tree textures are too much for the memory or something but the OS (in my case MacOS) points out that it must have something to do with the following box2d file:

Box2D/Collision/b2Distance.h

Heres the error splash: crash error

Heres some code:

Box2DMovementSystem:

public void update(float deltaTime) {

    float frameTime = Math.min(deltaTime, 0.25f);
    accumulator += frameTime;
    // if (accumulator >= MAX_STEP_TIME) {
    // accumulator -= MAX_STEP_TIME;

    // ------ Box2D World updaten
    world.step(MAX_STEP_TIME, 6, 2);

    for (Entity entity : entities) {
        TransformComponent transform = tim.get(entity);
        BodyComponent bodyComp = bod.get(entity);
        VelocityComponent velocity = vel.get(entity);
        MovementComponent movement = mov.get(entity);
        BrainComponent brainComponent = brc.get(entity);

        // ------ calculate Box2D veloctiy

        if (brainComponent.wantUp) {
            bodyComp.body.applyLinearImpulse(new Vector2(0, velocity.verticalVelocity), bodyComp.body.getWorldCenter(), true);
        }
        ...

Box2DTransformSyncSystem:

public void update(float deltaTime) {

    float frameTime = Math.min(deltaTime, 0.25f);
    accumulator += frameTime;

    // ------ Box2D World updaten
    world.step(MAX_STEP_TIME, 6, 2);

    for (Entity entity : entities) {
        TransformComponent transform = tim.get(entity);
        BodyComponent bodyComp = bod.get(entity);

        // ------ Transform updaten
        Vector2 position = bodyComp.body.getPosition();
        Vector2 offset = bodyComp.offset;
        transform.x = position.x + offset.x;
        transform.y = position.y + offset.y;
    }

I'm confused and have no idea how to solve this. Do you know whats happening here?

Tags : box2d crash


Related Questions


Updated January 23, 2018 00:13 AM

Updated January 09, 2019 06:13 AM

Updated April 24, 2019 16:13 PM

Updated June 22, 2019 21:13 PM

Updated June 07, 2019 08:13 AM