Have a body follow another body

I have two bodies.
One body is for my player, another is for a ground sensor.
(this is for a platform game by the way).

I'm using the box2d implementation used in the AndEngine lib for Android development.

My current problem is, my player is a dynamic body, while the ground sensor is a static body.

I need the following:
- Have the ground sensor follow my player around
- Allow my player to be collision free of the ground sensor

I have created a line joint that joins the two bodies together.

The ground sensor basically is just going to be used in my contact listener to tell if the player has hit the ground
(so that I don't jump infinitely in the air).
I've placed it perfectly where my player's feet are supposed to be.

The problem is, since it's a static body...my player just floats on top of it, not being able to move (since I've placed it directly
on my players foot, he can't move).

How would I make it so the player body can flow freely through the ground sensor body, as if it isn't there?
I just need the ground sensor body to follow my player (staying relative to his foot).

Code:
      spr = new AnimatedSprite(x, y, playerTextureRegion);
      setPosition(120, 120);
      spr.setCurrentTileIndex(0);

      bodyFixtureDef = PhysicsFactory.createFixtureDef(1, 0.5f, 0.5f);
      bodyFixtureDef.restitution = 0.0f;

      body = PhysicsFactory.createBoxBody(world, spr, BodyType.DynamicBody,
            bodyFixtureDef);
      body.setFixedRotation(true);
      body.setUserData("player");
      FixtureDef groundsensorFixtureDef = PhysicsFactory.createFixtureDef(0,
            0.5f, 0.5f);
      groundsensorFixtureDef.isSensor = true;

                //Place the block sprite (sprite for the ground sensor) at the player's foot area
      final AnimatedSprite block = new AnimatedSprite(x
            + (spr.getWidth() / 2) - 3, y + spr.getHeight() + 3,
            testTextureRegion);
      Body ground_sensor = PhysicsFactory.createBoxBody(world, block,
            BodyType.StaticBody, groundsensorFixtureDef);
      ground_sensor.setUserData("groundSensor");

      final LineJointDef lineJointDef = new LineJointDef();
                lineJointDef.initialize(body, ground_sensor, body.getWorldCenter(), new Vector2(1.0f, 0.0f));
                lineJointDef.enableMotor = false;
                lineJointDef.motorSpeed = 10;
                lineJointDef.maxMotorForce = 100;

                //Join the player's sprite and body together
      world.registerPhysicsConnector(new PhysicsConnector(spr, body, true,
            true));

                //Join the ground sensor's sprite and body together
      world.registerPhysicsConnector(new PhysicsConnector(block,
            ground_sensor, true, true));

      scene.attachChild(spr);
      scene.attachChild(block);
You can put more than one fixture on a body.
So if the sensor is attached at the foot of the player body, it will always be in the right place and the two fixtures will be 'collision free'.
I'm a bit confused then.
So here's my code for my ground fixture then:
Code:
      FixtureDef groundsensorFixtureDef = PhysicsFactory.createFixtureDef(1,
            0.5f, 0.5f);
      groundsensorFixtureDef.isSensor = true;
      
      body.createFixture(groundsensorFixtureDef);


A fixture does not have a method: "setUserData()" so..how would I get the collision in the contact listener?

Also, how do I set this fixture at the foot of the player?

edit:
Well that didn't work.
Quote:
body.createFixture(groundsensorFixtureDef);


I get a null pointer exception when this executes.
I don't know about the Java port, but the original C++ version of Box2D allows for user data in bodies, fixtures and joints.
To put the sensor at the foot of the player you just need to set sensible locations for the main body and the foot sensor. This might be useful: http://www.iforce2d.net/b2dtut/jumpability
All right so here is my new code:
Code:
      bodyFixtureDef = PhysicsFactory.createFixtureDef(1, 0.5f, 0.5f);
      bodyFixtureDef.restitution = 0.0f;

      body = PhysicsFactory.createBoxBody(world, spr, BodyType.DynamicBody,
            bodyFixtureDef);
      body.setFixedRotation(true);
      body.setUserData("player");
      
      FixtureDef groundsensorFixtureDef = PhysicsFactory.createFixtureDef(1,
            0.5f, 0.5f);

      PolygonShape polyShape = new PolygonShape();
      polyShape.setAsBox(1, 1);
      
      groundsensorFixtureDef.shape = polyShape;
      groundsensorFixtureDef.density = 1.0f;
      groundsensorFixtureDef.isSensor = true;
      Fixture footSensorFixture = body.createFixture(groundsensorFixtureDef);
      footSensorFixture.setUserData("foot");


Now in my contact listener I'm still not getting any collisions between "wall" and "foot", heh, not even "player" and "foot".
And after looking at this:
http://www.iforce2d.net/b2dtut/jumpability
I'm still not sure how to set the actual position of it to the player's foot.

I even tried making the shape as big as I could with: setRadius() but it still couldn't detect a collision...
You will not get any collisions between "player" and "foot" because they are on the same body.

I can't see where you define the shape for the player's body, but if you make it bigger than the foot sensor then the foot sensor will not have a chance to hit the ground.
You can change the position of the foot sensor by using the second form of SetAsBox which takes another parameter to specify the center of the box.
Hm well the shape of the player is determined by: "spr".
The "spr" (sprite) can be used as an argument in andengine's implementation.

Anyways, the sprite is 33x33 pixels...
So why wouldn't it be working? I made the foot shape bigger than the sprite didn't I?
Box2D doesn't know anything about pixels. You mean the player is 33x33 units in the physics world? If so, then the foot sensor being a 2x2 box would be smaller.
I recommend you use the debug draw feature so you can see exactly what is going on.
Finally got it working!
It would be nice if you could mention something about how you fixed the issue, for people who come to this thread later.

Original thread: https://box2d.org/forum/viewtopic.php?t=8047