简体   繁体   English

Andengine中的Box2d-附加儿童尸体-是否可能

[英]Box2d in Andengine - attaching child bodies - is it possible

I have a problem with Andengine Box2d Extension. 我对Andengine Box2d Extension有问题。

I have 2 rectangles: base and fuelStation . 我有2个矩形: basefuelStation

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. 当我使用setTransform方法旋转底座时, fuelStation sprite也会旋转,但物体保持在相同位置。

红色-基地,青色-儿童

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? 如何正确连接这两个主体?

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"). 如果要影响实体和物体,则只能通过对物理物体施加力来实现,并且由于它们与PhysicsConnector连接,所以这些转换将同时影响物理物体和子画面(“视觉物体”) 。
So, having the fuelStating as a child of base will affect nothing. 因此,将fuelStating作为base的子代不会有任何影响。 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 要影响两个实体,您需要在它们之间建立“连接”,并且可以通过创建将两个实体连接在一起的关节来实现这一点: http : //www.iforce2d.net/b2dtut/joints-overview

Hope it helps. 希望能帮助到你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM