编程语言
首页 > 编程语言> > java – 静态体桥接其他两个静态体

java – 静态体桥接其他两个静态体

作者:互联网

我有两个box2d体,如下所示,
enter image description here

我需要的是创造另一个身体,这两个身体的桥梁.我不是在寻找一个box2d关节,而是一个像触摸第一个身体最右边点和第二个身体最左边点的桥梁的身体,就像在这个图像中(借口我的绘画技巧).
enter image description here

问题是,看起来我没有得到关于如何创建第三(桥)体的坐标和角度.这是我的代码,

  Body body1, body2, bridge;

BodyDef bodyDef = new BodyDef();
bodyDef.type = BodyDef.BodyType.StaticBody;
bodyDef.position.set(350 / Constants.PIXELS_TO_METERS, 0 / Constants.PIXELS_TO_METERS);

body1 = world.createBody(bodyDef);

PolygonShape shape = new PolygonShape();
shape.setAsBox(300 / Constants.PIXELS_TO_METERS, 300 / Constants.PIXELS_TO_METERS);

FixtureDef fixtureDef = new FixtureDef();
fixtureDef.shape = shape;
fixtureDef.density = 1f;
body1.createFixture(fixtureDef);

BodyDef bodyDef2 = new BodyDef();
bodyDef2.type = BodyDef.BodyType.StaticBody;
bodyDef2.position.set(1600 / Constants.PIXELS_TO_METERS, 0 / Constants.PIXELS_TO_METERS);

body2 = world.createBody(bodyDef2);

PolygonShape shape2 = new PolygonShape();
shape2.setAsBox(300 / Constants.PIXELS_TO_METERS, 500 / Constants.PIXELS_TO_METERS);

FixtureDef fixtureDef2 = new FixtureDef();
fixtureDef2.shape = shape2;
fixtureDef2.density = 1f;
body2.createFixture(fixtureDef2);

BodyDef bodyDef3 = new BodyDef();
bodyDef3.type = BodyDef.BodyType.StaticBody;

Vector2 a = new Vector2(body1.getPosition().x + 300 / Constants.PIXELS_TO_METERS, body1.getPosition().y + (300 + 50 / 2) / Constants.PIXELS_TO_METERS);
Vector2 b = new Vector2(body2.getPosition().x - 300 / Constants.PIXELS_TO_METERS, body2.getPosition().y + (300 + 50 / 2) / Constants.PIXELS_TO_METERS);


float distance = a.dst(b);
Vector2 bridgePos = new Vector2(a.x + distance / 2, a.y + 50 / 2 / Constants.PIXELS_TO_METERS);

bodyDef3.position.set(bridgePos);

float angle = (float) Math.atan2(a.y + 50 / Constants.PIXELS_TO_METERS, b.x);
bridge = world.createBody(bodyDef3);

PolygonShape shape3 = new PolygonShape();
shape3.setAsBox(distance / 2, 50 / Constants.PIXELS_TO_METERS);

FixtureDef fixtureDef3 = new FixtureDef();
fixtureDef3.shape = shape3;
fixtureDef3.density = 1f;
bridge.createFixture(fixtureDef3);
bridge.setTransform(bridgePos, angle);

这并没有为桥梁提供正确的位置或角度,因为它适用于某些体型,有时则不适用(是的,我已经考虑了每次更改的硬编码值).我不确定我做错了什么.有帮助吗?

解决方法:

不要那样做!只需使用多边形向量以编程方式构建形状,如以下示例所示:

创建您的2个实体,将其设置为世界并调用createBridge方法:

private final Vector2 vecBodySize1 = new Vector2(100,50);
private final Vector2 vecBodySize2 = new Vector2(110,70);

private void createBodies(){
Body body1, body2;

BodyDef bodyDef = new BodyDef();
bodyDef.type = BodyDef.BodyType.StaticBody;
bodyDef.position.set(YOUR_POSITION.x, YOUR_POSITION.y);

body1 = world.createBody(bodyDef);

PolygonShape shape = new PolygonShape();
shape.setAsBox(vecBodySize1.x, vecBodySize1.y);

FixtureDef fixtureDef = new FixtureDef();
fixtureDef.shape = shape;
fixtureDef.density = 1f;
body1.createFixture(fixtureDef);

BodyDef bodyDef2 = new BodyDef();
bodyDef2.type = BodyDef.BodyType.StaticBody;
bodyDef2.position.set(YOUR_POSITION2.x, YOUR_POSITION2.y);

body2 = world.createBody(bodyDef2);

PolygonShape shape2 = new PolygonShape();
shape2.setAsBox(vecBodySize2.x, vecBodySize2.y);

FixtureDef fixtureDef2 = new FixtureDef();
fixtureDef2.shape = shape2;
fixtureDef2.density = 1f;
body2.createFixture(fixtureDef2);

///
// The body object gonna take the center position as default, this way you can set where the bridge starts and ends with 2 simple vectors
// you can set the thickness variable as you want to be the size of the bridge, never leave this null or 0
///
Vector2 vecBridge1 = new Vector2(body1.getPosition().x+vecBodySize1.x,body1.getPosition().y+vecBodySize1.y);
Vector2 vecBridge2 = new Vector2(body2.getPosition().x-vecBodySize2.x,body2.getPosition().y+vecBodySize2.y);
createBridge(vecBridge1,vecBridge2, 10.0f);
}

createBridge方法,用于创建连接2个对象的fixture的主体:

private void createBridge(Vector2 vecFrom, Vector2 vecTo, Float thickness){
        BodyDef bodyDef = new BodyDef();
        bodyDef.type = BodyDef.BodyType.StaticBody;
        bodyDef.position.set(vecFrom);

        Body tmpBody = Box2dGameWorld.getInstance().getWorldBox2D().createBody(bodyDef);

        PolygonShape shape = new PolygonShape();
        shape.set( new Vector2[]{
                new Vector2(0,0)
                ,new Vector2(vecTo.x-vecFrom.x,vecTo.y-vecFrom.y)
                ,new Vector2(vecTo.x-vecFrom.x,vecTo.y-vecFrom.y-thickness)
                ,new Vector2(0,0-thickness)});

        FixtureDef fixtureDef = new FixtureDef();
        fixtureDef.shape = shape;
        fixtureDef.density = 1f;
        tmpBody.createFixture(fixtureDef);
    }

结果:

Testing the bridge

你可以看到你现在可以创建给出2个向量和厚度的桥梁,你没有必要将它们连接到身体上,想象一下如果你现在想要在身体中间甚至在空中的桥梁,这个方法让事情变得容易多了..

标签:java,libgdx,box2d
来源: https://codeday.me/bug/20190829/1760829.html