eska
eska

Reputation: 61

Box2d in Andengine - attaching child bodies - is it possible

I have a problem with Andengine Box2d Extension.

I have 2 rectangles: base and fuelStation.

fuelStation is a child of base. When I'm rotating base with setTransform method, fuelStation sprite is rotating too, but body remains in the same position.

Red - base, Cyan - child

base = new Rectangle(CAMERA_WIDTH / 2, CAMERA_HEIGHT / 2 - 200, 200, 200, vbom);
fuelStation = new Rectangle(base.getWidth() / 2, 0, 500, 10, vbom);

this.attachChild(base);
base.attachChild(fuelStation);

final FixtureDef objectFixtureDef1 = PhysicsFactory.createFixtureDef(1, 0.0f, 0.5f);
final FixtureDef objectFixtureDef2 = PhysicsFactory.createFixtureDef(1, 0.0f, 0.5f);

baseBody = PhysicsFactory.createBoxBody(physicsWorld, base, BodyType.StaticBody, objectFixtureDef1);
baseBody.setUserData("base");

fuelStationBody = PhysicsFactory.createBoxBody(physicsWorld, fuelStation, BodyType.KinematicBody, objectFixtureDef2);
fuelStationBody.setUserData("station");

physicsWorld.registerPhysicsConnector(new PhysicsConnector(base, baseBody, true, true));
// physicsWorld.registerPhysicsConnector(new PhysicsConnector(fuelStation, fuelStationBody,
// true, true));

When I remove comment from the last line - sprite position changes but still not working properly (body stands still).

How to connect properly these 2 bodies?

Upvotes: 1

Views: 105

Answers (1)

GuilhE
GuilhE

Reputation: 11861

You have to keep in mind that all the transformations that you do to the entities (sprites) will only affect the "visual" content, in other words, will never affect the physic bodies. If you want to affect the entities and the bodies you will only achieve that by applying forces to the physic bodies and, because they are connected with a PhysicsConnector, those transformations will affect both the physic body and the the sprite ("visual body").
So, having the fuelStating as a child of base will affect nothing. To affect two bodies you need a "connection" between them and you can achieve that by creating a joint that will connect the two bodies: http://www.iforce2d.net/b2dtut/joints-overview

Hope it helps.

Upvotes: 1

Related Questions