【发布时间】:2015-03-03 15:41:11
【问题描述】:
我是 Libgdx 的新手。我正在检测两个物体的碰撞。使用“polygonshape”创建了两个实体,但不幸的是,没有任何方法可以检测“PolygonShapes”之间的碰撞,而是用于“矩形”。所以我想出了一个想法,将矩形附加在每个多边形的前面,然后检测矩形的碰撞,但是当我运行代码时,我看不到任何矩形,只看到多边形。
这是我的代码,
public class Player implements Screen, InputProcessor {
private Body polybody;
private Player player;
private World world;
Array<Rectangle> raindrops;
private Body enemybody;
private Sprite polysprite;
public final float width,height;
private Vector2 movement=new Vector2();
private float speed=580;
public Player(World world,float x,float y,float width)
{
this.width=width; //IMP
height=width*2;
BodyDef polygon=new BodyDef();
polygon.type=BodyType.DynamicBody;
polygon.position.set(x,y); //
PolygonShape poly =new PolygonShape();
poly.setAsBox(width/2,height/2); //
FixtureDef polyfixture=new FixtureDef();
polyfixture.shape=poly;
polyfixture.friction=0.8f; //
polyfixture.restitution=0.1f; //
polyfixture.density=3; //
//creating actual body
polybody=world.createBody(polygon);
polybody.createFixture(polyfixture);
//disposing the body
BodyDef rectangle=new BodyDef();
rectangle.type=BodyType.DynamicBody;
Rectangle rect=new Rectangle();
rect.setWidth(50);
rect.setHeight(50);
rect.setPosition(x, y);
enemybody=world.createBody(rectangle);
polysprite=new Sprite(new Texture("img/car.jpg"));
polysprite.setSize(0.5f, 1);
polysprite.setOrigin(polysprite.getWidth()/2, polysprite.getHeight()/2);
polybody.setUserData(polysprite);
poly.dispose();
}
public void update()
{
polybody.applyForceToCenter(movement, true);
enemybody.applyForceToCenter(movement,true);
}
public Body getBody(){
{
return polybody;
}
}
}
碰撞代码:
public Player(World world,float x,float y,float width)
{
this.width=width; //IMP
height=width*2;
BodyDef polygon=new BodyDef();
polygon.type=BodyType.DynamicBody;
polygon.position.set(x,y); //
// polygon.fixedRotation=true;
//polygon shape
//Rectangle poly=new Rectangle();
PolygonShape poly =new PolygonShape();
poly.setAsBox(width/2,height/2); //
//fixture defn
polygon.position.set(5,4);
FixtureDef polyfixture=new FixtureDef();
polyfixture.shape=poly;
polyfixture.friction=0.8f; //
polyfixture.restitution=0.1f; //
polyfixture.density=3; //
//creating actual body
polybody=world.createBody(polygon);
polybody.createFixture(polyfixture);
// polybody.applyAngularImpulse(52, true);
//disposing the body
polysprite=new Sprite(new Texture("img/car.jpg"));
polysprite.setSize(2, 3); //size of mario
polysprite.setOrigin(polysprite.getWidth()/2, polysprite.getHeight()/2);
polybody.setUserData(polysprite);
//第二个身体
BodyDef polygons=new BodyDef();
polygons.type=BodyType.DynamicBody;
PolygonShape polys=new PolygonShape();
polys.setAsBox(2,2);
FixtureDef polyxfixture=new FixtureDef();
polyxfixture.shape=polys;
polyxfixture.friction=0.8f;
polyxfixture.restitution=0.1f;
polyxfixture.density=3;
polybodys=world.createBody(polygons);
polybodys.createFixture(polyxfixture);
poly.dispose();
}
@Override
public void beginContact(Contact contact) {
// TODO Auto-generated method stub
Fixture fixtureA=contact.getFixtureA();
Fixture fixtureB=contact.getFixtureB();
System.out.println("collides");
}
这是我的代码。我使用“多边形”和“矩形”创建了 2 个实体,但没有创建矩形。我找不到是什么错误。请帮忙!!提前致谢
【问题讨论】:
-
您似乎在混合两种不同的东西:来自 libgdx API 的
Rectangle和来自 Box2D 的Bodys。Rectangle与 Box2D 物理引擎无关,因此它无法检测到与您的Rectangle的碰撞。相反,您应该创建一个类似矩形的Fixture,就像您已经为polyfixture所做的那样。然后将此Fixture添加到您的enemyBody。 -
当我检测到两个多边形形状之间的碰撞,如 if(Intersector.overlapConvexPolygons(poly,polys) 其中 poly 是“polygonshape”,polys 也是“polygonshape”,如上所述,它给了我类似 The method overlayConvexPolygons (Polygon, Polygon) 类型中的 Intersector 不适用于参数 (PolygonShape, PolygonShape) @Springrbua
-
我添加了一个答案,希望能解决这些问题。如果您使用 Box2D,则不需要检测碰撞,box2D 会为您完成这项工作。
-
如何使用 box2D 检测碰撞。 @Springrbua
-
我在回答中添加了一些信息。