所以,在我的游戏中,我有一个box2D动态体,其多边形形状作为我的播放器,box2D静态体,链形状作为静态框。调试渲染器正确显示所有框。当玩家从顶部与盒子碰撞时,它会发生碰撞,但是当从任何一侧碰撞时,它会向右移动。我曾尝试将多边形用于静态框,但它很奇怪。我无法弄清楚出了什么问题。
以下是代码:
玩家身体和夹具定义:
bdef=new BodyDef();
bdef.position.set((WIDTH/2+169)/PPM,100/PPM);
bdef.type=BodyType.DynamicBody;
body=world.createBody(bdef);
PolygonShape shape=new PolygonShape();
shape.setAsBox(16/PPM,16/PPM);
fdef=new FixtureDef();
fdef.shape=shape;
fdef.filter.categoryBits=B2DVars.BIT_PLAYER;
fdef.filter.maskBits=B2DVars.BIT_BLOCK;
body.createFixture(fdef).setUserData("player");
静态箱体和夹具定义:
bdef=new BodyDef();
fdef=new FixtureDef();
bdef.type=BodyType.StaticBody;
bdef.position.set((col+0.5f)*tileSize/PPM,(row+0.5f)*tileSize/PPM);
ChainShape chainShape=new ChainShape();
Vector2[] v=new Vector2[5];
v[0]=new Vector2(-tileSize/2/PPM,-tileSize/2/PPM);
v[1]=new Vector2(tileSize/2/PPM,-tileSize/2/PPM);
v[2]=new Vector2(tileSize/2/PPM,tileSize/2/PPM);
v[3]=new Vector2(-tileSize/2/PPM,tileSize/2/PPM);
v[4]=new Vector2(-tileSize/2/PPM,-tileSize/2/PPM);
chainShape.createChain(v);
fdef.friction=0;
fdef.shape=chainShape;
fdef.isSensor=false;
fdef.filter.categoryBits=B2DVars.BIT_BLOCK;
fdef.filter.maskBits=B2DVars.BIT_PLAYER;
world.createBody(bdef).createFixture(fdef).setUserData("block");
编辑:问题不再存在,我通过转换来设置玩家位置是一个问题,但现在我不知道,当我按下按钮然后停止时,如何让玩家移动释放它?
答案 0 :(得分:0)
不应通过DynamicBody
移动setTransform
。设置DynamicBody
位置的最顺畅的方法是通过MouseJoint
,但它是否会帮助您取决于您所追求的移动类型。这个example是一个很好的起点。
答案 1 :(得分:0)
以这种方式使用联系人侦听器来检查冲突
'if((contact.getFixtureA().getBody() == bodyEdgeScreen &&
contact.getFixtureB().getBody() == body2)
||
(contact.getFixtureA().getBody() == body2 &&
contact.getFixtureB().getBody() == bodyEdgeScreen))'