Officer Vimes Officer Vimes - 3 months ago 30
Java Question

Removing a Body in Box2D (libgdx) promptly crashes the game

This is NOT being done during the world.step() method that I'm aware, and everything I found in the internet is about people doing it with collision.

I basically have it so if I press space a segment of a rope will get destroyed. Its sucesfully destroyed but the game propmptly crashes afterwards. This is what I get:

This application has requested the Runtime to terminate it in an unusual way.
Please contact the application's support team for more information.
AL lib: (EE) alc_cleanup: 1 device not closed
Assertion failed!

Program: C:\Program Files\Java\jdk1.8.0_65\bin\java.exe

File: ./Box2D/Dynamics/b2Island.h, Line 54

Expression: m_bodyCount < m_bodyCapacity


Code:

GamePlay screen
http://pastebin.com/x1x62axh

Rope
http://pastebin.com/SgtsMKKQ

Answer

I guess when you press space it destroying too fast and when there is no more rope this code causes error.

world.destroyBody(segments.get(segments.size()-1));

Check for segments.size()>0 when you destroying.

I also suggest

Gdx.input.isKeyJustPressed(int key)

instead of

Gdx.input.isKeyPressed(int key)
Comments