How to properly add a fixture to a body

  ... the Physics Box2D Extension for AndEngine.

How to properly add a fixture to a body

Postby BarbaraPM » Wed Aug 27, 2014 9:00 am

I´m dealing with the classic problem of adding a sensor at the bottom of the player to handle the isJumping functionality. My initialization code looks like this right now:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. final FixtureDef mPlayerFixtureDef = PhysicsFactory.createFixtureDef(0, 0f, 0f, false, CATEGORYBIT_PLAYER, MASKBITS_PLAYER, (short) 0);
  2.                 Body mPlayerBody = PhysicsFactory.createBoxBody(this.mPhysicsWorld, mPlayerSprite, BodyType.DynamicBody, mPlayerFixtureDef);
  3.  
  4.                 // create fixturedef for player foot
  5.                 FixtureDef fdef = new FixtureDef();
  6.                 PolygonShape shape = new PolygonShape();
  7.  
  8.                 // create box shape for player foot
  9.                 shape = new PolygonShape();
  10.  
  11.                 shape.setAsBox(10f/PhysicsConstants.PIXEL_TO_METER_RATIO_DEFAULT,   5f/PhysicsConstants.PIXEL_TO_METER_RATIO_DEFAULT, new Vector2(mPlayerBody.getPosition().x, mPlayerBody.getPosition().y+100), 0);
  12.                
  13.                 fdef.shape = shape;
  14.                 fdef.isSensor = true;
  15.                
  16.                 // create player foot fixture
  17.                 mPlayerBody.createFixture(fdef).setUserData("foot");
  18.                 shape.dispose();
  19.                
  20.                
  21.                 mPlayerSprite.setUserData(playerName);
  22.                 mPlayerBody.setUserData(playerName);
  23.                 this.mPhysicsWorld.registerPhysicsConnector(new PhysicsConnector(mPlayerSprite, mPlayerBody, true, false));
  24.  
  25.                 mScene.attachChild(mPlayerSprite);
  26.  
Parsed in 0.019 seconds, using GeSHi 1.0.8.4



And in the ContactListener:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. Fixture fa = contact.getFixtureA();
  2. Fixture fb = contact.getFixtureB();
  3.  
  4. if(fa.getUserData() == null || fb.getUserData() == null) return;
  5.                                
  6. if(fa.getUserData().equals("floor") && fa.getUserData().equals("foot")) {
  7.                                         isLanded = true;
  8.                                 }
  9. if(fb.getUserData().equals("floor") && fb.getUserData().equals("foot")) {
  10.                                         isLanded = true;
  11.                                 }
  12.  
Parsed in 0.012 seconds, using GeSHi 1.0.8.4


I don't know for sure what it's wrong in the initialization process, I suppose it's the way I create the foot fixture or the shape.setAsBox parameters (I haven't tried the debugger to see if it's correct)

I've been struggling with this for 2 weeks so any kind of help would be highly appreciated.
BarbaraPM
 
Posts: 1
Joined: Tue Aug 26, 2014 9:09 am

Re: How to properly add a fixture to a body

Postby quantid » Mon May 25, 2015 10:29 pm

What exactly is your question, i.e. what isn't working about your code?

BTW, not sure if your source code is the same, but your if statements check if the same fixture, e.g. "fa", equals two different things, which will never be true..
quantid
 
Posts: 1
Joined: Mon May 25, 2015 10:25 pm


Return to Physics Box2D

Who is online

Users browsing this forum: No registered users and 6 guests