Box2D Forums

It is currently Sat May 18, 2013 7:44 pm

All times are UTC - 8 hours [ DST ]




Post new topic Reply to topic  [ 6 posts ] 
Author Message
PostPosted: Sun Jan 15, 2012 8:01 am 
Offline

Joined: Thu Aug 18, 2011 5:31 pm
Posts: 6
So I was working on my platformer project using the Slick and JBox2D engines, when suddenly the dynamic bodies seemed to stop colliding with each other. I've tried everything I can think of, and the only thing I've come up with so far is that they will only collide with static bodies. That is, a dynamic body will stop falling when it hits a static body, but will just fall through another dynamic one as if it wasn't even there.

I remember accidentally two dynamic bodies at the same position, and they pushed each other to the sides, which obviously doesn't happen any more. So apparently, it did work before.

Another thing that is strange is that when I tried to make one of the bodies fall by rotating off a static body, by making only its edge hit the static and nothing more, it just stayed on as if it had rotation turned off. I'm starting to think it's a bug caused by installing the engine wrong or so, or maybe messing with it too much (I tried moving my project to DropBox, causing the whole engine to disappear from the newly created workspace). But when I reinstalled it, nothing seemed different.

Here's some code from my project:

Where I initialize the objects with physical bodies:
Code:
    Image image = null;
        try {
            image = new Image("sprites/body_base.png");
        } catch (SlickException e) {
            e.printStackTrace();
        }
        
        PolygonShape shape = new PolygonShape();
        shape.setAsBox(gs.toMeters(image.getWidth()/2),
                -gs.toMeters(image.getHeight()/2));
        
        PolygonShape shape2 = new PolygonShape();
        shape2.setAsBox(gs.toMeters(image.getWidth()/2),
                -gs.toMeters(image.getHeight()/2));
        
        //This one's got a dynamic body
        Actor testActor = new Actor(gs, new Vec2(5f, 30f),
                shape, image);
        
        //This one's static
        Solid testSolid = new Solid(gs, new Vec2(5f, 5f),
                shape, image);
        
        //Dynamic
        Item testItem = new Item(gs, new Vec2(5f, 20f),
                shape, image);
        
        addActor(testActor);
        addSolid(testSolid);
        addItem(testItem); 


The class which all the classes above (actor, solid, item) inherit:
Code:
public class PhysicsEntity implements Entity {
   
   protected Body body;
   protected GameplayState gs;
   protected Image image = null;
   protected Vec2 position;
   
   public PhysicsEntity(GameplayState gs, Vec2 position, BodyDef bd,
         Shape shape, Image image) {
      this.gs = gs;
      this.position = position;
      
      if(image != null) {
         this.image = image;
      }
      
      FixtureDef fd = new FixtureDef();
      fd.shape = shape;
      fd.density = 1.0f;
      
      body = gs.getPhysics().getWorld().createBody(bd);
      body.createFixture(fd);
   }
   
   public void init() {
   }
   
   public void update() {
      position = body.getPosition();
   }
   
   public void render() {
      if(image != null) {
         image.draw(
               gs.toPixels(position.x),
               -gs.toPixels(position.y)+gs.getHeight());
      }
   }
}


The method which all of the classes use to generate the BodyDef. The variations are so minor I'll only include the one for Actor (note the fixedRotation=true), and I've even tried varying them but come up with that the only thing that makes a difference is the BodyType being static.
Code:
   protected static BodyDef makeBodyDef(Vec2 position, BodyType type) {
      BodyDef bd = new BodyDef();
      bd.position = position;
      bd.type = type;
      bd.fixedRotation = true;
      bd.allowSleep = false;
      
      return bd;
   }


Sorry for the long code samples, if there's a way to minimize the length of the post by for example using one of those "spoiler" buttons, I'd like to know. <:

Thanks for any replies.


Top
 Profile  
 
PostPosted: Sun Jan 15, 2012 7:47 pm 
Offline

Joined: Mon Jun 08, 2009 12:21 pm
Posts: 353
Can you try using the new 2.1.2.2 version? There was a pretty bad bug in there with polygon shapes, perhaps that solved your problem


Top
 Profile  
 
PostPosted: Tue Jan 17, 2012 9:37 am 
Offline

Joined: Thu Aug 18, 2011 5:31 pm
Posts: 6
Nope, I updated and it still doesn't work.


Top
 Profile  
 
PostPosted: Tue Jan 17, 2012 11:08 am 
Offline

Joined: Mon Jun 08, 2009 12:21 pm
Posts: 353
Are you setting a Filter anywhere in the code?

Edit:
To clarify, the only reasons I can think if this happening is
  • There is a filter on your fixtures that specifies that the bodies should not collide
  • The contacts are being disposed by a contact listener your may have put in there


Top
 Profile  
 
PostPosted: Wed Jan 18, 2012 6:35 am 
Offline

Joined: Thu Aug 18, 2011 5:31 pm
Posts: 6
Nope, no filters, and no contact listeners, that's what I find so odd. When I next get a chance to I'll try to create a body as separated from the Slick engine as possible, and see if there's any difference then.

Though I wonder, might it have some connection to that the testing environment runs at less than 10 FPS for me? I thought it might be because I use a 64-bit OS, since it worked well when I used 32-bit.


Top
 Profile  
 
PostPosted: Wed Jan 18, 2012 2:50 pm 
Offline

Joined: Thu Aug 18, 2011 5:31 pm
Posts: 6
I found the problem. It was like you said at first, that the polygon shapes caused it. When I tried it with circle shapes instead it worked flawlessly.

Apparently the polygon shapes were fixed as well when I updated with SVN instead of using the snapshot build.

Thanks for the help! (:


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 6 posts ] 

All times are UTC - 8 hours [ DST ]


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Powered by phpBB® Forum Software © phpBB Group