Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (475)
Games in Android Showcase (106)
games submitted by our members
Games in WIP (530)
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  
  [Box2D] Game crashes when I try to destroy bodies.  (Read 483 times)
0 Members and 1 Guest are viewing this topic.
Offline heisenbergman

JGO Coder


Medals: 14


L___ o_ G___ a__ P___


« Posted 2013-05-29 16:49:33 »

In my main class:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
      /* next step for physics events */
      toDestroy = new Vector<Body>();
      w.step(BOX_STEP, BOX_VELOCITY_ITERATIONS, BOX_POSITION_ITERATIONS);
      //System.out.println(theBall.getPos()+" | "+Math.abs(Math.toDegrees(theBall.getRotation()))%360);
     
      /* destroy objects for destruction (e.g. - experience gems that have been picked up, bullets that have collided with another object) */
      for (int i=0;i<toDestroy.size();i++){
         Body b = toDestroy.elementAt(i);
         b.setAwake(false);
         w.destroyBody(b);
      }


In my class that contains the physics world, its objects and the collision listener:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
   private void createCollisionListener() {
      ContactListener cl = new ContactListener() {


            @Override
            public void beginContact(Contact contact) {
                Fixture fixtureA = contact.getFixtureA();
                Fixture fixtureB = contact.getFixtureB();
                LogapBall lball = cast(fixtureA.getBody().getUserData(), fixtureB.getBody().getUserData(), LogapBall.class);
                LogapExpGem leg = cast(fixtureA.getBody().getUserData(), fixtureB.getBody().getUserData(), LogapExpGem.class);
                LogapBox lbox = cast(fixtureA.getBody().getUserData(), fixtureB.getBody().getUserData(), LogapBox.class);
                LogapBullet lbull = cast(fixtureA.getBody().getUserData(), fixtureB.getBody().getUserData(), LogapBullet.class);

               if(lbull !=null && lball == null){
                 ItLoGaP.toDestroy.addElement(lbull.getBody());
               }


I read somewhere that a usual cause of the app crashing when trying to destroy box2d objects is when you're trying to destroy the object inside the world step. This is why I just created a Vector in the main class, which is populated with bodies to destroy while the world step is ongoing so that once the step finishes, that's the only time when the bodies will actually be destroyed.

So now that my game is still crashing... I have no idea what's still causing it.

Offline 65K
« Reply #1 - Posted 2013-05-29 17:24:44 »

Do you clear the destroy list somewhere ? Do you mess with multiple threads ?
Btw, instead of Vector I would rather recommend to use ArrayList.

Offline pitbuller
« Reply #2 - Posted 2013-05-29 22:24:20 »

Removing object twice will crash box2d so you need to check and remove duplicate body references from toDestroy list before remove loop.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline UprightPath
« Reply #3 - Posted 2013-05-29 23:35:21 »

Another option, though I don't know how it would impact performance in the speed direction, would be to use a HashSet<Body> to hold your to destroy list. It abstracts out your need to actually remove duplicates.

Offline heisenbergman

JGO Coder


Medals: 14


L___ o_ G___ a__ P___


« Reply #4 - Posted 2013-05-30 02:45:15 »

Thanks everyone! It looks like it was being caused by the duplicates issue. I implemented it instead using HashSet as UprightPath suggested and it seems to be working now Smiley

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.

ctomni231 (34 views)
2014-07-18 06:55:21

Zero Volt (30 views)
2014-07-17 23:47:54

danieldean (25 views)
2014-07-17 23:41:23

MustardPeter (27 views)
2014-07-16 23:30:00

Cero (42 views)
2014-07-16 00:42:17

Riven (44 views)
2014-07-14 18:02:53

OpenGLShaders (32 views)
2014-07-14 16:23:47

Riven (33 views)
2014-07-14 11:51:35

quew8 (30 views)
2014-07-13 13:57:52

SHC (66 views)
2014-07-12 17:50:04
HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22

How do I start Java Game Development?
by ra4king
2014-05-17 11:13:37

HotSpot Options
by Roquen
2014-05-15 09:59:54

HotSpot Options
by Roquen
2014-05-06 15:03:10

Escape Analysis
by Roquen
2014-04-29 22:16:43

Experimental Toys
by Roquen
2014-04-28 13:24:22
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!