您应该为角色和树木创建固定装置
vertexArray = new Vector2[3];
vertexArray[0] = new Vector2(0, 0);
vertexArray[1] = new Vector2(2, 0);
vertexArray[2] = new Vector2(1, 3);
treePolygonShape = new PolygonShape();
treePolygonShape.set(vertexArray);
fixtureTree = new FixtureDef();
fixtureTree.shape = treePolygonShape;
fixtureTree.filter.categoryBits = Game.TREE; // 0x0001 short int
fixtureTree.filter.maskBits = Game.CHARACTER; //0x0002 short int
characterShape = new CircleShape();
shape.setRadius(1);
fixtureCharacter = new FixtureDef();
fixtureCharacter.shape = characterShape;
fixtureCharacter.filter.categoryBits = Game.CHARACTER;
fixtureCharacter.filter.maskBits = Game.TREE;
...
创建身体
characterBodyDef.type = BodyDef.BodyType.KinematicBody; //I suppose your body is kinematic but it can be dynamic too
Body body = world.createBody(characterBodyDef);
body.setUserData(characterView); //display object of your character which is linked to the physical body in this way
characterView.setBody(body); // you can create this setter to have a reference to the physical body from display object
body.createFixture(fixtureCharacter);
...
treeBodyDef.type = BodyDef.BodyType.StaticBody;
treeBodyDef.position.set(treeX, treeY); //you can set tree position here , or later using setTransform(x, y, angle) function
Body treeBody = world.createBody(treeBodyDef);
treeBody.setUserData(treeView); //display object of your tree linked to the body
treeView.setBody(treeBody); // you can create this setter to have a reference to the physical body from display object
treeBody.createFixture(fixtureTree);
您的显示对象坐标应遵循物理身体的位置(为方便起见,您可以将它们与一些系数相乘,例如 PIXELS_TO_METERS = 100)
现在可以
body.setLinearVelocity(velocityX, velocityY);
到你的角色的身体和渲染(绘制或行动)功能,你可以同步显示对象坐标与身体的位置
为树木设置一个位置,你可以
treeBody.setTransform(x, y, angle)
使用此功能
我希望这对你的游戏有所帮助;)祝你好运