提前感谢您的帮助!我正在学习关于在我的游戏中添加身体的box2d
教程,然而,他们似乎只是在使用调试器。
我只是在练习,我有一个塞尔达snes风格的游戏地图和英雄人物加载罚款。在我的游戏地图中,我有对象(矩形地图对象),推杆,树桩,矩形(我想用它来阻止英雄穿过树)。
在这个例子中可以使用Box2d
吗?我不想要任何gravity/friction/mass
等,但我想做的是做‘静态身体’从树桩矩形,我可以检测到碰撞我的英雄的矩形。当然,我可以编写我自己的代码来检查碰撞,但是它总是很糟糕,而且我很难想出正确的方法来循环所有树桩矩形,并阻止英雄穿过它。
(注意:我也是通过写一个平台来实现的。这一切运行良好,他降落在平台上,但我不使用Box2d
,例如,它不会掉下来,当你走在平台的边缘,它一直走,直到你跳,然后它检测到平台在哪里。-所以我想学box2d
)
box2d
能否使这些静态体(以我上面所说的方式),如果是的话,如何将这些身体链接到您已经拥有的精灵(以及我从TMX file
中提取的矩形数组)?
希望这对人们来说是清楚的。非常感谢
发布于 2015-11-17 07:15:36
您应该为角色和树创建固定装置。
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)
使用这个函数
我希望这将对你的游戏有所帮助;)祝你好运
https://stackoverflow.com/questions/33747516
复制相似问题