gpt4 book ai didi

java - 多个夹具的 Box2D 原点

转载 作者:行者123 更新时间:2023-12-01 13:47:00 25 4
gpt4 key购买 nike

我认为当你对物体施加力时,它会施加到 body 的原点(可能是质心)。现在我正在尝试创建类似俄罗斯方 block 的 block ,并通过应用 LinearPulse 使它们跳跃,如下所示:

body.applyLinearImpulse(0, 5f, this.body.getPosition().x, this.body.getPosition().y, true);

如果您只有一个盒子固定装置作为主体,那么这非常有效,但是当您创建多个固定装置时,原点会错位,我无法将其放置在固定装置的中心。

这是我的意思的图片:

Demonstration

我从矩阵创建灯具,如下所示:

int array[][]= {{0,0,0,0,0},
{0,0,1,0,0},
{0,1,1,1,0},
{0,0,0,0,0},
{0,0,0,0,0}};

我使用数组来创建这样的装置:

public void setBody(int[][] blocks){
BodyDef def = new BodyDef();
def.type = BodyType.DynamicBody;
def.position.set(new Vector2(100 * WORLD_TO_BOX, 100 * WORLD_TO_BOX));
Body body = world.createBody(def);
body.setTransform(150*WORLD_TO_BOX, 200*WORLD_TO_BOX, -90*MathUtils.degreesToRadians);
for (int x = 0; x < 5; x++) { // HARDCODED 5
for (int y = 0; y < 5; y++) { // HARDCODED 5
if(blocks[x][y] == 1){
PolygonShape poly = new PolygonShape();
Vector2 v = new Vector2((-5/2+x),(-5/2+y));
poly.setAsBox(size/2 * WORLD_TO_BOX, size/2 * WORLD_TO_BOX, v, 0);
body.createFixture(poly, 1);
poly.dispose();
}
}
}
this.body = body;
}

WORLD_TO_BOX 值为 0.032f一个 block 的大小是32f

所以我的问题是,如何手动设置复杂的多夹具主体的质心/原点?

最佳答案

我不相信你可以设置物体的质心,它是由系统计算的。来自手册:

You can access the center of mass position in local and world coordinates. Much of the internal simulation in Box2D uses the center of mass. However, you should normally not need to access it. Instead you will usually work with the body transform. For example, you may have a body that is square. The body origin might be a corner of the square, while the center of mass is located at the center of the square.

const b2Vec2& GetPosition() const;  
float32 GetAngle() const;
const b2Vec2& GetWorldCenter() const;
const b2Vec2& GetLocalCenter()

我相信您会使用 GetWorldCenter(),因为线性冲量应用于世界坐标(也根据手册):

You can apply forces, torques, and impulses to a body. When you apply a force or an impulse, you provide a world point where the load is applied.

这有帮助吗?

关于java - 多个夹具的 Box2D 原点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20292749/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com