Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (581)
Games in Android Showcase (163)
games submitted by our members
Games in WIP (632)
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 798 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 14:49:33 »

In my main class:

      /* next step for physics events */
      toDestroy = new Vector<Body>();
      //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);

In my class that contains the physics world, its objects and the collision listener:
   private void createCollisionListener() {
      ContactListener cl = new ContactListener() {

            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){

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 15: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 20: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 21: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 00: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.

MrMapcom (24 views)
2015-05-23 20:26:16

MrMapcom (31 views)
2015-05-23 20:23:34

Waterwolf (36 views)
2015-05-20 15:01:45

chrislo27 (44 views)
2015-05-20 03:42:21

BurntPizza (77 views)
2015-05-10 15:53:18

FrozenShade (61 views)
2015-05-07 09:11:21

TheLopais (226 views)
2015-05-06 13:36:48

TheLopais (207 views)
2015-05-06 13:35:14

TheLopais (213 views)
2015-05-06 13:33:39

TheLopais (234 views)
2015-05-06 13:32:48
List of Learning Resources
by SilverTiger
2015-05-05 10:20:32

How to: JGO Wiki
by Mac70
2015-02-17 20:56:16

2D Dynamic Lighting
by ThePixelPony
2015-01-01 20:25:42

How do I start Java Game Development?
by gouessej
2014-12-27 19:41:21

Resources for WIP games
by kpars
2014-12-18 10:26:14

Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00 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‑
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!