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