Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (497)
Games in Android Showcase (114)
games submitted by our members
Games in WIP (563)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  [LibGDX - Box2D] Deleting bodies  (Read 1506 times)
0 Members and 1 Guest are viewing this topic.
Offline kutucuk

Senior Member


Medals: 5
Exp: 3 years



« Posted 2014-03-02 14:03:58 »

Hi,
So I want to delete some bodies after they collide.
I have a Contact class which is a ContactListener class. In the postsolve method,
screen is my game screen, and delete is an array of bodies which I'm trying to remove:
1  
2  
3  
if(contact.getFixtureA().getBody().getUserData().equals("player")&&contact.getFixtureB().getBody().getUserData().equals("gold")){
   screen.delete.add(contact.getFixtureB().getBody());
}

Then I'm deleting my bodies like this:
1  
2  
3  
4  
5  
6  
7  
Iterator<Body> i = delete.iterator();
if(!world.isLocked()){
   while(i.hasNext()){
      Body b = i.next();
      world.destroyBody(b);
   }
}

But it gives me an error: Assertion failed: (m_bodyCount < m_bodyCapacity), function Add, file ./Box2D/Dynamics/b2Island.h, line 54.
I run this code just after I set the world to step.
Step is like this: world.step(1/60f, 6, 4);
Played with the numbers, didn't help.
I also have a randomGenerate() method which generates a platform, and adds it to the world. I thought that could be the reason because the error includes the word "Add" Cheesy But the problem persisted after I removed that method, meaning I only had two bodies in the world.


I also tried setting world step to 0, adding b.setAwake(false) and b.setActive(false) but nothing changed. The problem is always the same:
Quote
Assertion failed: (m_bodyCount < m_bodyCapacity), function Add, file ./Box2D/Dynamics/b2Island.h, line 54.

Now, I have no C/C++ experience but here is the 54th line:
1  
2  
3  
4  
5  
6  
7  
void Add(b2Body* body) <-- 54th line. 
{
   b2Assert(m_bodyCount < m_bodyCapacity);
   body->m_islandIndex = m_bodyCount;
   m_bodies[m_bodyCount] = body;
   ++m_bodyCount;
}


Please note that this happens even when there are only two bodies in the screen and nothing else is going on.

What can I do?
Offline kutucuk

Senior Member


Medals: 5
Exp: 3 years



« Reply #1 - Posted 2014-03-02 19:37:38 »

It was so simple. It was so simple that nobody ever had the same issue on the internet.
It was so simple that I feel stupid because I didn't think it before.

So, I have an array there, right? That's called delete. When the collision occurs, I have one element in the array. When the world is not locked, that is, the world is not stepping, I remove the body from the world. But not from the damn array.

This code always looked a little bit simple to me:
1  
2  
3  
4  
5  
6  
7  
Iterator<Body> i = delete.iterator();
if(!world.isLocked()){
   while(i.hasNext()){
      Body b = i.next();
      world.destroyBody(b);
   }
}


Add this, and it is done:
1  
i.remove();


Code is not important, just delete the object as well.

I'm not sure if I ever come across to a situation where this is problematic, but at least I should have thought that I needed to remove the object from the array as well. It would try to destroy a null 'thing' from the world again once the world is not locked.

Damn...

Damn 5 hours and I did nothing. I could be having a drink, I could be reading something useful, I could be meditating but no... That problem had to be stuck in my head. And that small damn idea had a business in somewhere else, that it didn't bother to check if it is needed in my head.

Man I'm furious right now.
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

BurntPizza (8 views)
2014-09-21 00:34:41

moogie (7 views)
2014-09-21 00:26:15

UprightPath (22 views)
2014-09-20 20:14:06

BurntPizza (27 views)
2014-09-19 03:14:18

Dwinin (40 views)
2014-09-12 09:08:26

Norakomi (70 views)
2014-09-10 13:57:51

TehJavaDev (96 views)
2014-09-10 06:39:09

Tekkerue (49 views)
2014-09-09 02:24:56

mitcheeb (70 views)
2014-09-08 06:06:29

BurntPizza (52 views)
2014-09-07 01:13:42
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!